store

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Now

func Now() string

Now returns the current time in ISO 8601 format.

func UnmarshalProps added in v0.1.0

func UnmarshalProps(data string) map[string]any

UnmarshalProps deserializes JSON properties. Exported for use by cypher executor.

Types

type Edge

type Edge struct {
	ID         int64
	Project    string
	SourceID   int64
	TargetID   int64
	Type       string
	Properties map[string]any
}

Edge represents a graph edge stored in SQLite.

type EdgeInfo

type EdgeInfo struct {
	FromName string
	ToName   string
	Type     string
}

EdgeInfo is a simplified edge for output.

type FileHash

type FileHash struct {
	Project string
	RelPath string
	SHA256  string
}

FileHash represents a stored file content hash for incremental reindex.

type LabelCount

type LabelCount struct {
	Label string `json:"label"`
	Count int    `json:"count"`
}

LabelCount is a label with its count.

type Node

type Node struct {
	ID            int64
	Project       string
	Label         string
	Name          string
	QualifiedName string
	FilePath      string
	StartLine     int
	EndLine       int
	Properties    map[string]any
}

Node represents a graph node stored in SQLite.

type NodeHop

type NodeHop struct {
	Node *Node
	Hop  int
}

NodeHop is a node with its BFS hop distance.

type Project

type Project struct {
	Name      string
	IndexedAt string
	RootPath  string
}

Project represents an indexed project.

type ProjectInfo added in v0.2.0

type ProjectInfo struct {
	Name     string
	DBPath   string
	RootPath string
}

ProjectInfo holds metadata about a discovered project database.

type Querier added in v0.1.0

type Querier interface {
	Exec(query string, args ...any) (sql.Result, error)
	Query(query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row
}

Querier abstracts *sql.DB and *sql.Tx so store methods work in both contexts.

type SchemaInfo

type SchemaInfo struct {
	NodeLabels           []LabelCount `json:"node_labels"`
	RelationshipTypes    []TypeCount  `json:"relationship_types"`
	RelationshipPatterns []string     `json:"relationship_patterns"`
	SampleFunctionNames  []string     `json:"sample_function_names"`
	SampleClassNames     []string     `json:"sample_class_names"`
	SampleQualifiedNames []string     `json:"sample_qualified_names"`
}

SchemaInfo contains graph schema statistics.

type SearchOutput added in v0.0.2

type SearchOutput struct {
	Results []*SearchResult
	Total   int
}

SearchOutput wraps search results with total count for pagination.

type SearchParams

type SearchParams struct {
	Project            string
	Label              string
	NamePattern        string
	FilePattern        string
	Relationship       string
	Direction          string // "inbound", "outbound", "any"
	MinDegree          int
	MaxDegree          int
	Limit              int
	Offset             int
	ExcludeEntryPoints bool     // when true, exclude nodes with is_entry_point=true
	IncludeConnected   bool     // when true, load connected node names (expensive, off by default)
	ExcludeLabels      []string // labels to exclude from results
	SortBy             string   // "relevance" (default), "name", "degree"
}

SearchParams defines structured search parameters.

type SearchResult

type SearchResult struct {
	Node           *Node
	InDegree       int
	OutDegree      int
	ConnectedNames []string
}

SearchResult is a node with edge degree info.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store wraps a SQLite connection for graph storage.

func Open

func Open(project string) (*Store, error)

Open opens or creates a SQLite database for the given project in the default cache dir.

func OpenInDir added in v0.2.0

func OpenInDir(dir, project string) (*Store, error)

OpenInDir opens or creates a SQLite database for the given project in a specific directory.

func OpenMemory

func OpenMemory() (*Store, error)

OpenMemory opens an in-memory SQLite database (for testing).

func OpenPath

func OpenPath(dbPath string) (*Store, error)

OpenPath opens a SQLite database at the given path.

func (*Store) AllNodes

func (s *Store) AllNodes(project string) ([]*Node, error)

AllNodes returns all nodes for a project.

func (*Store) BFS

func (s *Store) BFS(startNodeID int64, direction string, edgeTypes []string, maxDepth, maxResults int) (*TraverseResult, error)

BFS performs breadth-first traversal following edges of given types using a recursive CTE, replacing the previous per-node Go-side loop with a single SQL round-trip. direction: "outbound" follows source->target, "inbound" follows target->source. maxDepth caps the BFS depth, maxResults caps total visited nodes.

func (*Store) BeginBulkWrite added in v0.2.0

func (s *Store) BeginBulkWrite()

BeginBulkWrite switches to MEMORY journal mode for faster bulk writes. Call EndBulkWrite when done to restore WAL mode. MEMORY mode is rollback-safe on crash (unlike journal_mode=OFF).

func (*Store) Checkpoint added in v0.1.4

func (s *Store) Checkpoint()

Checkpoint forces a WAL checkpoint, moving pages from WAL to the main DB, then runs PRAGMA optimize so the query planner has up-to-date statistics. PRAGMA optimize (SQLite 3.46+) auto-limits sampling per index, only re-analyzing stale stats. Cost is absorbed during indexing rather than the first read query.

func (*Store) Close

func (s *Store) Close() error

Close closes the database connection.

func (*Store) CountEdges

func (s *Store) CountEdges(project string) (int, error)

CountEdges returns the number of edges in a project.

func (*Store) CountEdgesByType added in v0.2.0

func (s *Store) CountEdgesByType(project, edgeType string) (int, error)

CountEdgesByType returns the number of edges of a given type for a project.

func (*Store) CountNodes

func (s *Store) CountNodes(project string) (int, error)

CountNodes returns the number of nodes in a project.

func (*Store) DB

func (s *Store) DB() *sql.DB

DB returns the underlying sql.DB (for advanced queries).

func (*Store) DBPath added in v0.1.4

func (s *Store) DBPath() string

DBPath returns the filesystem path to the SQLite database.

func (*Store) DeleteEdgesByProject

func (s *Store) DeleteEdgesByProject(project string) error

DeleteEdgesByProject deletes all edges for a project.

func (*Store) DeleteEdgesBySourceFile added in v0.1.0

func (s *Store) DeleteEdgesBySourceFile(project, filePath, edgeType string) error

DeleteEdgesBySourceFile deletes edges of a given type where the source node belongs to a specific file. Used for incremental re-indexing of CALLS edges.

func (*Store) DeleteEdgesByType

func (s *Store) DeleteEdgesByType(project, edgeType string) error

DeleteEdgesByType deletes all edges of a given type for a project.

func (*Store) DeleteFileHash

func (s *Store) DeleteFileHash(project, relPath string) error

DeleteFileHash deletes a single file hash entry.

func (*Store) DeleteFileHashes

func (s *Store) DeleteFileHashes(project string) error

DeleteFileHashes deletes all file hashes for a project.

func (*Store) DeleteNodesByFile

func (s *Store) DeleteNodesByFile(project, filePath string) error

DeleteNodesByFile deletes all nodes for a specific file in a project.

func (*Store) DeleteNodesByLabel

func (s *Store) DeleteNodesByLabel(project, label string) error

DeleteNodesByLabel deletes all nodes with a given label in a project.

func (*Store) DeleteNodesByProject

func (s *Store) DeleteNodesByProject(project string) error

DeleteNodesByProject deletes all nodes for a project.

func (*Store) DeleteProject

func (s *Store) DeleteProject(name string) error

DeleteProject deletes a project and all associated data (CASCADE).

func (*Store) EndBulkWrite added in v0.2.0

func (s *Store) EndBulkWrite()

EndBulkWrite restores WAL journal mode and NORMAL synchronous after bulk writes.

func (*Store) FindEdgesBySource

func (s *Store) FindEdgesBySource(sourceID int64) ([]*Edge, error)

FindEdgesBySource finds all edges from a given source node.

func (*Store) FindEdgesBySourceAndType

func (s *Store) FindEdgesBySourceAndType(sourceID int64, edgeType string) ([]*Edge, error)

FindEdgesBySourceAndType finds edges from a source with a specific type.

func (*Store) FindEdgesBySourceIDs added in v0.2.0

func (s *Store) FindEdgesBySourceIDs(sourceIDs []int64, edgeTypes []string) (map[int64][]*Edge, error)

FindEdgesBySourceIDs returns all edges where source_id is in the given set, optionally filtered by edge types. Groups results by source_id for efficient lookup.

func (*Store) FindEdgesByTarget

func (s *Store) FindEdgesByTarget(targetID int64) ([]*Edge, error)

FindEdgesByTarget finds all edges to a given target node.

func (*Store) FindEdgesByTargetAndType

func (s *Store) FindEdgesByTargetAndType(targetID int64, edgeType string) ([]*Edge, error)

FindEdgesByTargetAndType finds edges to a target with a specific type.

func (*Store) FindEdgesByTargetIDs added in v0.2.0

func (s *Store) FindEdgesByTargetIDs(targetIDs []int64, edgeTypes []string) (map[int64][]*Edge, error)

FindEdgesByTargetIDs returns all edges where target_id is in the given set, optionally filtered by edge types. Groups results by target_id.

func (*Store) FindEdgesByType added in v0.1.3

func (s *Store) FindEdgesByType(project, edgeType string) ([]*Edge, error)

FindEdgesByType returns all edges of a given type for a project.

func (*Store) FindEdgesByURLPath added in v0.1.3

func (s *Store) FindEdgesByURLPath(project, pathSubstring string) ([]*Edge, error)

FindEdgesByURLPath returns edges where url_path contains the given substring. Uses the generated column index for prefix matches, falls back to json_extract for substring.

func (*Store) FindNodeByID

func (s *Store) FindNodeByID(id int64) (*Node, error)

FindNodeByID finds a node by its primary key ID.

func (*Store) FindNodeByQN

func (s *Store) FindNodeByQN(project, qualifiedName string) (*Node, error)

FindNodeByQN finds a node by project and qualified name.

func (*Store) FindNodeIDsByQNs added in v0.1.4

func (s *Store) FindNodeIDsByQNs(project string, qns []string) (map[string]int64, error)

FindNodeIDsByQNs returns a map of qualifiedName → ID for the given QNs in a project.

func (*Store) FindNodesByFile

func (s *Store) FindNodesByFile(project, filePath string) ([]*Node, error)

FindNodesByFile finds all nodes in a given file.

func (*Store) FindNodesByIDs added in v0.2.0

func (s *Store) FindNodesByIDs(ids []int64) (map[int64]*Node, error)

FindNodesByIDs returns a map of nodeID → *Node for the given IDs.

func (*Store) FindNodesByLabel

func (s *Store) FindNodesByLabel(project, label string) ([]*Node, error)

FindNodesByLabel finds all nodes with a given label in a project.

func (*Store) FindNodesByName

func (s *Store) FindNodesByName(project, name string) ([]*Node, error)

FindNodesByName finds nodes by project and name.

func (*Store) GetFileHashes

func (s *Store) GetFileHashes(project string) (map[string]string, error)

GetFileHashes returns all file hashes for a project.

func (*Store) GetProject

func (s *Store) GetProject(name string) (*Project, error)

GetProject returns a project by name.

func (*Store) GetSchema

func (s *Store) GetSchema(project string) (*SchemaInfo, error)

GetSchema returns graph schema statistics for a project.

func (*Store) InsertEdge

func (s *Store) InsertEdge(e *Edge) (int64, error)

InsertEdge inserts an edge (dedup by source_id, target_id, type).

func (*Store) InsertEdgeBatch added in v0.1.4

func (s *Store) InsertEdgeBatch(edges []*Edge) error

InsertEdgeBatch inserts multiple edges in batched multi-row INSERTs.

func (*Store) ListFilesForProject added in v0.1.0

func (s *Store) ListFilesForProject(project string) ([]string, error)

ListFilesForProject returns all distinct file paths indexed for a project.

func (*Store) ListProjects

func (s *Store) ListProjects() ([]*Project, error)

ListProjects returns all indexed projects.

func (*Store) Search

func (s *Store) Search(params *SearchParams) (*SearchOutput, error)

Search executes a parameterized search query with pagination support.

func (*Store) UpsertFileHash

func (s *Store) UpsertFileHash(project, relPath, sha256 string) error

UpsertFileHash stores a file's content hash.

func (*Store) UpsertFileHashBatch added in v0.1.4

func (s *Store) UpsertFileHashBatch(hashes []FileHash) error

UpsertFileHashBatch inserts or updates multiple file hashes in batched multi-row INSERTs.

func (*Store) UpsertNode

func (s *Store) UpsertNode(n *Node) (int64, error)

UpsertNode inserts or replaces a node (dedup by qualified_name). Note: LastInsertId() can return stale IDs for ON CONFLICT DO UPDATE, causing occasional FK failures in downstream edge inserts. This is accepted for performance — the fallback SELECT only runs when id==0.

func (*Store) UpsertNodeBatch added in v0.1.4

func (s *Store) UpsertNodeBatch(nodes []*Node) (map[string]int64, error)

UpsertNodeBatch inserts or updates multiple nodes in batched multi-row INSERTs. Returns a map of qualifiedName → ID for all upserted nodes.

func (*Store) UpsertProject

func (s *Store) UpsertProject(name, rootPath string) error

UpsertProject creates or updates a project record.

func (*Store) WithTransaction added in v0.1.0

func (s *Store) WithTransaction(fn func(txStore *Store) error) error

WithTransaction executes fn within a single SQLite transaction. The callback receives a transaction-scoped Store — all store methods called on txStore use the transaction. The receiver's q field is never mutated, so concurrent read-only handlers (using s.q == s.db) are unaffected.

type StoreRouter added in v0.2.0

type StoreRouter struct {
	// contains filtered or unexported fields
}

StoreRouter manages per-project SQLite databases. Each project gets its own .db file in the cache directory.

func NewRouter added in v0.2.0

func NewRouter() (*StoreRouter, error)

NewRouter creates a StoreRouter, ensuring the cache directory exists. Runs migration from single-DB layout if needed.

func NewRouterWithDir added in v0.2.0

func NewRouterWithDir(dir string) (*StoreRouter, error)

NewRouterWithDir creates a StoreRouter using a custom directory (for testing). No migration is run.

func (*StoreRouter) AllStores added in v0.2.0

func (r *StoreRouter) AllStores() map[string]*Store

AllStores opens all .db files in the cache dir and returns a name→Store map.

func (*StoreRouter) CloseAll added in v0.2.0

func (r *StoreRouter) CloseAll()

CloseAll closes all open Store connections.

func (*StoreRouter) DeleteProject added in v0.2.0

func (r *StoreRouter) DeleteProject(name string) error

DeleteProject closes the Store connection and removes the .db + WAL/SHM files.

func (*StoreRouter) Dir added in v0.2.0

func (r *StoreRouter) Dir() string

Dir returns the cache directory path.

func (*StoreRouter) ForProject added in v0.2.0

func (r *StoreRouter) ForProject(name string) (*Store, error)

ForProject returns the Store for the given project, opening it lazily.

func (*StoreRouter) HasProject added in v0.2.0

func (r *StoreRouter) HasProject(name string) bool

HasProject checks if a .db file exists for the given project (without opening it).

func (*StoreRouter) ListProjects added in v0.2.0

func (r *StoreRouter) ListProjects() ([]*ProjectInfo, error)

ListProjects scans .db files and queries each for metadata.

type TraverseResult

type TraverseResult struct {
	Root    *Node
	Visited []*NodeHop
	Edges   []EdgeInfo
}

TraverseResult holds BFS traversal results.

type TypeCount

type TypeCount struct {
	Type  string `json:"type"`
	Count int    `json:"count"`
}

TypeCount is a relationship type with its count.

Jump to

Keyboard shortcuts

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