Search in sources :

Example 16 with IdGeneratorImpl

use of org.neo4j.kernel.impl.store.id.IdGeneratorImpl 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 17 with IdGeneratorImpl

use of org.neo4j.kernel.impl.store.id.IdGeneratorImpl 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)

Example 18 with IdGeneratorImpl

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

the class IdGeneratorTest method testChurnIdBatchAtGrabSize.

@Test
public void testChurnIdBatchAtGrabSize() {
    IdGenerator idGenerator = null;
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        final int grabSize = 10, rounds = 10;
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), grabSize, 1000, true, 0);
        for (int i = 0; i < rounds; i++) {
            Set<Long> ids = new HashSet<>();
            for (int j = 0; j < grabSize; j++) {
                ids.add(idGenerator.nextId());
            }
            for (Long id : ids) {
                idGenerator.freeId(id);
            }
        }
        long newId = idGenerator.nextId();
        assertTrue("Expected IDs to be reused (" + grabSize + " at a time). high ID was: " + newId, newId < grabSize * rounds);
    } finally {
        if (idGenerator != null) {
            closeIdGenerator(idGenerator);
        }
        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) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

IdGeneratorImpl (org.neo4j.kernel.impl.store.id.IdGeneratorImpl)18 Test (org.junit.Test)15 IdGenerator (org.neo4j.kernel.impl.store.id.IdGenerator)14 File (java.io.File)10 StoreChannel (org.neo4j.io.fs.StoreChannel)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 NodeRecordFormat (org.neo4j.kernel.impl.store.format.standard.NodeRecordFormat)1 PropertyKeyTokenRecordFormat (org.neo4j.kernel.impl.store.format.standard.PropertyKeyTokenRecordFormat)1 StoreFile (org.neo4j.kernel.impl.storemigration.StoreFile)1