/** * Frees the <CODE>id</CODE> making it a defragged id that will be * returned by next id before any new id (that hasn't been used yet) is * returned. * <p> * This method will throw an <CODE>IOException</CODE> if id is negative or * if id is greater than the highest returned id. However as stated in the * class documentation above the id isn't validated to see if it really is * free. * * @param id * The id to be made available again */ @Override public synchronized void freeId( long id ) { idContainer.assertStillOpen(); if ( IdValidator.isReservedId( id ) ) { return; } if ( id < 0 || id >= highId ) { throw new IllegalArgumentException( "Illegal id[" + id + "], highId is " + highId ); } idContainer.freeId( id ); }
@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 ); }
@Override public void freeId( long id ) { idContainerLock.lock(); try { idContainer.freeId( id ); } finally { idContainerLock.unlock(); } }
/** * Frees the <CODE>id</CODE> making it a defragged id that will be * returned by next id before any new id (that hasn't been used yet) is * returned. * <p> * This method will throw an <CODE>IOException</CODE> if id is negative or * if id is greater than the highest returned id. However as stated in the * class documentation above the id isn't validated to see if it really is * free. * * @param id * The id to be made available again */ @Override public synchronized void freeId( long id ) { idContainer.assertStillOpen(); if ( IdValidator.isReservedId( id ) ) { return; } if ( id < 0 || id >= highId ) { throw new IllegalArgumentException( "Illegal id[" + id + "], highId is " + highId ); } idContainer.freeId( id ); }