private static String tokenById( TokenHolder tokenHolder, int tokenId, String tokenName ) { try { return tokenHolder.getTokenById( tokenId ).name(); } catch ( TokenNotFoundException e ) { // Ignore errors from reading key } return format( "%s[%d]", tokenName, tokenId ); } }
@Override public RelationshipType getRelationshipTypeById( int type ) { try { String name = tokenHolders.relationshipTypeTokens().getTokenById( type ).name(); return RelationshipType.withName( name ); } catch ( TokenNotFoundException e ) { throw new IllegalStateException( "Kernel API returned non-existent relationship type: " + type ); } }
private BatchRelationship batchRelationshipOf( long id, int type, long startNode, long endNode ) { try { return new BatchRelationship( id, startNode, endNode, RelationshipType.withName( tokenHolders.relationshipTypeTokens().getTokenById( type ).name() ) ); } catch ( TokenNotFoundException e ) { throw new RuntimeException( e ); } }
@Override public Label apply( long from ) { try { return label( tokenHolders.labelTokens().getTokenById( safeCastLongToInt( from ) ).name() ); } catch ( TokenNotFoundException e ) { throw new RuntimeException( e ); } } };
@Before public void setup() throws TokenNotFoundException { when(tokens.getTokenById( nonIndexedProperty )).thenReturn( new NamedToken( nonIndexedPropertyName, nonIndexedProperty ) ); when(tokens.getTokenById( indexedProperty )).thenReturn( new NamedToken( indexedPropertyName, indexedProperty ) ); when(tokens.getTokenById( indexedProperty2 )).thenReturn( new NamedToken( indexedPropertyName, indexedProperty2 ) ); index.enabled( true ); }
@Override public long countIndexedNodes( long nodeId, int[] propertyKeyIds, Value... propertyValues ) { try { String[] propertyKeys = new String[propertyKeyIds.length]; for ( int i = 0; i < propertyKeyIds.length; i++ ) { propertyKeys[i] = propertyKeyTokenHolder.getTokenById( propertyKeyIds[i] ).name(); } Query query = LuceneFulltextDocumentStructure.newCountNodeEntriesQuery( nodeId, propertyKeys, propertyValues ); TotalHitCountCollector collector = new TotalHitCountCollector(); getIndexSearcher().search( query, collector ); return collector.getTotalHits(); } catch ( Exception e ) { throw new RuntimeException( e ); } } }
@Override public String nodeLabelName( int labelId ) throws LabelNotFoundKernelException { ktx.assertOpen(); try { return tokenHolders.labelTokens().getTokenById( labelId ).name(); } catch ( TokenNotFoundException e ) { throw new LabelNotFoundKernelException( labelId, e ); } }
@Override public String relationshipTypeName( int relationshipTypeId ) throws RelationshipTypeIdNotFoundKernelException { ktx.assertOpen(); try { return tokenHolders.relationshipTypeTokens().getTokenById( relationshipTypeId ).name(); } catch ( TokenNotFoundException e ) { throw new RelationshipTypeIdNotFoundKernelException( relationshipTypeId, e ); } }
@Override public String propertyKeyName( int propertyKeyId ) throws PropertyKeyIdNotFoundKernelException { ktx.assertOpen(); try { return tokenHolders.propertyKeyTokens().getTokenById( propertyKeyId ).name(); } catch ( TokenNotFoundException e ) { throw new PropertyKeyIdNotFoundKernelException( propertyKeyId, e ); } }
private Map<String, Object> getPropertyChain( long nextProp ) { final Map<String, Object> map = new HashMap<>(); propertyTraverser.getPropertyChain( nextProp, recordAccess.getPropertyRecords(), propBlock -> { try { String key = tokenHolders.propertyKeyTokens().getTokenById( propBlock.getKeyIndexId() ).name(); Value propertyValue = propBlock.newPropertyValue( propertyStore ); map.put( key, propertyValue.asObject() ); } catch ( TokenNotFoundException e ) { throw new RuntimeException( e ); } } ); return map; }
static FulltextIndexDescriptor readOrInitialiseDescriptor( StoreIndexDescriptor descriptor, String defaultAnalyzerName, TokenHolder propertyKeyTokenHolder, PartitionedIndexStorage indexStorage, FileSystemAbstraction fileSystem ) { Properties indexConfiguration = new Properties(); if ( descriptor.schema() instanceof FulltextSchemaDescriptor ) { FulltextSchemaDescriptor schema = (FulltextSchemaDescriptor) descriptor.schema(); indexConfiguration.putAll( schema.getIndexConfiguration() ); } loadPersistedSettings( indexConfiguration, indexStorage, fileSystem ); boolean eventuallyConsistent = Boolean.parseBoolean( indexConfiguration.getProperty( INDEX_CONFIG_EVENTUALLY_CONSISTENT ) ); String analyzerName = indexConfiguration.getProperty( INDEX_CONFIG_ANALYZER, defaultAnalyzerName ); Analyzer analyzer = createAnalyzer( analyzerName ); List<String> names = new ArrayList<>(); for ( int propertyKeyId : descriptor.schema().getPropertyIds() ) { try { names.add( propertyKeyTokenHolder.getTokenById( propertyKeyId ).name() ); } catch ( TokenNotFoundException e ) { throw new IllegalStateException( "Property key id not found.", new PropertyKeyIdNotFoundKernelException( propertyKeyId, e ) ); } } List<String> propertyNames = Collections.unmodifiableList( names ); return new FulltextIndexDescriptor( descriptor, propertyNames, analyzer, analyzerName, eventuallyConsistent ); }
@Before public void setup() throws PropertyKeyIdNotFoundKernelException { when( transaction.tokenRead() ).thenReturn( tokenRead ); when( bridge.get() ).thenReturn( stmt ); when( tokenRead.propertyKeyName( anyInt() ) ).thenAnswer( invocationOnMock -> { int id = invocationOnMock.getArgument( 0 ); return ops.propertyKeyTokenHolder().getTokenById( id ).name(); } ); }
@Override public void propertyRemoved( ExplicitIndexWrite ops, long entityId, int propertyKey ) throws AutoIndexingKernelException { if ( enabled ) { try { String name = propertyKeyLookup.getTokenById( propertyKey ).name(); if ( propertyKeysToInclude.get().contains( name ) ) { ensureIndexExists( ops ); type.remove( ops, entityId, name ); } } catch ( KernelException e ) { throw new AutoIndexingKernelException( e ); } catch ( TokenNotFoundException e ) { // TODO: TokenNotFoundException was added before there was a kernel. It should be converted to a // KernelException now throw new AutoIndexingKernelException( new PropertyKeyIdNotFoundKernelException( propertyKey, e ) ); } } }
@Override public void propertyAdded( ExplicitIndexWrite ops, long entityId, int propertyKeyId, Value value ) throws AutoIndexingKernelException { if ( enabled ) { try { String name = propertyKeyLookup.getTokenById( propertyKeyId ).name(); if ( propertyKeysToInclude.get().contains( name ) ) { ensureIndexExists( ops ); type.add( ops, entityId, name, value.asObject() ); } } catch ( KernelException e ) { throw new AutoIndexingKernelException( e ); } catch ( TokenNotFoundException e ) { // TODO: TokenNotFoundException was added before there was a kernel. It should be converted to a // KernelException now throw new AutoIndexingKernelException( new PropertyKeyIdNotFoundKernelException( propertyKeyId, e ) ); } } }
@Override public void propertyChanged( ExplicitIndexWrite ops, long entityId, int propertyKeyId, Value oldValue, Value newValue ) throws AutoIndexingKernelException { if ( enabled ) { try { String name = propertyKeyLookup.getTokenById( propertyKeyId ).name(); if ( propertyKeysToInclude.get().contains( name ) ) { ensureIndexExists( ops ); type.remove( ops, entityId, name, oldValue.asObject() ); type.add( ops, entityId, name, newValue.asObject() ); } } catch ( KernelException e ) { throw new AutoIndexingKernelException( e ); } catch ( TokenNotFoundException e ) { // TODO: TokenNotFoundException was added before there was a kernel. It should be converted to a // KernelException now throw new AutoIndexingKernelException( new PropertyKeyIdNotFoundKernelException( propertyKeyId, e ) ); } } }
@Test public void batchTokenCreateMustDeduplicateTokenCreates() throws Exception { initialTokensABC(); AtomicInteger nextId = new AtomicInteger( 42 ); mockAssignNewTokenIdsInBatch( nextId ); // NOTE: the existing 'b', and the missing 'X', tokens are in here twice: String[] names = new String[]{"b", "b", "X", "a", "X", "c"}; int[] ids = new int[names.length]; holder.getOrCreateIds( names, ids ); assertThat( ids.length, is( 6 ) ); assertThat( ids[0], is( 2 ) ); assertThat( ids[1], is( 2 ) ); assertThat( ids[2], is( 42 ) ); assertThat( ids[3], is( 1 ) ); assertThat( ids[4], is( 42 ) ); assertThat( ids[5], is( 3 ) ); assertThat( nextId.get(), is( 43 ) ); // And this should not throw. holder.getTokenById( 42 ); }
@Test public void batchTokenCreateMustIgnoreExistingTokens() throws Exception { initialTokensABC(); AtomicInteger nextId = new AtomicInteger( 42 ); mockAssignNewTokenIdsInBatch( nextId ); String[] names = new String[]{"b", "X", "a", "Y", "c"}; int[] ids = new int[names.length]; holder.getOrCreateIds( names, ids ); assertThat( ids.length, is( 5 ) ); assertThat( ids[0], is( 2 ) ); assertThat( ids[1], isOneOf( 42, 43 ) ); assertThat( ids[2], is( 1 ) ); assertThat( ids[3], isOneOf( 42, 43 ) ); assertThat( ids[4], is( 3 ) ); assertThat( nextId.get(), is( 44 ) ); // And these should not throw. holder.getTokenById( 42 ); holder.getTokenById( 43 ); }
if ( data.propertyKeyId() == prop1.propertyKeyId() ) assertEquals( "prop1", propertyKeyTokenHolder.getTokenById( keyId ).name() ); assertEquals( "string2", data.value().asObject() ); relAddProperty( rel, prop1.propertyKeyId(), "-string2" ); assertEquals( "prop2", propertyKeyTokenHolder.getTokenById( keyId ).name() ); assertEquals( 2, data.value().asObject() ); relAddProperty( rel, prop2.propertyKeyId(), -2 ); assertEquals( "prop3", propertyKeyTokenHolder.getTokenById( keyId ).name() ); assertEquals( false, data.value().asObject() ); relAddProperty( rel, prop3.propertyKeyId(), true );
if ( data.propertyKeyId() == prop1.propertyKeyId() ) assertEquals( "prop1", propertyKeyTokenHolder.getTokenById( keyId ).name() ); assertEquals( "string1", data.value().asObject() ); relAddProperty( rel, prop1.propertyKeyId(), "-string1" ); assertEquals( "prop2", propertyKeyTokenHolder.getTokenById( keyId ).name() ); assertEquals( 1, data.value().asObject() ); relAddProperty( rel, prop2.propertyKeyId(), -1 ); assertEquals( "prop3", propertyKeyTokenHolder.getTokenById( keyId ).name() ); assertEquals( true, data.value().asObject() ); relAddProperty( rel, prop3.propertyKeyId(), false );
if ( data.propertyKeyId() == prop1.propertyKeyId() ) assertEquals( "prop1", propertyKeyTokenHolder.getTokenById( keyId ).name() ); assertEquals( "-string2", data.value().asObject() ); assertEquals( "prop2", propertyKeyTokenHolder.getTokenById( keyId ).name() ); assertEquals( -2, data.value().asObject() ); assertEquals( "prop3", propertyKeyTokenHolder.getTokenById( keyId ).name() ); assertEquals( true, data.value().asObject() ); transaction.nodeDoRemoveProperty( node, prop3.propertyKeyId() );