Search in sources :

Example 1 with NamedToken

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

the class RecordLoading method safeLoadTokens.

static <RECORD extends TokenRecord> List<NamedToken> safeLoadTokens(TokenStore<RECORD> tokenStore, CursorContext cursorContext) {
    long highId = tokenStore.getHighId();
    List<NamedToken> tokens = new ArrayList<>();
    DynamicStringStore nameStore = tokenStore.getNameStore();
    List<DynamicRecord> nameRecords = new ArrayList<>();
    MutableLongSet seenRecordIds = new LongHashSet();
    int nameBlockSize = nameStore.getRecordDataSize();
    try (RecordReader<RECORD> tokenReader = new RecordReader<>(tokenStore, true, cursorContext);
        RecordReader<DynamicRecord> nameReader = new RecordReader<>(nameStore, false, cursorContext)) {
        for (long id = 0; id < highId; id++) {
            RECORD record = tokenReader.read(id);
            nameRecords.clear();
            if (record.inUse()) {
                String name;
                if (!NULL_REFERENCE.is(record.getNameId()) && safeLoadDynamicRecordChain(r -> nameRecords.add(r.copy()), nameReader, seenRecordIds, record.getNameId(), nameBlockSize)) {
                    record.addNameRecords(nameRecords);
                    name = tokenStore.getStringFor(record, cursorContext);
                } else {
                    name = format("<name not loaded due to token(%d) referencing unused name record>", id);
                }
                tokens.add(new NamedToken(name, toIntExact(id), record.isInternal()));
            }
        }
    }
    return tokens;
}
Also used : DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) ThrowingIntFunction(org.neo4j.function.ThrowingIntFunction) CursorContext(org.neo4j.io.pagecache.context.CursorContext) Value(org.neo4j.values.storable.Value) ArrayList(java.util.ArrayList) IntObjectMap(org.eclipse.collections.api.map.primitive.IntObjectMap) InlineNodeLabels(org.neo4j.kernel.impl.store.InlineNodeLabels) NodeLabelsField(org.neo4j.kernel.impl.store.NodeLabelsField) TokenHolder(org.neo4j.token.api.TokenHolder) MutableLongCollection(org.eclipse.collections.api.collection.primitive.MutableLongCollection) NeoStores(org.neo4j.kernel.impl.store.NeoStores) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) BiConsumer(java.util.function.BiConsumer) AbstractBaseRecord(org.neo4j.kernel.impl.store.record.AbstractBaseRecord) ConsistencyReport(org.neo4j.consistency.report.ConsistencyReport) TokenRecord(org.neo4j.kernel.impl.store.record.TokenRecord) Math.toIntExact(java.lang.Math.toIntExact) NO_VALUE(org.neo4j.values.storable.Values.NO_VALUE) MutableIntCollection(org.eclipse.collections.api.collection.primitive.MutableIntCollection) MutablePrimitiveObjectMap(org.eclipse.collections.api.map.primitive.MutablePrimitiveObjectMap) NULL_REFERENCE(org.neo4j.kernel.impl.store.record.Record.NULL_REFERENCE) PropertyRecord(org.neo4j.kernel.impl.store.record.PropertyRecord) RecordType(org.neo4j.consistency.RecordType) TokenStore(org.neo4j.kernel.impl.store.TokenStore) String.format(java.lang.String.format) DynamicRecord(org.neo4j.kernel.impl.store.record.DynamicRecord) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) Consumer(java.util.function.Consumer) DynamicNodeLabels(org.neo4j.kernel.impl.store.DynamicNodeLabels) RecordStore(org.neo4j.kernel.impl.store.RecordStore) List(java.util.List) PrimitiveRecord(org.neo4j.kernel.impl.store.record.PrimitiveRecord) RelationshipGroupRecord(org.neo4j.kernel.impl.store.record.RelationshipGroupRecord) TokenNotFoundException(org.neo4j.token.api.TokenNotFoundException) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) NamedToken(org.neo4j.token.api.NamedToken) DynamicStringStore(org.neo4j.kernel.impl.store.DynamicStringStore) NodeRecord(org.neo4j.kernel.impl.store.record.NodeRecord) PropertySchemaType(org.neo4j.internal.schema.PropertySchemaType) RecordLoad(org.neo4j.kernel.impl.store.record.RecordLoad) RelationshipRecord(org.neo4j.kernel.impl.store.record.RelationshipRecord) DynamicStringStore(org.neo4j.kernel.impl.store.DynamicStringStore) ArrayList(java.util.ArrayList) LongHashSet(org.eclipse.collections.impl.set.mutable.primitive.LongHashSet) MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) NamedToken(org.neo4j.token.api.NamedToken)

Example 2 with NamedToken

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

the class PlainOperationsTest method indexedBackedConstraintCreateMustThrowOnRelationshipSchemas.

@Test
void indexedBackedConstraintCreateMustThrowOnRelationshipSchemas() throws Exception {
    // given
    when(tokenHolders.relationshipTypeTokens().getTokenById(anyInt())).thenReturn(new NamedToken("RelType", 123));
    when(tokenHolders.propertyKeyTokens().getTokenById(anyInt())).thenReturn(new NamedToken("prop", 456));
    SchemaDescriptor schema = SchemaDescriptor.forRelType(this.schema.getEntityTokenIds()[0], this.schema.getPropertyIds());
    IndexPrototype prototype = IndexPrototype.uniqueForSchema(schema).withName("constraint name").withIndexProvider(GenericNativeIndexProvider.DESCRIPTOR);
    IndexDescriptor constraintIndex = prototype.materialise(42);
    when(constraintIndexCreator.createUniquenessConstraintIndex(any(), any(), eq(prototype))).thenReturn(constraintIndex);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexProxy.getDescriptor()).thenReturn(constraintIndex);
    when(indexingService.getIndexProxy(constraintIndex)).thenReturn(indexProxy);
    when(storageReader.constraintsGetForSchema(schema)).thenReturn(Collections.emptyIterator());
    when(storageReader.indexGetForSchema(schema)).thenReturn(Collections.emptyIterator());
    // when
    var e = assertThrows(KernelException.class, () -> operations.uniquePropertyConstraintCreate(prototype));
    assertThat(e.getUserMessage(tokenHolders)).contains("relationship type schema");
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) NamedToken(org.neo4j.token.api.NamedToken) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Example 3 with NamedToken

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

the class SchemaRuleMigrationTest method mustOverwritePreviousDefaultConstraintNames.

@Test
void mustOverwritePreviousDefaultConstraintNames() throws KernelException {
    SchemaRule rule = ConstraintDescriptorFactory.uniqueForSchema(SchemaDescriptor.forLabel(1, 2)).withId(1).withName("constraint_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 4 with NamedToken

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

the class SchemaRuleMigrationTest method mustOverwritePreviousDefaultIndexNames.

@Test
void mustOverwritePreviousDefaultIndexNames() throws KernelException {
    SchemaRule rule = IndexPrototype.forSchema(SchemaDescriptor.forLabel(1, 2)).withName("index_1").materialise(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("index_c3fbd584", 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 5 with NamedToken

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

the class DelegatingTokenHolder method createToken.

/**
 * Create and put new token in cache.
 *
 * @param name token name
 * @return newly created token id
 */
@Override
protected synchronized int createToken(String name, boolean internal) throws KernelException {
    Integer id = internal ? tokenRegistry.getIdInternal(name) : tokenRegistry.getId(name);
    if (id != null) {
        return id;
    }
    id = tokenCreator.createToken(name, internal);
    try {
        tokenRegistry.put(new NamedToken(name, id, internal));
    } catch (NonUniqueTokenException e) {
        throw new UnspecifiedKernelException(Status.General.UnknownError, e, "Newly created token should be unique.");
    }
    return id;
}
Also used : NonUniqueTokenException(org.neo4j.token.api.NonUniqueTokenException) UnspecifiedKernelException(org.neo4j.exceptions.UnspecifiedKernelException) 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