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.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
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.neo4j.kernel.impl.store.UnderlyingStorageException;


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

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

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 )
{
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()
{
Expand Down Expand Up @@ -276,7 +276,7 @@ static void createEmptyIdFile( FileSystemAbstraction fs, File file, long highId,
@Override
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=" +
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;

private final long max;

private IdFile idFile;

private final IdContainer idContainer;
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 )
{
this.max = max;

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

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

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 );
}
idFile.freeId( id );
idContainer.freeId( id );
}

/**
Expand All @@ -239,7 +236,7 @@ public synchronized void freeId( long id )
@Override
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,
boolean throwIfFileExists )
{
IdFile.createEmptyIdFile( fs, fileName, highId, throwIfFileExists );
IdContainer.createEmptyIdFile( fs, fileName, highId, throwIfFileExists );
}

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

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

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

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

@Override
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 second = createDynamicRecord( 2, store, 0 );
DynamicRecord third = createDynamicRecord( 3, store, 10 );
store.setHighId( 3 );

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

first.setNextBlock( second.getId() );
store.updateRecord( first );
Expand Down
Expand Up @@ -40,7 +40,7 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
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
{
Expand All @@ -53,7 +53,7 @@ public void newlyConstructedInstanceShouldReportProperDefaultValues() throws Exc
// Given
StoreChannel channel = mock( StoreChannel.class );
int threshold = 10;
FreeIdKeeper keeper = getFreeIdKeeper( channel, threshold );
FreeIdKeeper keeper = getFreeIdKeeperAggressive( channel, threshold );

// when
// 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" ) );

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

// THEN
assertEquals( IdFile.HEADER_SIZE, (int) fs.getFileSize( file ) );
assertEquals( 30, IdFile.readHighId( fs, file ) );
idFile = new IdFile( fs, file, 5, false );
idFile.init();
assertEquals( 30, idFile.getInitialHighId() );
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() );

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

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.