use of org.neo4j.internal.id.IdRange in project neo4j by neo4j.
the class IndexedIdGeneratorTest method shouldAllocateConsecutiveIdBatches.
@Test
void shouldAllocateConsecutiveIdBatches() {
// given
AtomicInteger numAllocations = new AtomicInteger();
Race race = new Race().withEndCondition(() -> numAllocations.get() >= 10_000);
Collection<IdRange> allocations = ConcurrentHashMap.newKeySet();
race.addContestants(4, () -> {
int size = ThreadLocalRandom.current().nextInt(10, 1_000);
IdRange idRange = idGenerator.nextIdBatch(size, true, NULL);
assertEquals(0, idRange.getDefragIds().length);
assertEquals(size, idRange.getRangeLength());
allocations.add(idRange);
numAllocations.incrementAndGet();
});
// when
race.goUnchecked();
// then
IdRange[] sortedAllocations = allocations.toArray(new IdRange[allocations.size()]);
Arrays.sort(sortedAllocations, Comparator.comparingLong(IdRange::getRangeStart));
long prevEndExclusive = 0;
for (IdRange allocation : sortedAllocations) {
assertEquals(prevEndExclusive, allocation.getRangeStart());
prevEndExclusive = allocation.getRangeStart() + allocation.getRangeLength();
}
}
use of org.neo4j.internal.id.IdRange in project neo4j by neo4j.
the class BatchingIdGetter method nextId.
@Override
public long nextId(CursorContext cursorContext) {
long id;
if (batch == null || (id = batch.nextId(cursorContext)) == VALUE_REPRESENTING_NULL) {
idExpectation = NO_ID_EXPECTATION;
IdRange idRange = source.nextIdBatch(batchSize, true, cursorContext);
while (IdValidator.hasReservedIdInRange(idRange.getRangeStart(), idRange.getRangeStart() + idRange.getRangeLength())) {
idRange = source.nextIdBatch(batchSize, true, cursorContext);
}
batch = new IdRangeIterator(idRange);
id = batch.nextId(cursorContext);
}
if (idExpectation != NO_ID_EXPECTATION) {
if (id != idExpectation) {
throw new IllegalStateException(format("Id generator allocated range with non-consecutive IDs, expected:%d, but got:%d", idExpectation, id));
}
}
idExpectation = id + 1;
return id;
}
use of org.neo4j.internal.id.IdRange in project neo4j by neo4j.
the class IndexedIdGeneratorTest method shouldNotAllocateReservedIdsInBatchedAllocation.
@ValueSource(booleans = { true, false })
@ParameterizedTest
void shouldNotAllocateReservedIdsInBatchedAllocation(boolean consecutive) throws IOException {
// given
idGenerator.start(NO_FREE_IDS, NULL);
idGenerator.setHighId(IdValidator.INTEGER_MINUS_ONE - 100);
// when
IdRange batch = idGenerator.nextIdBatch(200, consecutive, NULL);
// then
assertFalse(IdValidator.hasReservedIdInRange(batch.getRangeStart(), batch.getRangeStart() + batch.getRangeLength()));
for (long defragId : batch.getDefragIds()) {
assertFalse(IdValidator.isReservedId(defragId));
}
}
Aggregations