TypeScript and Python: a whole-store flush applies the header the file on disk carries
TS pushAll wrote the header its instance last read (v1 when fresh) without reading the file. It now reads the on-disk header first, as push and delete do through pullAll. Python resets its remembered header when the file is gone or empty, so the disk decides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
cc6615b675
commit
9da5b35f34
4 changed files with 54 additions and 1 deletions
|
|
@ -95,6 +95,8 @@ class SingleFileMessagePackBackingStore:
|
|||
def pull_all(self) -> list[CultCacheEnvelope]:
|
||||
msgpack = self._msgpack()
|
||||
with self._lock:
|
||||
# The disk decides the header: a file that is gone or empty is not marked.
|
||||
self._format = STORE_FORMAT_VERSION
|
||||
if not self.path.exists():
|
||||
return []
|
||||
data = self.path.read_bytes()
|
||||
|
|
|
|||
|
|
@ -863,6 +863,18 @@ class CultCacheTests(unittest.TestCase):
|
|||
def test_single_file_rewrite_of_a_v1_store_stays_v1(self) -> None:
|
||||
self.assertEqual(self._header_after_rewrite(self._VECTORS / "v1-base.msgpack"), "cultcache.store.v1")
|
||||
|
||||
def test_single_file_flush_after_the_marked_file_is_gone_writes_unmarked(self) -> None:
|
||||
import msgpack
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
store_path = Path(tmp) / "store.msgpack"
|
||||
store_path.write_bytes((self._C2A_VECTORS / "v3-base.msgpack").read_bytes())
|
||||
store = SingleFileMessagePackBackingStore(store_path)
|
||||
envelopes = store.pull_all()
|
||||
store_path.unlink()
|
||||
store.push_all(envelopes)
|
||||
self.assertEqual(msgpack.unpackb(store_path.read_bytes(), raw=False)[0], "cultcache.store.v1")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
|
|
@ -140,10 +140,27 @@ export class SingleFileMessagePackBackingStore implements CacheBackingStore {
|
|||
}
|
||||
}
|
||||
|
||||
// A flush of the whole store writes the header the file on disk carries, read now: a file marked for element ids
|
||||
// stays marked, and one that is not (or is gone) is written unmarked.
|
||||
await this.#readDiskFormat();
|
||||
await this.#writeAll(entries);
|
||||
});
|
||||
}
|
||||
|
||||
async #readDiskFormat(): Promise<void> {
|
||||
try {
|
||||
const data = await readFile(this.filePath);
|
||||
const decoded = data.length === 0 ? undefined : decode(data);
|
||||
this.#format = isStoreSnapshot(decoded) ? decoded[0] : STORE_FORMAT_VERSION;
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
|
||||
throw error;
|
||||
}
|
||||
|
||||
this.#format = STORE_FORMAT_VERSION;
|
||||
}
|
||||
}
|
||||
|
||||
async #enqueue<T>(operation: () => Promise<T>): Promise<T> {
|
||||
let result!: T;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ import assert from "node:assert/strict";
|
|||
import { exec, execFile } from "node:child_process";
|
||||
import { EventEmitter } from "node:events";
|
||||
import { existsSync } from "node:fs";
|
||||
import { access, mkdtemp, readFile, writeFile } from "node:fs/promises";
|
||||
import { access, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { dirname, join, resolve, delimiter } from "node:path";
|
||||
import { test } from "node:test";
|
||||
|
|
@ -1512,6 +1512,28 @@ async function copyVector(directory: string, source: string, name: string): Prom
|
|||
return file;
|
||||
}
|
||||
|
||||
test("SingleFileMessagePackBackingStore pushAll applies the header the file on disk carries", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "cultcache-v3-pushall-"));
|
||||
const header = async (file: string) => (decode(await readFile(file)) as unknown[])[0];
|
||||
|
||||
// A store that never read the file still keeps a marked file marked.
|
||||
const v3 = await copyVector(dir, join(c2aVectors, "v3-base.msgpack"), "v3.msgpack");
|
||||
const envelopes = await new SingleFileMessagePackBackingStore(v3).pullAll();
|
||||
await new SingleFileMessagePackBackingStore(v3).pushAll(envelopes);
|
||||
assert.equal(await header(v3), "cultcache.store.v3");
|
||||
|
||||
// A store that read a marked file, whose file is then gone, writes unmarked: the disk decides, not the last read.
|
||||
const reader = new SingleFileMessagePackBackingStore(v3);
|
||||
await reader.pullAll();
|
||||
await rm(v3);
|
||||
await reader.pushAll(envelopes);
|
||||
assert.equal(await header(v3), "cultcache.store.v1");
|
||||
|
||||
const v1 = await copyVector(dir, join(variantVectors, "v1-base.msgpack"), "v1.msgpack");
|
||||
await new SingleFileMessagePackBackingStore(v1).pushAll(envelopes);
|
||||
assert.equal(await header(v1), "cultcache.store.v1");
|
||||
});
|
||||
|
||||
test("SingleFileMessagePackBackingStore reads a v3 element-id store and a rewrite keeps the marker", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "cultcache-v3-"));
|
||||
const v3 = await copyVector(dir, join(c2aVectors, "v3-base.msgpack"), "v3.msgpack");
|
||||
|
|
|
|||
Loading…
Reference in a new issue