Search in sources :

Example 11 with IdGenerator

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

the class IdGeneratorTest method testClose.

@Test
public void testClose() {
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 2, 1000, false, 0);
        closeIdGenerator(idGenerator);
        try {
            idGenerator.nextId();
            fail("nextId after close should throw exception");
        } catch (IllegalStateException e) {
        // good
        }
        try {
            idGenerator.freeId(0);
            fail("freeId after close should throw exception");
        } catch (IllegalStateException e) {
        // good
        }
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 2, 1000, false, 0);
        assertEquals(0L, idGenerator.nextId());
        assertEquals(1L, idGenerator.nextId());
        assertEquals(2L, idGenerator.nextId());
        closeIdGenerator(idGenerator);
        try {
            idGenerator.nextId();
            fail("nextId after close should throw exception");
        } catch (IllegalStateException e) {
        // good
        }
        try {
            idGenerator.freeId(0);
            fail("freeId after close should throw exception");
        } catch (IllegalStateException e) {
        // good
        }
    } 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 12 with IdGenerator

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

the class IdGeneratorTest method makeSureMagicMinusOneIsSkipped.

private void makeSureMagicMinusOneIsSkipped(RecordFormat format) {
    deleteIdGeneratorFile();
    IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
    IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 1, format.getMaxId(), false, 0);
    long id = (long) Math.pow(2, 32) - 3;
    idGenerator.setHighId(id);
    assertEquals(id, idGenerator.nextId());
    assertEquals(id + 1, idGenerator.nextId());
    // Here we make sure that id+2 (integer -1) is skipped
    assertEquals(id + 3, idGenerator.nextId());
    assertEquals(id + 4, idGenerator.nextId());
    assertEquals(id + 5, idGenerator.nextId());
    closeIdGenerator(idGenerator);
}
Also used : IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator)

Example 13 with IdGenerator

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

the class IdGeneratorTest method testStickyGenerator.

@Test
public void testStickyGenerator() {
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGen = new IdGeneratorImpl(fs, idGeneratorFile(), 3, 1000, false, 0);
        try {
            new IdGeneratorImpl(fs, idGeneratorFile(), 3, 1000, false, 0);
            fail("Opening sticky id generator should throw exception");
        } catch (StoreFailureException e) {
        // good
        }
        closeIdGenerator(idGen);
    } 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 14 with IdGenerator

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

the class IdGeneratorTest method testNextId.

@Test
public void testNextId() {
    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());
        }
        idGenerator.freeId(1);
        idGenerator.freeId(3);
        idGenerator.freeId(5);
        assertEquals(7L, idGenerator.nextId());
        idGenerator.freeId(6);
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 5, 1000, false, 0);
        idGenerator.freeId(2);
        idGenerator.freeId(4);
        assertEquals(1L, idGenerator.nextId());
        idGenerator.freeId(1);
        assertEquals(3L, idGenerator.nextId());
        idGenerator.freeId(3);
        assertEquals(5L, idGenerator.nextId());
        idGenerator.freeId(5);
        assertEquals(6L, idGenerator.nextId());
        idGenerator.freeId(6);
        assertEquals(8L, idGenerator.nextId());
        idGenerator.freeId(8);
        assertEquals(9L, idGenerator.nextId());
        idGenerator.freeId(9);
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 3, 1000, false, 0);
        assertEquals(2L, idGenerator.nextId());
        assertEquals(4L, idGenerator.nextId());
        assertEquals(1L, idGenerator.nextId());
        assertEquals(3L, idGenerator.nextId());
        assertEquals(5L, idGenerator.nextId());
        assertEquals(6L, idGenerator.nextId());
        assertEquals(8L, idGenerator.nextId());
        assertEquals(9L, idGenerator.nextId());
        assertEquals(10L, idGenerator.nextId());
        assertEquals(11L, 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 15 with IdGenerator

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

the class IdGeneratorTest method testOddAndEvenWorstCase.

@Test
public void testOddAndEvenWorstCase() {
    int capacity = 1024 * 8 + 1;
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 128, capacity * 2, false, 0);
        for (int i = 0; i < capacity; i++) {
            idGenerator.nextId();
        }
        Map<Long, Object> freedIds = new HashMap<>();
        for (long i = 1; i < capacity; i += 2) {
            idGenerator.freeId(i);
            freedIds.put(i, this);
        }
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 2000, capacity * 2, false, 0);
        long oldId = -1;
        for (int i = 0; i < capacity - 1; i += 2) {
            long id = idGenerator.nextId();
            if (freedIds.remove(id) == null) {
                throw new RuntimeException("Id=" + id + " prevId=" + oldId + " list.size()=" + freedIds.size());
            }
            oldId = id;
        }
        assertTrue(freedIds.values().size() == 0);
        closeIdGenerator(idGenerator);
    } finally {
        File file = idGeneratorFile();
        if (fs.fileExists(file)) {
            assertTrue(fs.deleteFile(file));
        }
    }
    try {
        IdGeneratorImpl.createGenerator(fs, idGeneratorFile(), 0, false);
        IdGenerator idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 128, capacity * 2, false, 0);
        for (int i = 0; i < capacity; i++) {
            idGenerator.nextId();
        }
        Map<Long, Object> freedIds = new HashMap<>();
        for (long i = 0; i < capacity; i += 2) {
            idGenerator.freeId(i);
            freedIds.put(i, this);
        }
        closeIdGenerator(idGenerator);
        idGenerator = new IdGeneratorImpl(fs, idGeneratorFile(), 2000, capacity * 2, false, 0);
        for (int i = 0; i < capacity; i += 2) {
            assertEquals(this, freedIds.remove(idGenerator.nextId()));
        }
        assertEquals(0, freedIds.values().size());
        closeIdGenerator(idGenerator);
    } finally {
        File file = idGeneratorFile();
        if (file.exists()) {
            assertTrue(file.delete());
        }
    }
}
Also used : HashMap(java.util.HashMap) IdGeneratorImpl(org.neo4j.kernel.impl.store.id.IdGeneratorImpl) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) 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