Search in sources :

Example 11 with IdGeneratorImpl

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

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

Example 13 with IdGeneratorImpl

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

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

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

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