use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.
the class RecoveryIT method recoverDatabaseWithRelationshipIndex.
@Test
void recoverDatabaseWithRelationshipIndex() throws Throwable {
GraphDatabaseService database = createDatabase();
int numberOfRelationships = 10;
RelationshipType type = RelationshipType.withName("TYPE");
String property = "prop";
String indexName = "my index";
try (Transaction transaction = database.beginTx()) {
transaction.schema().indexFor(type).on(property).withIndexType(IndexType.FULLTEXT).withName(indexName).create();
transaction.commit();
}
awaitIndexesOnline(database);
try (Transaction transaction = database.beginTx()) {
Node start = transaction.createNode();
Node stop = transaction.createNode();
for (int i = 0; i < numberOfRelationships; i++) {
Relationship relationship = start.createRelationshipTo(stop, type);
relationship.setProperty(property, "value");
}
transaction.commit();
}
managementService.shutdown();
RecoveryHelpers.removeLastCheckpointRecordFromLastLogFile(databaseLayout, fileSystem);
recoverDatabase();
GraphDatabaseAPI recoveredDatabase = createDatabase();
awaitIndexesOnline(recoveredDatabase);
try (Transaction transaction = recoveredDatabase.beginTx()) {
KernelTransaction ktx = ((InternalTransaction) transaction).kernelTransaction();
IndexDescriptor index = ktx.schemaRead().indexGetForName(indexName);
IndexReadSession indexReadSession = ktx.dataRead().indexReadSession(index);
int relationshipsInIndex = 0;
try (RelationshipValueIndexCursor cursor = ktx.cursors().allocateRelationshipValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
ktx.dataRead().relationshipIndexSeek(indexReadSession, cursor, unconstrained(), fulltextSearch("*"));
while (cursor.next()) {
relationshipsInIndex++;
}
}
assertEquals(numberOfRelationships, relationshipsInIndex);
} finally {
managementService.shutdown();
}
}
use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.
the class TransactionImpl method nodesByLabelAndProperties.
private ResourceIterator<Node> nodesByLabelAndProperties(KernelTransaction transaction, int labelId, PropertyIndexQuery.ExactPredicate... queries) {
Read read = transaction.dataRead();
if (isInvalidQuery(labelId, queries)) {
return emptyResourceIterator();
}
int[] propertyIds = getPropertyIds(queries);
IndexDescriptor index = findUsableMatchingCompositeIndex(transaction, SchemaDescriptor.forLabel(labelId, propertyIds), propertyIds, () -> transaction.schemaRead().indexesGetForLabel(labelId));
if (index != IndexDescriptor.NO_INDEX) {
try {
NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker());
IndexReadSession indexSession = read.indexReadSession(index);
read.nodeIndexSeek(indexSession, cursor, unconstrained(), getReorderedIndexQueries(index.schema().getPropertyIds(), queries));
return new CursorIterator<>(cursor, NodeIndexCursor::nodeReference, c -> newNodeEntity(c.nodeReference()), coreApiResourceTracker);
} catch (KernelException e) {
// weird at this point but ignore and fallback to a label scan
}
}
return getNodesByLabelAndPropertyWithoutPropertyIndex(transaction, labelId, queries);
}
use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.
the class TransactionImpl method nodesByLabelAndProperty.
private ResourceIterator<Node> nodesByLabelAndProperty(KernelTransaction transaction, int labelId, PropertyIndexQuery query) {
Read read = transaction.dataRead();
if (query.propertyKeyId() == TokenRead.NO_TOKEN || labelId == TokenRead.NO_TOKEN) {
return emptyResourceIterator();
}
var index = findUsableMatchingIndex(transaction, SchemaDescriptor.forLabel(labelId, query.propertyKeyId()));
if (index != IndexDescriptor.NO_INDEX) {
// Ha! We found an index - let's use it to find matching nodes
try {
NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(transaction.cursorContext(), transaction.memoryTracker());
IndexReadSession indexSession = read.indexReadSession(index);
read.nodeIndexSeek(indexSession, cursor, unconstrained(), query);
return new CursorIterator<>(cursor, NodeIndexCursor::nodeReference, c -> newNodeEntity(c.nodeReference()), coreApiResourceTracker);
} catch (KernelException e) {
// weird at this point but ignore and fallback to a label scan
}
}
return getNodesByLabelAndPropertyWithoutPropertyIndex(transaction, labelId, query);
}
use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.
the class IndexOrderTestBase method shouldNodeIndexScanInOrderPointsOnly.
@ParameterizedTest
@EnumSource(value = IndexOrder.class, names = { "ASCENDING", "DESCENDING" })
void shouldNodeIndexScanInOrderPointsOnly(IndexOrder indexOrder) throws Exception {
List<Pair<Long, Value>> expected = new ArrayList<>();
try (KernelTransaction tx = beginTransaction()) {
expected.add(entityWithProp(tx, pointValue(Cartesian, -500000, -500000)));
expected.add(entityWithProp(tx, pointValue(Cartesian, 500000, -500000)));
expected.add(entityWithProp(tx, pointValue(Cartesian, -500000, 500000)));
expected.add(entityWithProp(tx, pointValue(Cartesian, 500000, 500000)));
tx.commit();
}
createIndex();
// when
try (KernelTransaction tx = beginTransaction()) {
IndexReadSession index = tx.dataRead().indexReadSession(tx.schemaRead().indexGetForName(INDEX_NAME));
try (var cursor = getEntityValueIndexCursor(tx)) {
expected.add(entityWithProp(tx, pointValue(Cartesian, -400000, -400000)));
expected.add(entityWithProp(tx, pointValue(Cartesian, 400000, -400000)));
expected.add(entityWithProp(tx, pointValue(Cartesian, -400000, 400000)));
expected.add(entityWithProp(tx, pointValue(Cartesian, 400000, 400000)));
entityIndexScan(tx, index, cursor, constrained(indexOrder, true));
assertResultsInOrder(expected, cursor, indexOrder);
}
}
}
use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.
the class IndexOrderTestBase method shouldNodeCompositeIndexScanInOrderWithPointsAndSingleNodeAfterwards.
@ParameterizedTest
@EnumSource(value = IndexOrder.class, names = { "ASCENDING", "DESCENDING" })
void shouldNodeCompositeIndexScanInOrderWithPointsAndSingleNodeAfterwards(IndexOrder indexOrder) throws Exception {
List<Pair<Long, Value[]>> expected = new ArrayList<>();
try (KernelTransaction tx = beginTransaction()) {
expected.add(entityWithTwoProps(tx, new String[] { "a" }, new String[] { "b" }));
expected.add(entityWithTwoProps(tx, pointValue(Cartesian, -500000, -500000), "a"));
expected.add(entityWithTwoProps(tx, pointValue(Cartesian, 500000, -500000), "a"));
expected.add(entityWithTwoProps(tx, pointValue(Cartesian, -500000, 500000), "a"));
expected.add(entityWithTwoProps(tx, pointValue(Cartesian, 500000, 500000), "a"));
expected.add(entityWithTwoProps(tx, pointValue(Cartesian, -500000, -500000), "b"));
expected.add(entityWithTwoProps(tx, pointValue(Cartesian, 500000, -500000), "b"));
expected.add(entityWithTwoProps(tx, pointValue(Cartesian, -500000, 500000), "b"));
expected.add(entityWithTwoProps(tx, pointValue(Cartesian, 500000, 500000), "b"));
expected.add(entityWithTwoProps(tx, "a", pointValue(Cartesian, 500000, 500000)));
expected.add(entityWithTwoProps(tx, "b", new String[] { "b" }));
expected.add(entityWithTwoProps(tx, "b", pointValue(Cartesian, -500000, -500000)));
expected.add(entityWithTwoProps(tx, "b", pointValue(Cartesian, 500000, -500000)));
expected.add(entityWithTwoProps(tx, "b", pointValue(Cartesian, -500000, 500000)));
expected.add(entityWithTwoProps(tx, "b", pointValue(Cartesian, 500000, 500000)));
expected.add(entityWithTwoProps(tx, "c", new String[] { "b" }));
tx.commit();
}
createCompositeIndex();
// when
try (KernelTransaction tx = beginTransaction()) {
IndexReadSession index = tx.dataRead().indexReadSession(tx.schemaRead().indexGetForName(INDEX_NAME));
try (var cursor = getEntityValueIndexCursor(tx)) {
entityIndexScan(tx, index, cursor, constrained(indexOrder, true));
assertCompositeResultsInOrder(expected, cursor, indexOrder);
}
}
}
Aggregations