private FreeIdKeeper getFreeIdKeeper( StoreChannel channel, int batchSize, boolean aggressiveMode ) throws IOException { return new FreeIdKeeper( channel, batchSize, aggressiveMode ); }
/** * Initializes the id generator and performs a simple validation. Returns true if the initialization restored * properly on disk state, false otherwise (such as creating an id file from scratch). * Will throw {@link InvalidIdGeneratorException} if the id file is found to be damaged or unclean. */ public boolean init() { boolean result = true; try { if ( !fs.fileExists( file ) ) { createEmptyIdFile( fs, file, 0, false ); result = false; } fileChannel = fs.open( file, OpenMode.READ_WRITE ); initialHighId = readAndValidateHeader(); markAsSticky(); this.freeIdKeeper = new FreeIdKeeper( new OffsetChannel( fileChannel, HEADER_SIZE ), grabSize, aggressiveReuse ); closed = false; } catch ( IOException e ) { throw new UnderlyingStorageException( "Unable to init id file " + file, e ); } return result; }
@Test public void persistedIdsShouldStillBeCounted() throws Exception { // given StoreChannel channel = getStoreChannel(); int batchSize = 10; FreeIdKeeper keeper = new FreeIdKeeper( channel, batchSize, true ); // when // we store enough ids to cause overflow to file for ( int i = 0; i < batchSize; i++ ) { keeper.freeId( i ); } // and then some more int extraIds = 3; for ( int i = batchSize; i < batchSize + extraIds; i++ ) { keeper.freeId( i ); } // then // the count should be returned correctly assertEquals( batchSize + extraIds, keeper.getCount() ); }
/** * Initializes the id generator and performs a simple validation. Returns true if the initialization restored * properly on disk state, false otherwise (such as creating an id file from scratch). * Will throw {@link InvalidIdGeneratorException} if the id file is found to be damaged or unclean. */ public boolean init() { boolean result = true; try { if ( !fs.fileExists( file ) ) { createEmptyIdFile( fs, file, 0, false ); result = false; } fileChannel = fs.open( file, OpenMode.READ_WRITE ); initialHighId = readAndValidateHeader(); markAsSticky(); this.freeIdKeeper = new FreeIdKeeper( new OffsetChannel( fileChannel, HEADER_SIZE ), grabSize, aggressiveReuse ); closed = false; } catch ( IOException e ) { throw new UnderlyingStorageException( "Unable to init id file " + file, e ); } return result; }