Search in sources :

Example 41 with LabelSchemaDescriptor

use of org.neo4j.internal.schema.LabelSchemaDescriptor in project neo4j by neo4j.

the class IndexPopulationJobTest method tracePageCacheAccessIndexWithOneNodePopulation.

@Test
void tracePageCacheAccessIndexWithOneNodePopulation() throws KernelException {
    var value = "value";
    long nodeId = createNode(map(name, value), FIRST);
    IndexPopulator actualPopulator = indexPopulator(false);
    TrackingIndexPopulator populator = new TrackingIndexPopulator(actualPopulator);
    int label = tokenHolders.labelTokens().getIdByName(FIRST.name());
    int prop = tokenHolders.propertyKeyTokens().getIdByName(name);
    LabelSchemaDescriptor descriptor = SchemaDescriptor.forLabel(label, prop);
    var pageCacheTracer = new DefaultPageCacheTracer();
    IndexPopulationJob job = newIndexPopulationJob(populator, new FlippableIndexProxy(), EntityType.NODE, IndexPrototype.forSchema(descriptor), pageCacheTracer);
    job.run();
    IndexEntryUpdate<?> update = IndexEntryUpdate.add(nodeId, descriptor, Values.of(value));
    assertTrue(populator.created);
    assertEquals(Collections.singletonList(update), populator.includedSamples);
    assertEquals(1, populator.adds.size());
    assertTrue(populator.resultSampled);
    assertTrue(populator.closeCall);
    long pins = pageCacheTracer.pins();
    assertThat(pins).isGreaterThan(0);
    assertThat(pageCacheTracer.unpins()).isEqualTo(pins);
    assertThat(pageCacheTracer.hits()).isGreaterThan(0).isLessThanOrEqualTo(pins);
    assertThat(pageCacheTracer.faults()).isGreaterThan(0).isLessThanOrEqualTo(pins);
}
Also used : IndexPopulator(org.neo4j.kernel.api.index.IndexPopulator) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) Test(org.junit.jupiter.api.Test)

Example 42 with LabelSchemaDescriptor

use of org.neo4j.internal.schema.LabelSchemaDescriptor in project neo4j by neo4j.

the class IndexCRUDIT method addingANodeWithPropertyShouldGetIndexed.

@Test
void addingANodeWithPropertyShouldGetIndexed() throws Exception {
    // Given
    String indexProperty = "indexProperty";
    GatheringIndexWriter writer = newWriter();
    createIndex(db, myLabel, indexProperty);
    // When
    int value1 = 12;
    String otherProperty = "otherProperty";
    int otherValue = 17;
    Node node = createNode(map(indexProperty, value1, otherProperty, otherValue), myLabel);
    // Then, for now, this should trigger two NodePropertyUpdates
    try (Transaction tx = db.beginTx()) {
        KernelTransaction ktx = ((InternalTransaction) tx).kernelTransaction();
        TokenRead tokenRead = ktx.tokenRead();
        int propertyKey1 = tokenRead.propertyKey(indexProperty);
        int label = tokenRead.nodeLabel(myLabel.name());
        LabelSchemaDescriptor descriptor = SchemaDescriptor.forLabel(label, propertyKey1);
        assertThat(writer.updatesCommitted).isEqualTo(asSet(IndexEntryUpdate.add(node.getId(), descriptor, Values.of(value1))));
        tx.commit();
    }
// We get two updates because we both add a label and a property to be indexed
// in the same transaction, in the future, we should optimize this down to
// one NodePropertyUpdate.
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Node(org.neo4j.graphdb.Node) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) TokenRead(org.neo4j.internal.kernel.api.TokenRead) Test(org.junit.jupiter.api.Test)

Example 43 with LabelSchemaDescriptor

use of org.neo4j.internal.schema.LabelSchemaDescriptor in project neo4j by neo4j.

the class BuiltInProceduresIT method listAllIndexes.

@Test
void listAllIndexes() throws Throwable {
    // Given
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    int labelId1 = transaction.tokenWrite().labelGetOrCreateForName("Person");
    int labelId2 = transaction.tokenWrite().labelGetOrCreateForName("Age");
    int propertyKeyId1 = transaction.tokenWrite().propertyKeyGetOrCreateForName("foo");
    int propertyKeyId2 = transaction.tokenWrite().propertyKeyGetOrCreateForName("bar");
    LabelSchemaDescriptor personFooDescriptor = forLabel(labelId1, propertyKeyId1);
    LabelSchemaDescriptor ageFooDescriptor = forLabel(labelId2, propertyKeyId1);
    LabelSchemaDescriptor personFooBarDescriptor = forLabel(labelId1, propertyKeyId1, propertyKeyId2);
    transaction.schemaWrite().indexCreate(personFooDescriptor, "person foo index");
    transaction.schemaWrite().uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(ageFooDescriptor).withName("constraint name"));
    transaction.schemaWrite().indexCreate(personFooBarDescriptor, "person foo bar index");
    commit();
    // let indexes come online
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(2, MINUTES);
        tx.commit();
    }
    transaction = newTransaction();
    IndexDescriptor personFooIndex = single(transaction.schemaRead().index(personFooDescriptor));
    IndexDescriptor ageFooIndex = single(transaction.schemaRead().index(ageFooDescriptor));
    IndexDescriptor personFooBarIndex = single(transaction.schemaRead().index(personFooBarDescriptor));
    // When
    RawIterator<AnyValue[], ProcedureException> stream = procs().procedureCallRead(procs().procedureGet(procedureName("db", "indexes")).id(), new AnyValue[0], ProcedureCallContext.EMPTY);
    Set<AnyValue[]> result = new HashSet<>();
    while (stream.hasNext()) {
        result.add(stream.next());
    }
    // Then
    assertThat(result).contains(dbIndexesResult(ageFooIndex.getId(), ageFooIndex.getName(), "ONLINE", 100D, "UNIQUE", "BTREE", "NODE", singletonList("Age"), singletonList("foo"), ageFooIndex.getIndexProvider().name()), dbIndexesResult(personFooIndex.getId(), personFooIndex.getName(), "ONLINE", 100D, "NONUNIQUE", "BTREE", "NODE", singletonList("Person"), singletonList("foo"), personFooIndex.getIndexProvider().name()), dbIndexesResult(personFooBarIndex.getId(), personFooBarIndex.getName(), "ONLINE", 100D, "NONUNIQUE", "BTREE", "NODE", singletonList("Person"), Arrays.asList("foo", "bar"), personFooBarIndex.getIndexProvider().name()));
    commit();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 44 with LabelSchemaDescriptor

use of org.neo4j.internal.schema.LabelSchemaDescriptor in project neo4j by neo4j.

the class BuiltInProceduresIT method listAllIndexesMustNotBlockOnConstraintCreatingTransaction.

@Test
@Timeout(value = 6, unit = MINUTES)
void listAllIndexesMustNotBlockOnConstraintCreatingTransaction() throws Throwable {
    // Given
    KernelTransaction transaction = newTransaction(AUTH_DISABLED);
    int labelId1 = transaction.tokenWrite().labelGetOrCreateForName("Person");
    int labelId2 = transaction.tokenWrite().labelGetOrCreateForName("Age");
    int propertyKeyId1 = transaction.tokenWrite().propertyKeyGetOrCreateForName("foo");
    int propertyKeyId2 = transaction.tokenWrite().propertyKeyGetOrCreateForName("bar");
    int propertyKeyId3 = transaction.tokenWrite().propertyKeyGetOrCreateForName("baz");
    LabelSchemaDescriptor personFooDescriptor = forLabel(labelId1, propertyKeyId1);
    LabelSchemaDescriptor ageFooDescriptor = forLabel(labelId2, propertyKeyId1);
    LabelSchemaDescriptor personFooBarDescriptor = forLabel(labelId1, propertyKeyId1, propertyKeyId2);
    LabelSchemaDescriptor personBazDescriptor = forLabel(labelId1, propertyKeyId3);
    transaction.schemaWrite().indexCreate(personFooDescriptor, "person foo index");
    transaction.schemaWrite().uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(ageFooDescriptor).withName("age foo constraint"));
    transaction.schemaWrite().indexCreate(personFooBarDescriptor, "person foo bar index");
    commit();
    // let indexes come online
    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
        tx.schema().awaitIndexesOnline(2, MINUTES);
        tx.commit();
    }
    CountDownLatch constraintLatch = new CountDownLatch(1);
    CountDownLatch commitLatch = new CountDownLatch(1);
    AtomicLong personBazIndexId = new AtomicLong();
    FutureTask<Void> createConstraintTask = new FutureTask<>(() -> {
        SchemaWrite schemaWrite = schemaWriteInNewTransaction();
        try (Resource ignore = captureTransaction()) {
            ConstraintDescriptor personBazConstraint = schemaWrite.uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(personBazDescriptor).withName("person baz constraint"));
            personBazIndexId.set(personBazConstraint.asIndexBackedConstraint().ownedIndexId());
            // We now hold a schema lock on the "MyLabel" label. Let the procedure calling transaction have a go.
            constraintLatch.countDown();
            commitLatch.await();
        }
        rollback();
        return null;
    });
    Thread constraintCreator = new Thread(createConstraintTask);
    constraintCreator.start();
    // When
    constraintLatch.await();
    transaction = newTransaction();
    final SchemaReadCore schemaRead = transaction.schemaRead().snapshot();
    IndexDescriptor personFooIndex = single(schemaRead.index(personFooDescriptor));
    IndexDescriptor ageFooIndex = single(schemaRead.index(ageFooDescriptor));
    IndexDescriptor personFooBarIndex = single(schemaRead.index(personFooBarDescriptor));
    IndexDescriptor personBazIndex = single(schemaRead.index(personBazDescriptor));
    commit();
    RawIterator<AnyValue[], ProcedureException> stream = procs().procedureCallRead(procs().procedureGet(procedureName("db", "indexes")).id(), new AnyValue[0], ProcedureCallContext.EMPTY);
    Set<Object[]> result = new HashSet<>();
    while (stream.hasNext()) {
        result.add(stream.next());
    }
    // Then
    try {
        assertThat(result).contains(dbIndexesResult(ageFooIndex.getId(), ageFooIndex.getName(), "ONLINE", 100D, "UNIQUE", "BTREE", "NODE", singletonList("Age"), singletonList("foo"), ageFooIndex.getIndexProvider().name()), dbIndexesResult(personFooIndex.getId(), personFooIndex.getName(), "ONLINE", 100D, "NONUNIQUE", "BTREE", "NODE", singletonList("Person"), singletonList("foo"), personFooIndex.getIndexProvider().name()), dbIndexesResult(personFooBarIndex.getId(), personFooBarIndex.getName(), "ONLINE", 100D, "NONUNIQUE", "BTREE", "NODE", singletonList("Person"), Arrays.asList("foo", "bar"), personFooBarIndex.getIndexProvider().name()), dbIndexesResult(personBazIndex.getId(), personBazIndex.getName(), /*???*/
        "POPULATING", 100D, "UNIQUE", "BTREE", "NODE", singletonList("Person"), singletonList("baz"), personBazIndex.getIndexProvider().name()));
        commit();
    } finally {
        commitLatch.countDown();
    }
    createConstraintTask.get();
    constraintCreator.join();
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) Resource(org.neo4j.graphdb.Resource) CountDownLatch(java.util.concurrent.CountDownLatch) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) AtomicLong(java.util.concurrent.atomic.AtomicLong) FutureTask(java.util.concurrent.FutureTask) ConstraintDescriptor(org.neo4j.internal.schema.ConstraintDescriptor) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor) ProcedureException(org.neo4j.internal.kernel.api.exceptions.ProcedureException) SchemaReadCore(org.neo4j.internal.kernel.api.SchemaReadCore) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 45 with LabelSchemaDescriptor

use of org.neo4j.internal.schema.LabelSchemaDescriptor in project neo4j by neo4j.

the class KernelIT method createIndex.

private static IndexDescriptor createIndex(KernelTransaction transaction) throws KernelException {
    TokenWrite tokenWrite = transaction.tokenWrite();
    SchemaWrite schemaWrite = transaction.schemaWrite();
    LabelSchemaDescriptor schema = forLabel(tokenWrite.labelGetOrCreateForName("hello"), tokenWrite.propertyKeyGetOrCreateForName("hepp"));
    return schemaWrite.indexCreate(schema, null);
}
Also used : SchemaWrite(org.neo4j.internal.kernel.api.SchemaWrite) TokenWrite(org.neo4j.internal.kernel.api.TokenWrite) LabelSchemaDescriptor(org.neo4j.internal.schema.LabelSchemaDescriptor)

Aggregations

LabelSchemaDescriptor (org.neo4j.internal.schema.LabelSchemaDescriptor)63 Test (org.junit.jupiter.api.Test)41 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)24 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)16 SchemaWrite (org.neo4j.internal.kernel.api.SchemaWrite)5 UniquenessConstraintDescriptor (org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor)5 Value (org.neo4j.values.storable.Value)5 Transaction (org.neo4j.graphdb.Transaction)4 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)4 ConstraintDescriptor (org.neo4j.internal.schema.ConstraintDescriptor)4 IndexPopulator (org.neo4j.kernel.api.index.IndexPopulator)4 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 RepeatedTest (org.junit.jupiter.api.RepeatedTest)3 ConsistencyReport (org.neo4j.consistency.report.ConsistencyReport)3 Node (org.neo4j.graphdb.Node)3 TokenRead (org.neo4j.internal.kernel.api.TokenRead)3 TokenWrite (org.neo4j.internal.kernel.api.TokenWrite)3 IndexConfig (org.neo4j.internal.schema.IndexConfig)3