Skip to content

Commit

Permalink
Resolve TODOs where we don't cache records during StepFirst/Next/Prev…
Browse files Browse the repository at this point in the history
…/Last (#408)

* Cache btrieve records during StepFirst/Next/Previous/Last

* s/GetBtrieveRecord/GetAndCacheBtrieveRecord/g
  • Loading branch information
paladine authored Feb 21, 2021
1 parent 1de6866 commit 35070e9
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions MBBSEmu/Btrieve/BtrieveFileProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,22 +350,24 @@ public int GetRecordCount()
/// </summary>
private bool StepFirst()
{
// TODO consider grabbing data at the same time and prepopulating the cache
var cmd = GetSqliteCommand("SELECT id FROM data_t ORDER BY id LIMIT 1");
var cmd = GetSqliteCommand("SELECT id, data FROM data_t ORDER BY id LIMIT 1");
using var reader = cmd.ExecuteReader();

Position = reader.Read() ? (uint)reader.GetInt32(0) : 0;
if (!reader.Read())
return false;

Position = (uint)reader.GetInt32(0);
GetAndCacheBtrieveRecord(Position, reader, ordinal: 1);
reader.Close();
return Position > 0;
return true;
}

/// <summary>
/// Sets Position to the offset of the next logical Record in the loaded Btrieve File.
/// </summary>
private bool StepNext()
{
// TODO consider grabbing data at the same time and prepopulating the cache
var cmd = GetSqliteCommand("SELECT id FROM data_t WHERE id > @position ORDER BY id LIMIT 1");
var cmd = GetSqliteCommand("SELECT id, data FROM data_t WHERE id > @position ORDER BY id LIMIT 1");
cmd.Parameters.AddWithValue("@position", Position);
using var reader = cmd.ExecuteReader();
try
Expand All @@ -374,6 +376,7 @@ private bool StepNext()
return false;

Position = (uint)reader.GetInt32(0);
GetAndCacheBtrieveRecord(Position, reader, ordinal: 1);
return true;
}
finally
Expand All @@ -387,7 +390,7 @@ private bool StepNext()
/// </summary>
private bool StepPrevious()
{
var cmd = GetSqliteCommand("SELECT id FROM data_t WHERE id < @position ORDER BY id DESC LIMIT 1");
var cmd = GetSqliteCommand("SELECT id, data FROM data_t WHERE id < @position ORDER BY id DESC LIMIT 1");
cmd.Parameters.AddWithValue("@position", Position);
using var reader = cmd.ExecuteReader();
try
Expand All @@ -396,6 +399,7 @@ private bool StepPrevious()
return false;

Position = (uint)reader.GetInt32(0);
GetAndCacheBtrieveRecord(Position, reader, 1);
return true;
}
finally
Expand All @@ -409,11 +413,14 @@ private bool StepPrevious()
/// </summary>
private bool StepLast()
{
// TODO consider grabbing data at the same time and prepopulating the cache
var cmd = GetSqliteCommand("SELECT id FROM data_t ORDER BY id DESC LIMIT 1");
var cmd = GetSqliteCommand("SELECT id, data FROM data_t ORDER BY id DESC LIMIT 1");
using var reader = cmd.ExecuteReader();

Position = reader.Read() ? (uint)reader.GetInt32(0) : 0;
if (!reader.Read())
return false;

Position = (uint)reader.GetInt32(0);
GetAndCacheBtrieveRecord(Position, reader, 1);
reader.Close();
return Position > 0;
}
Expand All @@ -424,6 +431,16 @@ private bool StepLast()
/// <returns></returns>
public byte[] GetRecord() => GetRecord(Position)?.Data;

private BtrieveRecord GetAndCacheBtrieveRecord(uint id, SqliteDataReader reader, int ordinal)
{
using var stream = reader.GetStream(ordinal);
var data = BtrieveUtil.ReadEntireStream(stream);

var record = new BtrieveRecord(id, data);
_cache[id] = record;
return record;
}

/// <summary>
/// Returns the Record at the specified Offset, while also updating Position to match.
/// </summary>
Expand All @@ -442,12 +459,7 @@ public BtrieveRecord GetRecord(uint offset)
if (!reader.Read())
return null;

using var stream = reader.GetStream(0);
var data = BtrieveUtil.ReadEntireStream(stream);

record = new BtrieveRecord(offset, data);
_cache[offset] = record;
return record;
return GetAndCacheBtrieveRecord(offset, reader, 0);
}
finally
{
Expand Down

0 comments on commit 35070e9

Please sign in to comment.