Search in sources :

Example 6 with StoreChannel

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());
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

Example 7 with StoreChannel

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));
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with StoreChannel

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());
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) Test(org.junit.Test)

Example 9 with StoreChannel

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);
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

Example 10 with StoreChannel

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());
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) File(java.io.File) Lifespan(org.neo4j.kernel.lifecycle.Lifespan) Test(org.junit.Test)

Aggregations

StoreChannel (org.neo4j.io.fs.StoreChannel)173 ByteBuffer (java.nio.ByteBuffer)65 Test (org.junit.jupiter.api.Test)52 Path (java.nio.file.Path)50 File (java.io.File)44 Test (org.junit.Test)42 DelegatingStoreChannel (org.neo4j.io.fs.DelegatingStoreChannel)26 PhysicalLogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.PhysicalLogVersionedStoreChannel)25 IOException (java.io.IOException)24 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)13 PageSwapperFactory (org.neo4j.io.pagecache.PageSwapperFactory)13 PageSwapperTest (org.neo4j.io.pagecache.PageSwapperTest)13 LogVersionedStoreChannel (org.neo4j.kernel.impl.transaction.log.LogVersionedStoreChannel)13 LogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeader)11 InputStream (java.io.InputStream)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 EphemeralFileSystemAbstraction (org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction)8 HeapScopedBuffer (org.neo4j.io.memory.HeapScopedBuffer)8 ReadAheadLogChannel (org.neo4j.kernel.impl.transaction.log.ReadAheadLogChannel)8 LogHeaderReader.readLogHeader (org.neo4j.kernel.impl.transaction.log.entry.LogHeaderReader.readLogHeader)8