Open a directory store without creating anything

Opening a directory store, read-only or writable, created the .records
folder and its .commit.lock before reading a manifest that might not
exist. Only a write creates them now: PullAll takes the commit lease with
create: false, which opens an existing lock and returns no lease when
there is none (no writer has ever committed there, so there is nothing to
exclude). CommitBatch and PushAll create the folder and lock as before.

Test DirectoryMessagePackBackingStore_Open_CreatesNothing_UntilAWrite:
opening a missing store and a manifest-less .records folder, read-only
and writable, leaves the filesystem unchanged; the first flush creates the
manifest, the lock and one page. With the previous lease code restored
the test fails on the first read-only open.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-14 13:43:45 +02:00
commit debe616716
7 changed files with 66 additions and 120 deletions

View file

@ -284,6 +284,55 @@ namespace GameCult.Caching.Tests
}
}
[Test]
public async Task DirectoryMessagePackBackingStore_Open_CreatesNothing_UntilAWrite()
{
var root = Path.Combine(Path.GetTempPath(), $"cultlib-tests-{Guid.NewGuid():N}");
Directory.CreateDirectory(root);
var filePath = Path.Combine(root, "store.cc");
var recordsPath = DirectoryMessagePackBackingStore.DefaultRecordDirectoryPath(filePath);
try
{
foreach (var readOnly in new[] { true, false })
{
using (await CultCacheMessagePack.OpenAsync(filePath, new CultCacheOpenOptions { UseDirectoryStore = true, ReadOnly = readOnly }))
{
}
Assert.That(Directory.GetFileSystemEntries(root), Is.Empty,
$"opening a missing directory store (read-only {readOnly}) created files");
}
Directory.CreateDirectory(recordsPath);
foreach (var readOnly in new[] { true, false })
{
using (var cache = await CultCacheMessagePack.OpenAsync(filePath, new CultCacheOpenOptions { UseDirectoryStore = true, ReadOnly = readOnly }))
{
Assert.That(cache.AllStoredDocuments, Is.Empty);
}
Assert.That(Directory.GetFileSystemEntries(recordsPath), Is.Empty,
$"opening a manifest-less directory store (read-only {readOnly}) created a lock");
Assert.That(File.Exists(filePath), Is.False);
}
using (var writer = await CultCacheMessagePack.OpenAsync(filePath, new CultCacheOpenOptions { UseDirectoryStore = true }))
{
await writer.UpsertAsync(new NamedTestEntry { Name = "first-write", Value = "creates the store" });
await writer.FlushAsync();
}
Assert.That(File.Exists(filePath), Is.True);
Assert.That(File.Exists(Path.Combine(recordsPath, ".commit.lock")), Is.True);
Assert.That(Directory.GetFiles(recordsPath, "*.msgpack"), Has.Length.EqualTo(1));
}
finally
{
if (Directory.Exists(root)) Directory.Delete(root, recursive: true);
}
}
[Test]
public async Task CultCacheMessagePack_OpenAsync_HydratesPersistedGlobal()
{