Search in sources :

Example 21 with IdGenerator

use of org.neo4j.kernel.impl.store.id.IdGenerator in project neo4j by neo4j.

the class HaIdGeneratorFactoryTest method shouldMoveFromDefraggedToRange.

@Test
public void shouldMoveFromDefraggedToRange() throws Exception {
    // GIVEN
    long[] defragIds = { 42, 27172828, 314159 };
    IdAllocation firstResult = new IdAllocation(new IdRange(defragIds, 0, 10), 100, defragIds.length);
    Response<IdAllocation> response = response(firstResult);
    when(master.allocateIds(any(RequestContext.class), any(IdType.class))).thenReturn(response);
    // WHEN
    IdGenerator gen = switchToSlave();
    // THEN
    for (long defragId : defragIds) {
        assertEquals(defragId, gen.nextId());
    }
}
Also used : RequestContext(org.neo4j.com.RequestContext) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) IdRange(org.neo4j.kernel.impl.store.id.IdRange) IdType(org.neo4j.kernel.impl.store.id.IdType) Test(org.junit.Test)

Example 22 with IdGenerator

use of org.neo4j.kernel.impl.store.id.IdGenerator in project neo4j by neo4j.

the class HaIdGeneratorFactoryTest method slaveIdGeneratorShouldReturnFromAssignedRange.

@Test
public void slaveIdGeneratorShouldReturnFromAssignedRange() throws Exception {
    // GIVEN
    IdAllocation firstResult = new IdAllocation(new IdRange(new long[] {}, 42, 123), 123, 0);
    Response<IdAllocation> response = response(firstResult);
    when(master.allocateIds(any(RequestContext.class), any(IdType.class))).thenReturn(response);
    // WHEN
    IdGenerator gen = switchToSlave();
    // THEN
    for (long i = firstResult.getIdRange().getRangeStart(); i < firstResult.getIdRange().getRangeLength(); i++) {
        assertEquals(i, gen.nextId());
    }
    verify(master, times(1)).allocateIds(any(RequestContext.class), eq(IdType.NODE));
}
Also used : RequestContext(org.neo4j.com.RequestContext) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) IdRange(org.neo4j.kernel.impl.store.id.IdRange) IdType(org.neo4j.kernel.impl.store.id.IdType) Test(org.junit.Test)

Example 23 with IdGenerator

use of org.neo4j.kernel.impl.store.id.IdGenerator in project neo4j by neo4j.

the class IdGeneratorContractTest method shouldReportCorrectHighId.

@Test
public void shouldReportCorrectHighId() throws Exception {
    // given
    IdGenerator idGenerator = createIdGenerator(2);
    assertEquals(0, idGenerator.getHighId());
    assertEquals(-1, idGenerator.getHighestPossibleIdInUse());
    // when
    idGenerator.nextId();
    // then
    assertEquals(1, idGenerator.getHighId());
    assertEquals(0, idGenerator.getHighestPossibleIdInUse());
}
Also used : IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) Test(org.junit.Test)

Example 24 with IdGenerator

use of org.neo4j.kernel.impl.store.id.IdGenerator in project neo4j by neo4j.

the class IdGeneratorTest method testFreeId.

@Test
public void testFreeId() {
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 3, 1000, false, 0);
        for (long i = 0; i < 7; i++) {
            assertEquals(i, idGenerator.nextId());
        }
        try {
            idGenerator.freeId(-1);
            fail("Negative id should throw exception");
        } catch (IllegalArgumentException e) {
        // good
        }
        try {
            idGenerator.freeId(7);
            fail("Greater id than ever returned should throw exception");
        } catch (IllegalArgumentException e) {
        // good
        }
        for (int i = 0; i < 7; i++) {
            idGenerator.freeId(i);
        }
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 2, 1000, false, 0);
        assertEquals(0L, idGenerator.nextId());
        assertEquals(1L, idGenerator.nextId());
        assertEquals(2L, idGenerator.nextId());
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 30, 1000, false, 0);
        // Since idGenerator.nextId() (which returns 2) will read ids 2 and
        // 3, then
        // 3 will be written at the end during the next close. And hence
        // will be returned
        // after 6.
        assertEquals(4L, idGenerator.nextId());
        assertEquals(5L, idGenerator.nextId());
        assertEquals(6L, idGenerator.nextId());
        assertEquals(3L, idGenerator.nextId());
        closeIdGenerator(idGenerator);
    } finally {
        File file = idGeneratorFile();
        if (file.exists()) {
            assertTrue(file.delete());
        }
    }
}
Also used : IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) File(java.io.File) Test(org.junit.Test)

Example 25 with IdGenerator

use of org.neo4j.kernel.impl.store.id.IdGenerator in project neo4j by neo4j.

the class IdGeneratorTest method createIdGeneratorMustRefuseOverwritingExistingFile.

@Test(expected = IllegalStateException.class)
public void createIdGeneratorMustRefuseOverwritingExistingFile() throws IOException {
    IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
    IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 1008, 1000, false, 0);
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, true);
    } finally {
        closeIdGenerator(idGenerator);
        // verify that id generator is ok
        StoreChannel fileChannel = fs.open(idGeneratorFile(), "rw");
        ByteBuffer buffer = ByteBuffer.allocate(9);
        assertEquals(9, fileChannel.read(buffer));
        buffer.flip();
        assertEquals((byte) 0, buffer.get());
        assertEquals(0L, buffer.getLong());
        buffer.flip();
        int readCount = fileChannel.read(buffer);
        if (readCount != -1 && readCount != 0) {
            fail("Id generator header not ok read 9 + " + readCount + " bytes from file");
        }
        fileChannel.close();
        File file = idGeneratorFile();
        if (file.exists()) {
            assertTrue(file.delete());
        }
    }
}
Also used : StoreChannel(org.neo4j.io.fs.StoreChannel) IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) ByteBuffer(java.nio.ByteBuffer) File(java.io.File) Test(org.junit.Test)

Aggregations

IdGenerator (org.neo4j.kernel.impl.store.id.IdGenerator)31 Test (org.junit.Test)24 File (java.io.File)15 IdGeneratorImpl (org.neo4j.kernel.impl.store.id.IdGeneratorImpl)14 IdType (org.neo4j.kernel.impl.store.id.IdType)8 RequestContext (org.neo4j.com.RequestContext)6 IdRange (org.neo4j.kernel.impl.store.id.IdRange)5 IdGeneratorFactory (org.neo4j.kernel.impl.store.id.IdGeneratorFactory)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 EphemeralIdGenerator (org.neo4j.test.impl.EphemeralIdGenerator)2 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Function (java.util.function.Function)1 Collectors.toSet (java.util.stream.Collectors.toSet)1