Artifact fc7694ca546fdfab8184e5cb0e7276c7cd00cd264b0aaea1f2e791c26fb6515d:
- File
internal/search/index.go
— part of check-in
[f5744359d3]
at
2026-04-19 16:30:54
on branch trunk
— Initial open source release
libfossil is a pure-Go implementation of the Fossil SCM, reading and writing the same .fossil SQLite repository format as upstream Fossil.
This is the first public release of code that previously lived in a private repository. The module path is github.com/danmestas/libfossil and the binary is named libfossil to avoid $PATH collisions with upstream fossil.
Included: - Repo lifecycle (create, clone), checkout/checkin, timeline, merge, diff, annotate, shun, tag, branch, bisect, stash, undo/redo - Sync-over-HTTP protocol (client and handler) - Two SQLite drivers: modernc (pure Go, default) and ncruces (wasm-capable) - Observer interfaces (SyncObserver, CheckoutObserver) with an optional OpenTelemetry implementation in observer/otel (separate submodule) - Deterministic simulation testing harness with BUGGIFY fault injection - Kong-based embeddable CLI under cli/ and a standalone binary under cmd/libfossil/ (user: [email protected], size: 2955) [annotate] [blame] [check-ins using]
package search import ( "bytes" "fmt" "strconv" libfossil "github.com/danmestas/libfossil/internal/fsltype" "github.com/danmestas/libfossil/internal/blob" "github.com/danmestas/libfossil/internal/content" "github.com/danmestas/libfossil/db" "github.com/danmestas/libfossil/internal/manifest" "github.com/danmestas/libfossil/internal/repo" ) // maxBinaryProbe is the number of bytes checked for null bytes to detect binary files. const maxBinaryProbe = 8192 // isBinary returns true if data contains a null byte in the first maxBinaryProbe bytes. func isBinary(data []byte) bool { if data == nil { panic("isBinary: nil data") } probe := data if len(probe) > maxBinaryProbe { probe = probe[:maxBinaryProbe] } return bytes.ContainsRune(probe, 0) } // RebuildIndex walks the trunk tip manifest, expands blob content, // skips binaries and phantoms, and populates fts_content. // No-ops if already current. // // Panics if idx is nil (TigerStyle precondition). func (idx *Index) RebuildIndex() error { if idx == nil { panic("search.RebuildIndex: nil *Index") } d := idx.repo.DB() tip, err := trunkTip(d) if err != nil { return fmt.Errorf("search.RebuildIndex: %w", err) } if tip == 0 { return nil // empty repo, nothing to index } current, err := indexedRID(d) if err != nil { return fmt.Errorf("search.RebuildIndex: %w", err) } if tip == current { return nil // already up to date } if err := rebuildPopulate(idx.repo, d, tip); err != nil { return err } // Postcondition: verify the meta update took hold. stored, err := indexedRID(d) if err != nil { return fmt.Errorf("search.RebuildIndex: verify meta: %w", err) } if stored != tip { panic("search.RebuildIndex: postcondition: indexed_rid != tip after update") } return nil } // rebuildPopulate clears fts_content, walks the manifest at tip, // expands each text blob, and inserts into the FTS index. func rebuildPopulate(r *repo.Repo, d db.Querier, tip libfossil.FslID) error { if _, err := d.Exec("DELETE FROM fts_content"); err != nil { return fmt.Errorf("search.RebuildIndex: clear: %w", err) } files, err := manifest.ListFiles(r, tip) if err != nil { return fmt.Errorf("search.RebuildIndex: list files: %w", err) } for _, f := range files { rid, ok := blob.Exists(d, f.UUID) if !ok { continue // phantom — blob not yet received } data, err := content.Expand(d, rid) if err != nil { continue // phantom or corrupt — skip } if isBinary(data) { continue } if _, err := d.Exec( "INSERT INTO fts_content(path, content) VALUES(?, ?)", f.Name, string(data), ); err != nil { return fmt.Errorf("search.RebuildIndex: insert %s: %w", f.Name, err) } } if _, err := d.Exec( "INSERT OR REPLACE INTO fts_meta(key, value) VALUES('indexed_rid', ?)", strconv.FormatInt(int64(tip), 10), ); err != nil { return fmt.Errorf("search.RebuildIndex: update meta: %w", err) } return nil }