Search in sources :

Example 1 with IdRange

use of org.neo4j.kernel.impl.store.id.IdRange in project neo4j by neo4j.

the class IdRangeIteratorTest method shouldUseDefragIdsFirst.

@Test
public void shouldUseDefragIdsFirst() throws Exception {
    // given
    int rangeLength = 1024;
    IdRangeIterator iterator = new IdRangeIterator(new IdRange(new long[] { 7, 8, 9 }, 1024, rangeLength));
    // then
    assertEquals(7, iterator.next());
    assertEquals(8, iterator.next());
    assertEquals(9, iterator.next());
    assertEquals(1024, iterator.next());
}
Also used : IdRange(org.neo4j.kernel.impl.store.id.IdRange) Test(org.junit.Test)

Example 2 with IdRange

use of org.neo4j.kernel.impl.store.id.IdRange in project neo4j by neo4j.

the class IdRangeIteratorTest method shouldNotHaveAnyGaps.

@Test
public void shouldNotHaveAnyGaps() throws Exception {
    // given
    int rangeLength = 1024;
    IdRangeIterator iterator = new IdRangeIterator(new IdRange(new long[] {}, 0, rangeLength));
    // when
    Set<Long> seenIds = new HashSet<>();
    for (int i = 0; i < rangeLength; i++) {
        seenIds.add(iterator.next());
        if (i > 0) {
            // then
            assertTrue("Missing id " + (i - 1), seenIds.contains((long) i - 1));
        }
    }
}
Also used : IdRange(org.neo4j.kernel.impl.store.id.IdRange) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with IdRange

use of org.neo4j.kernel.impl.store.id.IdRange in project neo4j by neo4j.

the class HaIdGeneratorFactoryTest method slaveShouldNeverAllowReducingHighId.

@Test
public void slaveShouldNeverAllowReducingHighId() throws Exception {
    // GIVEN
    final int highIdFromAllocation = 123;
    IdAllocation firstResult = new IdAllocation(new IdRange(new long[] {}, 42, highIdFromAllocation), highIdFromAllocation, 0);
    Response<IdAllocation> response = response(firstResult);
    when(master.allocateIds(any(RequestContext.class), any(IdType.class))).thenReturn(response);
    // WHEN
    IdGenerator gen = switchToSlave();
    final int highIdFromUpdatedRecord = highIdFromAllocation + 1;
    // Assume this is from a received transaction
    gen.setHighId(highIdFromUpdatedRecord);
    // that will ask the master for an IdRange
    gen.nextId();
    // THEN
    assertEquals(highIdFromUpdatedRecord, gen.getHighId());
}
Also used : RequestContext(org.neo4j.com.RequestContext) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) IdRange(org.neo4j.kernel.impl.store.id.IdRange) IdType(org.neo4j.kernel.impl.store.id.IdType) Test(org.junit.Test)

Example 4 with IdRange

use of org.neo4j.kernel.impl.store.id.IdRange in project neo4j by neo4j.

the class HaIdGeneratorFactoryTest method shouldUseDefraggedIfPresent.

@Test
public void shouldUseDefraggedIfPresent() throws Exception {
    // GIVEN
    long[] defragIds = { 42, 27172828, 314159 };
    IdAllocation firstResult = new IdAllocation(new IdRange(defragIds, 0, 0), 0, 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());
    }
}
Also used : RequestContext(org.neo4j.com.RequestContext) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) IdRange(org.neo4j.kernel.impl.store.id.IdRange) IdType(org.neo4j.kernel.impl.store.id.IdType) Test(org.junit.Test)

Example 5 with IdRange

use of org.neo4j.kernel.impl.store.id.IdRange in project neo4j by neo4j.

the class HaIdGeneratorFactoryTest method slaveIdGeneratorShouldAskForMoreWhenRangeIsOver.

@Test
public void slaveIdGeneratorShouldAskForMoreWhenRangeIsOver() throws Exception {
    // GIVEN
    IdAllocation firstResult = new IdAllocation(new IdRange(new long[] {}, 42, 123), 42 + 123, 0);
    IdAllocation secondResult = new IdAllocation(new IdRange(new long[] {}, 1042, 223), 1042 + 223, 0);
    Response<IdAllocation> response = response(firstResult, secondResult);
    when(master.allocateIds(any(RequestContext.class), any(IdType.class))).thenReturn(response);
    // WHEN
    IdGenerator gen = switchToSlave();
    // THEN
    long startAt = firstResult.getIdRange().getRangeStart();
    long forThatMany = firstResult.getIdRange().getRangeLength();
    for (long i = startAt; i < startAt + forThatMany; i++) {
        assertEquals(i, gen.nextId());
    }
    verify(master, times(1)).allocateIds(any(RequestContext.class), eq(IdType.NODE));
    startAt = secondResult.getIdRange().getRangeStart();
    forThatMany = secondResult.getIdRange().getRangeLength();
    for (long i = startAt; i < startAt + forThatMany; i++) {
        assertEquals(i, gen.nextId());
    }
    verify(master, times(2)).allocateIds(any(RequestContext.class), eq(IdType.NODE));
}
Also used : RequestContext(org.neo4j.com.RequestContext) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) IdRange(org.neo4j.kernel.impl.store.id.IdRange) IdType(org.neo4j.kernel.impl.store.id.IdType) Test(org.junit.Test)

Aggregations

IdRange (org.neo4j.kernel.impl.store.id.IdRange)14 Test (org.junit.Test)12 RequestContext (org.neo4j.com.RequestContext)5 IdGenerator (org.neo4j.kernel.impl.store.id.IdGenerator)5 IdType (org.neo4j.kernel.impl.store.id.IdType)5 HashSet (java.util.HashSet)2 IdAllocation (org.neo4j.kernel.ha.id.IdAllocation)1