Search in sources :

Example 1 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class CountsRotationTest method shouldCreateEmptyCountsTrackerStoreWhenCreatingDatabase.

@Test
public void shouldCreateEmptyCountsTrackerStoreWhenCreatingDatabase() throws IOException {
    // GIVEN
    GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
    // WHEN
    db.shutdown();
    // THEN
    assertTrue(fs.fileExists(alphaStoreFile()));
    assertFalse(fs.fileExists(betaStoreFile()));
    try (Lifespan life = new Lifespan()) {
        CountsTracker store = life.add(createCountsTracker(pageCache));
        assertEquals(BASE_TX_ID, store.txId());
        assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
        assertEquals(0, store.totalEntriesStored());
        assertEquals(0, allRecords(store).size());
    }
    try (Lifespan life = new Lifespan()) {
        CountsTracker store = life.add(createCountsTracker(pageCache));
        assertEquals(BASE_TX_ID, store.txId());
        assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
        assertEquals(0, store.totalEntriesStored());
        assertEquals(0, allRecords(store).size());
    }
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Example 2 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class CountsRotationTest method shouldRotateCountsStoreWhenRotatingLog.

@Test
public void shouldRotateCountsStoreWhenRotatingLog() throws IOException {
    // GIVEN
    GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
    // WHEN doing a transaction (actually two, the label-mini-tx also counts)
    try (Transaction tx = db.beginTx()) {
        db.createNode(B);
        tx.success();
    }
    // and rotating the log (which implies flushing)
    checkPoint(db);
    // and creating another node after it
    try (Transaction tx = db.beginTx()) {
        db.createNode(C);
        tx.success();
    }
    // THEN
    assertTrue(fs.fileExists(alphaStoreFile()));
    assertTrue(fs.fileExists(betaStoreFile()));
    final PageCache pageCache = db.getDependencyResolver().resolveDependency(PageCache.class);
    try (Lifespan life = new Lifespan()) {
        CountsTracker store = life.add(createCountsTracker(pageCache));
        // NOTE since the rotation happens before the second transaction is committed we do not see those changes
        // in the stats
        // a transaction for creating the label and a transaction for the node
        assertEquals(BASE_TX_ID + 1 + 1, store.txId());
        assertEquals(INITIAL_MINOR_VERSION, store.minorVersion());
        // one for all nodes and one for the created "B" label
        assertEquals(1 + 1, store.totalEntriesStored());
        assertEquals(1 + 1, allRecords(store).size());
    }
    // on the other hand the tracker should read the correct value by merging data on disk and data in memory
    final CountsTracker tracker = db.getDependencyResolver().resolveDependency(RecordStorageEngine.class).testAccessNeoStores().getCounts();
    assertEquals(1 + 1, tracker.nodeCount(-1, newDoubleLongRegister()).readSecond());
    final LabelTokenHolder holder = db.getDependencyResolver().resolveDependency(LabelTokenHolder.class);
    int labelId = holder.getIdByName(C.name());
    assertEquals(1, tracker.nodeCount(labelId, newDoubleLongRegister()).readSecond());
    db.shutdown();
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) LabelTokenHolder(org.neo4j.kernel.impl.core.LabelTokenHolder) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 3 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class CountsRotationTest method rotationShouldNotCauseUnmappedFileProblem.

@Test
public void rotationShouldNotCauseUnmappedFileProblem() throws IOException {
    // GIVEN
    GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
    DependencyResolver resolver = db.getDependencyResolver();
    RecordStorageEngine storageEngine = resolver.resolveDependency(RecordStorageEngine.class);
    CountsTracker countStore = storageEngine.testAccessNeoStores().getCounts();
    AtomicBoolean workerContinueFlag = new AtomicBoolean(true);
    AtomicLong lookupsCounter = new AtomicLong();
    int rotations = 100;
    for (int i = 0; i < 5; i++) {
        threadingRule.execute(countStoreLookup(workerContinueFlag, lookupsCounter), countStore);
    }
    long startTxId = countStore.txId();
    for (int i = 1; (i < rotations) || (lookupsCounter.get() == 0); i++) {
        try (Transaction tx = db.beginTx()) {
            db.createNode(B);
            tx.success();
        }
        checkPoint(db);
    }
    workerContinueFlag.set(false);
    assertEquals("Should perform at least 100 rotations.", rotations, Math.min(rotations, countStore.txId() - startTxId));
    assertTrue("Should perform more then 0 lookups without exceptions.", lookupsCounter.get() > 0);
    db.shutdown();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) RecordStorageEngine(org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine) DependencyResolver(org.neo4j.graphdb.DependencyResolver) Test(org.junit.Test)

Example 4 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class TestGraphProperties method basicProperties.

@Test
public void basicProperties() throws Exception {
    GraphDatabaseAPI db = (GraphDatabaseAPI) factory.newImpermanentDatabase();
    PropertyContainer graphProperties = properties(db);
    assertThat(graphProperties, inTx(db, not(hasProperty("test"))));
    Transaction tx = db.beginTx();
    graphProperties.setProperty("test", "yo");
    assertEquals("yo", graphProperties.getProperty("test"));
    tx.success();
    tx.close();
    assertThat(graphProperties, inTx(db, hasProperty("test").withValue("yo")));
    tx = db.beginTx();
    assertNull(graphProperties.removeProperty("something non existent"));
    assertEquals("yo", graphProperties.removeProperty("test"));
    assertNull(graphProperties.getProperty("test", null));
    graphProperties.setProperty("other", 10);
    assertEquals(10, graphProperties.getProperty("other"));
    graphProperties.setProperty("new", "third");
    tx.success();
    tx.close();
    assertThat(graphProperties, inTx(db, not(hasProperty("test"))));
    assertThat(graphProperties, inTx(db, hasProperty("other").withValue(10)));
    assertThat(getPropertyKeys(db, graphProperties), containsOnly("other", "new"));
    tx = db.beginTx();
    graphProperties.setProperty("rollback", true);
    assertEquals(true, graphProperties.getProperty("rollback"));
    tx.close();
    assertThat(graphProperties, inTx(db, not(hasProperty("rollback"))));
    db.shutdown();
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 5 with GraphDatabaseAPI

use of org.neo4j.kernel.internal.GraphDatabaseAPI in project neo4j by neo4j.

the class TestGraphProperties method setManyGraphProperties.

@Test
public void setManyGraphProperties() throws Exception {
    GraphDatabaseAPI db = (GraphDatabaseAPI) factory.newImpermanentDatabase();
    Transaction tx = db.beginTx();
    Object[] values = new Object[] { 10, "A string value", new float[] { 1234.567F, 7654.321F }, "A rather longer string which wouldn't fit inlined #!)(&ยค" };
    int count = 200;
    for (int i = 0; i < count; i++) {
        properties(db).setProperty("key" + i, values[i % values.length]);
    }
    tx.success();
    tx.close();
    for (int i = 0; i < count; i++) {
        assertThat(properties(db), inTx(db, hasProperty("key" + i).withValue(values[i % values.length])));
    }
    for (int i = 0; i < count; i++) {
        assertThat(properties(db), inTx(db, hasProperty("key" + i).withValue(values[i % values.length])));
    }
    db.shutdown();
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Aggregations

GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)262 Test (org.junit.Test)91 Test (org.junit.jupiter.api.Test)88 Transaction (org.neo4j.graphdb.Transaction)87 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)61 Node (org.neo4j.graphdb.Node)38 Path (java.nio.file.Path)30 TestDatabaseManagementServiceBuilder (org.neo4j.test.TestDatabaseManagementServiceBuilder)30 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)28 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)26 File (java.io.File)25 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)24 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)19 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)18 DependencyResolver (org.neo4j.graphdb.DependencyResolver)17 Label (org.neo4j.graphdb.Label)15 PageCache (org.neo4j.io.pagecache.PageCache)15 Config (org.neo4j.kernel.configuration.Config)15 DatabaseStateService (org.neo4j.dbms.DatabaseStateService)14 IOException (java.io.IOException)11