Skip to content

Commit

Permalink
Collapses ComposableRecordStore into CommonAbstractStore
Browse files Browse the repository at this point in the history
because CommonAbstractStore already had knowledge about RecordFormat
at this point and so could implement the (previously) abstract methods
on its own. This results in shallower inheritance in store classes
and may make JITs job easier.

As a side effect MetaDataStore had to be changed to act as a real
record store in that it now has a proper MetaDataRecordFormat.
It still have the static record accessors, but that can be improved later.
  • Loading branch information
tinwelint committed Mar 9, 2016
1 parent 975133d commit 9be6016
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 276 deletions.
Expand Up @@ -62,7 +62,7 @@
* The record size is what's stored in the header (first record). {@link #getRecordDataSize()} returns * The record size is what's stored in the header (first record). {@link #getRecordDataSize()} returns
* the size which was configured at the store creation, {@link #getRecordSize()} returns what the store header says. * the size which was configured at the store creation, {@link #getRecordSize()} returns what the store header says.
*/ */
public abstract class AbstractDynamicStore extends ComposableRecordStore<DynamicRecord,IntStoreHeader> public abstract class AbstractDynamicStore extends CommonAbstractStore<DynamicRecord,IntStoreHeader>
implements DynamicRecordAllocator implements DynamicRecordAllocator
{ {
public static final byte[] NO_DATA = new byte[0]; public static final byte[] NO_DATA = new byte[0];
Expand Down
Expand Up @@ -58,7 +58,7 @@
/** /**
* Contains common implementation of {@link RecordStore}. * Contains common implementation of {@link RecordStore}.
*/ */
public abstract class CommonAbstractStore<RECORD extends AbstractBaseRecord> public abstract class CommonAbstractStore<RECORD extends AbstractBaseRecord,HEADER extends StoreHeader>
implements RecordStore<RECORD>, AutoCloseable implements RecordStore<RECORD>, AutoCloseable
{ {
public static final String UNKNOWN_VERSION = "Unknown"; public static final String UNKNOWN_VERSION = "Unknown";
Expand All @@ -78,6 +78,9 @@ public abstract class CommonAbstractStore<RECORD extends AbstractBaseRecord>
private final String typeDescriptor; private final String typeDescriptor;
protected int recordSize; protected int recordSize;


private final StoreHeaderFormat<HEADER> storeHeaderFormat;
private HEADER storeHeader;

/** /**
* Opens and validates the store contained in <CODE>fileName</CODE> * Opens and validates the store contained in <CODE>fileName</CODE>
* loading any configuration defined in <CODE>config</CODE>. After * loading any configuration defined in <CODE>config</CODE>. After
Expand All @@ -102,6 +105,7 @@ public CommonAbstractStore(
LogProvider logProvider, LogProvider logProvider,
String typeDescriptor, String typeDescriptor,
RecordFormat<RECORD> recordFormat, RecordFormat<RECORD> recordFormat,
StoreHeaderFormat<HEADER> storeHeaderFormat,
String storeVersion ) String storeVersion )
{ {
this.storageFileName = fileName; this.storageFileName = fileName;
Expand All @@ -111,6 +115,7 @@ public CommonAbstractStore(
this.idType = idType; this.idType = idType;
this.typeDescriptor = typeDescriptor; this.typeDescriptor = typeDescriptor;
this.recordFormat = recordFormat; this.recordFormat = recordFormat;
this.storeHeaderFormat = storeHeaderFormat;
this.storeVersion = storeVersion; this.storeVersion = storeVersion;
this.log = logProvider.getLog( getClass() ); this.log = logProvider.getLog( getClass() );
} }
Expand Down Expand Up @@ -218,7 +223,10 @@ protected void initialiseNewStoreFile( PagedFile file ) throws IOException


protected void createHeaderRecord( PageCursor cursor ) throws IOException protected void createHeaderRecord( PageCursor cursor ) throws IOException
{ {
assert getNumberOfReservedLowIds() == 0; int offset = cursor.getOffset();
storeHeaderFormat.writeHeader( cursor );
cursor.setOffset( offset );
readHeaderAndInitializeRecordFormat( cursor );
} }


/** /**
Expand Down Expand Up @@ -317,8 +325,9 @@ public byte[] getRawRecordData( long id ) throws IOException
* of the record format still happens in here. * of the record format still happens in here.
* @throws IOException if there were problems reading header information. * @throws IOException if there were problems reading header information.
*/ */
protected void readHeaderAndInitializeRecordFormat( PageCursor cursor ) throws IOException private void readHeaderAndInitializeRecordFormat( PageCursor cursor ) throws IOException
{ {
storeHeader = storeHeaderFormat.readHeader( cursor );
} }


private void loadIdGenerator() private void loadIdGenerator()
Expand Down Expand Up @@ -735,7 +744,10 @@ private boolean isJustLegacyStoreTrailer( PageCursor cursor, int offset, byte[]
} }
} }


protected abstract int determineRecordSize(); protected int determineRecordSize()
{
return recordFormat.getRecordSize( storeHeader );
}


@Override @Override
public final int getRecordSize() public final int getRecordSize()
Expand All @@ -746,10 +758,13 @@ public final int getRecordSize()
@Override @Override
public int getRecordDataSize() public int getRecordDataSize()
{ {
return getRecordSize(); return recordSize - recordFormat.getRecordHeaderSize();
} }


protected abstract boolean isInUse( PageCursor cursor ); private boolean isInUse( PageCursor cursor )
{
return recordFormat.isInUse( cursor );
}


protected boolean isRecordReserved( PageCursor cursor ) protected boolean isRecordReserved( PageCursor cursor )
{ {
Expand Down Expand Up @@ -882,7 +897,7 @@ public long getNumberOfIdsInUse()
@Override @Override
public int getNumberOfReservedLowIds() public int getNumberOfReservedLowIds()
{ {
return 0; return storeHeaderFormat.numberOfReservedRecords();
} }


public IdType getIdType() public IdType getIdType()
Expand Down Expand Up @@ -911,7 +926,7 @@ public void logIdUsage( Logger logger )
* {@link #logVersions(Logger)} * {@link #logVersions(Logger)}
* For a good samaritan to pick up later. * For a good samaritan to pick up later.
*/ */
public void visitStore( Visitor<CommonAbstractStore<RECORD>,RuntimeException> visitor ) public void visitStore( Visitor<CommonAbstractStore<RECORD,HEADER>,RuntimeException> visitor )
{ {
visitor.visit( this ); visitor.visit( this );
} }
Expand All @@ -937,7 +952,13 @@ final void deleteIdGenerator()
@Override @Override
public long getNextRecordReference( RECORD record ) public long getNextRecordReference( RECORD record )
{ {
return Record.NULL_REFERENCE.intValue(); return recordFormat.getNextRecordReference( record );
}

@Override
public RECORD newRecord()
{
return recordFormat.newRecord();
} }


/** /**
Expand Down Expand Up @@ -967,7 +988,7 @@ public RECORD getRecord( long id, RECORD record, RecordLoad mode )
do do
{ {
prepareForReading( cursor, offset, record ); prepareForReading( cursor, offset, record );
readRecord( cursor, record, mode ); recordFormat.read( record, cursor, mode, recordSize, storeFile );
} }
while ( cursor.shouldRetry() ); while ( cursor.shouldRetry() );
verifyAfterReading( record, mode ); verifyAfterReading( record, mode );
Expand All @@ -984,12 +1005,6 @@ public RECORD getRecord( long id, RECORD record, RecordLoad mode )
} }
} }


/**
* Reads data from {@link PageCursor} into the record.
* @throws IOException on error reading.
*/
protected abstract void readRecord( PageCursor cursor, RECORD record, RecordLoad mode ) throws IOException;

@Override @Override
public void updateRecord( RECORD record ) public void updateRecord( RECORD record )
{ {
Expand All @@ -1003,7 +1018,7 @@ public void updateRecord( RECORD record )
do do
{ {
cursor.setOffset( offset ); cursor.setOffset( offset );
writeRecord( cursor, record ); recordFormat.write( record, cursor, recordSize, storeFile );
} }
while ( cursor.shouldRetry() ); while ( cursor.shouldRetry() );
if ( !record.inUse() ) if ( !record.inUse() )
Expand All @@ -1024,7 +1039,14 @@ public void updateRecord( RECORD record )
} }
} }


protected abstract void writeRecord( PageCursor cursor, RECORD record ) throws IOException; @Override
public void prepareForCommit( RECORD record )
{
if ( record.inUse() )
{
recordFormat.prepare( record, recordSize, this );
}
}


/** /**
* Scan the given range of records both inclusive, and pass all the in-use ones to the given processor, one by one. * Scan the given range of records both inclusive, and pass all the in-use ones to the given processor, one by one.
Expand Down Expand Up @@ -1098,7 +1120,7 @@ public boolean next()
do do
{ {
prepareForReading( pageCursor, offset, record ); prepareForReading( pageCursor, offset, record );
readRecord( pageCursor, record, mode ); recordFormat.read( record, pageCursor, mode, recordSize, storeFile );
} }
while ( pageCursor.shouldRetry() ); while ( pageCursor.shouldRetry() );
verifyAfterReading( record, mode ); verifyAfterReading( record, mode );
Expand Down Expand Up @@ -1210,7 +1232,7 @@ public String toString()
@Override @Override
public int getStoreHeaderInt() public int getStoreHeaderInt()
{ {
throw new UnsupportedOperationException( "No header" ); return ((IntStoreHeader) storeHeader).value();
} }


public static abstract class Configuration public static abstract class Configuration
Expand Down

This file was deleted.

0 comments on commit 9be6016

Please sign in to comment.