Search in sources :

Example 26 with IdGenerator

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

the class IdGeneratorTest method makeSureMagicMinusOneCannotBeReturnedEvenIfFreed.

@Test
public void makeSureMagicMinusOneCannotBeReturnedEvenIfFreed() throws Exception {
    IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
    IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 1, new NodeRecordFormat().getMaxId(), false, 0);
    long magicMinusOne = (long) Math.pow(2, 32) - 1;
    idGenerator.setHighId(magicMinusOne);
    assertEquals(magicMinusOne + 1, idGenerator.nextId());
    idGenerator.freeId(magicMinusOne - 1);
    idGenerator.freeId(magicMinusOne);
    closeIdGenerator(idGenerator);
    idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 1, new NodeRecordFormat().getMaxId(), false, 0);
    assertEquals(magicMinusOne - 1, idGenerator.nextId());
    assertEquals(magicMinusOne + 2, idGenerator.nextId());
    closeIdGenerator(idGenerator);
}
Also used : IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) NodeRecordFormat(org.neo4j.kernel.impl.store.format.standard.NodeRecordFormat) Test(org.junit.Test)

Example 27 with IdGenerator

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

the class IdGeneratorTest method testUnsignedId.

@Test
public void testUnsignedId() {
    try {
        PropertyKeyTokenRecordFormat recordFormat = new PropertyKeyTokenRecordFormat();
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 1, recordFormat.getMaxId(), false, 0);
        idGenerator.setHighId(recordFormat.getMaxId());
        long id = idGenerator.nextId();
        assertEquals(recordFormat.getMaxId(), id);
        idGenerator.freeId(id);
        try {
            idGenerator.nextId();
            fail("Shouldn't be able to get next ID");
        } catch (StoreFailureException e) {
        // good, capacity exceeded
        }
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 1, recordFormat.getMaxId(), false, 0);
        assertEquals(recordFormat.getMaxId() + 1, idGenerator.getHighId());
        id = idGenerator.nextId();
        assertEquals(recordFormat.getMaxId(), id);
        try {
            idGenerator.nextId();
        } catch (StoreFailureException e) {
        // good, capacity exceeded
        }
        closeIdGenerator(idGenerator);
    } finally {
        File file = idGeneratorFile();
        if (file.exists()) {
            assertTrue(file.delete());
        }
    }
}
Also used : PropertyKeyTokenRecordFormat(org.neo4j.kernel.impl.store.format.standard.PropertyKeyTokenRecordFormat) 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 28 with IdGenerator

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

the class IdGeneratorTest method mustOverwriteExistingFileIfRequested.

@Test
public void mustOverwriteExistingFileIfRequested() throws Exception {
    IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
    IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 1008, 1000, false, 0);
    long[] firstFirstIds = new long[] { idGenerator.nextId(), idGenerator.nextId(), idGenerator.nextId() };
    idGenerator.close();
    IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
    idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 1008, 1000, false, 0);
    long[] secondFirstIds = new long[] { idGenerator.nextId(), idGenerator.nextId(), idGenerator.nextId() };
    idGenerator.close();
    // Basically, recreating the id file should be the same as start over with the ids.
    assertThat(secondFirstIds, is(firstFirstIds));
}
Also used : IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) Test(org.junit.Test)

Example 29 with IdGenerator

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

the class IdGeneratorTest method testRandomTest.

@Test
public void testRandomTest() {
    java.util.Random random = new java.util.Random(System.currentTimeMillis());
    int capacity = random.nextInt(1024) + 1024;
    int grabSize = random.nextInt(128) + 128;
    IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
    IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), grabSize, capacity * 2, false, 0);
    List<Long> idsTaken = new ArrayList<>();
    float releaseIndex = 0.25f;
    float closeIndex = 0.05f;
    int currentIdCount = 0;
    try {
        while (currentIdCount < capacity) {
            float rIndex = random.nextFloat();
            if (rIndex < releaseIndex && currentIdCount > 0) {
                idGenerator.freeId(idsTaken.remove(random.nextInt(currentIdCount)).intValue());
                currentIdCount--;
            } else {
                idsTaken.add(idGenerator.nextId());
                currentIdCount++;
            }
            if (rIndex > (1.0f - closeIndex) || rIndex < closeIndex) {
                closeIdGenerator(idGenerator);
                grabSize = random.nextInt(128) + 128;
                idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), grabSize, capacity * 2, false, 0);
            }
        }
        closeIdGenerator(idGenerator);
    } finally {
        File file = idGeneratorFile();
        if (file.exists()) {
            assertTrue(file.delete());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) 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 30 with IdGenerator

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

the class IdGeneratorTest method makeSureIdCapacityCannotBeExceeded.

private void makeSureIdCapacityCannotBeExceeded(RecordFormat format) {
    deleteIdGeneratorFile();
    IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
    long maxValue = format.getMaxId();
    IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 1, maxValue - 1, false, 0);
    long id = maxValue - 2;
    idGenerator.setHighId(id);
    assertEquals(id, idGenerator.nextId());
    assertEquals(id + 1, idGenerator.nextId());
    try {
        idGenerator.nextId();
        fail("Id capacity shouldn't be able to be exceeded for " + format);
    } catch (StoreFailureException e) {
    // Good
    }
    closeIdGenerator(idGenerator);
}
Also used : IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator)

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