Skip to content

Commit

Permalink
Renaming and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
klaren authored and MishaDemianenko committed Jul 4, 2017
1 parent 46b4df5 commit 70a2363
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 63 deletions.
Expand Up @@ -31,10 +31,10 @@


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


// sticky(byte), nextFreeId(long) // sticky(byte), nextFreeId(long)
public static final int HEADER_SIZE = Byte.BYTES + Long.BYTES; private static final int HEADER_SIZE = Byte.BYTES + Long.BYTES;


// if sticky the id generator wasn't closed properly so it has to be // if sticky the id generator wasn't closed properly so it has to be
// rebuilt (go through the node, relationship, property, rel type etc files) // rebuilt (go through the node, relationship, property, rel type etc files)
Expand All @@ -44,17 +44,15 @@ public class IdFile
private final File file; private final File file;
private final FileSystemAbstraction fs; private final FileSystemAbstraction fs;
private StoreChannel fileChannel; private StoreChannel fileChannel;

private boolean closed = true;
private FreeIdKeeper freeIdKeeper;


private final int grabSize; private final int grabSize;
private final boolean aggressiveReuse; private final boolean aggressiveReuse;
private FreeIdKeeper freeIdKeeper;


private long initialHighId; private long initialHighId;


private boolean closed = true; IdFile( FileSystemAbstraction fs, File file, int grabSize, boolean aggressiveReuse )

public IdFile( FileSystemAbstraction fs, File file, int grabSize, boolean aggressiveReuse )
{ {
if ( grabSize < 1 ) if ( grabSize < 1 )
{ {
Expand All @@ -81,8 +79,7 @@ void init()
} }
catch ( IOException e ) catch ( IOException e )
{ {
throw new UnderlyingStorageException( throw new UnderlyingStorageException( "Unable to init id file " + file, e );
"Unable to init id generator " + file, e );
} }
} }


Expand All @@ -91,7 +88,7 @@ public boolean isClosed()
return closed; return closed;
} }


public long getInitialHighId() long getInitialHighId()
{ {
return initialHighId; return initialHighId;
} }
Expand All @@ -100,7 +97,7 @@ void assertStillOpen()
{ {
if ( closed ) if ( closed )
{ {
throw new IllegalStateException( "Closed id generator " + file ); throw new IllegalStateException( "Closed id file " + file );
} }
} }


Expand Down Expand Up @@ -130,13 +127,13 @@ private static long readAndValidate( StoreChannel channel, File fileName ) throw
byte storageStatus = buffer.get(); byte storageStatus = buffer.get();
if ( storageStatus != CLEAN_GENERATOR ) if ( storageStatus != CLEAN_GENERATOR )
{ {
throw new InvalidIdGeneratorException( "Sticky generator[ " + throw new InvalidIdGeneratorException( "Id file not properly shutdown [ " +
fileName + "] delete this id file and build a new one" ); fileName + " ], delete this id file and build a new one" );
} }
return buffer.getLong(); return buffer.getLong();
} }


public static long readHighId( FileSystemAbstraction fileSystem, File file ) throws IOException static long readHighId( FileSystemAbstraction fileSystem, File file ) throws IOException
{ {
try ( StoreChannel channel = fileSystem.open( file, "r" ) ) try ( StoreChannel channel = fileSystem.open( file, "r" ) )
{ {
Expand Down Expand Up @@ -164,31 +161,24 @@ private void markAsCleanlyClosed( ) throws IOException


public void close( long highId ) public void close( long highId )
{ {
if ( closed ) if ( !closed )
{
return;
}

try
{
freeIdKeeper.close(); // first write out free ids, then mark as clean
writeHeader( highId );
fileChannel.force( false );

markAsCleanlyClosed( );

closeChannel();
}
catch ( IOException e )
{ {
throw new UnderlyingStorageException( try
"Unable to close id generator " + file, e ); {
freeIdKeeper.close();
writeHeader( highId );
markAsCleanlyClosed();
closeChannel();
}
catch ( IOException e )
{
throw new UnderlyingStorageException( "Unable to close id file " + file, e );
}
} }
} }


private void closeChannel() throws IOException private void closeChannel() throws IOException
{ {
// flush and close
fileChannel.force( false ); fileChannel.force( false );
fileChannel.close(); fileChannel.close();
fileChannel = null; fileChannel = null;
Expand All @@ -213,21 +203,20 @@ public void delete()
} }
catch ( IOException e ) catch ( IOException e )
{ {
throw new UnderlyingStorageException( "Unable to safe close id generator " + file, e ); throw new UnderlyingStorageException( "Unable to close id file " + file, e );
} }
} }


if ( !fs.deleteFile( file ) ) if ( !fs.deleteFile( file ) )
{ {
throw new UnderlyingStorageException( "Unable to delete id generator " + file ); throw new UnderlyingStorageException( "Unable to delete id file " + file );
} }
} }


/** /**
* * @return next free id or {@link IdFile#NO_RESULT} if not available
* @return -1 if no availabele
*/ */
public long getReuseableId() long getReusableId()
{ {
return freeIdKeeper.getId(); return freeIdKeeper.getId();
} }
Expand All @@ -237,36 +226,34 @@ public void freeId( long id )
freeIdKeeper.freeId( id ); freeIdKeeper.freeId( id );
} }


public long getFreeIdCount() long getFreeIdCount()
{ {
return freeIdKeeper.getCount(); return freeIdKeeper.getCount();
} }


/** /**
* Creates a new id generator. * Creates a new id file.
* *
* @param fileName The name of the id generator * @param file The name of the id generator
* @param throwIfFileExists if {@code true} will cause an {@link UnderlyingStorageException} to be thrown if * @param throwIfFileExists if {@code true} will cause an {@link IllegalStateException} to be thrown if
* the file already exists. if {@code false} will truncate the file writing the header in it. * the file already exists. if {@code false} will truncate the file writing the header in it.
*/ */
public static void createEmptyIdFile( FileSystemAbstraction fs, File fileName, long highId, static void createEmptyIdFile( FileSystemAbstraction fs, File file, long highId, boolean throwIfFileExists )
boolean throwIfFileExists )
{ {
// sanity checks // sanity checks
if ( fs == null ) if ( fs == null )
{ {
throw new IllegalArgumentException( "Null filesystem" ); throw new IllegalArgumentException( "Null filesystem" );
} }
if ( fileName == null ) if ( file == null )
{ {
throw new IllegalArgumentException( "Null filename" ); throw new IllegalArgumentException( "Null filename" );
} }
if ( throwIfFileExists && fs.fileExists( fileName ) ) if ( throwIfFileExists && fs.fileExists( file ) )
{ {
throw new IllegalStateException( "Can't create IdGeneratorFile[" throw new IllegalStateException( "Can't create id file [" + file + "], file already exists" );
+ fileName + "], file already exists" );
} }
try ( StoreChannel channel = fs.create( fileName ) ) try ( StoreChannel channel = fs.create( file ) )
{ {
// write the header // write the header
channel.truncate( 0 ); channel.truncate( 0 );
Expand All @@ -277,8 +264,7 @@ public static void createEmptyIdFile( FileSystemAbstraction fs, File fileName, l
} }
catch ( IOException e ) catch ( IOException e )
{ {
throw new UnderlyingStorageException( throw new UnderlyingStorageException( "Unable to create id file " + file, e );
"Unable to create id generator" + fileName, e );
} }
} }


Expand Down
Expand Up @@ -123,7 +123,7 @@ public IdGeneratorImpl( FileSystemAbstraction fs, File file, int grabSize, long
public synchronized long nextId() public synchronized long nextId()
{ {
assertStillOpen(); assertStillOpen();
long nextDefragId = idFile.getReuseableId(); long nextDefragId = idFile.getReusableId();
if ( nextDefragId != IdFile.NO_RESULT ) if ( nextDefragId != IdFile.NO_RESULT )
{ {
return nextDefragId; return nextDefragId;
Expand All @@ -137,11 +137,6 @@ public synchronized long nextId()
return highId++; return highId++;
} }


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

@Override @Override
public synchronized IdRange nextIdBatch( int size ) public synchronized IdRange nextIdBatch( int size )
{ {
Expand All @@ -152,7 +147,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.getReuseableId(); long id = idFile.getReusableId();
if ( id == -1 ) if ( id == -1 )
{ {
break; break;
Expand All @@ -178,7 +173,7 @@ public synchronized IdRange nextIdBatch( int size )
* @param id The next free id returned from {@link #nextId()} if there are no existing free ids. * @param id The next free id returned from {@link #nextId()} if there are no existing free ids.
*/ */
@Override @Override
public void setHighId( long id ) public synchronized void setHighId( long id )
{ {
IdValidator.assertIdWithinCapacity( id, max ); IdValidator.assertIdWithinCapacity( id, max );
highId = id; highId = id;
Expand All @@ -191,15 +186,15 @@ public void setHighId( long id )
* @return The next free "high" id * @return The next free "high" id
*/ */
@Override @Override
public long getHighId() public synchronized long getHighId()
{ {
return highId; return highId;
} }


@Override @Override
public long getHighestPossibleIdInUse() public synchronized long getHighestPossibleIdInUse()
{ {
return getHighId() - 1; return highId - 1;
} }


/** /**
Expand Down Expand Up @@ -272,7 +267,7 @@ public synchronized long getNumberOfIdsInUse()
} }


@Override @Override
public long getDefragCount() public synchronized long getDefragCount()
{ {
return idFile.getFreeIdCount(); return idFile.getFreeIdCount();
} }
Expand All @@ -283,6 +278,11 @@ public synchronized void delete()
idFile.delete(); idFile.delete();
} }


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

@Override @Override
public String toString() public String toString()
{ {
Expand Down

0 comments on commit 70a2363

Please sign in to comment.