Skip to content

Commit

Permalink
Renamed IdFile to IdContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
klaren authored and MishaDemianenko committed Jul 4, 2017
1 parent 6fb7ed1 commit 813360a
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 73 deletions.
Expand Up @@ -27,7 +27,7 @@
import org.neo4j.io.fs.StoreChannel; import org.neo4j.io.fs.StoreChannel;
import org.neo4j.kernel.impl.store.UnderlyingStorageException; import org.neo4j.kernel.impl.store.UnderlyingStorageException;


import static org.neo4j.kernel.impl.store.id.IdFile.NO_RESULT; import static org.neo4j.kernel.impl.store.id.IdContainer.NO_RESULT;


/** /**
* Instances of this class maintain a list of free ids with the potential to overflow to disk if the number * Instances of this class maintain a list of free ids with the potential to overflow to disk if the number
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.neo4j.kernel.impl.store.UnderlyingStorageException; import org.neo4j.kernel.impl.store.UnderlyingStorageException;




public class IdFile public class IdContainer
{ {
public static final long NO_RESULT = -1; public static final long NO_RESULT = -1;


Expand All @@ -52,7 +52,7 @@ public class IdFile


private long initialHighId; private long initialHighId;


public IdFile( FileSystemAbstraction fs, File file, int grabSize, boolean aggressiveReuse ) public IdContainer( FileSystemAbstraction fs, File file, int grabSize, boolean aggressiveReuse )
{ {
if ( grabSize < 1 ) if ( grabSize < 1 )
{ {
Expand Down Expand Up @@ -219,7 +219,7 @@ public void delete()
} }


/** /**
* @return next free id or {@link IdFile#NO_RESULT} if not available * @return next free id or {@link IdContainer#NO_RESULT} if not available
*/ */
public long getReusableId() public long getReusableId()
{ {
Expand Down Expand Up @@ -276,7 +276,7 @@ static void createEmptyIdFile( FileSystemAbstraction fs, File file, long highId,
@Override @Override
public String toString() public String toString()
{ {
return "IdFile{" + "file=" + file + ", fs=" + fs + ", fileChannel=" + fileChannel + ", defragCount=" + return "IdContainer{" + "file=" + file + ", fs=" + fs + ", fileChannel=" + fileChannel + ", defragCount=" +
freeIdKeeper.getCount() + ", grabSize=" + grabSize + ", aggressiveReuse=" + freeIdKeeper.getCount() + ", grabSize=" + grabSize + ", aggressiveReuse=" +
aggressiveReuse + ", closed=" + closed + '}'; aggressiveReuse + ", closed=" + closed + '}';
} }
Expand Down
Expand Up @@ -67,9 +67,7 @@ public class IdGeneratorImpl implements IdGenerator
public static final long INTEGER_MINUS_ONE = 0xFFFFFFFFL; // 4294967295L; public static final long INTEGER_MINUS_ONE = 0xFFFFFFFFL; // 4294967295L;


private final long max; private final long max;

private final IdContainer idContainer;
private IdFile idFile;

private long highId = INTEGER_MINUS_ONE; private long highId = INTEGER_MINUS_ONE;


/** /**
Expand Down Expand Up @@ -102,10 +100,9 @@ public IdGeneratorImpl( FileSystemAbstraction fs, File file, int grabSize, long
long highId ) long highId )
{ {
this.max = max; this.max = max;

this.idContainer = new IdContainer( fs, file, grabSize, aggressiveReuse );
this.idFile = new IdFile( fs, file, grabSize, aggressiveReuse ); this.idContainer.init();
this.idFile.init(); this.highId = max( idContainer.getInitialHighId(), highId );
this.highId = max( idFile.getInitialHighId(), highId );
} }


/** /**
Expand All @@ -123,8 +120,8 @@ public IdGeneratorImpl( FileSystemAbstraction fs, File file, int grabSize, long
public synchronized long nextId() public synchronized long nextId()
{ {
assertStillOpen(); assertStillOpen();
long nextDefragId = idFile.getReusableId(); long nextDefragId = idContainer.getReusableId();
if ( nextDefragId != IdFile.NO_RESULT ) if ( nextDefragId != IdContainer.NO_RESULT )
{ {
return nextDefragId; return nextDefragId;
} }
Expand All @@ -147,7 +144,7 @@ public synchronized IdRange nextIdBatch( int size )
long[] defragIds = new long[size]; long[] defragIds = new long[size];
while ( count < size ) while ( count < size )
{ {
long id = idFile.getReusableId(); long id = idContainer.getReusableId();
if ( id == -1 ) if ( id == -1 )
{ {
break; break;
Expand Down Expand Up @@ -213,7 +210,7 @@ public synchronized long getHighestPossibleIdInUse()
@Override @Override
public synchronized void freeId( long id ) public synchronized void freeId( long id )
{ {
idFile.assertStillOpen(); idContainer.assertStillOpen();


if ( IdValidator.isReservedId( id ) ) if ( IdValidator.isReservedId( id ) )
{ {
Expand All @@ -224,7 +221,7 @@ public synchronized void freeId( long id )
{ {
throw new IllegalArgumentException( "Illegal id[" + id + "], highId is " + highId ); throw new IllegalArgumentException( "Illegal id[" + id + "], highId is " + highId );
} }
idFile.freeId( id ); idContainer.freeId( id );
} }


/** /**
Expand All @@ -239,7 +236,7 @@ public synchronized void freeId( long id )
@Override @Override
public synchronized void close() public synchronized void close()
{ {
idFile.close( highId ); idContainer.close( highId );
} }


/** /**
Expand All @@ -252,12 +249,12 @@ public synchronized void close()
public static void createGenerator( FileSystemAbstraction fs, File fileName, long highId, public static void createGenerator( FileSystemAbstraction fs, File fileName, long highId,
boolean throwIfFileExists ) boolean throwIfFileExists )
{ {
IdFile.createEmptyIdFile( fs, fileName, highId, throwIfFileExists ); IdContainer.createEmptyIdFile( fs, fileName, highId, throwIfFileExists );
} }


public static long readHighId( FileSystemAbstraction fileSystem, File file ) throws IOException public static long readHighId( FileSystemAbstraction fileSystem, File file ) throws IOException
{ {
return IdFile.readHighId( fileSystem, file ); return IdContainer.readHighId( fileSystem, file );
} }


@Override @Override
Expand All @@ -269,23 +266,23 @@ public synchronized long getNumberOfIdsInUse()
@Override @Override
public synchronized long getDefragCount() public synchronized long getDefragCount()
{ {
return idFile.getFreeIdCount(); return idContainer.getFreeIdCount();
} }


@Override @Override
public synchronized void delete() public synchronized void delete()
{ {
idFile.delete(); idContainer.delete();
} }


private void assertStillOpen() private void assertStillOpen()
{ {
idFile.assertStillOpen(); idContainer.assertStillOpen();
} }


@Override @Override
public String toString() public String toString()
{ {
return "IdGeneratorImpl " + hashCode() + " [max=" + max + ", idFile=" + idFile + "]"; return "IdGeneratorImpl " + hashCode() + " [max=" + max + ", idContainer=" + idContainer + "]";
} }
} }
Expand Up @@ -83,6 +83,7 @@ public void dynamicRecordCursorReadsInUseRecords()
DynamicRecord first = createDynamicRecord( 1, store, 0 ); DynamicRecord first = createDynamicRecord( 1, store, 0 );
DynamicRecord second = createDynamicRecord( 2, store, 0 ); DynamicRecord second = createDynamicRecord( 2, store, 0 );
DynamicRecord third = createDynamicRecord( 3, store, 10 ); DynamicRecord third = createDynamicRecord( 3, store, 10 );
store.setHighId( 3 );


first.setNextBlock( second.getId() ); first.setNextBlock( second.getId() );
store.updateRecord( first ); store.updateRecord( first );
Expand All @@ -108,6 +109,7 @@ public void dynamicRecordCursorReadsNotInUseRecords()
DynamicRecord first = createDynamicRecord( 1, store, 0 ); DynamicRecord first = createDynamicRecord( 1, store, 0 );
DynamicRecord second = createDynamicRecord( 2, store, 0 ); DynamicRecord second = createDynamicRecord( 2, store, 0 );
DynamicRecord third = createDynamicRecord( 3, store, 10 ); DynamicRecord third = createDynamicRecord( 3, store, 10 );
store.setHighId( 3 );


first.setNextBlock( second.getId() ); first.setNextBlock( second.getId() );
store.updateRecord( first ); store.updateRecord( first );
Expand Down
Expand Up @@ -40,7 +40,7 @@
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.verifyZeroInteractions;
import static org.neo4j.kernel.impl.store.id.IdFile.NO_RESULT; import static org.neo4j.kernel.impl.store.id.IdContainer.NO_RESULT;


public class FreeIdKeeperTest public class FreeIdKeeperTest
{ {
Expand All @@ -53,7 +53,7 @@ public void newlyConstructedInstanceShouldReportProperDefaultValues() throws Exc
// Given // Given
StoreChannel channel = mock( StoreChannel.class ); StoreChannel channel = mock( StoreChannel.class );
int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, threshold );


// when // when
// then // then
Expand Down Expand Up @@ -107,7 +107,7 @@ public void shouldOnlyOverflowWhenThresholdIsReached() throws Exception
StoreChannel channel = spy( fs.get().open( new File( "id.file" ), "rw" ) ); StoreChannel channel = spy( fs.get().open( new File( "id.file" ), "rw" ) );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, threshold );
reset( channel ); // because we get the position in the constructor, we need to reset all calls on the spy reset( channel ); // because we get the position in the constructor, we need to reset all calls on the spy


// when // when
Expand Down Expand Up @@ -249,7 +249,7 @@ public void shouldStoreAndRestoreIds() throws Exception
StoreChannel channel = getStoreChannel(); StoreChannel channel = getStoreChannel();


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold ); FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, threshold );
Set<Long> freeIds = new HashSet<>(); // stack guarantees are not maintained between restarts Set<Long> freeIds = new HashSet<>(); // stack guarantees are not maintained between restarts


// when // when
Expand All @@ -271,7 +271,7 @@ public void shouldStoreAndRestoreIds() throws Exception
channel.close(); channel.close();
// and then we open a new one over the same file // and then we open a new one over the same file
channel = fs.get().open( new File( "id.file" ), "rw" ); channel = fs.get().open( new File( "id.file" ), "rw" );
keeper = getFreeIdKeeper( channel, threshold ); keeper = getFreeIdKeeperAggressive( channel, threshold );


// then // then
// the count should be returned correctly // the count should be returned correctly
Expand All @@ -292,7 +292,7 @@ public void shouldNotReturnNewlyReleasedIdsIfAggressiveIsFalse() throws Exceptio
StoreChannel channel = getStoreChannel(); StoreChannel channel = getStoreChannel();


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold );


// when // when
keeper.freeId( 1 ); keeper.freeId( 1 );
Expand All @@ -309,7 +309,7 @@ public void shouldNotReturnIdsPersistedDuringThisRunIfAggressiveIsFalse() throws
StoreChannel channel = spy( fs.get().open( new File( "id.file" ), "rw" ) ); StoreChannel channel = spy( fs.get().open( new File( "id.file" ), "rw" ) );


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold );


// when // when
// enough ids are persisted to overflow // enough ids are persisted to overflow
Expand All @@ -332,7 +332,7 @@ public void shouldReturnIdsRestoredAndIgnoreNewlyReleasedIfAggressiveReuseIsFals
StoreChannel channel = getStoreChannel(); StoreChannel channel = getStoreChannel();


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold );
Set<Long> freeIds = new HashSet<>(); Set<Long> freeIds = new HashSet<>();
for ( long i = 0; i < threshold; i++ ) for ( long i = 0; i < threshold; i++ )
{ {
Expand All @@ -343,7 +343,7 @@ public void shouldReturnIdsRestoredAndIgnoreNewlyReleasedIfAggressiveReuseIsFals
channel.close(); channel.close();
// and then we open a new one over the same file // and then we open a new one over the same file
channel = fs.get().open( new File( "id.file" ), "rw" ); channel = fs.get().open( new File( "id.file" ), "rw" );
keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); keeper = getFreeIdKeeper( channel, threshold );


// when // when
// we release some ids that spill to disk // we release some ids that spill to disk
Expand Down Expand Up @@ -372,7 +372,7 @@ public void shouldReturnNoResultIfIdsAreRestoredAndExhaustedAndThereAreFreeIdsFr
StoreChannel channel = getStoreChannel(); StoreChannel channel = getStoreChannel();


int threshold = 10; int threshold = 10;
FreeIdKeeper keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold );
Set<Long> freeIds = new HashSet<>(); Set<Long> freeIds = new HashSet<>();
for ( long i = 0; i < threshold; i++ ) for ( long i = 0; i < threshold; i++ )
{ {
Expand All @@ -383,7 +383,7 @@ public void shouldReturnNoResultIfIdsAreRestoredAndExhaustedAndThereAreFreeIdsFr
channel.close(); channel.close();
// and then we open a new one over the same file // and then we open a new one over the same file
channel = fs.get().open( new File( "id.file" ), "rw" ); channel = fs.get().open( new File( "id.file" ), "rw" );
keeper = getFreeIdKeeper( (StoreChannel) channel, (int) threshold ); keeper = getFreeIdKeeper( channel, threshold );


// when - then // when - then
// we exhaust all ids restored // we exhaust all ids restored
Expand Down
Expand Up @@ -38,7 +38,7 @@
import static org.junit.Assert.fail; import static org.junit.Assert.fail;




public class IdFileTest public class IdContainerTest
{ {
@Rule @Rule
public final TestDirectory testDirectory = TestDirectory.testDirectory(); public final TestDirectory testDirectory = TestDirectory.testDirectory();
Expand All @@ -59,29 +59,29 @@ public void shouldDeleteIfOpen() throws Exception
{ {
// GIVEN // GIVEN
createEmptyFile(); createEmptyFile();
IdFile idFile = new IdFile( fs, file, 100, false ); IdContainer idContainer = new IdContainer( fs, file, 100, false );
idFile.init(); idContainer.init();


// WHEN // WHEN
idFile.delete(); idContainer.delete();


// THEN // THEN
assertFalse( fs.fileExists( file ) ); assertFalse( fs.fileExists( file ) );


idFile.close( 0 ); idContainer.close( 0 );
} }


@Test @Test
public void shouldDeleteIfClosed() throws Exception public void shouldDeleteIfClosed() throws Exception
{ {
// GIVEN // GIVEN
createEmptyFile(); createEmptyFile();
IdFile idFile = new IdFile( fs, file, 100, false ); IdContainer idContainer = new IdContainer( fs, file, 100, false );
idFile.init(); idContainer.init();
idFile.close( 0 ); idContainer.close( 0 );


// WHEN // WHEN
idFile.delete(); idContainer.delete();


// THEN // THEN
assertFalse( fs.fileExists( file ) ); assertFalse( fs.fileExists( file ) );
Expand All @@ -94,13 +94,13 @@ public void shouldForceStickyMark() throws Exception
createEmptyFile(); createEmptyFile();


// WHEN opening the id generator, where the jvm crashes right after // WHEN opening the id generator, where the jvm crashes right after
IdFile idFile = new IdFile( fs, file, 100, false ); IdContainer idContainer = new IdContainer( fs, file, 100, false );
idFile.init(); idContainer.init();


// THEN // THEN
try try
{ {
IdFile.readHighId( fs, file ); IdContainer.readHighId( fs, file );
fail( "Should have thrown, saying something with sticky generator" ); fail( "Should have thrown, saying something with sticky generator" );
} }
catch ( InvalidIdGeneratorException e ) catch ( InvalidIdGeneratorException e )
Expand All @@ -109,39 +109,39 @@ public void shouldForceStickyMark() throws Exception
} }
finally finally
{ {
idFile.close( 0 ); idContainer.close( 0 );
} }
} }


@Test @Test
public void shouldTruncateTheFileIfOverwriting() throws Exception public void shouldTruncateTheFileIfOverwriting() throws Exception
{ {
// GIVEN // GIVEN
IdFile.createEmptyIdFile( fs, file, 30, false ); IdContainer.createEmptyIdFile( fs, file, 30, false );
IdFile idFile = new IdFile( fs, file, 5, false ); IdContainer idContainer = new IdContainer( fs, file, 5, false );
idFile.init(); idContainer.init();
for ( int i = 0; i < 17; i++ ) for ( int i = 0; i < 17; i++ )
{ {
idFile.freeId( i ); idContainer.freeId( i );
} }
idFile.close( 30 ); idContainer.close( 30 );
assertThat( (int) fs.getFileSize( file ), greaterThan( IdFile.HEADER_SIZE ) ); assertThat( (int) fs.getFileSize( file ), greaterThan( IdContainer.HEADER_SIZE ) );


// WHEN // WHEN
IdFile.createEmptyIdFile( fs, file, 30, false ); IdContainer.createEmptyIdFile( fs, file, 30, false );


// THEN // THEN
assertEquals( IdFile.HEADER_SIZE, (int) fs.getFileSize( file ) ); assertEquals( IdContainer.HEADER_SIZE, (int) fs.getFileSize( file ) );
assertEquals( 30, IdFile.readHighId( fs, file ) ); assertEquals( 30, IdContainer.readHighId( fs, file ) );
idFile = new IdFile( fs, file, 5, false ); idContainer = new IdContainer( fs, file, 5, false );
idFile.init(); idContainer.init();
assertEquals( 30, idFile.getInitialHighId() ); assertEquals( 30, idContainer.getInitialHighId() );


idFile.close( 30 ); idContainer.close( 30 );
} }


private void createEmptyFile() private void createEmptyFile()
{ {
IdFile.createEmptyIdFile( fs, file, 42, false ); IdContainer.createEmptyIdFile( fs, file, 42, false );
} }
} }

0 comments on commit 813360a

Please sign in to comment.