database

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	URL              string
	AuthToken        string
	ProjectsDir      string
	MultiProjectMode bool
	EmbeddingDims    int
	MaxOpenConns     int
	MaxIdleConns     int
	ConnMaxIdleSec   int
	ConnMaxLifeSec   int
	// Embeddings provider hints (optional)
	EmbeddingsProvider string // e.g., "openai", "ollama"
}

Config holds the database configuration

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config from environment variables

type DBManager

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

DBManager handles all database operations

func NewDBManager

func NewDBManager(config *Config) (*DBManager, error)

NewDBManager creates a new database manager

func (*DBManager) AddObservations

func (dm *DBManager) AddObservations(ctx context.Context, projectName string, entityName string, observations []string) error

AddObservations appends observations to an existing entity

func (*DBManager) Close

func (dm *DBManager) Close() error

Close closes all database connections

func (*DBManager) Config

func (dm *DBManager) Config() Config

Config returns a copy of the database configuration

func (*DBManager) CreateEntities

func (dm *DBManager) CreateEntities(ctx context.Context, projectName string, entities []apptype.Entity) error

CreateEntities creates or updates entities with their observations

func (*DBManager) CreateRelations

func (dm *DBManager) CreateRelations(ctx context.Context, projectName string, relations []apptype.Relation) error

CreateRelations creates multiple relations between entities

func (*DBManager) DeleteEntities

func (dm *DBManager) DeleteEntities(ctx context.Context, projectName string, names []string) error

DeleteEntities deletes multiple entities by name within a single transaction

func (*DBManager) DeleteEntity

func (dm *DBManager) DeleteEntity(ctx context.Context, projectName string, name string) error

DeleteEntity deletes an entity and all associated data

func (*DBManager) DeleteObservations

func (dm *DBManager) DeleteObservations(ctx context.Context, projectName string, entityName string, ids []int64, contents []string) (int64, error)

DeleteObservations deletes observations by ids or exact contents for an entity

func (*DBManager) DeleteRelation

func (dm *DBManager) DeleteRelation(ctx context.Context, projectName string, source, target, relationType string) error

DeleteRelation deletes a specific relation

func (*DBManager) DeleteRelations

func (dm *DBManager) DeleteRelations(ctx context.Context, projectName string, tuples []apptype.Relation) error

DeleteRelations deletes multiple relations within a transaction

func (*DBManager) DisableHybridSearch

func (dm *DBManager) DisableHybridSearch()

DisableHybridSearch restores default (built-in) search behavior.

func (*DBManager) EnableHybridSearch

func (dm *DBManager) EnableHybridSearch(textWeight, vectorWeight, rrfK float64)

EnableHybridSearch enables hybrid search strategy with custom weights and k.

func (*DBManager) ExtractVector

func (dm *DBManager) ExtractVector(ctx context.Context, embedding []byte) ([]float32, error)

ExtractVector extracts vector from binary format (F32_BLOB)

func (*DBManager) GetEntities

func (dm *DBManager) GetEntities(ctx context.Context, projectName string, names []string) ([]apptype.Entity, error)

GetEntities retrieves a list of entities by names

func (*DBManager) GetEntity

func (dm *DBManager) GetEntity(ctx context.Context, projectName string, name string) (*apptype.Entity, error)

GetEntity retrieves a single entity by name

func (*DBManager) GetNeighbors

func (dm *DBManager) GetNeighbors(ctx context.Context, projectName string, names []string, direction string, limit int) ([]apptype.Entity, []apptype.Relation, error)

GetNeighbors returns 1-hop neighbors and the connecting relations for given names.

func (*DBManager) GetRecentEntities

func (dm *DBManager) GetRecentEntities(ctx context.Context, projectName string, limit int) ([]apptype.Entity, error)

GetRecentEntities retrieves the most recently created entities up to limit.

func (*DBManager) GetRelations

func (dm *DBManager) GetRelations(ctx context.Context, projectName string, entityNames []string) ([]apptype.Relation, error)

GetRelations returns all relations where either source or target belongs to the provided entity names. This is a convenience wrapper around GetRelationsForEntities.

func (*DBManager) GetRelationsForEntities

func (dm *DBManager) GetRelationsForEntities(ctx context.Context, projectName string, entities []apptype.Entity) ([]apptype.Relation, error)

GetRelationsForEntities returns all relations touching any of the provided entities.

func (*DBManager) PoolStats

func (dm *DBManager) PoolStats() (inUse int, idle int)

PoolStats returns aggregate pool stats across known project DBs.

func (*DBManager) ReadGraph

func (dm *DBManager) ReadGraph(ctx context.Context, projectName string, limit int) ([]apptype.Entity, []apptype.Relation, error)

ReadGraph returns a recent subgraph snapshot with entities and their relations.

func (*DBManager) SearchEntities

func (dm *DBManager) SearchEntities(ctx context.Context, projectName string, query string, limit int, offset int) ([]apptype.Entity, error)

SearchEntities returns entities matched by text search, using FTS5 when available.

func (*DBManager) SearchNodes

func (dm *DBManager) SearchNodes(ctx context.Context, projectName string, query interface{}, limit int, offset int) ([]apptype.Entity, []apptype.Relation, error)

SearchNodes returns entities and relations for a vector or text query.

func (*DBManager) SearchSimilar

func (dm *DBManager) SearchSimilar(ctx context.Context, projectName string, embedding []float32, limit int, offset int) ([]apptype.SearchResult, error)

SearchSimilar returns entities ranked by vector similarity to the provided embedding.

func (*DBManager) SetEmbeddingsProvider

func (dm *DBManager) SetEmbeddingsProvider(p embeddings.Provider)

SetEmbeddingsProvider overrides the embeddings provider (primarily for tests)

func (*DBManager) ShortestPath

func (dm *DBManager) ShortestPath(ctx context.Context, projectName, from, to, direction string) ([]apptype.Entity, []apptype.Relation, error)

ShortestPath finds one shortest path (if any) between two entities and returns the path subgraph.

This function uses the Breadth-First Search (BFS) algorithm to find the shortest path between the 'from' and 'to' entities in the graph. BFS explores the graph level by level, ensuring that the first path found is one of the shortest possible paths.

The returned relations use the RelationType "path" to indicate that these edges are part of the discovered shortest path between the two entities, rather than representing the original relation type in the graph.

Parameters:

  • ctx: context for cancellation and deadlines
  • projectName: the name of the project/graph
  • from: the starting entity name
  • to: the target entity name
  • direction: the direction of traversal ("out", "in", etc.)

Returns:

  • []apptype.Entity: the entities along the shortest path (including endpoints)
  • []apptype.Relation: the relations along the path, with RelationType "path"
  • error: error if any occurred during traversal

func (*DBManager) UpdateEntities

func (dm *DBManager) UpdateEntities(ctx context.Context, projectName string, updates []apptype.UpdateEntitySpec) error

UpdateEntities applies partial updates to entities

func (*DBManager) UpdateRelations

func (dm *DBManager) UpdateRelations(ctx context.Context, projectName string, updates []apptype.UpdateRelationChange) error

UpdateRelations updates relation tuples via delete/insert

func (*DBManager) ValidateProjectAuth

func (dm *DBManager) ValidateProjectAuth(projectName string, providedToken string) error

ValidateProjectAuth enforces per-project authorization in multi-project mode. Token is stored under <ProjectsDir>/<projectName>/.auth_token. If missing, a non-empty provided token will be written as the initial token. Subsequent calls must present the same token. No auth is enforced outside multi-project mode.

func (*DBManager) Walk

func (dm *DBManager) Walk(ctx context.Context, projectName string, seeds []string, maxDepth int, direction string, limit int) ([]apptype.Entity, []apptype.Relation, error)

Walk traverses outward from seed entities up to maxDepth and returns the subgraph.

type SearchStrategy

type SearchStrategy interface {
	Search(ctx context.Context, projectName string, query interface{}, limit int, offset int) ([]apptype.Entity, []apptype.Relation, error)
}

SearchStrategy allows pluggable search implementations (text/vector/hybrid)

Jump to

Keyboard shortcuts

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