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