Search in sources :

Example 1 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class FreeIdKeeperTest method persistedIdsShouldStillBeCounted.

@Test
public void persistedIdsShouldStillBeCounted() throws Exception {
    // 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);
    }
    // then
    // the count should be returned correctly
    assertEquals(threshold + extraIds, keeper.getCount());
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) Test(org.junit.Test)

Example 2 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class FreeIdKeeperTest method shouldNotReturnIdsPersistedDuringThisRunIfAggressiveIsFalse.

@Test
public void shouldNotReturnIdsPersistedDuringThisRunIfAggressiveIsFalse() throws Exception {
    // given
    StoreChannel channel = spy(fs.get().open(new File("id.file"), "rw"));
    int threshold = 10;
    FreeIdKeeper keeper = new FreeIdKeeper(channel, threshold, false);
    // enough ids are persisted to overflow
    for (int i = 0; i < threshold; i++) {
        keeper.freeId(i);
    }
    // then
    // stuff must have been written to disk
    verify(channel, times(1)).write(any(ByteBuffer.class));
    // and no ids can be returned
    assertEquals(NO_RESULT, keeper.getId());
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class FreeIdKeeperTest method shouldOnlyOverflowWhenThresholdIsReached.

@Test
public void shouldOnlyOverflowWhenThresholdIsReached() throws Exception {
    // Given
    StoreChannel channel = spy(fs.get().open(new File("id.file"), "rw"));
    int threshold = 10;
    FreeIdKeeper keeper = new FreeIdKeeper(channel, threshold, true);
    // because we get the position in the constructor, we need to reset all calls on the spy
    reset(channel);
    // we free 9 ids
    for (int i = 0; i < threshold - 1; i++) {
        keeper.freeId(i);
    }
    // then
    verifyZeroInteractions(channel);
    // when we free one more
    keeper.freeId(10);
    // then
    verify(channel).write(any(ByteBuffer.class));
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class FreeIdKeeperTest method shouldReturnIdsRestoredAndIgnoreNewlyReleasedIfAggressiveReuseIsFalse.

@Test
public void shouldReturnIdsRestoredAndIgnoreNewlyReleasedIfAggressiveReuseIsFalse() throws Exception {
    // given
    StoreChannel channel = fs.get().open(new File("id.file"), "rw");
    int threshold = 10;
    FreeIdKeeper keeper = new FreeIdKeeper(channel, threshold, false);
    Set<Long> freeIds = new HashSet<>();
    for (long i = 0; i < threshold; i++) {
        keeper.freeId(i);
        freeIds.add(i);
    }
    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, false);
    // we release some ids that spill to disk
    for (int i = 0; i < threshold; i++) {
        keeper.freeId(i);
    }
    // we should retrieve all ids restored
    for (int i = 0; i < threshold; i++) {
        assertTrue(freeIds.remove(keeper.getId()));
    }
    // then
    // we should have no ids to return
    assertEquals(NO_RESULT, keeper.getId());
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with StoreChannel

use of org.neo4j.io.fs.StoreChannel in project neo4j by neo4j.

the class FreeIdKeeperTest method shouldReturnNoResultIfIdsAreRestoredAndExhaustedAndThereAreFreeIdsFromThisRunWithAggressiveFalse.

@Test
public void shouldReturnNoResultIfIdsAreRestoredAndExhaustedAndThereAreFreeIdsFromThisRunWithAggressiveFalse() throws Exception {
    // given
    StoreChannel channel = fs.get().open(new File("id.file"), "rw");
    int threshold = 10;
    FreeIdKeeper keeper = new FreeIdKeeper(channel, threshold, false);
    Set<Long> freeIds = new HashSet<>();
    for (long i = 0; i < threshold; i++) {
        keeper.freeId(i);
        freeIds.add(i);
    }
    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, false);
    // we exhaust all ids restored
    for (int i = 0; i < threshold; i++) {
        assertTrue(freeIds.remove(keeper.getId()));
    }
    // we release some ids that spill to disk
    for (int i = 0; i < threshold; i++) {
        keeper.freeId(i);
    }
    // then
    // we should have no ids to return
    assertEquals(NO_RESULT, keeper.getId());
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) File(java.io.File) HashSet(java.util.HashSet) 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