Search in sources :

Example 16 with IdGenerator

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

the class ReplicatedTokenHolderTest method shouldReplicateTokenRequestForNewToken.

@Test
public void shouldReplicateTokenRequestForNewToken() throws Exception {
    // given
    StorageEngine storageEngine = mockedStorageEngine();
    when(dependencies.resolveDependency(StorageEngine.class)).thenReturn(storageEngine);
    IdGeneratorFactory idGeneratorFactory = mock(IdGeneratorFactory.class);
    IdGenerator idGenerator = mock(IdGenerator.class);
    when(idGenerator.nextId()).thenReturn(1L);
    when(idGeneratorFactory.get(any(IdType.class))).thenReturn(idGenerator);
    TokenRegistry<Token> registry = new TokenRegistry<>("Label");
    int generatedTokenId = 1;
    ReplicatedTokenHolder<Token> tokenHolder = new ReplicatedLabelTokenHolder(registry, (content, trackResult) -> {
        CompletableFuture<Object> completeFuture = new CompletableFuture<>();
        completeFuture.complete(generatedTokenId);
        return completeFuture;
    }, idGeneratorFactory, dependencies);
    // when
    Integer tokenId = tokenHolder.getOrCreateId("name1");
    // then
    assertThat(tokenId, equalTo(generatedTokenId));
}
Also used : IdGeneratorFactory(org.neo4j.kernel.impl.store.id.IdGeneratorFactory) Token(org.neo4j.storageengine.api.Token) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) StorageEngine(org.neo4j.storageengine.api.StorageEngine) IdType(org.neo4j.kernel.impl.store.id.IdType) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Example 17 with IdGenerator

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

the class HaIdGeneratorFactory method open.

@Override
public IdGenerator open(File fileName, int grabSize, IdType idType, long highId, long maxId) {
    HaIdGenerator previous = generators.remove(idType);
    if (previous != null) {
        previous.close();
    }
    IdGenerator initialIdGenerator;
    switch(globalState) {
        case MASTER:
            initialIdGenerator = localFactory.open(fileName, grabSize, idType, highId, maxId);
            break;
        case SLAVE:
            // Initially we may call switchToSlave() before calling open, so we need this additional
            // (and, you might say, hacky) call to delete the .id file here as well as in switchToSlave().
            fs.deleteFile(fileName);
            initialIdGenerator = new SlaveIdGenerator(idType, highId, master.cement(), log, requestContextFactory);
            break;
        default:
            throw new IllegalStateException(globalState.name());
    }
    HaIdGenerator haIdGenerator = new HaIdGenerator(initialIdGenerator, fileName, grabSize, idType, globalState, maxId);
    generators.put(idType, haIdGenerator);
    return haIdGenerator;
}
Also used : IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator)

Example 18 with IdGenerator

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

the class JumpingIdGeneratorFactory method get.

@Override
public IdGenerator get(IdType idType) {
    if (idType == IdType.NODE || idType == IdType.RELATIONSHIP || idType == IdType.PROPERTY || idType == IdType.STRING_BLOCK || idType == IdType.ARRAY_BLOCK) {
        IdGenerator generator = generators.get(idType);
        if (generator == null) {
            generator = new JumpingIdGenerator();
            generators.put(idType, generator);
        }
        return generator;
    }
    return forTheRest;
}
Also used : EphemeralIdGenerator(org.neo4j.test.impl.EphemeralIdGenerator) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator)

Example 19 with IdGenerator

use of org.neo4j.kernel.impl.store.id.IdGenerator 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)

Example 20 with IdGenerator

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

the class HaIdGeneratorFactoryTest method shouldTranslateComExceptionsIntoTransientTransactionFailures.

@Test(expected = TransientTransactionFailureException.class)
public void shouldTranslateComExceptionsIntoTransientTransactionFailures() throws Exception {
    when(master.allocateIds(any(RequestContext.class), any(IdType.class))).thenThrow(new ComException());
    IdGenerator generator = switchToSlave();
    generator.nextId();
}
Also used : ComException(org.neo4j.com.ComException) RequestContext(org.neo4j.com.RequestContext) IdGenerator(org.neo4j.kernel.impl.store.id.IdGenerator) IdType(org.neo4j.kernel.impl.store.id.IdType) Test(org.junit.Test)

Aggregations

IdGenerator (org.neo4j.kernel.impl.store.id.IdGenerator)31 Test (org.junit.Test)24 File (java.io.File)15 IdGeneratorImpl (org.neo4j.kernel.impl.store.id.IdGeneratorImpl)14 IdType (org.neo4j.kernel.impl.store.id.IdType)8 RequestContext (org.neo4j.com.RequestContext)6 IdRange (org.neo4j.kernel.impl.store.id.IdRange)5 IdGeneratorFactory (org.neo4j.kernel.impl.store.id.IdGeneratorFactory)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 EphemeralIdGenerator (org.neo4j.test.impl.EphemeralIdGenerator)2 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Function (java.util.function.Function)1 Collectors.toSet (java.util.stream.Collectors.toSet)1