@Override public Long get() { if (currentIdIndex < OVERFLOW_ID_CACHE_SIZE) { return currentBatchStartId + currentIdIndex++; } currentBatchStartId = conns.get() .selectLongUnregisteredQuery( "SELECT " + tablePrefix + AtlasDbConstants.ORACLE_OVERFLOW_SEQUENCE + ".NEXTVAL FROM DUAL"); currentIdIndex = 0; return currentBatchStartId + currentIdIndex++; } }
@Test public void shouldGetConsecutiveOverflowIdsFromSameSupplier() throws Exception { final ConnectionSupplier conns = mock(ConnectionSupplier.class); final SqlConnection sqlConnection = mock(SqlConnection.class); when(conns.get()).thenReturn(sqlConnection); when(sqlConnection.selectLongUnregisteredQuery(anyString())).thenReturn(1L); OverflowSequenceSupplier sequenceSupplier = OverflowSequenceSupplier.create(conns, "a_"); long firstSequenceId = sequenceSupplier.get(); long nextSequenceId = sequenceSupplier.get(); assertThat(nextSequenceId - firstSequenceId, is(1L)); }
@Test public void shouldNotGetOverflowIdsWithOverlappingCachesFromDifferentSuppliers() throws Exception { final ConnectionSupplier conns = mock(ConnectionSupplier.class); final SqlConnection sqlConnection = mock(SqlConnection.class); when(conns.get()).thenReturn(sqlConnection); when(sqlConnection.selectLongUnregisteredQuery(anyString())).thenReturn(1L, 1001L); long firstSequenceId = OverflowSequenceSupplier.create(conns, "a_").get(); long secondSequenceId = OverflowSequenceSupplier.create(conns, "a_").get(); assertThat(secondSequenceId - firstSequenceId, greaterThanOrEqualTo(1000L)); }
@Test public void shouldSkipValuesReservedByOtherSupplier() throws Exception { final ConnectionSupplier conns = mock(ConnectionSupplier.class); final SqlConnection sqlConnection = mock(SqlConnection.class); when(conns.get()).thenReturn(sqlConnection); when(sqlConnection.selectLongUnregisteredQuery(anyString())).thenReturn(1L, 1001L, 2001L); OverflowSequenceSupplier firstSupplier = OverflowSequenceSupplier.create(conns, "a_"); firstSupplier.get(); // gets 1 OverflowSequenceSupplier.create(conns, "a_").get(); // gets 1001 // After 1000 gets from the first supplier, we should get to 1000 long id = 0; for (int i = 0; i < 999; i++) { id = firstSupplier.get(); } assertThat(id, equalTo(1000L)); // Should then skip to 2001 assertThat(firstSupplier.get(), equalTo(2001L)); } }
@Override public Long get() { if (currentIdIndex < OVERFLOW_ID_CACHE_SIZE) { return currentBatchStartId + currentIdIndex++; } currentBatchStartId = conns.get() .selectLongUnregisteredQuery( "SELECT " + tablePrefix + AtlasDbConstants.ORACLE_OVERFLOW_SEQUENCE + ".NEXTVAL FROM DUAL"); currentIdIndex = 0; return currentBatchStartId + currentIdIndex++; } }