Spaces:
Running
Running
File size: 1,780 Bytes
1d919a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | // Thought log with the same rule as WASMKV: a newer txn on a key replaces
// the older one, and a snapshot read ignores txns after `asOf`.
// onCommit sees every new record (persist.js writes them to IndexedDB);
// replay puts records back on load without calling it.
const LIMIT = 800;
export function createStore(opts) {
const onCommit = (opts && opts.onCommit) || null;
let head = 0;
const recs = [];
return {
head() { return head; },
commit(page, key, value, limit) {
head += 1;
const rec = { page: String(page), key: String(key), txn: head, value: String(value).slice(0, limit || LIMIT) };
recs.push(rec);
if (onCommit) onCommit(rec);
return head;
},
replay(txn, page, key, value) {
const id = Number(txn);
if (id > head) head = id;
recs.push({ page: String(page), key: String(key), txn: id, value: String(value) });
},
read(page, key, asOf) {
const seen = asOf == null ? head : asOf;
let best = null;
for (const r of recs) {
if (r.page !== page || r.key !== key || r.txn > seen) continue;
if (!best || r.txn > best.txn) best = r;
}
return best ? best.value : null;
},
// Every version of one key, oldest first.
history(page, key) {
return recs.filter((r) => r.page === page && r.key === key).sort((a, b) => a.txn - b.txn);
},
scan(page, prefix, asOf) {
const seen = asOf == null ? head : asOf;
const map = new Map();
for (const r of recs) {
if (r.page !== page || r.txn > seen || !r.key.startsWith(prefix)) continue;
const cur = map.get(r.key);
if (!cur || r.txn > cur.txn) map.set(r.key, r);
}
return [...map.values()].sort((a, b) => a.txn - b.txn);
},
};
}
|