use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class CountsComputerTest method shouldCreateACountStoreWhenDBContainsDenseNodes.
@Test
public void shouldCreateACountStoreWhenDBContainsDenseNodes() throws IOException {
@SuppressWarnings("deprecation") final GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.setConfig(GraphDatabaseSettings.dense_node_threshold, "2").newGraphDatabase();
try (Transaction tx = db.beginTx()) {
Node nodeA = db.createNode(Label.label("A"));
Node nodeC = db.createNode(Label.label("C"));
Node nodeD = db.createNode(Label.label("D"));
nodeA.createRelationshipTo(nodeA, RelationshipType.withName("TYPE1"));
nodeA.createRelationshipTo(nodeC, RelationshipType.withName("TYPE2"));
nodeA.createRelationshipTo(nodeD, RelationshipType.withName("TYPE3"));
nodeD.createRelationshipTo(nodeC, RelationshipType.withName("TYPE4"));
tx.success();
}
long lastCommittedTransactionId = getLastTxId(db);
db.shutdown();
rebuildCounts(lastCommittedTransactionId);
try (Lifespan life = new Lifespan()) {
CountsTracker store = life.add(createCountsTracker());
assertEquals(BASE_TX_ID + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1, store.txId());
assertEquals(22, store.totalEntriesStored());
assertEquals(3, get(store, nodeKey(-1)));
assertEquals(1, get(store, nodeKey(0)));
assertEquals(1, get(store, nodeKey(1)));
assertEquals(1, get(store, nodeKey(2)));
assertEquals(0, get(store, nodeKey(3)));
assertEquals(4, get(store, relationshipKey(-1, -1, -1)));
assertEquals(1, get(store, relationshipKey(-1, 0, -1)));
assertEquals(1, get(store, relationshipKey(-1, 1, -1)));
assertEquals(1, get(store, relationshipKey(-1, 2, -1)));
assertEquals(1, get(store, relationshipKey(-1, 3, -1)));
assertEquals(0, get(store, relationshipKey(-1, 4, -1)));
assertEquals(1, get(store, relationshipKey(-1, 1, 1)));
assertEquals(2, get(store, relationshipKey(-1, -1, 1)));
assertEquals(3, get(store, relationshipKey(0, -1, -1)));
}
}
use of org.neo4j.kernel.lifecycle.Lifespan 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());
}
}
use of org.neo4j.kernel.lifecycle.Lifespan 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();
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class CountsTrackerTest method shouldStoreCounts.
@Test
public void shouldStoreCounts() throws Exception {
// given
CountsOracle oracle = someData();
// when
try (Lifespan life = new Lifespan()) {
CountsTracker tracker = life.add(newTracker());
oracle.update(tracker, 2);
tracker.rotate(2);
}
// then
try (Lifespan life = new Lifespan()) {
oracle.verify(life.add(newTracker()));
}
}
use of org.neo4j.kernel.lifecycle.Lifespan in project neo4j by neo4j.
the class AbstractKeyValueStoreTest method shouldPickTheUncorruptedStoreWhenTruncatingAfterTheHeader.
@Test
public void shouldPickTheUncorruptedStoreWhenTruncatingAfterTheHeader() throws IOException {
/*
* The problem was that if we were succesfull in writing the header but failing immediately after, we would
* read 0 as counter for entry data and pick the corrupted store thinking that it was simply empty.
*/
Store store = createTestStore();
Pair<File, KeyValueStoreFile> file = store.rotationStrategy.create(EMPTY_DATA_PROVIDER, 1);
Pair<File, KeyValueStoreFile> next = store.rotationStrategy.next(file.first(), Headers.headersBuilder().put(TX_ID, (long) 42).headers(), data(new Entry() {
@Override
public void write(WritableBuffer key, WritableBuffer value) {
key.putByte(0, (byte) 'f');
key.putByte(1, (byte) 'o');
key.putByte(2, (byte) 'o');
value.putInt(0, 42);
}
}));
file.other().close();
File correct = next.first();
Pair<File, KeyValueStoreFile> nextNext = store.rotationStrategy.next(correct, Headers.headersBuilder().put(TX_ID, (long) 43).headers(), data(new Entry() {
@Override
public void write(WritableBuffer key, WritableBuffer value) {
key.putByte(0, (byte) 'f');
key.putByte(1, (byte) 'o');
key.putByte(2, (byte) 'o');
value.putInt(0, 42);
}
}, new Entry() {
@Override
public void write(WritableBuffer key, WritableBuffer value) {
key.putByte(0, (byte) 'b');
key.putByte(1, (byte) 'a');
key.putByte(2, (byte) 'r');
value.putInt(0, 4242);
}
}));
next.other().close();
File corrupted = nextNext.first();
nextNext.other().close();
try (StoreChannel channel = resourceManager.fileSystem().open(corrupted, "rw")) {
channel.truncate(16 * 4);
}
// then
try (Lifespan life = new Lifespan()) {
life.add(store);
assertNotNull(store.get("foo"));
assertEquals(42L, store.headers().get(TX_ID).longValue());
}
}
Aggregations