ftsengine

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MemoryDBBaseDir   = ":memory:"
	ColNameExternalID = "externalid"
	ColNameRowID      = "rowid"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	// SQL identifier.
	Name string `json:"name"`
	// Stored but not tokenised.
	Unindexed bool `json:"unindexed"`
	// Bm25 weight (0 is treated as 1).
	Weight float64 `json:"weight"`
}

Column declares one FTS5 column.

type Config

type Config struct {
	BaseDir    string   `json:"baseDir"`
	DBFileName string   `json:"dbFileName"`
	Table      string   `json:"table"`
	Columns    []Column `json:"columns"`
}

type Engine

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

func NewEngine

func NewEngine(cfg Config, opts ...Option) (*Engine, error)

func (*Engine) BatchDelete

func (e *Engine) BatchDelete(ctx context.Context, ids []string) error

func (*Engine) BatchList

func (e *Engine) BatchList(
	ctx context.Context,
	compareColumn string,
	wantedCols []string,
	pageToken string,
	pageSize int,
) (rows []ListResult, nextToken string, err error)

BatchList pages over the whole table ordered by `compareColumn` + rowid. If compareColumn == "" it falls back to ordering by rowid only (fast path). WantedCols limits the data that is returned to the caller. The slice must be a subset of cfg.Columns. Nil / empty means "all".

Returns rows, an opaque nextToken ("" == no more rows) and an error.

func (*Engine) BatchUpsert

func (e *Engine) BatchUpsert(
	ctx context.Context,
	docs map[string]map[string]string,
) error

BatchUpsert writes / updates all docs inside ONE transaction. The map key is the externalID, the value is the column map.

func (*Engine) Close

func (e *Engine) Close() error

func (*Engine) Delete

func (e *Engine) Delete(ctx context.Context, id string) error

func (*Engine) IsEmpty

func (e *Engine) IsEmpty(ctx context.Context) (bool, error)

func (*Engine) Search

func (e *Engine) Search(
	ctx context.Context,
	query string,
	pageToken string,
	pageSize int,
) (hits []SearchResult, nextToken string, err error)

Search executes an FTS5 query with pagination. If more results exist, an opaque token is returned for the next page. The tokens are bound to the query string. The raw query string is cleaned to an OR-joined token list:

  • Non-alphanumeric characters are ignored.
  • Single-letter non-digit tokens are dropped.
  • Duplicate tokens are removed.

func (*Engine) Upsert

func (e *Engine) Upsert(ctx context.Context, id string, vals map[string]string) error

Upsert inserts a new document, or replaces the existing one whose string id is present. The logic works with every SQLite ≥ 3.9 because it uses INSERT and INSERT OR REPLACE, both supported by FTS5. This is not multi process safe as this is serialized at application level.

type GetPrevCmp

type GetPrevCmp func(id string) string

GetPrevCmp allows producers to query the compareColumn value that is currently stored for a specific ID ("" == not indexed yet).

type Iterate

type Iterate func(getPrev GetPrevCmp, emit func(SyncDecision) error) error

Iterate is the generic producer contract. GetPrev lets the producer look at the current compareColumn value. Emit(dec) must be invoked exactly once for every document that belongs to this dataset.

type ListResult

type ListResult struct {
	// String id stored in the ColNameExternalID column.
	ID     string
	Values map[string]string
}

ListResult is returned by BatchList().

type Option

type Option func(*Engine)

Option is a functional option for configuring the FTSEngine.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger adds a logger to use for the fts engine.

type ProcessFile

type ProcessFile func(
	ctx context.Context,
	baseDir, fullPath string,
	getPrev GetPrevCmp,
) (SyncDecision, error)

ProcessFile is the directory-walker callback.

type SearchResult

type SearchResult struct {
	// String id stored in the ColNameExternalID column.
	ID string
	// Bm25.
	Score float64
}

type SyncDecision

type SyncDecision struct {
	// External identifier (rowid in the virtual table).
	// Must be non-empty f Skip == false.
	ID string
	// Value for compareColumn (mtime / hash / ...). Ignored when Unchanged.
	CmpOut string
	// Column -> text map for FTS. Ignored when Unchanged.
	Vals map[string]string
	// The row is already up-to-date, nothing to do.
	Unchanged bool
	// Ignore this document entirely (also triggers delete if it existed).
	Skip bool
}

type SyncStats

type SyncStats struct {
	TimeTaken time.Duration
	Processed int
	Upserted  int
	Unchanged int
	Skipped   int
	Deleted   int
}

func SyncDirToFTS

func SyncDirToFTS(
	ctx context.Context,
	engine *Engine,
	baseDir string,
	compareColumn string,
	batchSize int,
	processFile ProcessFile,
) (stats *SyncStats, err error)

func SyncIterToFTS

func SyncIterToFTS(
	ctx context.Context,
	engine *Engine,
	compareColumn string,
	batchSize int,
	iter Iterate,
	belongs func(id string) bool,
) (stats *SyncStats, err error)

SyncIterToFTS. Belongs(id) must return true for all rows owned by this producer so that vanished rows can be deleted.

Jump to

Keyboard shortcuts

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