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); }
@Test public void testReadWriteSkippable() { PositionedByteRange buff = new SimplePositionedByteRange(14); for (OrderedString t : new OrderedString[] { OrderedString.ASCENDING, OrderedString.DESCENDING }) { for (byte[] term : TERMINATORS) { for (String val : VALUES_STRINGS) { buff.setPosition(0); DataType<String> type = new TerminatedWrapper<String>(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()); } } } }
@Test public void testReadWriteNonSkippable() { PositionedByteRange buff = new SimplePositionedByteRange(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<byte[]>(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()); } } } }
@Test public void testSkipSkippable() { PositionedByteRange buff = new SimplePositionedByteRange(14); for (OrderedString t : new OrderedString[] { OrderedString.ASCENDING, OrderedString.DESCENDING }) { for (byte[] term : TERMINATORS) { for (String val : VALUES_STRINGS) { buff.setPosition(0); DataType<String> type = new TerminatedWrapper<String>(t, term); int expected = val.length() + 2 + term.length; assertEquals(expected, type.encode(buff, val)); buff.setPosition(0); assertEquals(expected, type.skip(buff)); assertEquals(expected, buff.getPosition()); } } } }
@Test public void testSkipNonSkippable() { PositionedByteRange buff = new SimplePositionedByteRange(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<byte[]>(new RawBytes(ord), term); int expected = type.encode(buff, val); buff.setPosition(0); assertEquals(expected, type.skip(buff)); assertEquals(expected, buff.getPosition()); } } } }
@Test public void testReadWrite() { for (int limit : limits) { PositionedByteRange buff = new SimplePositionedByteRange(limit); for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (byte[] val : VALUES) { buff.setPosition(0); DataType<byte[]> type = new FixedLengthWrapper<byte[]>(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)); } } } }
@Test public void testReadWrite() { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { RawString type = Order.ASCENDING == ord ? RawString.ASCENDING : RawString.DESCENDING; for (String val : VALUES) { PositionedByteRange buff = new SimplePositionedByteRange(Bytes.toBytes(val).length); assertEquals(buff.getLength(), type.encode(buff, val)); 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()); } } }
@Test public void testReadWrite() { for (int limit : limits) { PositionedByteRange buff = new SimplePositionedByteRange(limit); for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (byte[] val : VALUES) { buff.setPosition(0); DataType<byte[]> type = new FixedLengthWrapper<byte[]>(new RawBytes(ord), limit); assertEquals(limit, type.encode(buff, val)); byte[] expected = Arrays.copyOf(val, limit); buff.setPosition(0); byte[] actual = type.decode(buff); assertArrayEquals(expected, actual); buff.setPosition(0); assertEquals(limit, type.skip(buff)); } } } }
/** * Basic test to verify utility methods in {@link PBType} and delegation to protobuf works. */ @Test public void testRoundTrip() { final Cell cell = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("fam"), Bytes.toBytes("qual"), Bytes.toBytes("val")); CellProtos.Cell c = ProtobufUtil.toCell(cell), decoded; PositionedByteRange pbr = new SimplePositionedByteRange(c.getSerializedSize()); pbr.setPosition(0); int encodedLength = CODEC.encode(pbr, c); pbr.setPosition(0); decoded = CODEC.decode(pbr); assertEquals(encodedLength, pbr.getPosition()); assertTrue(CellComparator.equals(cell, ProtobufUtil.toCell(decoded))); }
public LabelConnection deserialize(Result result) { byte[] bytes = result.getRow(); PositionedByteRange buffer = new SimplePositionedByteRange(bytes); String outVertexLabel = OrderedBytes.decodeString(buffer); String edgeLabel = OrderedBytes.decodeString(buffer); String inVertexLabel = OrderedBytes.decodeString(buffer); Cell createdAtCell = result.getColumnLatestCell(Constants.DEFAULT_FAMILY_BYTES, Constants.CREATED_AT_BYTES); Long createdAt = ValueUtils.deserialize(CellUtil.cloneValue(createdAtCell)); return new LabelConnection(outVertexLabel, edgeLabel, inVertexLabel, createdAt); }
@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 SimplePositionedByteRange(length); src.get(b.getBytes()); return base.decode(b); }
/** * 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 SimplePositionedByteRange(4); s.encode(buf, new Object[1]); }
@Test public void testEncodedLength() { PositionedByteRange buff = new SimplePositionedByteRange(20); for (DataType<byte[]> type : new OrderedBlob[] { OrderedBlob.ASCENDING, OrderedBlob.DESCENDING }) { for (byte[] val : VALUES) { buff.setPosition(0); type.encode(buff, val); assertEquals( "encodedLength does not match actual, " + Bytes.toStringBinary(val), buff.getPosition(), type.encodedLength(val)); } } }
@Test public void testEncodeDecode() { Integer intVal = Integer.valueOf(10); String strVal = "hello"; PositionedByteRange buff = new SimplePositionedByteRange(10); SampleUnion1 type = new SampleUnion1(); type.encode(buff, intVal); buff.setPosition(0); assertTrue(0 == intVal.compareTo(type.decodeA(buff))); buff.setPosition(0); type.encode(buff, strVal); buff.setPosition(0); assertTrue(0 == strVal.compareTo(type.decodeB(buff))); }
@Test public void testSkip() { Integer intVal = Integer.valueOf(10); String strVal = "hello"; PositionedByteRange buff = new SimplePositionedByteRange(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)); }
@Test public void testEncodedLength() { PositionedByteRange buff = new SimplePositionedByteRange(20); for (DataType<byte[]> type : new OrderedBlobVar[] { OrderedBlobVar.ASCENDING, OrderedBlobVar.DESCENDING }) { for (byte[] val : VALUES) { buff.setPosition(0); type.encode(buff, val); assertEquals( "encodedLength does not match actual, " + Bytes.toStringBinary(val), buff.getPosition(), type.encodedLength(val)); } } }
@Test public void testEncodedLength() { PositionedByteRange buff = new SimplePositionedByteRange(20); for (DataType<String> type : new OrderedString[] { OrderedString.ASCENDING, OrderedString.DESCENDING }) { for (String val : VALUES) { buff.setPosition(0); type.encode(buff, val); assertEquals( "encodedLength does not match actual, " + val, buff.getPosition(), type.encodedLength(val)); } } }
@Test @SuppressWarnings("unchecked") public void testOrderPreservation() throws Exception { Object[] vals = new Object[constructorArgs.length]; PositionedByteRange[] encodedGeneric = new PositionedByteRange[constructorArgs.length]; PositionedByteRange[] encodedSpecialized = new PositionedByteRange[constructorArgs.length]; Constructor<?> ctor = specialized.encodedClass().getConstructor(Object[].class); for (int i = 0; i < vals.length; i++) { vals[i] = ctor.newInstance(new Object[] { constructorArgs[i] }); encodedGeneric[i] = new SimplePositionedByteRange(generic.encodedLength(constructorArgs[i])); encodedSpecialized[i] = new SimplePositionedByteRange(specialized.encodedLength(vals[i])); } // populate our arrays for (int i = 0; i < vals.length; i++) { generic.encode(encodedGeneric[i], constructorArgs[i]); encodedGeneric[i].setPosition(0); specialized.encode(encodedSpecialized[i], vals[i]); encodedSpecialized[i].setPosition(0); assertArrayEquals(encodedGeneric[i].getBytes(), encodedSpecialized[i].getBytes()); } Arrays.sort(vals); Arrays.sort(encodedGeneric); Arrays.sort(encodedSpecialized); for (int i = 0; i < vals.length; i++) { assertEquals( "Struct encoder does not preserve sort order at position " + i, vals[i], ctor.newInstance(new Object[] { generic.decode(encodedGeneric[i]) })); assertEquals( "Specialized encoder does not preserve sort order at position " + i, vals[i], specialized.decode(encodedSpecialized[i])); } }
/** * Basic test to verify utility methods in {@link PBType} and delegation to protobuf works. */ @Test public void testRoundTrip() { final Cell cell = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("fam"), Bytes.toBytes("qual"), Bytes.toBytes("val")); CellProtos.Cell c = ProtobufUtil.toCell(cell), decoded; PositionedByteRange pbr = new SimplePositionedByteRange(c.getSerializedSize()); pbr.setPosition(0); int encodedLength = CODEC.encode(pbr, c); pbr.setPosition(0); decoded = CODEC.decode(pbr); assertEquals(encodedLength, pbr.getPosition()); assertTrue(CellUtil.equals(cell, ProtobufUtil .toCell(ExtendedCellBuilderFactory.create(CellBuilderType.SHALLOW_COPY), decoded))); }