/** * Creates a new id generator. * * @param fileName The name of the id generator * @param throwIfFileExists if {@code true} will cause an {@link UnderlyingStorageException} to be thrown if * the file already exists. if {@code false} will truncate the file writing the header in it. */ public static void createGenerator( FileSystemAbstraction fs, File fileName, long highId, boolean throwIfFileExists ) { IdContainer.createEmptyIdFile( fs, fileName, highId, throwIfFileExists ); }
private void createEmptyFile() { IdContainer.createEmptyIdFile( fs, file, 42, false ); }
@Test public void constructorShouldNotCallHighIdSupplierOnCleanIdFile() { // Given // A non empty, clean id file IdContainer.createEmptyIdFile( fsr.get(), file, 42, true ); // and a mock supplier to test against LongSupplier highId = mock( LongSupplier.class ); // When // An IdGenerator is created over the previous properly closed file IdGenerator idGenerator = new IdGeneratorImpl( fsr.get(), file, 100, 100, false, IdType.NODE, highId ); idGenerator.close(); // Then // The supplier must have remained untouched verifyZeroInteractions( highId ); } }
/** * 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 shouldTruncateTheFileIfOverwriting() throws Exception { // GIVEN IdContainer.createEmptyIdFile( fs, file, 30, false ); IdContainer idContainer = new IdContainer( fs, file, 5, false ); idContainer.init(); for ( int i = 0; i < 17; i++ ) { idContainer.freeId( i ); } idContainer.close( 30 ); assertThat( (int) fs.getFileSize( file ), greaterThan( IdContainer.HEADER_SIZE ) ); // WHEN IdContainer.createEmptyIdFile( fs, file, 30, false ); // THEN assertEquals( IdContainer.HEADER_SIZE, (int) fs.getFileSize( file ) ); assertEquals( 30, IdContainer.readHighId( fs, file ) ); idContainer = new IdContainer( fs, file, 5, false ); idContainer.init(); assertEquals( 30, idContainer.getInitialHighId() ); idContainer.close( 30 ); }
/** * Creates a new id generator. * * @param fileName The name of the id generator * @param throwIfFileExists if {@code true} will cause an {@link UnderlyingStorageException} to be thrown if * the file already exists. if {@code false} will truncate the file writing the header in it. */ public static void createGenerator( FileSystemAbstraction fs, File fileName, long highId, boolean throwIfFileExists ) { IdContainer.createEmptyIdFile( fs, fileName, highId, throwIfFileExists ); }
static void createGenerator( FileSystemAbstraction fs, File fileName, long highId, boolean throwIfFileExists ) { IdContainer.createEmptyIdFile( fs, fileName, highId, throwIfFileExists ); }
/** * 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; }