| 1 | package historycatalog |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | ) |
| 7 | |
| 8 | type searchViewKey struct{} |
| 9 | type searchView struct { |
| 10 | owner *Catalog |
| 11 | tx *sql.Tx |
| 12 | } |
| 13 | type searchReader interface { |
| 14 | QueryContext(context.Context, string, ...any) (*sql.Rows, error) |
| 15 | } |
| 16 | |
| 17 | func (c *Catalog) searchDB(ctx context.Context) searchReader { |
| 18 | if v, _ := ctx.Value(searchViewKey{}).(*searchView); v != nil && v.owner == c { |
| 19 | return v.tx |
| 20 | } |
| 21 | return c.db |
| 22 | } |
| 23 | |
| 24 | // CaptureSearch freezes ranking against one FTS read view. The visitor must |
| 25 | // only copy candidates; source file reads belong after this transaction. |
| 26 | func (c *Catalog) CaptureSearch(ctx context.Context, req SearchRequest, visit func(Candidate) error) error { |
| 27 | tx, err := c.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true}) |
| 28 | if err != nil { |
| 29 | return err |
| 30 | } |
| 31 | defer func() { _ = tx.Rollback() }() |
| 32 | ctx = context.WithValue(ctx, searchViewKey{}, &searchView{c, tx}) |
| 33 | req.After = nil |
| 34 | return c.searchCandidates(ctx, req, true, visit) |
| 35 | } |
| 36 |