Java Code Examples for org.apache.hadoop.hbase.util.PositionedByteRange

The following examples show how to use org.apache.hadoop.hbase.util.PositionedByteRange. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: hbase   Source File: TerminatedWrapper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return the position at which {@code term} begins within {@code src},
 * or {@code -1} if {@code term} is not found.
 */
protected int terminatorPosition(PositionedByteRange src) {
  byte[] a = src.getBytes();
  final int offset = src.getOffset();
  int i;
  SKIP: for (i = src.getPosition(); i < src.getLength(); i++) {
    if (a[offset + i] != term[0]) {
      continue;
    }
    int j;
    for (j = 1; j < term.length && offset + j < src.getLength(); j++) {
      if (a[offset + i + j] != term[j]) {
        continue SKIP;
      }
    }
    if (j == term.length) {
      return i; // success
    }
  }
  return -1;
}
 
Example 2
Source Project: hbase   Source File: TestFixedLengthWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testReadWrite() {
  for (int limit : limits) {
    PositionedByteRange buff = new SimplePositionedMutableByteRange(limit);
    for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
      for (byte[] val : VALUES) {
        buff.setPosition(0);
        DataType<byte[]> type = new FixedLengthWrapper<>(new RawBytes(ord), limit);
        assertEquals(limit, type.encode(buff, val));
        buff.setPosition(0);
        byte[] actual = type.decode(buff);
        assertTrue("Decoding output differs from expected",
          Bytes.equals(val, 0, val.length, actual, 0, val.length));
        buff.setPosition(0);
        assertEquals(limit, type.skip(buff));
      }
    }
  }
}
 
Example 3
Source Project: hgraphdb   Source File: IndexMetadataModel.java    License: Apache License 2.0 6 votes vote down vote up
public IndexMetadata deserialize(Result result) {
    byte[] bytes = result.getRow();
    PositionedByteRange buffer = new SimplePositionedByteRange(bytes);
    String label = OrderedBytes.decodeString(buffer);
    String propertyKey = OrderedBytes.decodeString(buffer);
    ElementType type = OrderedBytes.decodeInt8(buffer) == 1 ? ElementType.VERTEX : ElementType.EDGE;
    Cell uniqueCell = result.getColumnLatestCell(Constants.DEFAULT_FAMILY_BYTES, Constants.UNIQUE_BYTES);
    boolean isUnique = ValueUtils.deserialize(CellUtil.cloneValue(uniqueCell));
    Cell stateCell = result.getColumnLatestCell(Constants.DEFAULT_FAMILY_BYTES, Constants.INDEX_STATE_BYTES);
    State state = State.valueOf(ValueUtils.deserialize(CellUtil.cloneValue(stateCell)));
    Cell createdAtCell = result.getColumnLatestCell(Constants.DEFAULT_FAMILY_BYTES, Constants.CREATED_AT_BYTES);
    Long createdAt = ValueUtils.deserialize(CellUtil.cloneValue(createdAtCell));
    Cell updatedAtCell = result.getColumnLatestCell(Constants.DEFAULT_FAMILY_BYTES, Constants.UPDATED_AT_BYTES);
    Long updatedAt = ValueUtils.deserialize(CellUtil.cloneValue(updatedAtCell));
    return new IndexMetadata(type, label, propertyKey, isUnique, state, createdAt, updatedAt);
}
 
Example 4
@Test
public void testPutGetAPIsCompareWithBBAPIs() throws Exception {
    // confirm that the long/int/short writing is same as BBs
    PositionedByteRange pbr = new DynamicPositionedMutableByteRange(1);
    int i1 = -234, i2 = 2;
    short s1 = 0;
    long l1 = 1234L;
    pbr.putInt(i1);
    pbr.putShort(s1);
    pbr.putInt(i2);
    pbr.putLong(l1);
    // rewind
    pbr.setPosition(0);
    Assert.assertEquals(i1, pbr.getInt());
    Assert.assertEquals(s1, pbr.getShort());
    Assert.assertEquals(i2, pbr.getInt());
    Assert.assertEquals(l1, pbr.getLong());
    // Read back using BB APIs
    ByteBuffer bb = ByteBuffer.wrap(pbr.getBytes());
    Assert.assertEquals(i1, bb.getInt());
    Assert.assertEquals(s1, bb.getShort());
    Assert.assertEquals(i2, bb.getInt());
    Assert.assertEquals(l1, bb.getLong());
}
 
Example 5
Source Project: hbase   Source File: TestTerminatedWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testReadWriteNonSkippable() {
  PositionedByteRange buff = new SimplePositionedMutableByteRange(12);
  for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
    for (byte[] term : TERMINATORS) {
      for (byte[] val : VALUES_BYTES) {
        buff.setPosition(0);
        DataType<byte[]> type = new TerminatedWrapper<>(new RawBytes(ord), term);
        assertEquals(val.length + term.length, type.encode(buff, val));
        buff.setPosition(0);
        assertArrayEquals(val, type.decode(buff));
        assertEquals(val.length + term.length, buff.getPosition());
      }
    }
  }
}
 
Example 6
Source Project: hbase   Source File: TestTerminatedWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testReadWriteSkippable() {
  final PositionedByteRange buff = new SimplePositionedMutableByteRange(14);
  for (final OrderedString t : new OrderedString[] {
    new OrderedString(Order.ASCENDING), new OrderedString(Order.DESCENDING)
  }) {
    for (final byte[] term : TERMINATORS) {
      for (final String val : VALUES_STRINGS) {
        buff.setPosition(0);
        final DataType<String> type = new TerminatedWrapper<>(t, term);
        assertEquals(val.length() + 2 + term.length, type.encode(buff, val));
        buff.setPosition(0);
        assertEquals(val, type.decode(buff));
        assertEquals(val.length() + 2 + term.length, buff.getPosition());
      }
    }
  }
}
 
Example 7
Source Project: hbase   Source File: FixedLengthWrapper.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public int encode(PositionedByteRange dst, T val) {
  if (dst.getRemaining() < length) {
    throw new IllegalArgumentException("Not enough buffer remaining. dst.offset: "
        + dst.getOffset() + " dst.length: " + dst.getLength() + " dst.position: "
        + dst.getPosition() + " max length: " + length);
  }
  int written = base.encode(dst, val);
  if (written > length) {
    throw new IllegalArgumentException("Length of encoded value (" + written
        + ") exceeds max length (" + length + ").");
  }
  // TODO: is the zero-padding appropriate?
  for (; written < length; written++) {
    dst.put((byte) 0x00);
  }
  return written;
}
 
Example 8
Source Project: hbase   Source File: Struct.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public int encode(PositionedByteRange dst, Object[] val) {
  if (val.length == 0) {
    return 0;
  }
  assert fields.length >= val.length;
  int end, written = 0;
  // find the last occurrence of a non-null or null and non-nullable value
  for (end = val.length - 1; end > -1; end--) {
    if (null != val[end] || (null == val[end] && !fields[end].isNullable())) {
      break;
    }
  }
  for (int i = 0; i <= end; i++) {
    written += fields[i].encode(dst, val[i]);
  }
  return written;
}
 
Example 9
@Override
public PositionedByteRange set(int capacity) {
    this.position = 0;
    super.set(capacity);
    this.limit = capacity;
    return this;
}
 
Example 10
@Override
public PositionedByteRange set(byte[] bytes) {
    this.position = 0;
    super.set(bytes);
    this.limit = bytes.length;
    return this;
}
 
Example 11
Source Project: hbase   Source File: TestOrderedInt64.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodedFloatLength() {
  final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
  for (final OrderedInt64 type : new OrderedInt64[] { new OrderedInt64(Order.ASCENDING),
    new OrderedInt64(Order.DESCENDING) }) {
    for (final Long val : VALUES) {
      buffer.setPosition(0);
      type.encodeLong(buffer, val);
      assertEquals("encodedLength does not match actual, " + val,
          buffer.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example 12
@Override
public PositionedByteRange put(byte[] val) {
    if (0 == val.length) {
        return this;
    }
    return this.put(val, 0, val.length);
}
 
Example 13
@Override
public PositionedByteRange put(byte[] val, int offset, int length) {
    if (0 == length) {
        return this;
    }
    ensureCapacity(position + length);
    put(position, val, offset, length);
    this.position += length;
    return this;
}
 
Example 14
Source Project: hbase   Source File: OrderedInt16.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int encode(PositionedByteRange dst, Short val) {
  if (null == val) {
    throw new IllegalArgumentException("Null values not supported.");
  }
  return OrderedBytes.encodeInt16(dst, val, order);
}
 
Example 15
@Override
public PositionedByteRange put(int index, byte[] val) {
    if (0 == val.length) {
        return this;
    }
    return put(index, val, 0, val.length);
}
 
Example 16
@Override
public PositionedByteRange put(int index, byte[] val, int offset, int length) {
    if (0 == length) {
        return this;
    }
    System.arraycopy(val, offset, this.bytes, this.offset + index, length);
    return this;
}
 
Example 17
@Override
public PositionedByteRange deepCopy() {
    DynamicPositionedMutableByteRange clone = new DynamicPositionedMutableByteRange(
        deepCopyToNewArray());
    clone.position = this.position;
    return clone;
}
 
Example 18
Source Project: hbase   Source File: TestRawBytes.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodedLength() {
  final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
  for (final DataType<byte[]> type : new RawBytes[] { new RawBytes(Order.ASCENDING),
    new RawBytes(Order.DESCENDING) }) {
    for (final byte[] val : VALUES) {
      buffer.setPosition(0);
      type.encode(buffer, val);
      assertEquals("encodedLength does not match actual, " + Arrays.toString(val),
          buffer.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example 19
Source Project: hbase   Source File: PBCell.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CellMessage.Cell decode(PositionedByteRange src) {
  CellMessage.Cell.Builder builder = CellMessage.Cell.newBuilder();
  CodedInputStream is = inputStreamFromByteRange(src);
  is.setSizeLimit(src.getLength());
  try {
    CellMessage.Cell ret = builder.mergeFrom(is).build();
    src.setPosition(src.getPosition() + is.getTotalBytesRead());
    return ret;
  } catch (IOException e) {
    throw new RuntimeException("Error while decoding type.", e);
  }
}
 
Example 20
Source Project: hbase   Source File: TerminatedWrapper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Write instance {@code val} into buffer {@code dst}.
 * @throws IllegalArgumentException when the encoded representation of
 *           {@code val} contains the {@code term} sequence.
 */
@Override
public int encode(PositionedByteRange dst, T val) {
  final int start = dst.getPosition();
  int written = wrapped.encode(dst, val);
  PositionedByteRange b = dst.shallowCopy();
  b.setLength(dst.getPosition());
  b.setPosition(start);
  if (-1 != terminatorPosition(b)) {
    dst.setPosition(start);
    throw new IllegalArgumentException("Encoded value contains terminator sequence.");
  }
  dst.put(term);
  return written + term.length;
}
 
Example 21
Source Project: hbase   Source File: TestStruct.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  int skipped = stringField.skip(src);
  skipped += intField.skip(src);
  skipped += doubleField.skip(src);
  return skipped;
}
 
Example 22
@Override
public PositionedByteRange putInt(int index, int val) {
    // This writing is same as BB's putInt. When byte[] is wrapped in a BB and
    // call getInt(), one
    // can get the same result.
    for (int i = Bytes.SIZEOF_INT - 1; i > 0; i--) {
        bytes[offset + index + i] = (byte) val;
        val >>>= 8;
    }
    bytes[offset + index] = (byte) val;
    clearHashCache();
    return this;
}
 
Example 23
Source Project: hbase   Source File: TestUnion2.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeDecode() {
  Integer intVal = 10;
  String strVal = "hello";
  PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
  SampleUnion1 type = new SampleUnion1();

  type.encode(buff, intVal);
  buff.setPosition(0);
  assertEquals(0, intVal.compareTo(type.decodeA(buff)));
  buff.setPosition(0);
  type.encode(buff, strVal);
  buff.setPosition(0);
  assertEquals(0, strVal.compareTo(type.decodeB(buff)));
}
 
Example 24
Source Project: hbase   Source File: TestOrderedInt64.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodedLength() {
  final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
  for (final DataType<Long> type : new OrderedInt64[] { new OrderedInt64(Order.ASCENDING),
    new OrderedInt64(Order.DESCENDING) }) {
    for (final Long val : VALUES) {
      buffer.setPosition(0);
      type.encode(buffer, val);
      assertEquals("encodedLength does not match actual, " + val,
          buffer.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example 25
Source Project: phoenix   Source File: PDataType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int encode(PositionedByteRange pbr, T val) {
  // default implementation based on existing PDataType methods.
  int pos = pbr.getPosition();
  pbr.put(toBytes(val));
  return pbr.getPosition() - pos;
}
 
Example 26
Source Project: phoenix   Source File: PDataType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public T decode(PositionedByteRange pbr) {
  // default implementation based on existing PDataType methods.
  byte[] b = new byte[getByteSize()];
  pbr.get(b);
  return (T) toObject(b, 0, b.length, this, SortOrder.ASC, getMaxLength(null), getScale(null));
}
 
Example 27
Source Project: hbase   Source File: OrderedInt8.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int encode(PositionedByteRange dst, Byte val) {
  if (null == val) {
    throw new IllegalArgumentException("Null values not supported.");
  }
  return OrderedBytes.encodeInt8(dst, val, order);
}
 
Example 28
Source Project: hbase   Source File: PBCell.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  CellMessage.Cell.Builder builder = CellMessage.Cell.newBuilder();
  CodedInputStream is = inputStreamFromByteRange(src);
  is.setSizeLimit(src.getLength());
  try {
    builder.mergeFrom(is);
    int consumed = is.getTotalBytesRead();
    src.setPosition(src.getPosition() + consumed);
    return consumed;
  } catch (IOException e) {
    throw new RuntimeException("Error while skipping type.", e);
  }
}
 
Example 29
Source Project: hbase   Source File: TestStructNullExtension.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Verify null extension respects the type's isNullable field.
 */
@Test(expected = NullPointerException.class)
public void testNonNullableNullExtension() {
  Struct s = new StructBuilder()
      .add(new RawStringTerminated("|")) // not nullable
      .toStruct();
  PositionedByteRange buf = new SimplePositionedMutableByteRange(4);
  s.encode(buf, new Object[1]);
}
 
Example 30
Source Project: hbase   Source File: TestUnion2.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSkip() {
  Integer intVal = 10;
  String strVal = "hello";
  PositionedByteRange buff = new SimplePositionedMutableByteRange(10);
  SampleUnion1 type = new SampleUnion1();

  int len = type.encode(buff, intVal);
  buff.setPosition(0);
  assertEquals(len, type.skip(buff));
  buff.setPosition(0);
  len = type.encode(buff, strVal);
  buff.setPosition(0);
  assertEquals(len, type.skip(buff));
}