Java Code Examples for org.apache.hadoop.hbase.util.PositionedByteRange#get()

The following examples show how to use org.apache.hadoop.hbase.util.PositionedByteRange#get() . 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: LabelConnectionModel.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
public byte[] serialize(String outVertexLabel, String edgeLabel, String inVertexLabel) {
    PositionedByteRange buffer = new DynamicPositionedMutableByteRange(4096);
    OrderedBytes.encodeString(buffer, outVertexLabel, Order.ASCENDING);
    OrderedBytes.encodeString(buffer, edgeLabel, Order.ASCENDING);
    OrderedBytes.encodeString(buffer, inVertexLabel, Order.ASCENDING);
    buffer.setLength(buffer.getPosition());
    buffer.setPosition(0);
    byte[] bytes = new byte[buffer.getRemaining()];
    buffer.get(bytes);
    return bytes;
}
 
Example 2
Source File: IndexMetadataModel.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
public byte[] serialize(IndexMetadata.Key index) {
    PositionedByteRange buffer = new DynamicPositionedMutableByteRange(4096);
    OrderedBytes.encodeString(buffer, index.label(), Order.ASCENDING);
    OrderedBytes.encodeString(buffer, index.propertyKey(), Order.ASCENDING);
    OrderedBytes.encodeInt8(buffer, index.type() == ElementType.VERTEX ? (byte) 1 : (byte) 0, Order.ASCENDING);
    buffer.setLength(buffer.getPosition());
    buffer.setPosition(0);
    byte[] bytes = new byte[buffer.getRemaining()];
    buffer.get(bytes);
    return bytes;
}
 
Example 3
Source File: DynamicPositionedMutableByteRangeTest.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Test
public void testPosition() {
    PositionedByteRange r = new DynamicPositionedMutableByteRange(new byte[5], 1, 3);

    // exercise single-byte put
    r.put(Bytes.toBytes("f")[0]).put(Bytes.toBytes("o")[0]).put(Bytes.toBytes("o")[0]);
    Assert.assertEquals(3, r.getPosition());
    Assert.assertArrayEquals(
        new byte[]{0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0},
        r.getBytes()
    );

    // exercise multi-byte put
    r.setPosition(0);
    r.put(Bytes.toBytes("f")).put(Bytes.toBytes("o")).put(Bytes.toBytes("o"));
    Assert.assertEquals(3, r.getPosition());
    Assert.assertArrayEquals(
        new byte[]{0, Bytes.toBytes("f")[0], Bytes.toBytes("o")[0], Bytes.toBytes("o")[0], 0},
        r.getBytes()
    );

    // exercise single-byte get
    r.setPosition(0);
    Assert.assertEquals(Bytes.toBytes("f")[0], r.get());
    Assert.assertEquals(Bytes.toBytes("o")[0], r.get());
    Assert.assertEquals(Bytes.toBytes("o")[0], r.get());

    r.setPosition(1);
    Assert.assertEquals(Bytes.toBytes("o")[0], r.get());

    // exercise multi-byte get
    r.setPosition(0);
    byte[] dst = new byte[3];
    r.get(dst);
    Assert.assertArrayEquals(Bytes.toBytes("foo"), dst);

    // set position to the end of the range; this should not throw.
    r.setPosition(3);
}
 
Example 4
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 5
Source File: FixedLengthWrapper.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public T decode(PositionedByteRange src) {
  if (src.getRemaining() < length) {
    throw new IllegalArgumentException("Not enough buffer remaining. src.offset: "
        + src.getOffset() + " src.length: " + src.getLength() + " src.position: "
        + src.getPosition() + " max length: " + length);
  }
  // create a copy range limited to length bytes. boo.
  PositionedByteRange b = new SimplePositionedMutableByteRange(length);
  src.get(b.getBytes());
  return base.decode(b);
}
 
Example 6
Source File: RawString.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public String decode(PositionedByteRange src) {
  if (Order.ASCENDING == this.order) {
    // avoid unnecessary array copy for ASC case.
    String val =
        Bytes.toString(src.getBytes(), src.getOffset() + src.getPosition(), src.getRemaining());
    src.setPosition(src.getLength());
    return val;
  } else {
    byte[] b = new byte[src.getRemaining()];
    src.get(b);
    order.apply(b, 0, b.length);
    return Bytes.toString(b);
  }
}
 
Example 7
Source File: TestUnion2.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  switch (src.get()) {
    case IS_INTEGER:
      return 1 + typeA.skip(src);
    case IS_STRING:
      return 1 + typeB.skip(src);
    default:
      throw new IllegalArgumentException("Unrecognized encoding format.");
  }
}
 
Example 8
Source File: TestUnion2.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(PositionedByteRange src) {
  switch (src.get()) {
    case IS_INTEGER:
      return typeA.decode(src);
    case IS_STRING:
      return typeB.decode(src);
    default:
      throw new IllegalArgumentException("Unrecognized encoding format.");
  }
}
 
Example 9
Source File: PDataType.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@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: RawBytes.java    From hbase with Apache License 2.0 2 votes vote down vote up
/**
 * Read a {@code byte[]} from the buffer {@code src}.
 *
 * @param src the {@link PositionedByteRange} to read the {@code byte[]} from
 * @param length the length to read from the buffer
 * @return the {@code byte[]} read from the buffer
 */
public byte[] decode(PositionedByteRange src, int length) {
  byte[] val = new byte[length];
  src.get(val);
  return val;
}