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