Children are listed in subtree order (escaped segment + separator) so the skip-scan stays O(children) even when sibling keys extend each other with characters below the separator. Model-based property test vs a reference Map. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017Qxbkp4tA7G5SCWjzWuxmE
27 KiB
PotentialStorage — Design
This document is the design brief agreed with the project owner (sections 0–11), followed by implementation decisions made where the brief was silent (section 12) and benchmark baselines (section 13).
0. Setup and workflow
- Repository:
git@git.harvmaster.com:Harvmaster/PotentialStorage.git(SSH; self-hosted Git, not GitHub). Clone it. If you can't reach it, stop and tell the owner rather than starting a fresh repo elsewhere. - Inspect what's already in the repo first. If it has tooling (package.json, tsconfig, test runner), keep it. If it's empty, set up:
- TypeScript (
strict: true,noUncheckedIndexedAccess: true), ESM, Node ≥ 20 - Vitest for tests and
vitest benchfor benchmarks;expectTypeOffor type tests - Package name
potential-storageunless one already exists - No runtime dependencies in the core. Dev dependencies are fine (e.g.
fast-checkfor property tests).
- TypeScript (
- Copy this brief into the repo as
DESIGN.mdand create a shortCLAUDE.mdpointing at it plus the build/test commands. - Work milestone by milestone (section 9). Commit after each milestone with a clear message, and keep tests green. Ask the owner before pushing.
1. Principles
- Three layers, each usable on its own: KV → Event (append-only, built on KV) → Document (Mongo-like, built on Events or native).
- Drivers vs stores. Backends implement a tiny, flat, string-keyed driver. All path, child, index, escaping and typing logic lives once in the user-facing node classes, so every backend gets identical behaviour.
- One navigation primitive:
child(key). There are no collection/document classes. Every node is a store over its direct children, andchild(key)re-roots to that key. Navigation is synchronous and does no I/O. - No methods on data. Reads return plain values.
- Performance is a requirement. Batch operations are the primitive; scans are ordered and streamed; nothing replays or scans more than it must (section 8).
2. Paths and key encoding (src/core/path.ts)
- A node's path is
readonly string[]of unescaped segments. Number keys are converted withString(). - The separator is a single character, configurable, default
/. - Escaping (per segment): percent-encode
%, the separator, and a leading~. Escaped segments never contain a raw separator. WriteescapeSegment/unescapeSegmentwith round-trip property tests. - Reserved segments start with
~(user segments can't, because a leading~is escaped):~iindex entries~uunique-index claims~eevent commits~pdocument projections root
- Each node precomputes in its constructor:
selfKey= escaped segments joined by the separator (e.g.users/123)prefix=selfKey + sep(''for the root)- No string work per call beyond
prefix + escapeSegment(key).
- Keys are ordered lexicographically (so
10sorts before9). Document this. An order-preserving numeric key encoding is a possible later option, not part of v1.
Example layout:
users/123 → KV value of users/123
users/123/posts/p1 → KV value of a nested child
users/~i/byEmail/sada@x.io/123 → index entry (value: empty / true)
orders/o1/~e/000000000007 → event commit #7 of stream orders/o1
~p/users/123 → document projection { state, version, commit }
3. Layer 1: KV
3.1 Driver interface (src/kv/driver.ts)
export interface ScanOptions {
gte?: string
lt?: string
limit?: number
reverse?: boolean
values?: boolean // when false/omitted, yield value as undefined
}
export interface KVDriver {
getMany(keys: string[]): Promise<(unknown | undefined)[]> // same order as keys
setMany(entries: [key: string, value: unknown][]): Promise<void>
deleteMany(keys: string[]): Promise<void>
scan(opts: ScanOptions): AsyncIterable<[key: string, value: unknown]> // MUST be key-ordered
// Optional capabilities. Stores feature-detect them and fall back where possible.
setIfAbsent?(entries: [string, unknown][]): Promise<boolean[]> // true = written
batch?(ops: BatchOp[]): Promise<void> // atomic multi-op write
deleteRange?(gte: string, lt: string): Promise<void>
}
export type BatchOp =
| { type: 'set'; key: string; value: unknown }
| { type: 'delete'; key: string }
- Values are passed through as JSON-serialisable values. Drivers that need bytes (HTTP, Redis, files) serialise internally via a pluggable
Codec(default JSON). - Provide an abstract
BaseKVDriverwith default implementations ofdeleteRange(scan + deleteMany) and helpers, so a minimal driver implements only the four required methods. InMemoryKVDriver: a sorted structure (sorted array with binary search is fine for v1; keep it behind an interface so it can become a B-tree). It implements every optional capability:setIfAbsent, atomicbatchanddeleteRange.- Write a reusable driver conformance suite
runKVDriverConformance(name, makeDriver)covering ordering,gte/ltbounds, reverse, limit,valueson/off,getManyorder with missing keys, and each optional capability when present.
3.2 KVStorage / KVNode (src/kv/node.ts)
const db = new KVStorage(driver, { separator: '/', indexes: { ... } }) // db is the root KVNode
const users = db.child('users')
const user = users.child('123')
API of every KVNode (types in section 7):
| Method | Meaning |
|---|---|
child(key) |
Sync. Returns a node at path + key. |
as<S>() |
Type assertion for untyped use; returns the same node retyped. |
get() / get(key) |
Own value at selfKey / value at child key. |
getMany(keys) |
Batch child read, same order, undefined for missing. |
set(value) / set(key, value) |
Write own value / child value. |
setMany(entries) |
Batch child write. |
delete() / delete(key) |
Delete own value / child value only (not descendants). |
deleteMany(keys) |
Batch child delete. |
deleteTree() / deleteTree(key) |
Delete own value and all descendants (uses deleteRange if available). |
keys(opts?) |
AsyncIterable<string> of direct child keys (unescaped). Options: limit, after, reverse. |
entries(opts?) |
AsyncIterable<[key, value]> of direct children that have an own value. |
tree({ depth }) |
Assemble an object from descendant keys up to depth (for field-per-key style). Bounded and explicit; never the default. |
findBy(index, value | { gte?, lt? }, opts?) |
Index lookup over direct children (section 3.3). |
Arity dispatch (important):
- Self vs child is decided by
arguments.length, never by checking forundefined. - If a key argument is passed but is
undefinedornull, throw aTypeError(this preventsusers.get(maybeId)silently reading self). - The root node (
path = []) has no own value:get(),set(v)anddelete()on root throw. - Implement one private
#resolve(args)that every method uses.
Direct-children listing (keys) uses a skip-scan:
- Scan
gte: prefix,lt: prefix + succ(sep). - For each key, take the first segment after
prefix. Skip reserved (~…) segments. Yield the segment (unescaped) if it differs from the previous one. - When the next key is a descendant of the segment just yielded, re-seek the driver to
gte: prefix + escSeg + succ(sep)(wheresucc(c)is the next char code) instead of iterating through the subtree. Keep using the same iterator while keys are siblings.
Listing must be O(direct children × seek cost), not O(subtree). A child that has no own value but has descendants is listed.
3.3 Indexes (src/kv/indexes.ts)
Indexes are declared on path patterns in the KVStorage constructor. A pattern names a parent node, and its index applies to that node's direct children's values:
new KVStorage(driver, {
indexes: {
'users': { byEmail: (u: User) => u.email },
'users/*/posts': { byDate: (p: Post) => p.createdAt, byTag: (p: Post) => p.tags },
},
})
- An index function returns
IndexValue | IndexValue[] | undefined, whereIndexValue = string | number | boolean | null. An array means multi-entry;undefinedmeans not indexed. - Compile patterns into a segment trie (
*matches one segment). A node resolves its index set once, at construction, in O(depth). - Entry key:
<nodePrefix>~i/<indexName>/<encodedValue>/<escapedChildKey>with atruevalue. encodeIndexValuemust be order-preserving and separator-safe:- Type tag first, so values order by type:
null<false<true< numbers < strings. - Numbers use an order-preserving float64 encoding (flip the sign bit for positives and all bits for negatives, then fixed-width hex).
- Strings are escaped like segments.
- This enables
findBy(index, { gte, lt })range scans.
- Type tag first, so values order by type:
- Writes to an indexed node:
- Read the old value(s) (
getMany) and compute the diff of index entries. - If the driver has
batch, write the value and the index adds/removes atomically. - Otherwise, write index adds first, then the value, then index removes. Treat index entries as hints:
findByre-reads each value, re-checks the index function, and lazily deletes stale entries (read-repair).
- Read the old value(s) (
- Non-indexed nodes must not pay the read-before-write cost.
4. Layer 2: Events (src/events/)
An append-only log per stream. A stream is identified by a node path. Same child() navigation as KV.
const events = new EventStorage(kv) // takes a KVStorage (or a KVNode as root)
const orders = events.child('orders')
await orders.append('o1', [{ type: 'created', total: 40 }], { expectedVersion: 0 })
await orders.child('o1').append([{ type: 'paid' }]) // self form
for await (const e of orders.read('o1', { from: 1 })) { /* { version, commit, at, data } */ }
await orders.version('o1')
for await (const id of orders.keys()) { } // child streams
orders.child('o1').child('shipments')
EventStorageis the abstract interface.KVEventStorageis the KV-backed implementation. Leave room for native backends (EventStoreDB, Kafka) implementing the same interface.- Arity:
append(events, opts?)vsappend(key, events, opts?). Dispatch onArray.isArray(args[0]), plus the undefined-key rule from 3.2.read,versionandkeysfollow the 3.2 rules. - Versions: an empty stream has version 0, and the first event has version 1.
expectedVersion: number | 'any'(default'any'). - Storage: one key per commit, densely numbered.
- Key:
<streamSelfKey>/~e/<commitSeq zero-padded to 12> - Value:
{ firstVersion, at, events: E[] } - The commit sequence (not the event version) is the key so that any stale writer collides with the existing key. Keying by version would let a stale writer create overlapping version ranges.
- Key:
- Append:
- Get the head
{ commitSeq, version }(from the cache, or a reverse scan withlimit: 1). - If
expectedVersionis a number and doesn't match the head, throwConcurrencyError. - Call
setIfAbsent(nextCommitKey). - On
false(collision): refresh the head. IfexpectedVersionwas given, throwConcurrencyError; otherwise retry up to 5 times. - Without
setIfAbsent, fall back to a per-stream in-process mutex. Document that this mode is single-writer only.
- Get the head
- Head cache: a bounded LRU keyed by stream path. Update it on successful append; invalidate it on collision.
read({ from, to, reverse, limit })streams events with metadata. To start at versionfrom, locate the containing commit by binary search over commit numbers usinggetManyprobes (O(log commits) round trips), then forward-scan.- There is no update or delete. Keys inside
~eare never rewritten.
5. Layer 3: Documents (src/documents/)
DocumentStorage is the interface. There are two implementations:
EventSourcedDocumentStorage(events, { projections?: KVNode })is the v1 target. Projections default to the~proot on the same KV.MongoDocumentStorage(client)is a later milestone (section 9).
const docs = new EventSourcedDocumentStorage(events, { indexes: { 'users': [{ fields: ['email'], unique: true }, { fields: ['age'] }] } })
const users = docs.child('users')
const id = await users.insert({ name: 'Ada', age: 36 }) // generates a time-sortable id
await users.set('123', { name: 'Ada', age: 36 })
await users.update('123', { $set: { name: 'Ada L.' }, $inc: { logins: 1 } })
await users.child('123').update({ $unset: { nickname: true } }) // self form
await users.get('123')
await users.delete('123')
for await (const { key, data } of users.find({ age: { $gte: 18 } }, { sort: { age: 1 }, limit: 20 })) { }
await users.findOne({ email: 'ada@x.io' })
await users.history('123') // event-sourced only
await users.getAt('123', 17) // state as of version 17
users.child('123').child('posts') // nested docs, same as KV
5.1 Events and projections
- The doc at path
Phas its event stream atPinEventStorage. Its projection is at~p/Pin the projections KV:{ state, version }(anddeleted: truefor tombstones). - Event types:
{ op: 'set', doc },{ op: 'patch', set?: Record<path, value>, unset?: path[] },{ op: 'delete' }. - Store resolved deltas, not operators. For example,
$incresolves to{ set: { logins: 6 } }, so replay is deterministic. - Supported update operators in v1:
$set,$unset,$inc,$push,$pull,$addToSet, with dot-path support.
Write path:
- Read the projection.
- Compute the new state and patch.
appendwithexpectedVersion = projection.version.- Write the projection and index entries (atomic
batchif available). - On
ConcurrencyError: catch up the projection from the stream, recompute, and retry (bounded).
Reads are a single projection get. Never replay on read.
Crash between append and projection write:
- The projection lags the stream. The next write's
expectedVersionfails, which triggers catch-up, so the system self-heals. - Provide
get(key, { consistent: true })that checks the stream head and catches up first. - Provide a
catchUp(node?)maintenance method to repair a subtree. insertids are time-sortable (ULID-like). Implement a small generator, with no dependency.
5.2 Queries and document indexes
- Filter subset:
$eq,$ne,$gt,$gte,$lt,$lte,$in,$nin,$exists$and,$or,$not- implicit equality and dot paths
- Options:
sort,limit,skip,projection(fields). findon a node queries that node's direct children.- Indexes are declared per path pattern (same trie mechanism as 3.3).
- Each index has
fields(compound allowed); entries go under~p/<node>/~i/<name>/<enc(field1)>/<enc(field2)>/…/<childKey>, usingencodeIndexValue. unique: trueadditionally claims~u/<name>/<enc(values)>viasetIfAbsent. Throw at construction if the driver lackssetIfAbsent.
- Each index has
- Planner:
- Pick the index that covers the most leading equality fields, then at most one range field.
- Use the index order for
sortwhen it matches. - Otherwise do a full scan of children projections, with a one-time dev-mode
console.warnnaming the filter.
6. Shared conventions
- Every layer uses the same navigation (
child,as,keys) and the same self/child arity rules. - All multi-result reads are
AsyncIterable(streamed, cancellable). Never build large arrays internally. - Errors are typed classes exported from
src/errors.ts:ConcurrencyError,UniqueConstraintError,CapabilityError,InvalidKeyError. - Public exports go through
src/index.ts, with subpath exports per layer if convenient.
7. Types (src/types/schema.ts)
Each node is typed by a single Spec that describes its own value and its children by key:
export type Spec = { value?: unknown; children?: Record<string, Spec> }
export type AnySpec = { value: unknown; children: Record<string, AnySpec> } // untyped default
export type ValueOf<S> = S extends { value: infer V } ? V : never
export type ChildrenOf<S> = S extends { children: infer C } ? C : {}
export type KeyOf<S> = Extract<keyof ChildrenOf<S>, string | number>
export type ChildOf<S, K extends KeyOf<S>> = ChildrenOf<S>[K]
// Helpers
export type Many<V, C extends Record<string, Spec> = {}> =
{ children: { [id: string]: { value: V; children: C } } }
export type Fields<T> = { [K in keyof T & string]: { value: T[K] } }
Node signatures:
class KVNode<S extends Spec = AnySpec> {
child<K extends KeyOf<S>>(key: K): KVNode<ChildOf<S, K>>
get(): Promise<ValueOf<S> | undefined>
get<K extends KeyOf<S>>(key: K): Promise<ValueOf<ChildOf<S, K>> | undefined>
// set/delete/getMany/etc. follow the same pattern
}
Example to cover with expectTypeOf tests:
type User = { username: string; age: number }
type Post = { title: string; createdAt: string }
type Schema = { children: {
users: Many<User, { posts: Many<Post> }>
profile: { children: { [id: string]: { children: Fields<User> } } } // field-per-key style
} }
const db = new KVStorage<Schema>(driver)
db.child('users').get('123') // User | undefined
db.child('users').child('123').get() // User | undefined (same key)
db.child('users').child('123').child('posts') // KVNode<Many<Post>>
db.child('users').child('123').child('postz') // ✗ type error
db.child('profile').child('9').get('age') // number | undefined
db.child('profile').child('9').get('agee') // ✗ type error
Notes:
- Named levels check keys at compile time; dynamic
[id: string]levels accept any string. - One node's children can't mix an index signature with named keys (a TS restriction), so schemas alternate between dynamic and named levels. That alternation exists only in types; the runtime API is just
child(). EventStorageandDocumentStoragenodes use the same Spec machinery (the value type is the event type or the document type respectively).
8. Performance requirements
- Single-key ops: exactly one driver call (no hidden reads), except indexed writes (one
getManybefore the write) and document writes (projection read plus append plus projection write). *Manyops: one driver call each (plus index work), never N calls.keys(): skip-scan, O(direct children) seeks.- Event head lookup: cached; cold lookup is a single reverse scan with
limit: 1. - Document
get: one driverget. - Add
vitest benchbenchmarks for:- KV
get/set/getManyat 10k/100k keys keys()on a node with 1k children each having 100 descendants- event append and read
- document
get/update/indexedfindvs full-scanfind
- KV
- Record baseline numbers in
DESIGN.md.
9. Milestones
- Core:
path.ts(escape, unescape, encode,succ),encodeIndexValue, errors. Property tests for round-trips and order preservation. - KV driver:
KVDriver,BaseKVDriver,InMemoryKVDriver, conformance suite. - KV node: full API from 3.2, including arity rules, skip-scan
keys,deleteTree,tree. Add a model-based property test: random ops vs a referenceMap. - KV indexes: pattern trie, atomic and non-atomic write paths, read-repair, range
findBy. - Events:
KVEventStoragewith commit keys,setIfAbsentand mutex modes, head cache, binary-searchread({ from }), concurrency tests (simulated interleaving with two storage instances on one driver). - Documents:
EventSourcedDocumentStorage, update operators, projections, catch-up, queries, planner, unique indexes,historyandgetAt. - Types: Spec machinery wired through all three layers, plus
expectTypeOftests from section 7. - HTTP driver:
- A
createKVHttpHandler(driver)(framework-agnosticRequest → Response) plus anHttpKVDriverclient. - Endpoints:
POST /getMany,POST /setMany,POST /deleteMany,POST /batch,POST /setIfAbsent,GET /scan?gte<&limit&reverse&values(NDJSON stream). - It must pass the conformance suite end-to-end.
- A
- Benchmarks and a
DESIGN.mdupdate with results. - (Later, ask first)
MongoDocumentStorage:- Path pattern maps to a collection (e.g.
users/*/posts→users.posts), with a_parentfield and a compound index(_parent, _id). - Native indexes map from the same index declarations.
- It should pass the document test suite where semantics allow (no
history/getAt).
- Path pattern maps to a collection (e.g.
10. Decisions already made (don't revisit without asking)
child()-only navigation; no collection/doc classes.get()with no argument reads the node's own key. It never assembles the subtree;tree()does that explicitly.- A node's own value and its children are independent keys.
- Self/child dispatch by arity; a passed-but-undefined key throws.
- Indexes are declared on path patterns at construction.
- Events use one key per commit, densely numbered.
- Documents store resolved deltas, and reads hit projections only.
- Keys sort lexicographically.
- Values are JSON-serialisable; byte drivers use a codec.
11. Open questions to raise with the owner (don't block on them)
- Order-preserving numeric keys (not just index values)?
- Should multi-writer safety be required (a hard error without
setIfAbsent) rather than a documented single-writer fallback? - The hard-delete story for immutable event logs (compaction or crypto-shredding) for privacy deletion requests.
12. Implementation decisions
Decisions taken where the brief was silent. Each is the simplest option consistent with sections 1–10.
M1 — Core
- Separator must be a single printable ASCII character other than
%and~. - Empty keys are rejected (
InvalidKeyError), as areNaN/±Infinitynumber keys. An empty segment would producea//b, which is ambiguous with the subtree layout. - Separator escaping uses
%+ two uppercase hex digits (/→%2F,%→%25, leading~→%7E). InvalidKeyErrorextendsTypeError, so "passed-but-undefined key throws aTypeError" (3.2) and the typed error class (6) are the same throw.- Index string encoding deviates from "escaped like segments". Percent-escaping is not
order-preserving (
"a/"→"a%2F"sorts below"a&", and"a"+ sep sorts above"a!"), which would breakfindByrange scans. Instead every character the index encoder emits sorts strictly above the separator: code units<= succ(sep)are written assucc(sep)followed bychr(succ(sep) + 1 + c). This is monotone and prefix-safe, soa < b ⇔ enc(a)+sep+… < enc(b)+sep+…exactly (property-tested). Encodings are still separator-free. - Type tags and number digits: tags are the first five digits of the digit alphabet; numbers use
16 digits of
0-9a-fwhen the separator sorts below'0'(e.g./), otherwise the 16 characters just above the separator.-0is stored as0;NaNcannot be indexed. - Ordering of strings is by UTF-16 code unit (JS
<). Drivers that compare UTF-8 bytes agree except for astral-plane characters vs. U+E000–U+FFFF; noted as a driver-author caveat.
M2 — KV driver
- Conformance suite location:
src/testing/kv-conformance.ts, exported aspotential-storage/testing. It importsvitest, which is an optional peer dependency; the core entry point never imports it. InMemoryKVDriverclones values on write and read (structuredClone, skipped for primitives) so stored state can't be mutated through shared references, matching serialising drivers. Opt out with{ clone: false }.- Scans tolerate concurrent writes: the in-memory scan remembers the last key yielded and re-seeks
if the map changed while suspended. Read-repair and
deleteTreerely on this. The conformance suite requires it of every driver (deleting an already-yielded key mid-scan must not skip keys). setManybulk path: ≥32 new keys are sorted and merged (O(n + k log k)); fewer are spliced.BaseKVDriver.deleteRangedeletes in chunks of 1000 (scan withlimit, thendeleteMany, repeat), so it never deletes from under its own live iterator. It also providesget/set/deletesingle-key helpers.- No fake
batch:BaseKVDriverdeliberately does not provide a non-atomicbatch, since stores use its presence to mean "atomic".
M3 — KV node
- Child order is "subtree order", not plain segment order. The brief's skip-scan assumes a
child's keys are contiguous, but they aren't when a sibling extends a segment with a character
that sorts below the separator (for
/: space and!"#$%&'()*+,-.). With childrenaanda-1the key space isp/a,p/a-1,p/a-1/x,p/a/x, soa's descendants sort aftera-1, and "yield if different from the previous segment" would emitatwice.keys()/entries()therefore order children by escaped segment followed by the separator (a-1/<a/), which is the order of their subtrees in key space. For keys without such characters this is plain lexicographic order.- Forward listing defers a child seen only through its own key on a small stack (bounded by
segment length) until the scan passes
p/<seg>/. It costs no extra driver calls beyond the brief's one seek per child with descendants. - Reverse listing needs one batched
getManyprobe per new child whose segment contains a below-separator character, to find shorter siblings that only have an own key. afteris exclusive in the direction of travel and follows the same order.- Implemented once in
src/kv/children.ts(scanChildren) and reused by the event and document layers. A model-based property test checks forward, reverse,afterandlimitagainst brute force with keys likea,a-1,a.b,a/x,%,~t. - Open question for the owner: keep subtree order, or change segment escaping so the key layout nests exactly. That would mean escaping every below-separator character with an escape character above the separator, at the cost of the readable percent-encoding.
- Forward listing defers a child seen only through its own key on a small stack (bounded by
segment length) until the scan passes
- Errors from async methods are rejections. Every async method (
get,set, …) is anasyncfunction, so a bad key yields a rejected promise rather than a synchronous throw.child()is sync and throws synchronously. set(undefined)is rejected (TypeError: usedelete());undefinedmeans "missing" in reads.deleteTree()on the root is allowed and clears the whole store (chunked scan + delete, since the root range has no upper bound). Subtree deletion isdelete(selfKey)plusdeleteRange(prefix, selfKey + succ(sep)). It is not[selfKey, …), which would also catch siblings such asa!.tree({ depth })builds a plain object: leaves are values, interior nodes are objects. A node with both an own value and children exposes its own value under the''property (empty keys are invalid, so it can't collide). Reserved subtrees are skipped. Deeper levels are skipped by seeking rather than reading.keys()andentries()yield string keys (number keys come back as strings).- Exposed low-level properties on every node:
path,selfKey,prefix,end(subtree upper bound),driver,separator,isRoot. The event and document layers build on these.
13. Benchmark baselines
Filled in at milestone 9.