use of org.neo4j.kernel.impl.store.id.IdGenerator in project neo4j by neo4j.
the class HaIdGeneratorFactoryTest method shouldMoveFromDefraggedToRange.
@Test
public void shouldMoveFromDefraggedToRange() throws Exception {
// GIVEN
long[] defragIds = { 42, 27172828, 314159 };
IdAllocation firstResult = new IdAllocation(new IdRange(defragIds, 0, 10), 100, defragIds.length);
Response<IdAllocation> response = response(firstResult);
when(master.allocateIds(any(RequestContext.class), any(IdType.class))).thenReturn(response);
// WHEN
IdGenerator gen = switchToSlave();
// THEN
for (long defragId : defragIds) {
assertEquals(defragId, gen.nextId());
}
}
use of org.neo4j.kernel.impl.store.id.IdGenerator in project neo4j by neo4j.
the class HaIdGeneratorFactoryTest method slaveIdGeneratorShouldReturnFromAssignedRange.
@Test
public void slaveIdGeneratorShouldReturnFromAssignedRange() throws Exception {
// GIVEN
IdAllocation firstResult = new IdAllocation(new IdRange(new long[] {}, 42, 123), 123, 0);
Response<IdAllocation> response = response(firstResult);
when(master.allocateIds(any(RequestContext.class), any(IdType.class))).thenReturn(response);
// WHEN
IdGenerator gen = switchToSlave();
// THEN
for (long i = firstResult.getIdRange().getRangeStart(); i < firstResult.getIdRange().getRangeLength(); i++) {
assertEquals(i, gen.nextId());
}
verify(master, times(1)).allocateIds(any(RequestContext.class), eq(IdType.NODE));
}
use of org.neo4j.kernel.impl.store.id.IdGenerator in project neo4j by neo4j.
the class IdGeneratorContractTest method shouldReportCorrectHighId.
@Test
public void shouldReportCorrectHighId() throws Exception {
// given
IdGenerator idGenerator = createIdGenerator(2);
assertEquals(0, idGenerator.getHighId());
assertEquals(-1, idGenerator.getHighestPossibleIdInUse());
// when
idGenerator.nextId();
// then
assertEquals(1, idGenerator.getHighId());
assertEquals(0, idGenerator.getHighestPossibleIdInUse());
}
use of org.neo4j.kernel.impl.store.id.IdGenerator 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());
}
}
}
use of org.neo4j.kernel.impl.store.id.IdGenerator 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());
}
}
}
Aggregations