Search in sources :

Example 56 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class SchemaRuleMigrationTest method constraintsWithoutNamesMustBeGivenGeneratedOnes.

@Test
void constraintsWithoutNamesMustBeGivenGeneratedOnes() throws KernelException {
    SchemaRule rule = ConstraintDescriptorFactory.uniqueForSchema(SchemaDescriptor.forLabel(1, 2)).withId(1);
    srcTokenHolders.labelTokens().setInitialTokens(List.of(new NamedToken("Label", 1)));
    srcTokenHolders.propertyKeyTokens().setInitialTokens(List.of(new NamedToken("prop", 2)));
    when(src.getAll(any())).thenReturn(List.of(rule));
    RecordStorageMigrator.migrateSchemaRules(srcTokenHolders, src, dst, NULL);
    assertEquals(1, writtenRules.size());
    assertEquals("constraint_952591e6", writtenRules.get(0).getName());
}
Also used : SchemaRule(org.neo4j.internal.schema.SchemaRule) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 57 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class SchemaRuleMigrationTest method mustEnsureThatMigratedSchemaRuleNamesAreUnique.

@Test
void mustEnsureThatMigratedSchemaRuleNamesAreUnique() throws KernelException {
    SchemaRule rule1 = IndexPrototype.forSchema(SchemaDescriptor.forLabel(1, 2)).withName("a").materialise(1);
    SchemaRule rule2 = IndexPrototype.forSchema(SchemaDescriptor.forLabel(1, 3)).withName("a").materialise(2);
    srcTokenHolders.labelTokens().setInitialTokens(List.of(new NamedToken("Label", 1)));
    srcTokenHolders.propertyKeyTokens().setInitialTokens(List.of(new NamedToken("a", 2), new NamedToken("b", 3)));
    when(src.getAll(any())).thenReturn(List.of(rule1, rule2));
    RecordStorageMigrator.migrateSchemaRules(srcTokenHolders, src, dst, NULL);
    long distinctNames = writtenRules.stream().map(SchemaRule::getName).distinct().count();
    assertEquals(2, distinctNames);
}
Also used : SchemaRule(org.neo4j.internal.schema.SchemaRule) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 58 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class SchemaRuleMigrationTest method mustEnsureUniqueNamesEvenWhenOldNamesMatchesNewDefaults.

@Test
void mustEnsureUniqueNamesEvenWhenOldNamesMatchesNewDefaults() throws KernelException {
    SchemaRule rule1 = ConstraintDescriptorFactory.uniqueForSchema(SchemaDescriptor.forLabel(1, 2)).withId(1);
    SchemaRule rule2 = ConstraintDescriptorFactory.uniqueForSchema(SchemaDescriptor.forLabel(1, 2)).withId(2);
    srcTokenHolders.labelTokens().setInitialTokens(List.of(new NamedToken("Label", 1)));
    srcTokenHolders.propertyKeyTokens().setInitialTokens(List.of(new NamedToken("prop", 2), new NamedToken("bla", 3)));
    when(src.getAll(any())).thenReturn(List.of(rule1, rule2));
    RecordStorageMigrator.migrateSchemaRules(srcTokenHolders, src, dst, NULL);
    assertEquals(2, writtenRules.size());
    Set<String> names = writtenRules.stream().map(SchemaRule::getName).collect(Collectors.toSet());
    // Collisions in generated names (however fantastically unlikely) must be resolved by appending a count.
    assertEquals(Set.of("constraint_952591e6", "constraint_952591e6_1"), names);
}
Also used : SchemaRule(org.neo4j.internal.schema.SchemaRule) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 59 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class FulltextIndexProvider method validateIndexRef.

private void validateIndexRef(IndexRef<?> ref) {
    String providerName = getProviderDescriptor().name();
    if (ref.getIndexType() != IndexType.FULLTEXT) {
        throw new IllegalArgumentException("The '" + providerName + "' index provider only supports FULLTEXT index types: " + ref);
    }
    if (!ref.schema().isFulltextSchemaDescriptor()) {
        throw new IllegalArgumentException("The " + ref.schema() + " index schema is not a full-text index schema, " + "which it is required to be for the '" + providerName + "' index provider to be able to create an index.");
    }
    Value value = ref.getIndexConfig().get(ANALYZER);
    if (value != null) {
        if (value.valueGroup() == ValueGroup.TEXT) {
            String analyzerName = ((TextValue) value).stringValue();
            Optional<AnalyzerProvider> analyzerProvider = listAvailableAnalyzers().filter(analyzer -> analyzer.getName().equals(analyzerName)).findFirst();
            if (analyzerProvider.isPresent()) {
                // Verify that the analyzer provider works.
                Analyzer analyzer = analyzerProvider.get().createAnalyzer();
                Objects.requireNonNull(analyzer, "The '" + analyzerName + "' analyzer returned a 'null' analyzer.");
            } else {
                throw new IllegalArgumentException("No such full-text analyzer: '" + analyzerName + "'.");
            }
        } else {
            throw new IllegalArgumentException("Wrong index setting value type for fulltext analyzer: '" + value + "'.");
        }
    }
    TokenHolder propertyKeyTokens = tokenHolders.propertyKeyTokens();
    for (int propertyId : ref.schema().getPropertyIds()) {
        try {
            NamedToken token = propertyKeyTokens.getTokenById(propertyId);
            if (token.name().equals(LuceneFulltextDocumentStructure.FIELD_ENTITY_ID)) {
                throw new IllegalArgumentException("Unable to index the property, the name is reserved for internal use " + LuceneFulltextDocumentStructure.FIELD_ENTITY_ID);
            }
        } catch (TokenNotFoundException e) {
            throw new IllegalArgumentException("Schema references non-existing property key token id: " + propertyId + ".", e);
        }
    }
}
Also used : AnalyzerProvider(org.neo4j.graphdb.schema.AnalyzerProvider) DroppableLuceneIndex(org.neo4j.kernel.api.impl.index.DroppableLuceneIndex) SchemaIndex(org.neo4j.kernel.api.impl.schema.SchemaIndex) Log(org.neo4j.logging.Log) CursorContext(org.neo4j.io.pagecache.context.CursorContext) TokenNameLookup(org.neo4j.common.TokenNameLookup) IndexType(org.neo4j.internal.schema.IndexType) IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) Config(org.neo4j.configuration.Config) DatabaseIndex(org.neo4j.kernel.api.impl.index.DatabaseIndex) Value(org.neo4j.values.storable.Value) IndexSamplingConfig(org.neo4j.kernel.impl.api.index.IndexSamplingConfig) StringUtils(org.apache.commons.lang3.StringUtils) FulltextIndexSettings.createAnalyzer(org.neo4j.kernel.api.impl.fulltext.FulltextIndexSettings.createAnalyzer) LuceneSchemaIndexBuilder(org.neo4j.kernel.api.impl.schema.LuceneSchemaIndexBuilder) DroppableIndex(org.neo4j.kernel.api.impl.index.DroppableIndex) IndexProviderDescriptor(org.neo4j.internal.schema.IndexProviderDescriptor) Exceptions(org.neo4j.internal.helpers.Exceptions) PageCache(org.neo4j.io.pagecache.PageCache) StringUtils.defaultIfEmpty(org.apache.commons.lang3.StringUtils.defaultIfEmpty) IndexStorageFactory(org.neo4j.kernel.api.impl.index.storage.IndexStorageFactory) PartitionedIndexStorage(org.neo4j.kernel.api.impl.index.storage.PartitionedIndexStorage) TextValue(org.neo4j.values.storable.TextValue) MinimalIndexAccessor(org.neo4j.kernel.api.index.MinimalIndexAccessor) Services(org.neo4j.service.Services) Objects(java.util.Objects) Stream(java.util.stream.Stream) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) Optional(java.util.Optional) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) DatabaseReadOnlyChecker(org.neo4j.configuration.helpers.DatabaseReadOnlyChecker) DirectoryFactory(org.neo4j.kernel.api.impl.index.storage.DirectoryFactory) IndexCapability(org.neo4j.internal.schema.IndexCapability) FulltextSettings(org.neo4j.configuration.FulltextSettings) InternalIndexState(org.neo4j.internal.kernel.api.InternalIndexState) ANALYZER(org.neo4j.kernel.api.impl.fulltext.FulltextIndexSettingsKeys.ANALYZER) LuceneMinimalIndexAccessor(org.neo4j.kernel.api.impl.index.LuceneMinimalIndexAccessor) IndexAccessor(org.neo4j.kernel.api.index.IndexAccessor) ReadOnlyIndexPartitionFactory(org.neo4j.kernel.api.impl.index.partition.ReadOnlyIndexPartitionFactory) IndexProvider(org.neo4j.kernel.api.index.IndexProvider) ByteBufferFactory(org.neo4j.io.memory.ByteBufferFactory) Values(org.neo4j.values.storable.Values) TokenHolder(org.neo4j.token.api.TokenHolder) JobScheduler(org.neo4j.scheduler.JobScheduler) IndexConfig(org.neo4j.internal.schema.IndexConfig) MemoryTracker(org.neo4j.memory.MemoryTracker) FulltextIndexSettings.isEventuallyConsistent(org.neo4j.kernel.api.impl.fulltext.FulltextIndexSettings.isEventuallyConsistent) Analyzer(org.apache.lucene.analysis.Analyzer) IOException(java.io.IOException) IndexDirectoryStructure(org.neo4j.kernel.api.index.IndexDirectoryStructure) StoreMigrationParticipant(org.neo4j.storageengine.migration.StoreMigrationParticipant) StorageEngineFactory(org.neo4j.storageengine.api.StorageEngineFactory) TokenHolders(org.neo4j.token.TokenHolders) TokenNotFoundException(org.neo4j.token.api.TokenNotFoundException) FulltextIndexSettings.createPropertyNames(org.neo4j.kernel.api.impl.fulltext.FulltextIndexSettings.createPropertyNames) NamedToken(org.neo4j.token.api.NamedToken) ValueGroup(org.neo4j.values.storable.ValueGroup) SchemaIndexMigrator(org.neo4j.storageengine.migration.SchemaIndexMigrator) IndexRef(org.neo4j.internal.schema.IndexRef) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) AnalyzerProvider(org.neo4j.graphdb.schema.AnalyzerProvider) TextValue(org.neo4j.values.storable.TextValue) Value(org.neo4j.values.storable.Value) TextValue(org.neo4j.values.storable.TextValue) TokenHolder(org.neo4j.token.api.TokenHolder) FulltextIndexSettings.createAnalyzer(org.neo4j.kernel.api.impl.fulltext.FulltextIndexSettings.createAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) NamedToken(org.neo4j.token.api.NamedToken) TokenNotFoundException(org.neo4j.token.api.TokenNotFoundException)

Example 60 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class SchemaCalculator method addNamesToCollection.

private static void addNamesToCollection(Iterator<NamedToken> labelIterator, Map<Integer, String> collection) {
    while (labelIterator.hasNext()) {
        NamedToken label = labelIterator.next();
        collection.put(label.id(), label.name());
    }
}
Also used : NamedToken(org.neo4j.token.api.NamedToken)

Aggregations

NamedToken (org.neo4j.token.api.NamedToken)63 Test (org.junit.jupiter.api.Test)41 ArrayList (java.util.ArrayList)6 TokenNotFoundException (org.neo4j.token.api.TokenNotFoundException)6 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)5 SchemaRule (org.neo4j.internal.schema.SchemaRule)5 NonUniqueTokenException (org.neo4j.token.api.NonUniqueTokenException)5 TokenHolder (org.neo4j.token.api.TokenHolder)5 AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 IntSupplier (java.util.function.IntSupplier)3 KernelException (org.neo4j.exceptions.KernelException)3 Iterators (org.neo4j.internal.helpers.collection.Iterators)3 IdCapacityExceededException (org.neo4j.internal.id.IdCapacityExceededException)3 Token (org.neo4j.internal.kernel.api.Token)3 TokenWrite.checkValidTokenName (org.neo4j.internal.kernel.api.TokenWrite.checkValidTokenName)3 LabelNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.LabelNotFoundKernelException)3 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)3 RelationshipTypeIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException)3