/** * @return next free id or {@link IdContainer#NO_RESULT} if not available */ public long getReusableId() { return freeIdKeeper.getId(); }
public IdRange getReusableIdBatch( int maxSize ) { long[] tmpIdArr = new long[maxSize]; int count = 0; while ( count < maxSize ) { long id = freeIdKeeper.getId(); if ( id == NO_RESULT ) { break; } tmpIdArr[count++] = id; } long[] defragIdArr = count == maxSize ? tmpIdArr : Arrays.copyOfRange( tmpIdArr, 0, count ); return new IdRange( defragIdArr, 0, 0 ); }
@Test public void shouldNotReturnIdsPersistedDuringThisRunIfAggressiveIsFalse() throws Exception { // given StoreChannel channel = spy( fs.get().open( new File( "id.file" ), OpenMode.READ_WRITE ) ); int batchSize = 10; FreeIdKeeper keeper = getFreeIdKeeper( channel, batchSize ); // when // enough ids are persisted to overflow for ( int i = 0; i < batchSize; i++ ) { keeper.freeId( i ); } // then // stuff must have been written to disk verify( channel, times( 1 ) ).write( any( ByteBuffer.class ) ); // and no ids can be returned assertEquals( NO_RESULT, keeper.getId() ); }
@Test public void shouldNotReturnNewlyReleasedIdsIfAggressiveIsFalse() throws Exception { // given FreeIdKeeper keeper = getFreeIdKeeper(); // when keeper.freeId( 1 ); long nextFree = keeper.getId(); // then assertEquals( NO_RESULT, nextFree ); }
@Test public void shouldReadBackManyPersistedIdBatchesWhenAggressiveModeIsSet() throws Exception { // given StoreChannel channel = getStoreChannel(); int batchSize = 10; FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, batchSize ); Set<Long> freeIds = new HashSet<>(); // when // we store enough ids to cause overflow to file, in two batches for ( long i = 0; i < batchSize * 2; i++ ) { keeper.freeId( i ); freeIds.add( i ); } // then // they should be returned assertEquals( freeIds.size(), keeper.getCount() ); for ( int i = batchSize * 2 - 1; i >= 0; i-- ) { assertTrue( freeIds.remove( keeper.getId() ) ); } }
@Test public void newlyConstructedInstanceShouldReportProperDefaultValues() throws Exception { // Given FreeIdKeeper keeper = getFreeIdKeeperAggressive(); // then assertEquals( NO_RESULT, keeper.getId() ); assertEquals( 0, keeper.getCount() ); }
@Test public void shouldReadBackPersistedIdsWhenAggressiveModeIsSet() throws Exception { // given StoreChannel channel = getStoreChannel(); int batchSize = 10; FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, batchSize ); // when // we store enough ids to cause overflow to file for ( int i = 0; i < batchSize; i++ ) { keeper.freeId( i ); } // then // they should be returned in order for ( int i = 0; i < batchSize; i++ ) { assertEquals( i, keeper.getId() ); } }
long id = keeper.getId(); assertTrue( freeIds.contains( id ) );
@Test public void freeingAnIdShouldReturnThatIdAndUpdateTheCountWhenAggressiveModeIsSet() throws Exception { // Given FreeIdKeeper keeper = getFreeIdKeeperAggressive(); // when keeper.freeId( 13 ); // then assertEquals( 1, keeper.getCount() ); // when long result = keeper.getId(); // then assertEquals( 13, result ); assertEquals( 0, keeper.getCount() ); }
@Test public void shouldTruncateFileInAggressiveMode() throws Exception { // given StoreChannel channel = getStoreChannel(); int batchSize = 10; FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, batchSize ); // free 4 batches for ( long i = 0; i < batchSize * 4; i++ ) { keeper.freeId( i ); } assertEquals( channel.size(), 4 * batchSize * Long.BYTES ); // when for ( int i = 0; i < batchSize * 2; i++ ) { keeper.getId(); } // then assertEquals( channel.size(), 2 * batchSize * Long.BYTES ); }
keeper.getId();
/** * @return next free id or {@link IdContainer#NO_RESULT} if not available */ public long getReusableId() { return freeIdKeeper.getId(); }
public IdRange getReusableIdBatch( int maxSize ) { long[] tmpIdArr = new long[maxSize]; int count = 0; while ( count < maxSize ) { long id = freeIdKeeper.getId(); if ( id == NO_RESULT ) { break; } tmpIdArr[count++] = id; } long[] defragIdArr = count == maxSize ? tmpIdArr : Arrays.copyOfRange( tmpIdArr, 0, count ); return new IdRange( defragIdArr, 0, 0 ); }