private SchemaRule serialiseAndDeserialise( StoreIndexDescriptor indexRule ) throws MalformedSchemaRuleException { ByteBuffer buffer = ByteBuffer.wrap( SchemaRuleSerialization.serialize( indexRule ) ); return SchemaRuleSerialization.deserialize( indexRule.getId(), buffer ); }
/** * Parse a SchemaRule from the provided buffer. * * @param id the id to give the returned Schema Rule * @param source the buffer to parse from * @return a SchemaRule * @throws MalformedSchemaRuleException if bytes in the buffer do encode a valid SchemaRule */ public static SchemaRule deserialize( long id, ByteBuffer source ) throws MalformedSchemaRuleException { int legacyLabelOrRelTypeId = source.getInt(); byte schemaRuleType = source.get(); switch ( schemaRuleType ) { case INDEX_RULE: return readIndexRule( id, source ); case CONSTRAINT_RULE: return readConstraintRule( id, source ); default: if ( SchemaRuleDeserializer2_0to3_1.isLegacySchemaRule( schemaRuleType ) ) { return SchemaRuleDeserializer2_0to3_1.deserialize( id, legacyLabelOrRelTypeId, schemaRuleType, source ); } throw new MalformedSchemaRuleException( format( "Got unknown schema rule type '%d'.", schemaRuleType ) ); } }
private static SchemaDescriptor readSchema( ByteBuffer source ) throws MalformedSchemaRuleException { int[] propertyIds; byte schemaDescriptorType = source.get(); switch ( schemaDescriptorType ) { case SIMPLE_LABEL: int labelId = source.getInt(); propertyIds = readTokenIdList( source ); return SchemaDescriptorFactory.forLabel( labelId, propertyIds ); case SIMPLE_REL_TYPE: int relTypeId = source.getInt(); propertyIds = readTokenIdList( source ); return SchemaDescriptorFactory.forRelType( relTypeId, propertyIds ); case GENERIC_MULTI_TOKEN_TYPE: return readMultiTokenSchema( source ); default: throw new MalformedSchemaRuleException( format( "Got unknown schema descriptor type '%d'.", schemaDescriptorType ) ); } }
private static StoreIndexDescriptor readIndexRule( long id, ByteBuffer source ) throws MalformedSchemaRuleException { IndexProviderDescriptor indexProvider = readIndexProviderDescriptor( source ); byte indexRuleType = source.get(); Optional<String> name; switch ( indexRuleType ) { case GENERAL_INDEX: { SchemaDescriptor schema = readSchema( source ); name = readRuleName( source ); return IndexDescriptorFactory.forSchema( schema, name, indexProvider ).withId( id ); } case UNIQUE_INDEX: { long owningConstraint = source.getLong(); SchemaDescriptor schema = readSchema( source ); name = readRuleName( source ); IndexDescriptor descriptor = IndexDescriptorFactory.uniqueForSchema( schema, name, indexProvider ); return owningConstraint == NO_OWNING_CONSTRAINT_YET ? descriptor.withId( id ) : descriptor.withIds( id, owningConstraint ); } default: throw new MalformedSchemaRuleException( format( "Got unknown index rule type '%d'.", indexRuleType ) ); } }
private static ConstraintRule readConstraintRule( long id, ByteBuffer source ) throws MalformedSchemaRuleException { SchemaDescriptor schema; byte constraintRuleType = source.get(); String name; switch ( constraintRuleType ) { case EXISTS_CONSTRAINT: schema = readSchema( source ); name = readRuleName( source ).orElse( null ); return ConstraintRule.constraintRule( id, ConstraintDescriptorFactory.existsForSchema( schema ), name ); case UNIQUE_CONSTRAINT: long ownedUniqueIndex = source.getLong(); schema = readSchema( source ); UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForSchema( schema ); name = readRuleName( source ).orElse( null ); return ConstraintRule.constraintRule( id, descriptor, ownedUniqueIndex, name ); case UNIQUE_EXISTS_CONSTRAINT: long ownedNodeKeyIndex = source.getLong(); schema = readSchema( source ); NodeKeyConstraintDescriptor nodeKeyConstraintDescriptor = ConstraintDescriptorFactory.nodeKeyForSchema( schema ); name = readRuleName( source ).orElse( null ); return ConstraintRule.constraintRule( id, nodeKeyConstraintDescriptor, ownedNodeKeyIndex, name ); default: throw new MalformedSchemaRuleException( format( "Got unknown constraint rule type '%d'.", constraintRuleType ) ); } }
/** * Serialize the provided SchemaRule onto the target buffer * * @param schemaRule the SchemaRule to serialize */ public static byte[] serialize( SchemaRule schemaRule ) { if ( schemaRule instanceof StoreIndexDescriptor ) { return serialize( (StoreIndexDescriptor)schemaRule ); } else if ( schemaRule instanceof ConstraintRule ) { return serialize( (ConstraintRule)schemaRule ); } throw new IllegalStateException( "Unknown schema rule type: " + schemaRule.getClass() ); }
static SchemaRule readSchemaRule( long id, Collection<DynamicRecord> records, byte[] buffer ) throws MalformedSchemaRuleException { ByteBuffer scratchBuffer = concatData( records, buffer ); return SchemaRuleSerialization.deserialize( id, scratchBuffer ); } }
ByteBuffer target = ByteBuffer.allocate( lengthOf( constraintRule ) ); target.putInt( LEGACY_LABEL_OR_REL_TYPE_ID ); target.put( CONSTRAINT_RULE );
public List<DynamicRecord> allocateFrom( SchemaRule rule ) { List<DynamicRecord> records = new ArrayList<>(); DynamicRecord record = getRecord( rule.getId(), nextRecord(), CHECK ); DynamicRecordAllocator recordAllocator = new ReusableRecordsCompositeAllocator( singleton( record ), this ); allocateRecordsFromBytes( records, SchemaRuleSerialization.serialize( rule ), recordAllocator ); return records; }
private SchemaRule readSchemaRule( Collection<DynamicRecord> recordsBefore ) { // TODO: Why was this assertion here? // assert first(recordsBefore).inUse() : "Asked to deserialize schema records that were not in // use."; SchemaRule rule; ByteBuffer deserialized = AbstractDynamicStore.concatData( recordsBefore, new byte[100] ); try { rule = SchemaRuleSerialization.deserialize( Iterables.first( recordsBefore ).getId(), deserialized ); } catch ( MalformedSchemaRuleException e ) { return null; } return rule; }
private static StoreIndexDescriptor readIndexRule( long id, ByteBuffer source ) throws MalformedSchemaRuleException { IndexProviderDescriptor indexProvider = readIndexProviderDescriptor( source ); byte indexRuleType = source.get(); Optional<String> name; switch ( indexRuleType ) { case GENERAL_INDEX: { SchemaDescriptor schema = readSchema( source ); name = readRuleName( source ); return IndexDescriptorFactory.forSchema( schema, name, indexProvider ).withId( id ); } case UNIQUE_INDEX: { long owningConstraint = source.getLong(); SchemaDescriptor schema = readSchema( source ); name = readRuleName( source ); IndexDescriptor descriptor = IndexDescriptorFactory.uniqueForSchema( schema, name, indexProvider ); return owningConstraint == NO_OWNING_CONSTRAINT_YET ? descriptor.withId( id ) : descriptor.withIds( id, owningConstraint ); } default: throw new MalformedSchemaRuleException( format( "Got unknown index rule type '%d'.", indexRuleType ) ); } }
ByteBuffer target = ByteBuffer.allocate( lengthOf( indexDescriptor ) ); target.putInt( LEGACY_LABEL_OR_REL_TYPE_ID ); target.put( INDEX_RULE );
private static SchemaDescriptor readSchema( ByteBuffer source ) throws MalformedSchemaRuleException { int[] propertyIds; byte schemaDescriptorType = source.get(); switch ( schemaDescriptorType ) { case SIMPLE_LABEL: int labelId = source.getInt(); propertyIds = readTokenIdList( source ); return SchemaDescriptorFactory.forLabel( labelId, propertyIds ); case SIMPLE_REL_TYPE: int relTypeId = source.getInt(); propertyIds = readTokenIdList( source ); return SchemaDescriptorFactory.forRelType( relTypeId, propertyIds ); case GENERIC_MULTI_TOKEN_TYPE: return readMultiTokenSchema( source ); default: throw new MalformedSchemaRuleException( format( "Got unknown schema descriptor type '%d'.", schemaDescriptorType ) ); } }
private static ConstraintRule readConstraintRule( long id, ByteBuffer source ) throws MalformedSchemaRuleException { SchemaDescriptor schema; byte constraintRuleType = source.get(); String name; switch ( constraintRuleType ) { case EXISTS_CONSTRAINT: schema = readSchema( source ); name = readRuleName( source ).orElse( null ); return ConstraintRule.constraintRule( id, ConstraintDescriptorFactory.existsForSchema( schema ), name ); case UNIQUE_CONSTRAINT: long ownedUniqueIndex = source.getLong(); schema = readSchema( source ); UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForSchema( schema ); name = readRuleName( source ).orElse( null ); return ConstraintRule.constraintRule( id, descriptor, ownedUniqueIndex, name ); case UNIQUE_EXISTS_CONSTRAINT: long ownedNodeKeyIndex = source.getLong(); schema = readSchema( source ); NodeKeyConstraintDescriptor nodeKeyConstraintDescriptor = ConstraintDescriptorFactory.nodeKeyForSchema( schema ); name = readRuleName( source ).orElse( null ); return ConstraintRule.constraintRule( id, nodeKeyConstraintDescriptor, ownedNodeKeyIndex, name ); default: throw new MalformedSchemaRuleException( format( "Got unknown constraint rule type '%d'.", constraintRuleType ) ); } }
private SchemaRule serialiseAndDeserialise( ConstraintRule constraintRule ) throws MalformedSchemaRuleException { ByteBuffer buffer = ByteBuffer.wrap( SchemaRuleSerialization.serialize( constraintRule ) ); return SchemaRuleSerialization.deserialize( constraintRule.getId(), buffer ); }
private static Collection<DynamicRecord> serializeRule( SchemaRule rule, DynamicRecord... records ) { byte[] data = SchemaRuleSerialization.serialize( rule ); DynamicRecordAllocator dynamicRecordAllocator = new ReusableRecordsCompositeAllocator( asList( records ), schemaAllocator ); Collection<DynamicRecord> result = new ArrayList<>(); AbstractDynamicStore.allocateRecordsFromBytes( result, data, dynamicRecordAllocator ); return result; }
private SchemaRule readSchemaRule( Collection<DynamicRecord> recordsBefore ) { // TODO: Why was this assertion here? // assert first(recordsBefore).inUse() : "Asked to deserialize schema records that were not in // use."; SchemaRule rule; ByteBuffer deserialized = AbstractDynamicStore.concatData( recordsBefore, new byte[100] ); try { rule = SchemaRuleSerialization.deserialize( Iterables.first( recordsBefore ).getId(), deserialized ); } catch ( MalformedSchemaRuleException e ) { return null; } return rule; }
ByteBuffer target = ByteBuffer.allocate( lengthOf( constraintRule ) ); target.putInt( LEGACY_LABEL_OR_REL_TYPE_ID ); target.put( CONSTRAINT_RULE );