Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions src/Build5Nines.SharpVector/VectorStore/BasicDiskVectorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,33 +191,40 @@ private void RecoverFromWalOrIndex()

// Replay WAL to recover any operations after the last checkpoint
if (!File.Exists(_walPath)) return;
using var fs = new FileStream(_walPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var br = new BinaryReader(fs);
while (fs.Position < fs.Length)

// Scope the read handles so the file is closed before we overwrite it
// below; `using var` would otherwise hold the handle until the end of
// the method and File.WriteAllBytes would race against it on Windows.
using (var fs = new FileStream(_walPath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var br = new BinaryReader(fs))
{
bool isDelete = br.ReadBoolean();
var idJson = br.ReadString();
var id = JsonSerializer.Deserialize<TId>(idJson)!;
if (isDelete)
{
_index.TryRemove(id, out _);
_cache.TryRemove(id, out _);
}
else
while (fs.Position < fs.Length)
{
var itemJson = br.ReadString();
var item = JsonSerializer.Deserialize<VectorTextItem<TVocabularyKey, TMetadata>>(itemJson)!;

// Append item to items file to bring storage up-to-date
using var ofs = new FileStream(_itemsPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
ofs.Seek(0, SeekOrigin.End);
var offset = ofs.Position;
WriteItem(ofs, item);
ofs.Flush(true);
_index[id] = offset;
_cache[id] = item;
bool isDelete = br.ReadBoolean();
var idJson = br.ReadString();
var id = JsonSerializer.Deserialize<TId>(idJson)!;
if (isDelete)
{
_index.TryRemove(id, out _);
_cache.TryRemove(id, out _);
}
else
{
var itemJson = br.ReadString();
var item = JsonSerializer.Deserialize<VectorTextItem<TVocabularyKey, TMetadata>>(itemJson)!;

// Append item to items file to bring storage up-to-date
using var ofs = new FileStream(_itemsPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
ofs.Seek(0, SeekOrigin.End);
var offset = ofs.Position;
WriteItem(ofs, item);
ofs.Flush(true);
_index[id] = offset;
_cache[id] = item;
}
}
}

// After successful replay, truncate WAL (commit)
File.WriteAllBytes(_walPath, Array.Empty<byte>());
PersistIndex();
Expand Down
29 changes: 18 additions & 11 deletions src/Build5Nines.SharpVector/Vocabulary/BasicDiskVocabularyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,30 @@ private void RecoverFromWalOrIndex()
{
LoadIfExists();
if (!File.Exists(_walPath)) return;
using var fs = new FileStream(_walPath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var br = new BinaryReader(fs);
while (fs.Position < fs.Length)

// Scope the read handles so the file is closed before we overwrite it
// below; `using var` would otherwise hold the handle until the end of
// the method and File.WriteAllBytes would race against it on Windows.
using (var fs = new FileStream(_walPath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var br = new BinaryReader(fs))
{
int count = br.ReadInt32();
for (int i = 0; i < count; i++)
while (fs.Position < fs.Length)
{
var tokenJson = br.ReadString();
var token = JsonSerializer.Deserialize<TKey>(tokenJson)!;
if (!_vocab.ContainsKey(token))
int count = br.ReadInt32();
for (int i = 0; i < count; i++)
{
var idx = _vocab.Count;
_vocab[token] = idx;
_cache[token] = idx;
var tokenJson = br.ReadString();
var token = JsonSerializer.Deserialize<TKey>(tokenJson)!;
if (!_vocab.ContainsKey(token))
{
var idx = _vocab.Count;
_vocab[token] = idx;
_cache[token] = idx;
}
}
}
}

File.WriteAllBytes(_walPath, Array.Empty<byte>());
Persist();
}
Expand Down