use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.
the class CountsComputerTest method shouldCreateACountsStoreWhenThereAreUnusedRelationshipRecordsInTheDB.
@Test
void shouldCreateACountsStoreWhenThereAreUnusedRelationshipRecordsInTheDB() throws IOException, KernelException {
DatabaseManagementService managementService = dbBuilder.build();
final GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
Label[] labels = null;
int[] labelIds = null;
RelationshipType[] relTypes = null;
int[] relTypeIds = null;
Node[] nodes = null;
Relationship[] rels = null;
try (Transaction tx = db.beginTx()) {
labels = createLabels(4);
labelIds = getLabelIdsFrom(tx, labels);
relTypes = createRelationShipTypes(2);
relTypeIds = getRelTypeIdsFrom(tx, relTypes);
nodes = new Node[] { tx.createNode(labels[0]), tx.createNode(labels[1]) };
rels = new Relationship[] { nodes[0].createRelationshipTo(nodes[1], relTypes[0]), nodes[1].createRelationshipTo(nodes[0], relTypes[1]) };
rels[0].delete();
tx.commit();
}
long lastCommittedTransactionId = getLastTxId(db);
managementService.shutdown();
rebuildCounts(lastCommittedTransactionId);
try (GBPTreeCountsStore store = createCountsStore()) {
softly.assertThat(store.txId()).as("Store Transaction id").isEqualTo(lastCommittedTransactionId);
softly.assertThat(store.nodeCount(ANY_LABEL, NULL)).as("count: ()").isEqualTo(nodes.length);
softly.assertThat(store.nodeCount(labelIds[0], NULL)).as("count: (:%s)", labels[0]).isEqualTo(1);
softly.assertThat(store.nodeCount(labelIds[1], NULL)).as("count: (:%s)", labels[1]).isEqualTo(1);
softly.assertThat(store.nodeCount(labelIds[2], NULL)).as("count: (:%s)", labels[2]).isEqualTo(0);
softly.assertThat(store.nodeCount(labelIds[3], NULL)).as("count: (:%s)", labels[3]).isEqualTo(0);
softly.assertThat(store.relationshipCount(ANY_LABEL, ANY_RELATIONSHIP_TYPE, ANY_LABEL, NULL)).as("()-[]->()").isEqualTo(rels.length - 1);
softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[0], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[0]).isEqualTo(0);
softly.assertThat(store.relationshipCount(ANY_LABEL, relTypeIds[1], ANY_LABEL, NULL)).as("count: ()-[:%s]->()", relTypes[1]).isEqualTo(1);
}
}
use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.
the class StoreMigratorTest method extractTransactionalInformationFromLogs.
private void extractTransactionalInformationFromLogs(Path customLogsLocation) throws IOException {
Config config = Config.defaults(transaction_logs_root_path, customLogsLocation);
LogService logService = new SimpleLogService(NullLogProvider.getInstance(), NullLogProvider.getInstance());
DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).setConfig(transaction_logs_root_path, customLogsLocation).build();
GraphDatabaseAPI database = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
StorageEngineFactory storageEngineFactory = database.getDependencyResolver().resolveDependency(StorageEngineFactory.class);
for (int i = 0; i < 10; i++) {
try (Transaction transaction = database.beginTx()) {
transaction.createNode();
transaction.commit();
}
}
DatabaseLayout databaseLayout = database.databaseLayout();
Path neoStore = databaseLayout.metadataStore();
managementService.shutdown();
MetaDataStore.setRecord(pageCache, neoStore, MetaDataStore.Position.LAST_CLOSED_TRANSACTION_LOG_VERSION, MetaDataRecordFormat.FIELD_NOT_PRESENT, databaseLayout.getDatabaseName(), NULL);
RecordStorageMigrator migrator = new RecordStorageMigrator(fileSystem, pageCache, config, logService, jobScheduler, PageCacheTracer.NULL, batchImporterFactory, INSTANCE);
LogPosition logPosition = migrator.extractTransactionLogPosition(neoStore, databaseLayout, 100, NULL);
LogFiles logFiles = LogFilesBuilder.activeFilesBuilder(databaseLayout, fileSystem, pageCache).withConfig(config).withCommandReaderFactory(storageEngineFactory.commandReaderFactory()).build();
assertEquals(0, logPosition.getLogVersion());
assertEquals(Files.size(logFiles.getLogFile().getHighestLogFile()), logPosition.getByteOffset());
}
use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.
the class CheckPointerIntegrationTest method shouldNotCheckPointWhenThereAreNoCommits.
@Test
void shouldNotCheckPointWhenThereAreNoCommits() throws Throwable {
// given
DatabaseManagementService managementService = builder.setConfig(check_point_interval_time, Duration.ofSeconds(1)).setConfig(check_point_interval_tx, 10000).setConfig(logical_log_rotation_threshold, gibiBytes(1)).build();
GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
GraphDatabaseAPI databaseAPI = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
getCheckPointer(databaseAPI).forceCheckPoint(new SimpleTriggerInfo("given"));
var checkPointsBefore = checkPointInTxLog(db).size();
// when
// nothing happens
triggerCheckPointAttempt(db);
assertThat(checkPointInTxLog(db)).hasSize(checkPointsBefore);
managementService.shutdown();
managementService = builder.build();
try {
// then - 1 check point has been written in the log
var checkPoints = checkPointsInTxLog(managementService.database(DEFAULT_DATABASE_NAME));
assertEquals(checkPointsBefore + 1, checkPoints.size());
} finally {
managementService.shutdown();
}
}
use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.
the class VersionAwareLogEntryReaderIT method setUp.
@BeforeEach
void setUp() {
GraphDatabaseService database = managementService.database(DEFAULT_DATABASE_NAME);
createNode(database);
GraphDatabaseAPI dbApi = (GraphDatabaseAPI) database;
databaseLayout = dbApi.databaseLayout();
entryReader = new VersionAwareLogEntryReader(dbApi.getDependencyResolver().resolveDependency(StorageEngineFactory.class).commandReaderFactory());
managementService.shutdown();
}
use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.
the class PartialTransactionFailureIT method concurrentlyCommittingTransactionsMustNotRotateOutLoggedCommandsOfFailingTransaction.
@Test
void concurrentlyCommittingTransactionsMustNotRotateOutLoggedCommandsOfFailingTransaction() throws Exception {
final ClassGuardedAdversary adversary = new ClassGuardedAdversary(new CountingAdversary(1, false), Command.RelationshipCommand.class);
adversary.disable();
Path storeDir = testDirectory.homePath();
final Map<Setting<?>, Object> params = Map.of(GraphDatabaseSettings.pagecache_memory, "8m");
managementService = new TestDatabaseManagementServiceBuilder(storeDir).setFileSystem(new AdversarialFileSystemAbstraction(adversary)).setConfig(params).build();
GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
Node a;
Node b;
Node c;
Node d;
try (Transaction tx = db.beginTx()) {
a = tx.createNode();
b = tx.createNode();
c = tx.createNode();
d = tx.createNode();
tx.commit();
}
adversary.enable();
CountDownLatch latch = new CountDownLatch(1);
Thread t1 = new Thread(createRelationship(db, a, b, latch), "T1");
Thread t2 = new Thread(createRelationship(db, c, d, latch), "T2");
t1.start();
t2.start();
// Wait for both threads to get going
t1.join(10);
t2.join(10);
latch.countDown();
// Wait for the transactions to finish
t1.join(25000);
t2.join(25000);
managementService.shutdown();
// We should observe the store in a consistent state
managementService = new TestDatabaseManagementServiceBuilder(storeDir).setConfig(params).build();
GraphDatabaseService database = managementService.database(DEFAULT_DATABASE_NAME);
try (Transaction tx = database.beginTx()) {
Node x = tx.getNodeById(a.getId());
Node y = tx.getNodeById(b.getId());
Node z = tx.getNodeById(c.getId());
Node w = tx.getNodeById(d.getId());
Iterator<Relationship> itrRelX = x.getRelationships().iterator();
Iterator<Relationship> itrRelY = y.getRelationships().iterator();
Iterator<Relationship> itrRelZ = z.getRelationships().iterator();
Iterator<Relationship> itrRelW = w.getRelationships().iterator();
if (itrRelX.hasNext() != itrRelY.hasNext()) {
fail("Node x and y have inconsistent relationship counts");
} else if (itrRelX.hasNext()) {
Relationship rel = itrRelX.next();
assertEquals(rel, itrRelY.next());
assertFalse(itrRelX.hasNext());
assertFalse(itrRelY.hasNext());
}
if (itrRelZ.hasNext() != itrRelW.hasNext()) {
fail("Node z and w have inconsistent relationship counts");
} else if (itrRelZ.hasNext()) {
Relationship rel = itrRelZ.next();
assertEquals(rel, itrRelW.next());
assertFalse(itrRelZ.hasNext());
assertFalse(itrRelW.hasNext());
}
}
}
Aggregations