/** * Read the high-id count from the given id-file. * * Note that this method should only be used when the file is not currently in use by an IdGenerator, since this * method does not take any in-memory state into account. * * @param fileSystem The file system to use for accessing the given file. * @param file The path to the id-file from which to read the high-id. * @return The high-id from the given file. * @throws IOException If anything goes wrong when accessing the file, for instance if the file does not exist. */ public static long readHighId( FileSystemAbstraction fileSystem, File file ) throws IOException { return IdContainer.readHighId( fileSystem, file ); }
@Test public void shouldForceStickyMark() throws Exception { // GIVEN createEmptyFile(); // WHEN opening the id generator, where the jvm crashes right after IdContainer idContainer = new IdContainer( fs, file, 100, false ); idContainer.init(); // THEN try { IdContainer.readHighId( fs, file ); fail( "Should have thrown, saying something with sticky generator" ); } catch ( InvalidIdGeneratorException e ) { // THEN Good } finally { idContainer.close( 0 ); } }
@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 ); }
/** * Read the high-id count from the given id-file. * * Note that this method should only be used when the file is not currently in use by an IdGenerator, since this * method does not take any in-memory state into account. * * @param fileSystem The file system to use for accessing the given file. * @param file The path to the id-file from which to read the high-id. * @return The high-id from the given file. * @throws IOException If anything goes wrong when accessing the file, for instance if the file does not exist. */ public static long readHighId( FileSystemAbstraction fileSystem, File file ) throws IOException { return IdContainer.readHighId( fileSystem, file ); }