v0.7.0

Architecture

Last updated


This page shows how ToriiDB's layers fit together: the socket daemon, the embedded store, per-database state, persistence, and the embedding client.

System Overview


graph TB
    APP[Application process] --> NEW["daemon.New(dir)"]
    NEW -->|socket answers| CLI[ModeClient: pooled unix socket client]
    NEW -->|no live socket| SRV[ModeServer: owns store.Store]
    CLI -->|4-byte length + JSON frame| SOCK["/tmp/toriidb-uid-hash.sock"]
    SOCK --> DISP["dispatch: Session() + Select(db)"]
    SRV --> DISP
    REPL[cmd/test REPL] --> EXEC["Store.Exec command router"]
    DISP --> CORE[store core: KV, JSON fields, TTL, scans, vectors]
    EXEC --> CORE
    CORE --> DBS["db_0 ... db_15: RWMutex + map of Entry"]
    DBS --> DISK["NNNNNNNN.snap + NNNNNNNN.log generations"]
    CORE --> EMB["openai.Client (text-embedding-3-small)"]
    EMB --> CACHE["__torii:embed:* cache entries"]
    CACHE --> DBS
  1. graph TB
  2. APP[Application process] --> NEW["daemon.New(dir)"]
  3. NEW -->|socket answers| CLI[ModeClient: pooled unix socket client]
  4. NEW -->|no live socket| SRV[ModeServer: owns store.Store]
  5. CLI -->|4-byte length + JSON frame| SOCK["/tmp/toriidb-uid-hash.sock"]
  6. SOCK --> DISP["dispatch: Session() + Select(db)"]
  7. SRV --> DISP
  8. REPL[cmd/test REPL] --> EXEC["Store.Exec command router"]
  9. DISP --> CORE[store core: KV, JSON fields, TTL, scans, vectors]
  10. EXEC --> CORE
  11. CORE --> DBS["db_0 ... db_15: RWMutex + map of Entry"]
  12. DBS --> DISK["NNNNNNNN.snap + NNNNNNNN.log generations"]
  13. CORE --> EMB["openai.Client (text-embedding-3-small)"]
  14. EMB --> CACHE["__torii:embed:* cache entries"]
  15. CACHE --> DBS

Layers


LayerSourceResponsibility
Socket daemoncore/daemon/daemon.go, socket.goOne server per data directory; later New calls on the same directory become clients
Wire protocolcore/daemon/protocol.go, client.go, handler.goLength-prefixed JSON frames, connection pool, op dispatch, error kinds
Command routercore/store/exec.goParses Redis-like text commands for the REPL and Exec callers
Store lifecyclecore/store/store.go16 lazy databases, sessions, expiry timer, shutdown compaction
Entry enginecore/store/set.go, get.go, setField.go, del.go, incr*.go, ttl.goType detection, JSON field mutation, TTL, parsed-cache invariants
Scanscore/store/keys.go, find.go, query.go, filter/Glob key listing, value comparison, JSON filter expressions
Persistencecore/store/aof.go, gc.goAppend + fsync, snapshot compaction, generation fallback, garbage collection
Vectorscore/store/vector.go, vcache.go, vsearch.go, vsim.go, core/openai/client.goEmbedding, cache, cosine top-K search

Cross-Cutting Principles


PrincipleWhere it shows up
Socket identity is a pure function of locationresolveDir runs CheckDir → filepath.Abs → filepath.EvalSymlinks; the socket lives in hard-coded /tmp, never $TMPDIR
Callers never branch on modeDaemon exposes one method set; server mode dispatches in-process, client mode sends a frame
Every request is isolatedEach daemon request creates its own Session and Selects its database, because the selected index is an unlocked int
Memory is authoritative, disk is replayableWrites update the map, then append a synced log line; startup replays snapshot + logs
Destructive rewrites are guardedCompaction refuses to run after a failed replay and only garbage-collects old generations after it succeeds
Internal keys stay internal__torii:* keys are skipped by KEYS, FIND, QUERY, and VSEARCH; point reads still see them

Further Reading


中文