@Override public EdgeRowKey fromComposite( final CompositeParser composite ) { final Id sourceId = ID_SER.fromComposite( composite ); final String edgeType = composite.readString(); final Id targetId = ID_SER.fromComposite( composite ); final long shard = composite.readLong(); return new EdgeRowKey( sourceId, edgeType, targetId, shard ); }
@Override public void toComposite( final CompositeBuilder builder, final EdgeIdTypeKey value ) { ID_SER.toComposite( builder, value.node ); builder.addString( value.edgeType ); }
@Test public void testSerialization() { Id testId = new SimpleId( "test" ); IdRowCompositeSerializer rowSerializer = IdRowCompositeSerializer.get(); final CompositeBuilder builder = Composites.newCompositeBuilder(); rowSerializer.toComposite( builder, testId ); final CompositeParser parser = Composites.newCompositeParser( builder.build() ); //now convert it back Id deserialized = rowSerializer.fromComposite( parser ); assertEquals( "Serialization works correctly", testId, deserialized ); }
@Test public void testSerialization() { final Id testId = new SimpleId( "scopeType" ); final String name = "scopeName"; final Id testKey = new SimpleId( "testKey" ); final Id applicationId = new SimpleId( "application" ); final CollectionPrefixedKey<Id> collectionPrefixedKey = new CollectionPrefixedKey<>( name, testId, testKey ); final ScopedRowKey<CollectionPrefixedKey<Id>> rowKey = ScopedRowKey.fromKey( applicationId, collectionPrefixedKey ); CollectionScopedRowKeySerializer<Id> collectionScopedRowKeySerializer = new CollectionScopedRowKeySerializer<Id>( IdRowCompositeSerializer.get() ); ByteBuffer buff = collectionScopedRowKeySerializer.toByteBuffer( rowKey ); ScopedRowKey<CollectionPrefixedKey<Id>> parsedRowKey = collectionScopedRowKeySerializer.fromByteBuffer( buff ); assertEquals( "Row key serialized correctly", rowKey, parsedRowKey ); } }
@Override public void toComposite( final CompositeBuilder builder, final EdgeRowKey key ) { //add the row id to the composite ID_SER.toComposite( builder, key.sourceId ); builder.addString( key.edgeType ); ID_SER.toComposite( builder, key.targetId ); builder.addLong( key.shardId ); }
@Override public EdgeIdTypeKey fromComposite( final CompositeParser composite ) { final Id id = ID_SER.fromComposite( composite ); final String edgeType = composite.readString(); return new EdgeIdTypeKey( id, edgeType ); }
@Override public void toComposite( final CompositeBuilder builder, final EdgeIdTypeKey value ) { ID_SER.toComposite( builder, value.node ); builder.addString( value.edgeType ); }
@Override public EdgeIdTypeKey fromComposite( final CompositeParser composite ) { final Id id = ID_SER.fromComposite( composite ); final String edgeType = composite.readString(); return new EdgeIdTypeKey( id, edgeType ); }
@Override public void toComposite( final CompositeBuilder builder, final RowKey key ) { //add the row id to the composite ID_SER.toComposite( builder, key.nodeId ); builder.addString( key.edgeType ); builder.addLong( key.shardId ); }
@Override public ScopedRowKey<CollectionPrefixedKey<K>> fromByteBuffer( final ByteBuffer byteBuffer ) { final CompositeParser parser = Composites.newCompositeParser( byteBuffer ); //read back the id final Id orgId = ID_SER.fromComposite( parser ); final Id scopeId = ID_SER.fromComposite( parser ); final String scopeName = parser.readString(); final K value = keySerializer.fromComposite( parser ); final CollectionPrefixedKey<K> collectionPrefixedKey = new CollectionPrefixedKey<>( scopeName, scopeId, value ); return new ScopedRowKey<>( orgId, collectionPrefixedKey ); } }
@Override public void toComposite( final CompositeBuilder builder, final RowKeyType keyType ) { //add the row id to the composite ID_SER.toComposite( builder, keyType.nodeId ); builder.addString( keyType.edgeType ); builder.addString( keyType.idType ); builder.addLong( keyType.shardId ); }
@Override public RowKey fromComposite( final CompositeParser composite ) { final Id id = ID_SER.fromComposite( composite ); final String edgeType = composite.readString(); final long shard = composite.readLong(); return new RowKey( id, edgeType, shard ); }
@Override public ByteBuffer toByteBuffer( final ScopedRowKey<K> scopedRowKey ) { final CompositeBuilder builder = Composites.newCompositeBuilder(); //add the organization's id ID_SER.toComposite( builder, scopedRowKey.getScope() ); //add the key type keySerializer.toComposite( builder, scopedRowKey.getKey() ); return builder.build(); }
@Override public ScopedRowKey<K> fromByteBuffer( final ByteBuffer byteBuffer ) { final CompositeParser parser = Composites.newCompositeParser( byteBuffer ); //read back the id final Id orgId = ID_SER.fromComposite( parser ); final K value = keySerializer.fromComposite( parser ); return new ScopedRowKey<K>( orgId, value ); } }
@Override public ByteBuffer toByteBuffer( final ScopedRowKey<CollectionPrefixedKey<K>> scopedRowKey ) { final CompositeBuilder builder = Composites.newCompositeBuilder(); //add the organization's id ID_SER.toComposite( builder, scopedRowKey.getScope() ); final CollectionPrefixedKey<K> key = scopedRowKey.getKey(); //add the scope's owner id to the composite ID_SER.toComposite( builder, key.getOwner() ); //add the scope's name builder.addString( key.getCollectionName() ); //add the key type keySerializer.toComposite( builder, key.getSubKey() ); //addOtherComponents( builder, scopedRowKey ); return builder.build(); }
@Override public RowKeyType fromComposite( final CompositeParser composite ) { final Id id = ID_SER.fromComposite( composite ); final String edgeType = composite.readString(); final String idType = composite.readString(); final long shard = composite.readLong(); return new RowKeyType( id, edgeType, idType, shard); }
@Override public ByteBuffer toByteBuffer( final BucketScopedRowKey<K> scopedRowKey ) { final CompositeBuilder builder = Composites.newCompositeBuilder(); //add the organization's id ID_SER.toComposite( builder, scopedRowKey.getScope() ); //add the bucket builder.addInteger( scopedRowKey.getBucketNumber() ); //add the key type keySerializer.toComposite( builder, scopedRowKey.getKey() ); return builder.build(); }
@Override public BucketScopedRowKey<K> fromByteBuffer( final ByteBuffer byteBuffer ) { final CompositeParser parser = Composites.newCompositeParser( byteBuffer ); //read back the id final Id orgId = ID_SER.fromComposite( parser ); final int bucket = parser.readInteger(); final K value = keySerializer.fromComposite( parser ); return new BucketScopedRowKey<>( orgId, value, bucket ); } }
@Override public void toComposite( final CompositeBuilder builder, final DirectedEdgeMeta meta ) { final DirectedEdgeMeta.NodeMeta[] nodeMeta = meta.getNodes(); //add the stored value builder.addInteger( meta.getType().getStorageValue() ); final int length = nodeMeta.length; builder.addInteger( length ); for ( DirectedEdgeMeta.NodeMeta node : nodeMeta ) { ID_SER.toComposite( builder, node.getId() ); builder.addInteger( node.getNodeType().getStorageValue() ); } final String[] edgeTypes = meta.getTypes(); builder.addInteger( edgeTypes.length ); for ( String type : edgeTypes ) { builder.addString( type ); } }
@Override public DirectedEdgeMeta fromComposite( final CompositeParser composite ) { final int storageType = composite.readInteger(); final DirectedEdgeMeta.MetaType metaType = DirectedEdgeMeta.MetaType.fromStorage( storageType ); final int idLength = composite.readInteger(); final DirectedEdgeMeta.NodeMeta[] nodePairs = new DirectedEdgeMeta.NodeMeta[idLength]; for ( int i = 0; i < idLength; i++ ) { final Id sourceId = ID_SER.fromComposite( composite ); final NodeType type = NodeType.get( composite.readInteger() ); nodePairs[i] = new DirectedEdgeMeta.NodeMeta( sourceId, type ); } final int length = composite.readInteger(); String[] types = new String[length]; for ( int i = 0; i < length; i++ ) { types[i] = composite.readString(); } return DirectedEdgeMeta.fromStorage( metaType, nodePairs, types ); }