entdriver

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2026 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package entdriver

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EntDriver

type EntDriver struct {
	Client  *ent.Client
	DB      *sql.DB
	Dialect string
}

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

func (ed *EntDriver) Ancestry(ctx context.Context, hash string) ([]*merkle.Node, error)

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

func (ed *EntDriver) AncestryChain(ctx context.Context, hash string) (*storage.Chain, error)

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) Close

func (ed *EntDriver) Close() error

Close closes the database connection.

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) Depth

func (ed *EntDriver) Depth(ctx context.Context, hash string) (int, error)

Depth returns the depth of a node (0 for roots).

func (*EntDriver) Get

func (ed *EntDriver) Get(ctx context.Context, hash string) (*merkle.Node, error)

Get retrieves a node by its hash.

func (*EntDriver) GetByParent

func (ed *EntDriver) GetByParent(ctx context.Context, parentHash *string) ([]*merkle.Node, error)

GetByParent retrieves all nodes that have the given parent hash. Uses the children edge for efficient lookups.

func (*EntDriver) Has

func (ed *EntDriver) Has(ctx context.Context, hash string) (bool, error)

Has checks if a node exists by its hash.

func (*EntDriver) Leaves

func (ed *EntDriver) Leaves(ctx context.Context) ([]*merkle.Node, error)

Leaves returns all leaf nodes (nodes with no children). Uses the children edge for efficient detection.

func (*EntDriver) List

func (ed *EntDriver) List(ctx context.Context) ([]*merkle.Node, error)

List returns all nodes in the store.

func (*EntDriver) ListParentRefs added in v0.4.0

func (ed *EntDriver) ListParentRefs(ctx context.Context) ([]storage.ParentRef, error)

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

func (ed *EntDriver) Put(ctx context.Context, n *merkle.Node) (bool, error)

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.

func (*EntDriver) Roots

func (ed *EntDriver) Roots(ctx context.Context) ([]*merkle.Node, error)

Roots returns all root nodes (nodes with no parent).

func (*EntDriver) UpdateUsage

func (ed *EntDriver) UpdateUsage(ctx context.Context, hash string, usage *llm.Usage) error

UpdateUsage updates only the token usage fields on an existing node by hash.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL