sqlite

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sqlite implements the SQLite memory store for Cortex.

It provides CRUD operations for observations with deduplication by normalized_hash, topic key upsert, and soft/hard delete support. The store implements the domain.ObservationRepository interface.

Package sqlite provides concrete implementations of repository interfaces for SQLite-based persistence in Cortex.

This package contains the actual database implementations that bridge the domain models with SQLite storage, implementing all the repository interfaces defined in the domain package.

Package sqlite implements the SQLite memory store for Cortex.

This file provides a stub implementation of the VectorStore when the cortex_vectors build tag is not enabled. All methods return ErrVectorSearchDisabled.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConsolidationGroup added in v1.0.0

type ConsolidationGroup struct {
	TopicKey string
	Count    int
	Latest   string
}

ConsolidationGroup represents a topic key with multiple observations.

type ExportData

type ExportData struct {
	Version      string                `json:"version"`
	ExportedAt   string                `json:"exported_at"`
	Sessions     []*domain.Session     `json:"sessions"`
	Observations []*domain.Observation `json:"observations"`
	Prompts      []*domain.Prompt      `json:"prompts"`
}

ExportData holds all data for sync export.

type MergeResult

type MergeResult struct {
	Canonical           string   `json:"canonical"`
	SourcesMerged       []string `json:"sources_merged"`
	ObservationsUpdated int64    `json:"observations_updated"`
	SessionsUpdated     int64    `json:"sessions_updated"`
}

MergeResult holds the outcome of a project merge operation.

type MetricsRepository

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

MetricsRepository implements the MetricsRepository interface using SQLite.

func NewMetricsRepository

func NewMetricsRepository(db *sql.DB) *MetricsRepository

NewMetricsRepository creates a new metrics repository with the given database connection.

func (*MetricsRepository) CreateMetric

func (r *MetricsRepository) CreateMetric(ctx context.Context, metric *domain.Metrics) error

CreateMetric records a performance metric.

func (*MetricsRepository) GetAggregatedMetrics

func (r *MetricsRepository) GetAggregatedMetrics(ctx context.Context, from, to time.Time) (*domain.AggregatedMetrics, error)

GetAggregatedMetrics gets aggregated metrics for a time range.

func (*MetricsRepository) GetByOperationType

func (r *MetricsRepository) GetByOperationType(ctx context.Context, operationType string, from, to time.Time) ([]*domain.Metrics, error)

GetByOperationType retrieves metrics filtered by operation type.

func (*MetricsRepository) GetTemporalMetrics

func (r *MetricsRepository) GetTemporalMetrics(ctx context.Context, sessionID string, from, to time.Time) ([]*domain.Metrics, error)

GetTemporalMetrics retrieves metrics for a session within a time range.

type QualityMetricsRepository

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

QualityMetricsRepository implements the QualityMetricsRepository interface.

func NewQualityMetricsRepository

func NewQualityMetricsRepository(db *sql.DB) *QualityMetricsRepository

NewQualityMetricsRepository creates a new quality metrics repository.

func (*QualityMetricsRepository) CreateQualityMetric

func (r *QualityMetricsRepository) CreateQualityMetric(ctx context.Context, quality *domain.QualityMetrics) error

CreateQualityMetric records a quality evaluation result.

func (*QualityMetricsRepository) GetBySession

func (r *QualityMetricsRepository) GetBySession(ctx context.Context, sessionID string, limit int) ([]*domain.QualityMetrics, error)

GetBySession retrieves quality metrics for a session.

func (*QualityMetricsRepository) GetByType

func (r *QualityMetricsRepository) GetByType(ctx context.Context, evaluationType string, from, to time.Time) ([]*domain.QualityMetrics, error)

GetByType retrieves quality metrics filtered by evaluation type.

func (*QualityMetricsRepository) GetLatest

func (r *QualityMetricsRepository) GetLatest(ctx context.Context, limit int) ([]*domain.QualityMetrics, error)

GetLatest gets the most recent quality metrics.

type Stats

type Stats struct {
	TotalObservations int            `json:"total_observations"`
	Projects          []string       `json:"projects"`
	ByType            map[string]int `json:"by_type"`
}

Stats holds aggregated statistics about observations.

type Store

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

Store implements the SQLite observation store. It provides CRUD operations with deduplication, topic key upsert, and soft/hard delete support.

func NewStore

func NewStore(db *sql.DB) *Store

NewStore creates a new observation store with the given database connection.

func (*Store) CountAll

func (s *Store) CountAll(ctx context.Context) (int, error)

CountAll counts all non-deleted observations in the system.

func (*Store) CountByRoot

func (s *Store) CountByRoot(ctx context.Context, rootObsID int64) (int, error)

CountByRoot counts distinct observations reachable from a root observation.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, id int64) error

Delete removes an observation by ID (soft delete by default). Returns ErrNotFound if the observation doesn't exist or is already deleted.

func (*Store) ExportAll

func (s *Store) ExportAll(ctx context.Context) (*ExportData, error)

ExportAll exports all sessions, observations, and prompts for sync.

func (*Store) FindConsolidationCandidates added in v1.0.0

func (s *Store) FindConsolidationCandidates(ctx context.Context, project string, minCount int) ([]ConsolidationGroup, error)

FindConsolidationCandidates finds topic keys with multiple observations in a project.

func (*Store) GetByID

func (s *Store) GetByID(ctx context.Context, id int64) (*domain.Observation, error)

GetByID retrieves an observation by its ID. Returns ErrNotFound if the observation doesn't exist or is soft-deleted.

func (*Store) GetBySource

func (s *Store) GetBySource(ctx context.Context, source string, limit int) ([]*domain.Observation, error)

GetBySource retrieves observations filtered by source type.

func (*Store) GetByTopicKey

func (s *Store) GetByTopicKey(ctx context.Context, project, topicKey string) (*domain.Observation, error)

GetByTopicKey retrieves an observation by its topic key within a project. Returns ErrNotFound if no matching observation exists or is soft-deleted.

func (*Store) GetByType

func (s *Store) GetByType(ctx context.Context, obsType string, limit int) ([]*domain.Observation, error)

GetByType retrieves observations filtered by type.

func (*Store) GetSearchFeedbackStats

func (s *Store) GetSearchFeedbackStats(ctx context.Context) (totalEntries int, uniqueQueries int, err error)

GetSearchFeedbackStats returns basic stats about search feedback data.

func (*Store) GetSyncedChunks

func (s *Store) GetSyncedChunks(ctx context.Context) (map[string]bool, error)

GetSyncedChunks returns a set of chunk IDs that have been imported/exported.

func (*Store) HardDelete

func (s *Store) HardDelete(ctx context.Context, id int64) error

HardDelete permanently removes an observation from the database. Returns ErrNotFound if the observation doesn't exist.

func (*Store) ImportData

func (s *Store) ImportData(ctx context.Context, data *ExportData) (*SyncImportResult, error)

ImportData imports sessions, observations and prompts from an export. Sessions are skipped if they already exist (by ID). Observations and prompts get new auto-increment IDs.

func (*Store) List

func (s *Store) List(ctx context.Context, filter domain.ObservationFilter) ([]*domain.Observation, error)

List retrieves observations based on filter criteria. An empty filter returns all observations up to the default limit (20).

func (*Store) ListArchivable

func (s *Store) ListArchivable(ctx context.Context, cutoff time.Time, minScore float64, limit int) ([]*domain.Observation, error)

ListArchivable retrieves observations older than cutoff with score below minScore. Uses a JOIN with importance_scores to avoid N+1 queries during archival.

func (*Store) ListByTopicKey added in v1.0.0

func (s *Store) ListByTopicKey(ctx context.Context, project, topicKey string) ([]*domain.Observation, error)

ListByTopicKey retrieves all observations for a project with the given topic key.

func (*Store) MergeProjects

func (s *Store) MergeProjects(ctx context.Context, sources []string, canonical string) (*MergeResult, error)

MergeProjects moves all observations and sessions from source projects into a canonical project name. Sources that normalize to the canonical name are silently skipped.

func (*Store) OrphanObservations added in v1.0.0

func (s *Store) OrphanObservations(ctx context.Context, project string, limit int) ([]*domain.Observation, error)

OrphanObservations returns observations with no graph edges.

func (*Store) RecordSearchFeedback

func (s *Store) RecordSearchFeedback(ctx context.Context, query string, observationID int64, rankPosition int) error

RecordSearchFeedback logs an implicit signal: the user accessed an observation after performing a search. This data enables Learning-to-Rank model training.

func (*Store) RecordSyncedChunk

func (s *Store) RecordSyncedChunk(ctx context.Context, chunkID string) error

RecordSyncedChunk marks a chunk as imported/exported (idempotent).

func (*Store) Restore added in v1.2.0

func (s *Store) Restore(ctx context.Context, id int64) error

Restore restores a soft-deleted observation (alias for Unarchive).

func (*Store) Save

func (s *Store) Save(ctx context.Context, obs *domain.Observation) error

Save creates a new observation or updates an existing one if topic_key matches. It implements deduplication by normalized_hash within a configurable window.

Business Rules:

  • If topic_key is provided and an observation with the same topic_key exists in the same project, update it instead of creating a new one.
  • If normalized_hash matches an existing observation within the deduplication window, increment duplicate_count instead of creating a new one.
  • Sets created_at and updated_at timestamps.
  • Normalizes scope to "project" or "personal".

func (*Store) SoftDelete

func (s *Store) SoftDelete(ctx context.Context, id int64) error

SoftDelete marks an observation as deleted without removing it from the database. Returns ErrNotFound if the observation doesn't exist or is already deleted.

func (*Store) StaleObservations added in v1.0.0

func (s *Store) StaleObservations(ctx context.Context, project string, minScore float64, daysSinceAccess int) ([]*domain.Observation, error)

StaleObservations returns observations with high importance score but no recent access.

func (*Store) Stats

func (s *Store) Stats(ctx context.Context) (*Stats, error)

Stats returns aggregated statistics about observations.

func (*Store) Unarchive added in v1.2.0

func (s *Store) Unarchive(ctx context.Context, id int64) error

Unarchive restores a soft-deleted observation by clearing its deleted_at field. Returns ErrNotFound if the observation doesn't exist or is not archived.

func (*Store) Update

func (s *Store) Update(ctx context.Context, obs *domain.Observation) error

Update modifies an existing observation. Returns ErrNotFound if the observation doesn't exist or is soft-deleted.

type SyncImportResult

type SyncImportResult struct {
	SessionsImported     int `json:"sessions_imported"`
	ObservationsImported int `json:"observations_imported"`
	PromptsImported      int `json:"prompts_imported"`
}

SyncImportResult holds the outcome of a sync import.

type TemporalSnapshotRepository

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

TemporalSnapshotRepository implements the TemporalSnapshotRepository interface.

func NewTemporalSnapshotRepository

func NewTemporalSnapshotRepository(db *sql.DB) *TemporalSnapshotRepository

NewTemporalSnapshotRepository creates a new temporal snapshot repository.

func (*TemporalSnapshotRepository) CreateSnapshot

func (r *TemporalSnapshotRepository) CreateSnapshot(ctx context.Context, snapshot *domain.TemporalSnapshot) error

CreateSnapshot creates a point-in-time snapshot of the knowledge graph.

func (*TemporalSnapshotRepository) GetByID

GetByID retrieves a snapshot by its ID.

func (*TemporalSnapshotRepository) GetByRootObservation

func (r *TemporalSnapshotRepository) GetByRootObservation(ctx context.Context, rootObsID int64) ([]*domain.TemporalSnapshot, error)

GetByRootObservation retrieves snapshots for a root observation.

func (*TemporalSnapshotRepository) GetBySnapshotKey

func (r *TemporalSnapshotRepository) GetBySnapshotKey(ctx context.Context, snapshotKey string) ([]*domain.TemporalSnapshot, error)

GetBySnapshotKey retrieves snapshots by their key.

func (*TemporalSnapshotRepository) GetSnapshotsInRange

func (r *TemporalSnapshotRepository) GetSnapshotsInRange(ctx context.Context, from, to time.Time) ([]*domain.TemporalSnapshot, error)

GetSnapshotsInRange retrieves snapshots within a time range.

type VectorStore

type VectorStore struct{}

VectorStore implements the vector similarity search store. This is the stub implementation used when cortex_vectors build tag is disabled.

func NewVectorStore

func NewVectorStore(_ *sql.DB) *VectorStore

NewVectorStore creates a new vector store stub. When cortex_vectors is not enabled, this returns a stub that always returns ErrVectorSearchDisabled.

func (*VectorStore) DeleteEmbedding

func (s *VectorStore) DeleteEmbedding(ctx context.Context, observationID int64) error

DeleteEmbedding is disabled in stub mode.

func (*VectorStore) GetEmbedding

func (s *VectorStore) GetEmbedding(ctx context.Context, observationID int64) ([]float32, string, error)

GetEmbedding is disabled in stub mode.

func (*VectorStore) IsAvailable

func (s *VectorStore) IsAvailable() bool

IsAvailable returns false in stub mode.

func (*VectorStore) SearchByVector

SearchByVector is disabled in stub mode.

func (*VectorStore) StoreEmbedding

func (s *VectorStore) StoreEmbedding(ctx context.Context, observationID int64, embedding []float32, model string) error

StoreEmbedding is disabled in stub mode.

Jump to

Keyboard shortcuts

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