/** * Utility function to generate a new key from the scope */ public static <K> ScopedRowKey< K> fromKey( final Id scope, K key ) { return new ScopedRowKey<>( scope, key ); } }
@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 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 ); } }
/** * Remove the edge * * @param scope The scope * @param rowKeyId The id to use in the row key * @param edgeType The edge type * @param version The version of the edge * @param cf The column family */ private MutationBatch removeEdgeType( final ApplicationScope scope, final Id rowKeyId, final String edgeType, final long version, final MultiTenantColumnFamily<ScopedRowKey<Id>, String> cf ) { //write target<--source edge type meta data final ScopedRowKey< Id> rowKey = new ScopedRowKey< Id>( scope.getApplication(), rowKeyId ); final MutationBatch batch = keyspace.prepareMutationBatch().withTimestamp( version ); batch.withRow( cf, rowKey ).deleteColumn( edgeType ); return batch; }
/** * Remove the id type * * @param scope The scope to use * @param rowId The id to use in the row key * @param idType The id type to use in the column * @param edgeType The edge type to use in the column * @param version The version to use on the column * @param cf The column family to use * * @return A populated mutation with the remove operations */ private MutationBatch removeIdType( final ApplicationScope scope, final Id rowId, final String idType, final String edgeType, final long version, final MultiTenantColumnFamily<ScopedRowKey<EdgeIdTypeKey>, String> cf ) { final MutationBatch batch = keyspace.prepareMutationBatch().withTimestamp( version ); //write target<--source edge type and id type to meta data final ScopedRowKey< EdgeIdTypeKey> rowKey = new ScopedRowKey<>( scope.getApplication(), new EdgeIdTypeKey( rowId, edgeType ) ); batch.withRow( cf, rowKey ).deleteColumn( idType ); return batch; }
@Override public MutationBatch writeEdge( final ApplicationScope scope, final Edge edge ) { ValidationUtils.validateApplicationScope( scope ); GraphValidation.validateEdge( edge ); final Id scopeId = scope.getApplication(); final Id source = edge.getSourceNode(); final Id target = edge.getTargetNode(); final String edgeType = edge.getType(); final long timestamp = edge.getTimestamp(); final MutationBatch batch = keyspace.prepareMutationBatch().withConsistencyLevel( cassandraConfig.getWriteCL() ) .withTimestamp( timestamp ); //add source->target edge type to meta data final ScopedRowKey< Id> sourceKey = new ScopedRowKey<>( scopeId, source ); batch.withRow( CF_SOURCE_EDGE_TYPES, sourceKey ).putColumn( edgeType, HOLDER ); //write source->target edge type and id type to meta data EdgeIdTypeKey tk = new EdgeIdTypeKey( source, edgeType ); final ScopedRowKey<EdgeIdTypeKey> sourceTypeKey = new ScopedRowKey<>( scopeId, tk ); batch.withRow( CF_SOURCE_EDGE_ID_TYPES, sourceTypeKey ).putColumn( target.getType(), HOLDER ); //write target<--source edge type meta data final ScopedRowKey< Id> targetKey = new ScopedRowKey<>( scopeId, target ); batch.withRow( CF_TARGET_EDGE_TYPES, targetKey ).putColumn( edgeType, HOLDER ); //write target<--source edge type and id type to meta data final ScopedRowKey<EdgeIdTypeKey> targetTypeKey = new ScopedRowKey<>( scopeId, new EdgeIdTypeKey( target, edgeType ) ); batch.withRow( CF_TARGET_EDGE_ID_TYPES, targetTypeKey ).putColumn( source.getType(), HOLDER ); return batch; }
/** * Get the edge types from the search criteria. * * @param scope The org scope * @param search The edge type search info * @param cf The column family to execute on */ private Iterator<String> getEdgeTypes( final ApplicationScope scope, final SearchEdgeType search, final MultiTenantColumnFamily<ScopedRowKey<Id>, String> cf ) { ValidationUtils.validateApplicationScope( scope ); GraphValidation.validateSearchEdgeType( search ); final ScopedRowKey< Id> sourceKey = new ScopedRowKey<>( scope.getApplication(), search.getNode() ); //resume from the last if specified. Also set the range final RangeBuilder rangeBuilder = createRange( search ); RowQuery<ScopedRowKey<Id>, String> query = keyspace.prepareQuery( cf ).getKey( sourceKey ).autoPaginate( true ) .withColumnRange( rangeBuilder.build() ); return new ColumnNameIterator<>( query, PARSER, search.getLast().isPresent() ); }
/** * Get the id types from the specified column family * * @param scope The organization scope to use * @param search The search criteria * @param cf The column family to search */ public Iterator<String> getIdTypes( final ApplicationScope scope, final SearchIdType search, final MultiTenantColumnFamily<ScopedRowKey<EdgeIdTypeKey>, String> cf ) { ValidationUtils.validateApplicationScope( scope ); GraphValidation.validateSearchEdgeIdType( search ); final ScopedRowKey<EdgeIdTypeKey> sourceTypeKey = new ScopedRowKey<>( scope.getApplication(), new EdgeIdTypeKey( search.getNode(), search.getEdgeType() ) ); final RangeBuilder rangeBuilder = createRange( search ); RowQuery<ScopedRowKey<EdgeIdTypeKey>, String> query = keyspace.prepareQuery( cf ).getKey( sourceTypeKey ).autoPaginate( true ) .withColumnRange( rangeBuilder.build() ); return new ColumnNameIterator<>( query, PARSER, search.getLast().isPresent() ); }