Documentation
¶
Overview ¶
Package entdriver
Index ¶
- type EntDriver
- func (ed *EntDriver) Ancestry(ctx context.Context, hash string) ([]*merkle.Node, error)
- func (ed *EntDriver) AncestryChain(ctx context.Context, hash string) (*storage.Chain, error)
- func (ed *EntDriver) AncestryChains(ctx context.Context, hashes []string) (map[string]*storage.Chain, error)
- func (ed *EntDriver) Close() error
- func (ed *EntDriver) CountSessions(ctx context.Context, opts storage.ListOpts) (storage.SessionStats, error)
- func (ed *EntDriver) Depth(ctx context.Context, hash string) (int, error)
- func (ed *EntDriver) Get(ctx context.Context, hash string) (*merkle.Node, error)
- func (ed *EntDriver) GetByParent(ctx context.Context, parentHash *string) ([]*merkle.Node, error)
- func (ed *EntDriver) Has(ctx context.Context, hash string) (bool, error)
- func (ed *EntDriver) Leaves(ctx context.Context) ([]*merkle.Node, error)
- func (ed *EntDriver) List(ctx context.Context) ([]*merkle.Node, error)
- func (ed *EntDriver) ListParentRefs(ctx context.Context) ([]storage.ParentRef, error)
- func (ed *EntDriver) ListSessions(ctx context.Context, opts storage.ListOpts) (*storage.Page[*merkle.Node], error)
- func (ed *EntDriver) Put(ctx context.Context, n *merkle.Node) (bool, error)
- func (ed *EntDriver) Roots(ctx context.Context) ([]*merkle.Node, error)
- func (ed *EntDriver) UpdateUsage(ctx context.Context, hash string, usage *llm.Usage) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EntDriver ¶
EntDriver provides storage operations using an ent client. It is database-agnostic and can be embedded by specific drivers.
DB is the underlying *sql.DB held by the wrapping driver (sqlite, postgres). It is optional — when populated together with Dialect, the perf-critical bulk paths (currently AncestryChains) drop down to raw SQL via a recursive CTE, which is roughly 20× faster than ent's per-row ORM scaffolding at the cost of being database-specific. When DB is nil those paths fall back to the ent-only implementation.
Dialect must be one of "sqlite3" or "postgres" — the values from entgo's dialect package — and is used to pick the right placeholder syntax for the raw query.
func (*EntDriver) Ancestry ¶
Ancestry returns the path from a node back to its root (node first, root last). Uses the parent edge for traversal. See AncestryChain for a variant that also signals when the walk stopped at a missing parent.
func (*EntDriver) AncestryChain ¶ added in v0.4.0
AncestryChain walks the parent chain starting at hash and returns a Chain describing whether the walk reached a real root or stopped at a parent that is not present in this store. A missing parent is treated as an expected edge case (e.g. trimmed history, foreign chain, offloaded data) and surfaced via Chain.Incomplete / Chain.MissingParent rather than as an error.
func (*EntDriver) AncestryChains ¶ added in v0.4.0
func (ed *EntDriver) AncestryChains(ctx context.Context, hashes []string) (map[string]*storage.Chain, error)
AncestryChains walks the ancestry of each input hash and returns a Chain per starting hash. When the underlying *sql.DB is available the fast path issues a single recursive CTE query that walks every chain in one round trip; otherwise it falls back to a batched BFS that issues one query per BFS depth level.
Both SQLite and Postgres have dialect-specific CTE templates — see cteQuerySQLite and cteQueryPostgres — that differ only in the label_hint extraction (json_each/json_extract vs jsonb_array_elements/->>) and bind-parameter syntax (? vs $N).
This is the hot path behind /v1/sessions/summary. The naive loop of calling AncestryChain per leaf issues one SQL query per parent edge per leaf; on a real store with tens of thousands of leaves, that never completes. The CTE path collapses the walk to a single round trip and is roughly 20× faster than the BFS fallback on the same data.
func (*EntDriver) CountSessions ¶ added in v0.4.0
func (ed *EntDriver) CountSessions(ctx context.Context, opts storage.ListOpts) (storage.SessionStats, error)
CountSessions returns aggregate counts for the slice of data matching opts. Pagination fields on opts are ignored.
func (*EntDriver) GetByParent ¶
GetByParent retrieves all nodes that have the given parent hash. Uses the children edge for efficient lookups.
func (*EntDriver) Leaves ¶
Leaves returns all leaf nodes (nodes with no children). Uses the children edge for efficient detection.
func (*EntDriver) ListParentRefs ¶ added in v0.4.0
ListParentRefs returns the (hash, parent_hash) tuple for every node in the store. It projects only the two edge columns so integrity checks over large databases don't pay the cost of deserializing every node's bucket JSON.
func (*EntDriver) ListSessions ¶ added in v0.4.0
func (ed *EntDriver) ListSessions(ctx context.Context, opts storage.ListOpts) (*storage.Page[*merkle.Node], error)
ListSessions returns a page of leaf nodes (sessions), ordered by created_at descending then hash descending, optionally filtered by opts.
func (*EntDriver) Put ¶
Put stores a node. Returns true if the node was newly inserted, false if it already existed. This is a no-op due to content-addressing.