use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class FreeIdKeeperTest method shouldFirstReturnNonPersistedIdsAndThenPersistedOnesWhenAggressiveReuse.
@Test
public void shouldFirstReturnNonPersistedIdsAndThenPersistedOnesWhenAggressiveReuse() throws Exception {
// this is testing the stack property, but from the viewpoint of avoiding unnecessary disk reads
// given
StoreChannel channel = fs.get().open(new File("id.file"), "rw");
int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper(channel, threshold, true);
// we store enough ids to cause overflow to file
for (int i = 0; i < threshold; i++) {
keeper.freeId(i);
}
// and then some more
int extraIds = 3;
for (int i = threshold; i < threshold + extraIds; i++) {
keeper.freeId(i);
}
// the first returned should be the newly freed ones
for (int i = threshold; i < threshold + extraIds; i++) {
assertEquals(i, keeper.getId());
}
// and then there should be the persisted ones
for (int i = 0; i < threshold; i++) {
assertEquals(i, keeper.getId());
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class FreeIdKeeperTest method shouldStoreAndRestoreIds.
@Test
public void shouldStoreAndRestoreIds() throws Exception {
// given
StoreChannel channel = fs.get().open(new File("id.file"), "rw");
int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper(channel, threshold, true);
// stack guarantees are not maintained between restarts
Set<Long> freeIds = new HashSet<>();
// we store enough ids to cause overflow to file
for (long i = 0; i < threshold; i++) {
keeper.freeId(i);
freeIds.add(i);
}
// and then some more
int extraIds = 3;
for (long i = threshold; i < threshold + extraIds; i++) {
keeper.freeId(i);
freeIds.add(i);
}
// and then we close the keeper
keeper.close();
channel.close();
// and then we open a new one over the same file
channel = fs.get().open(new File("id.file"), "rw");
keeper = new FreeIdKeeper(channel, threshold, true);
// then
// the count should be returned correctly
assertEquals(threshold + extraIds, keeper.getCount());
assertEquals(freeIds.size(), keeper.getCount());
// and the ids, including the ones that did not cause a write, are still there (as a stack)
for (int i = threshold + extraIds - 1; i >= 0; i--) {
long id = keeper.getId();
assertTrue(freeIds.contains(id));
}
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class FreeIdKeeperTest method newlyConstructedInstanceShouldReportProperDefaultValues.
@Test
public void newlyConstructedInstanceShouldReportProperDefaultValues() throws Exception {
// Given
StoreChannel channel = mock(StoreChannel.class);
int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper(channel, threshold, true);
// when
// then
assertEquals(NO_RESULT, keeper.getId());
assertEquals(0, keeper.getCount());
}
use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.
the class FreeIdKeeperTest method shouldNotReturnNewlyReleasedIdsIfAggressiveIsFalse.
@Test
public void shouldNotReturnNewlyReleasedIdsIfAggressiveIsFalse() throws Exception {
// given
StoreChannel channel = fs.get().open(new File("id.file"), "rw");
int threshold = 10;
FreeIdKeeper keeper = new FreeIdKeeper(channel, threshold, false);
// when
keeper.freeId(1);
long nextFree = keeper.getId();
// then
assertEquals(NO_RESULT, nextFree);
}
use of org.neo4j.io.fs.StoreChannel 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