use of org.neo4j.internal.kernel.api.SchemaRead in project neo4j by neo4j.
the class FulltextAnalyzerTest method shouldNotReindexNodesWhenDefaultAnalyzerIsChanged.
@Test
void shouldNotReindexNodesWhenDefaultAnalyzerIsChanged() throws Exception {
long secondID;
applySetting(FulltextSettings.fulltext_default_analyzer, ENGLISH);
try (Transaction tx = db.beginTx()) {
tx.schema().indexFor(LABEL).on(PROP).withIndexType(FULLTEXT).withName("nodes").create();
tx.commit();
}
try (Transaction tx = db.beginTx()) {
tx.schema().awaitIndexOnline("nodes", 1, TimeUnit.MINUTES);
}
try (Transaction tx = db.beginTx()) {
createNodeIndexableByPropertyValue(tx, LABEL, "Hello and hello again, in the end.");
secondID = createNodeIndexableByPropertyValue(tx, LABEL, "En apa och en tomte bodde i ett hus.");
tx.commit();
}
try (Transaction tx = db.beginTx()) {
KernelTransaction ktx = kernelTransaction(tx);
assertQueryFindsNothing(ktx, true, "nodes", "and");
assertQueryFindsNothing(ktx, true, "nodes", "in");
assertQueryFindsNothing(ktx, true, "nodes", "the");
assertQueryFindsIds(ktx, true, "nodes", "en", secondID);
assertQueryFindsIds(ktx, true, "nodes", "och", secondID);
assertQueryFindsIds(ktx, true, "nodes", "ett", secondID);
}
applySetting(FulltextSettings.fulltext_default_analyzer, SWEDISH);
try (KernelTransactionImplementation ktx = getKernelTransaction()) {
SchemaRead schemaRead = ktx.schemaRead();
await(schemaRead.indexGetForName("nodes"));
// These results should be exactly the same as before the configuration change and restart.
assertQueryFindsNothing(ktx, true, "nodes", "and");
assertQueryFindsNothing(ktx, true, "nodes", "in");
assertQueryFindsNothing(ktx, true, "nodes", "the");
assertQueryFindsIds(ktx, true, "nodes", "en", secondID);
assertQueryFindsIds(ktx, true, "nodes", "och", secondID);
assertQueryFindsIds(ktx, true, "nodes", "ett", secondID);
}
}
use of org.neo4j.internal.kernel.api.SchemaRead in project neo4j by neo4j.
the class StartOldDbOnCurrentVersionAndCreateFusionIndexIT method verifyExpectedProvider.
private static void verifyExpectedProvider(GraphDatabaseAPI db, Label label, IndexProviderDescriptor expectedDescriptor) throws TransactionFailureException {
try (InternalTransaction tx = (InternalTransaction) db.beginTx();
KernelTransaction kernelTransaction = tx.kernelTransaction()) {
TokenRead tokenRead = kernelTransaction.tokenRead();
SchemaRead schemaRead = kernelTransaction.schemaRead();
int labelId = tokenRead.nodeLabel(label.name());
int key1Id = tokenRead.propertyKey(KEY1);
int key2Id = tokenRead.propertyKey(KEY2);
IndexDescriptor index = single(schemaRead.index(SchemaDescriptor.forLabel(labelId, key1Id)));
assertIndexHasExpectedProvider(expectedDescriptor, index);
index = single(schemaRead.index(SchemaDescriptor.forLabel(labelId, key1Id, key2Id)));
assertIndexHasExpectedProvider(expectedDescriptor, index);
tx.commit();
}
}
use of org.neo4j.internal.kernel.api.SchemaRead in project neo4j by neo4j.
the class BuiltInProceduresTest method setup.
@BeforeEach
void setup() throws Exception {
procs.registerComponent(KernelTransaction.class, ctx -> ctx.internalTransaction().kernelTransaction(), false);
procs.registerComponent(DependencyResolver.class, Context::dependencyResolver, false);
procs.registerComponent(GraphDatabaseAPI.class, Context::graphDatabaseAPI, false);
procs.registerComponent(Transaction.class, Context::internalTransaction, true);
procs.registerComponent(SecurityContext.class, Context::securityContext, true);
procs.registerComponent(ProcedureCallContext.class, Context::procedureCallContext, true);
procs.registerComponent(SystemGraphComponents.class, ctx -> systemGraphComponents, false);
procs.registerComponent(Log.class, ctx -> log, false);
procs.registerType(Node.class, NTNode);
procs.registerType(Relationship.class, NTRelationship);
procs.registerType(Path.class, NTPath);
new SpecialBuiltInProcedures("1.3.37", Edition.COMMUNITY.toString()).accept(procs);
procs.registerProcedure(BuiltInProcedures.class);
procs.registerProcedure(BuiltInDbmsProcedures.class);
when(transaction.kernelTransaction()).thenReturn(tx);
when(tx.tokenRead()).thenReturn(tokens);
when(tx.dataRead()).thenReturn(read);
when(tx.schemaRead()).thenReturn(schemaRead);
when(tx.securityContext()).thenReturn(SecurityContext.AUTH_DISABLED);
when(callContext.isCalledFromCypher()).thenReturn(false);
when(schemaRead.snapshot()).thenReturn(schemaReadCore);
when(tokens.propertyKeyGetAllTokens()).thenAnswer(asTokens(propKeys));
when(tokens.labelsGetAllTokens()).thenAnswer(asTokens(labels));
when(tokens.relationshipTypesGetAllTokens()).thenAnswer(asTokens(relTypes));
when(schemaReadCore.indexesGetAll()).thenAnswer(i -> Iterators.concat(indexes.iterator(), uniqueIndexes.iterator()));
when(schemaReadCore.index(any(SchemaDescriptor.class))).thenAnswer((Answer<IndexDescriptor>) invocationOnMock -> {
SchemaDescriptor schema = invocationOnMock.getArgument(0);
return getIndexReference(schema);
});
when(schemaReadCore.constraintsGetAll()).thenAnswer(i -> constraints.iterator());
when(tokens.propertyKeyName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
when(tokens.nodeLabelName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
when(tokens.relationshipTypeName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
when(tokens.propertyKeyGetName(anyInt())).thenAnswer(invocation -> propKeys.get(invocation.getArgument(0)));
when(tokens.labelGetName(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
when(tokens.relationshipTypeGetName(anyInt())).thenAnswer(invocation -> relTypes.get(invocation.getArgument(0)));
when(tokens.entityTokensGetNames(any(), any())).then(invocation -> {
EntityType type = invocation.getArgument(0);
int[] ids = invocation.getArgument(1);
Map<Integer, String> mapping = type == EntityType.NODE ? labels : relTypes;
return Arrays.stream(ids).mapToObj(mapping::get).toArray(String[]::new);
});
when(schemaReadCore.constraintsGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
when(schemaReadCore.indexesGetForLabel(anyInt())).thenReturn(emptyIterator());
when(schemaReadCore.indexesGetForRelationshipType(anyInt())).thenReturn(emptyIterator());
when(schemaReadCore.constraintsGetForLabel(anyInt())).thenReturn(emptyIterator());
when(read.countsForNode(anyInt())).thenReturn(1L);
when(read.countsForRelationship(anyInt(), anyInt(), anyInt())).thenReturn(1L);
when(schemaReadCore.indexGetState(any(IndexDescriptor.class))).thenReturn(InternalIndexState.ONLINE);
}
use of org.neo4j.internal.kernel.api.SchemaRead in project neo4j by neo4j.
the class ResampleIndexProcedureTest method setup.
@BeforeEach
void setup() {
KernelTransaction transaction = mock(KernelTransaction.class);
schemaRead = mock(SchemaRead.class);
when(transaction.schemaRead()).thenReturn(schemaRead);
indexingService = mock(IndexingService.class);
procedure = new IndexProcedures(transaction, indexingService);
}
use of org.neo4j.internal.kernel.api.SchemaRead in project neo4j by neo4j.
the class ConstraintIndexCreator method createUniquenessConstraintIndex.
/**
* You MUST hold a label write lock before you call this method.
* However the label write lock is temporarily released while populating the index backing the constraint.
* It goes a little like this:
* <ol>
* <li>Prerequisite: Getting here means that there's an open schema transaction which has acquired the
* LABEL WRITE lock.</li>
* <li>Index schema rule which is backing the constraint is created in a nested mini-transaction
* which doesn't acquire any locking, merely adds tx state and commits so that the index rule is applied
* to the store, which triggers the index population</li>
* <li>Release the LABEL WRITE lock</li>
* <li>Await index population to complete</li>
* <li>Acquire the LABEL WRITE lock (effectively blocking concurrent transactions changing
* data related to this constraint, and it so happens, most other transactions as well) and verify
* the uniqueness of the built index</li>
* <li>Leave this method, knowing that the uniqueness constraint rule will be added to tx state
* and this tx committed, which will create the uniqueness constraint</li>
* </ol>
*/
public IndexDescriptor createUniquenessConstraintIndex(KernelTransactionImplementation transaction, IndexBackedConstraintDescriptor constraint, IndexPrototype prototype) throws TransactionFailureException, CreateConstraintFailureException, UniquePropertyValueValidationException, AlreadyConstrainedException {
String constraintString = constraint.userDescription(transaction.tokenRead());
log.info("Starting constraint creation: %s.", constraintString);
IndexDescriptor index;
SchemaRead schemaRead = transaction.schemaRead();
try {
index = checkAndCreateConstraintIndex(schemaRead, transaction.tokenRead(), constraint, prototype);
} catch (AlreadyConstrainedException e) {
throw e;
} catch (KernelException e) {
throw new CreateConstraintFailureException(constraint, e);
}
boolean success = false;
boolean reacquiredLabelLock = false;
Client locks = transaction.lockClient();
ResourceType keyType = constraint.schema().keyType();
long[] lockingKeys = constraint.schema().lockingKeys();
try {
locks.acquireShared(transaction.lockTracer(), keyType, lockingKeys);
IndexProxy proxy = indexingService.getIndexProxy(index);
// Release the LABEL WRITE lock during index population.
// At this point the integrity of the constraint to be created was checked
// while holding the lock and the index rule backing the soon-to-be-created constraint
// has been created. Now it's just the population left, which can take a long time
locks.releaseExclusive(keyType, lockingKeys);
awaitConstraintIndexPopulation(constraint, proxy, transaction);
log.info("Constraint %s populated, starting verification.", constraintString);
// Index population was successful, but at this point we don't know if the uniqueness constraint holds.
// Acquire LABEL WRITE lock and verify the constraints here in this user transaction
// and if everything checks out then it will be held until after the constraint has been
// created and activated.
locks.acquireExclusive(transaction.lockTracer(), keyType, lockingKeys);
reacquiredLabelLock = true;
try (NodePropertyAccessor propertyAccessor = new DefaultNodePropertyAccessor(transaction.newStorageReader(), transaction.cursorContext(), transaction.memoryTracker())) {
indexingService.getIndexProxy(index).verifyDeferredConstraints(propertyAccessor);
}
log.info("Constraint %s verified.", constraintString);
success = true;
return index;
} catch (IndexNotFoundKernelException e) {
String indexString = index.userDescription(transaction.tokenRead());
throw new TransactionFailureException(format("Index (%s) that we just created does not exist.", indexString), e);
} catch (IndexEntryConflictException e) {
throw new UniquePropertyValueValidationException(constraint, VERIFICATION, e, transaction.tokenRead());
} catch (InterruptedException | IOException e) {
throw new CreateConstraintFailureException(constraint, e);
} finally {
if (!success) {
if (!reacquiredLabelLock) {
locks.acquireExclusive(transaction.lockTracer(), keyType, lockingKeys);
}
if (indexStillExists(schemaRead, index)) {
dropUniquenessConstraintIndex(index);
}
}
}
}
Aggregations