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

The following examples show how to use org.apache.hadoop.hbase.util.PositionedByteRange. 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 File: Struct.java    From hbase with 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 #2
Source File: FixedLengthWrapper.java    From hbase with 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 #3
Source File: TestTerminatedWrapper.java    From hbase with 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 #4
Source File: TestTerminatedWrapper.java    From hbase with 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 #5
Source File: DynamicPositionedMutableByteRangeTest.java    From hgraphdb with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: TestFixedLengthWrapper.java    From hbase with 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 #7
Source File: TerminatedWrapper.java    From hbase with 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 #8
Source File: IndexMetadataModel.java    From hgraphdb with 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 #9
Source File: PDataType.java    From phoenix with 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 #10
Source File: DynamicPositionedMutableByteRange.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@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 #11
Source File: PDataType.java    From phoenix with 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 #12
Source File: DynamicPositionedMutableByteRange.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public PositionedByteRange set(int capacity) {
    this.position = 0;
    super.set(capacity);
    this.limit = capacity;
    return this;
}
 
Example #13
Source File: TestOrderedInt64.java    From hbase with 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 #14
Source File: TestUnion2.java    From hbase with 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 #15
Source File: OrderedInt64.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public int encode(PositionedByteRange dst, Long val) {
  if (null == val) {
    throw new IllegalArgumentException("Null values not supported.");
  }
  return OrderedBytes.encodeInt64(dst, val, order);
}
 
Example #16
Source File: DynamicPositionedMutableByteRange.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public PositionedByteRange put(int index, byte[] val) {
    if (0 == val.length) {
        return this;
    }
    return put(index, val, 0, val.length);
}
 
Example #17
Source File: TestOrderedInt64.java    From hbase with 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 #18
Source File: TestStruct.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public Pojo1 decode(PositionedByteRange src) {
  Object[] ret = new Object[3];
  ret[0] = stringField.decode(src);
  ret[1] = intField.decode(src);
  ret[2] = doubleField.decode(src);
  return new Pojo1(ret);
}
 
Example #19
Source File: OrderedInt16.java    From hbase with 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 #20
Source File: TestOrderedFloat64.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodedFloatLength() {
  final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
  for (final OrderedFloat64 type : new OrderedFloat64[] { new OrderedFloat64(Order.ASCENDING),
    new OrderedFloat64(Order.DESCENDING) }) {
    for (final Double val : VALUES) {
      buffer.setPosition(0);
      type.encodeDouble(buffer, val);
      assertEquals("encodedLength does not match actual, " + val,
          buffer.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example #21
Source File: TestOrderedNumeric.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodedDoubleLength() {
  final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
  for (final OrderedNumeric type : new OrderedNumeric[] { new OrderedNumeric(Order.ASCENDING),
    new OrderedNumeric(Order.DESCENDING) }) {
    for (final Double val : DOUBLE_VALUES) {
      buffer.setPosition(0);
      type.encodeDouble(buffer, val);
      assertEquals("encodedLength does not match actual, " + val,
          buffer.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example #22
Source File: OrderedInt8.java    From hbase with 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 #23
Source File: PBCell.java    From hbase with 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 #24
Source File: TestStructNullExtension.java    From hbase with 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 #25
Source File: DynamicPositionedMutableByteRange.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public PositionedByteRange set(byte[] bytes) {
    this.position = 0;
    super.set(bytes);
    this.limit = bytes.length;
    return this;
}
 
Example #26
Source File: Struct.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Read the field at {@code index}. {@code src}'s position is not affected.
 */
public Object decode(PositionedByteRange src, int index) {
  assert index >= 0;
  StructIterator it = iterator(src.shallowCopy());
  for (; index > 0; index--) {
    it.skip();
  }
  return it.next();
}
 
Example #27
Source File: TestOrderedFloat64.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodedLength() {
  final PositionedByteRange buffer = new SimplePositionedMutableByteRange(20);
  for (final DataType<Double> type : new OrderedFloat64[] { new OrderedFloat64(Order.ASCENDING),
    new OrderedFloat64(Order.DESCENDING) }) {
    for (final Double val : VALUES) {
      buffer.setPosition(0);
      type.encode(buffer, val);
      assertEquals("encodedLength does not match actual, " + val,
          buffer.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example #28
Source File: Struct.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  StructIterator it = iterator(src);
  int skipped = 0;
  while (it.hasNext()) {
    skipped += it.skip();
  }
  return skipped;
}
 
Example #29
Source File: TestUnion2.java    From hbase with 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));
}
 
Example #30
Source File: OrderedFloat32.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public int encode(PositionedByteRange dst, Float val) {
  if (null == val) {
    throw new IllegalArgumentException("Null values not supported.");
  }
  return OrderedBytes.encodeFloat32(dst, val, order);
}