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());
}
}
}
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);
}
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());
}
}
}
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());
}
}
}
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());
}
}
}
Aggregations