private void Preload()
{
if ( !File.Exists( FileName ) )
return;
using ( FileStream fs = OpenRead() ) {
CacheSize = ( int )( fs.Length / sizeof( BlockDBEntry ) );
EnsureCapacity( CacheSize );
LastFlushedIndex = CacheSize;
// Converting from byte[] to BlockDBEntry[] on the fly
// This is possible because BlockDBEntry is a sequentially packed struct
fixed ( BlockDBEntry* pCacheStart = cacheStore ) {
fixed ( byte* pBuffer = ioBuffer ) {
byte* pCache = ( byte* )pCacheStart;
while ( fs.Position < fs.Length ) {
int bytesToRead = Math.Min( BufferSize, ( int )( fs.Length - fs.Position ) );
int bytesInBuffer = 0;
do {
int bytesRead = fs.Read( ioBuffer, bytesInBuffer, BufferSize - bytesInBuffer );
bytesInBuffer += bytesRead;
} while ( bytesInBuffer < bytesToRead );
BufferUtil.MemCpy( pBuffer, pCache, bytesInBuffer );
pCache += bytesInBuffer;
}
}
}
}
}