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

The following examples show how to use org.apache.hadoop.hbase.util.PositionedByteRange#setPosition() . 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: TestRawString.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadWrite() {
  for (final Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
    final RawString type =
        Order.ASCENDING == ord ? new RawString(Order.ASCENDING) : new RawString(Order.DESCENDING);
    for (final String val : VALUES) {
      final PositionedByteRange buff =
          new SimplePositionedMutableByteRange(Bytes.toBytes(val).length);
      assertEquals(buff.getLength(), type.encode(buff, val));
      final byte[] expected = Bytes.toBytes(val);
      ord.apply(expected);
      assertArrayEquals(expected, buff.getBytes());
      buff.setPosition(0);
      assertEquals(val, type.decode(buff));
      buff.setPosition(0);
      assertEquals(buff.getLength(), type.skip(buff));
      assertEquals(buff.getLength(), buff.getPosition());
    }
  }
}
 
Example 2
Source File: TestTerminatedWrapper.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testSkipNonSkippable() {
  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);
        int expected = type.encode(buff, val);
        buff.setPosition(0);
        assertEquals(expected, type.skip(buff));
        assertEquals(expected, buff.getPosition());
      }
    }
  }
}
 
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: TestOrderedFloat32.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<Float> type : new OrderedFloat32[] { new OrderedFloat32(Order.ASCENDING),
    new OrderedFloat32(Order.DESCENDING) }) {
    for (final Float val : VALUES) {
      buffer.setPosition(0);
      type.encode(buffer, val);
      assertEquals("encodedLength does not match actual, " + val,
          buffer.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example 6
Source File: TestOrderedInt16.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<Short> type : new OrderedInt16[] { new OrderedInt16(Order.ASCENDING),
    new OrderedInt16(Order.DESCENDING) }) {
    for (final Short val : VALUES) {
      buffer.setPosition(0);
      type.encode(buffer, val);
      assertEquals("encodedLength does not match actual, " + val,
          buffer.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example 7
Source File: TestOrderedInt8.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<Byte> type : new OrderedInt8[] { new OrderedInt8(Order.ASCENDING),
    new OrderedInt8(Order.DESCENDING) }) {
    for (final Byte val : VALUES) {
      buffer.setPosition(0);
      type.encode(buffer, val);
      assertEquals("encodedLength does not match actual, " + val,
          buffer.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example 8
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 9
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 10
Source File: TestOrderedBlobVar.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodedLength() {
  final PositionedByteRange buff = new SimplePositionedMutableByteRange(20);
  for (final DataType<byte[]> type :
    new OrderedBlobVar[] { new OrderedBlobVar(Order.ASCENDING),
      new OrderedBlobVar(Order.DESCENDING) }) {
    for (final byte[] val : VALUES) {
      buff.setPosition(0);
      type.encode(buff, val);
      assertEquals("encodedLength does not match actual, " + Bytes.toStringBinary(val),
        buff.getPosition(), type.encodedLength(val));
    }
  }
}
 
Example 11
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 12
Source File: RawFloat.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  src.setPosition(src.getPosition() + Bytes.SIZEOF_FLOAT);
  return Bytes.SIZEOF_FLOAT;
}
 
Example 13
Source File: RawString.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  int skipped = src.getRemaining();
  src.setPosition(src.getLength());
  return skipped;
}
 
Example 14
Source File: FixedLengthWrapper.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  src.setPosition(src.getPosition() + this.length);
  return this.length;
}
 
Example 15
Source File: RawLong.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  src.setPosition(src.getPosition() + Bytes.SIZEOF_LONG);
  return Bytes.SIZEOF_LONG;
}
 
Example 16
Source File: RawByte.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  src.setPosition(src.getPosition() + Bytes.SIZEOF_BYTE);
  return Bytes.SIZEOF_BYTE;
}
 
Example 17
Source File: RawBytes.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  int skipped = src.getRemaining();
  src.setPosition(src.getLength());
  return skipped;
}
 
Example 18
Source File: RawDouble.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  src.setPosition(src.getPosition() + Bytes.SIZEOF_DOUBLE);
  return Bytes.SIZEOF_DOUBLE;
}
 
Example 19
Source File: RawShort.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Override
public int skip(PositionedByteRange src) {
  src.setPosition(src.getPosition() + Bytes.SIZEOF_SHORT);
  return Bytes.SIZEOF_SHORT;
}
 
Example 20
Source File: RawBytes.java    From hbase with Apache License 2.0 2 votes vote down vote up
/**
 * Write {@code val} into {@code dst}, respecting {@code voff} and {@code vlen}.
 *
 * @param dst the {@link PositionedByteRange} to write to
 * @param val the value to write to {@code dst}
 * @param voff the offset in {@code dst} where to write {@code val} to
 * @param vlen the length of {@code val}
 * @return number of bytes written
 */
public int encode(PositionedByteRange dst, byte[] val, int voff, int vlen) {
  Bytes.putBytes(dst.getBytes(), dst.getOffset() + dst.getPosition(), val, voff, vlen);
  dst.setPosition(dst.getPosition() + vlen);
  return vlen;
}