private Record GetRecord(Sha256Hash hash)
{
var startPos = _channel.Position;
// Use our own file pointer within the tight loop as updating channel positions is really expensive.
var pos = startPos;
var record = new Record();
do
{
if (!record.Read(_channel, pos, _buf))
throw new IOException("Failed to read buffer");
if (record.GetHeader(_params).Hash.Equals(hash))
{
// Found it. Update file position for next time.
_channel.Position = pos;
return record;
}
// Did not find it.
if (pos == 1 + 32)
{
// At the start so wrap around to the end.
pos = _channel.Length - Record.Size;
}
else
{
// Move backwards.
pos = pos - Record.Size;
Debug.Assert(pos >= 1 + 32, pos.ToString());
}
} while (pos != startPos);
// Was never stored.
_channel.Position = pos;
return null;
}