@@ -0,0 +1,313 @@
1+// Package store provides a SQLite-backed cache for web fetch and search results.
2+package store
3+
4+import (
5+ "database/sql"
6+ "fmt"
7+ "strings"
8+ "sync"
9+ "time"
10+
11+ "github.com/rs/zerolog/log"
12+ _ "modernc.org/sqlite" // register sqlite driver
13+)
14+
15+const schema = `
16+CREATE TABLE IF NOT EXISTS fetch_cache (
17+ url TEXT PRIMARY KEY,
18+ result TEXT NOT NULL,
19+ created INTEGER NOT NULL
20+);
21+
22+CREATE TABLE IF NOT EXISTS search_cache (
23+ query TEXT PRIMARY KEY,
24+ result TEXT NOT NULL,
25+ created INTEGER NOT NULL
26+);
27+
28+CREATE INDEX IF NOT EXISTS idx_fetch_created ON fetch_cache(created);
29+CREATE INDEX IF NOT EXISTS idx_search_created ON search_cache(created);
30+`
31+
32+// Cache is a SQLite-backed cache for web results.
33+type Cache struct {
34+ mu sync.Mutex
35+ db *sql.DB
36+ ttl time.Duration
37+}
38+
39+// Open creates or opens a cache database at the given path.
40+// ttl controls how long entries remain fresh.
41+func Open(dbPath string, ttl time.Duration) (*Cache, error) {
42+ db, err := sql.Open("sqlite", dbPath)
43+ if err != nil {
44+ return nil, fmt.Errorf("open cache db: %w", err)
45+ }
46+
47+ // SQLite pragmas for performance.
48+ for _, pragma := range []string{
49+ "PRAGMA journal_mode = WAL",
50+ "PRAGMA synchronous = NORMAL",
51+ "PRAGMA busy_timeout = 5000",
52+ } {
53+ if _, err := db.Exec(pragma); err != nil {
54+ db.Close()
55+ return nil, fmt.Errorf("pragma %q: %w", pragma, err)
56+ }
57+ }
58+
59+ // Migrate: drop old search_cache with keywords column and recreate.
60+ // This is a cache, so losing data is acceptable.
61+ if hasColumn(db, "search_cache", "keywords") {
62+ db.Exec("DROP TABLE search_cache") //nolint:errcheck // best-effort migration
63+ }
64+
65+ if _, err := db.Exec(schema); err != nil {
66+ db.Close()
67+ return nil, fmt.Errorf("create schema: %w", err)
68+ }
69+
70+ c := &Cache{db: db, ttl: ttl}
71+ c.purgeStale()
72+ return c, nil
73+}
74+
75+// Close closes the database.
76+func (c *Cache) Close() error {
77+ if c == nil {
78+ return nil
79+ }
80+ return c.db.Close()
81+}
82+
83+// --- Fetch cache ---
84+
85+// GetFetch returns a cached fetch result for the given URL, or "" if miss/stale.
86+// Safe to call on a nil receiver (returns miss).
87+func (c *Cache) GetFetch(url string) (string, bool) {
88+ if c == nil {
89+ return "", false
90+ }
91+ c.mu.Lock()
92+ defer c.mu.Unlock()
93+
94+ cutoff := time.Now().Add(-c.ttl).Unix()
95+ var result string
96+ err := c.db.QueryRow(
97+ "SELECT result FROM fetch_cache WHERE url = ? AND created > ?",
98+ url, cutoff,
99+ ).Scan(&result)
100+ if err != nil {
101+ return "", false
102+ }
103+ return result, true
104+}
105+
106+// SetFetch stores a fetch result. No-op on nil receiver.
107+func (c *Cache) SetFetch(url, result string) {
108+ if c == nil {
109+ return
110+ }
111+ c.mu.Lock()
112+ defer c.mu.Unlock()
113+
114+ _, err := c.db.Exec(
115+ "INSERT OR REPLACE INTO fetch_cache (url, result, created) VALUES (?, ?, ?)",
116+ url, result, time.Now().Unix(),
117+ )
118+ if err != nil {
119+ log.Warn().Err(err).Str("url", url).Msg("failed to cache fetch result")
120+ }
121+}
122+
123+// --- Search cache ---
124+
125+// GetSearch returns a cached search result for the exact query, or "" if miss/stale.
126+// Safe to call on a nil receiver (returns miss).
127+func (c *Cache) GetSearch(query string) (string, bool) {
128+ if c == nil {
129+ return "", false
130+ }
131+ c.mu.Lock()
132+ defer c.mu.Unlock()
133+
134+ normalized := normalizeQuery(query)
135+ cutoff := time.Now().Add(-c.ttl).Unix()
136+ var result string
137+ err := c.db.QueryRow(
138+ "SELECT result FROM search_cache WHERE query = ? AND created > ?",
139+ normalized, cutoff,
140+ ).Scan(&result)
141+ if err != nil {
142+ return "", false
143+ }
144+ return result, true
145+}
146+
147+// SearchCachedContent looks for a cached result whose text content contains
148+// enough of the query keywords. This searches the actual cached results, not
149+// the original queries — so if the answer already exists in any cached result,
150+// we return it instead of making a new API call.
151+func (c *Cache) SearchCachedContent(query string) (string, bool) {
152+ if c == nil {
153+ return "", false
154+ }
155+ c.mu.Lock()
156+ defer c.mu.Unlock()
157+
158+ queryKw := tokenize(query)
159+ if len(queryKw) < 2 {
160+ return "", false
161+ }
162+
163+ cutoff := time.Now().Add(-c.ttl).Unix()
164+ rows, err := c.db.Query(
165+ "SELECT result FROM search_cache WHERE created > ?",
166+ cutoff,
167+ )
168+ if err != nil {
169+ return "", false
170+ }
171+ defer rows.Close()
172+
173+ var bestResult string
174+ var bestScore float64
175+ var bestHits int
176+
177+ for rows.Next() {
178+ var result string
179+ if err := rows.Scan(&result); err != nil {
180+ continue
181+ }
182+ resultLower := strings.ToLower(result)
183+ score, hits := contentOverlap(queryKw, resultLower)
184+ if score > bestScore {
185+ bestScore = score
186+ bestHits = hits
187+ bestResult = result
188+ }
189+ }
190+
191+ // Require at least 75% of query keywords found in content AND at least 3 hits.
192+ if bestScore >= 0.75 && bestHits >= 3 {
193+ return bestResult, true
194+ }
195+ return "", false
196+}
197+
198+// SetSearch stores a search result. No-op on nil receiver.
199+func (c *Cache) SetSearch(query, result string) {
200+ if c == nil {
201+ return
202+ }
203+ c.mu.Lock()
204+ defer c.mu.Unlock()
205+
206+ normalized := normalizeQuery(query)
207+ _, err := c.db.Exec(
208+ "INSERT OR REPLACE INTO search_cache (query, result, created) VALUES (?, ?, ?)",
209+ normalized, result, time.Now().Unix(),
210+ )
211+ if err != nil {
212+ log.Warn().Err(err).Str("query", query).Msg("failed to cache search result")
213+ }
214+}
215+
216+// hasColumn checks if a table has a specific column.
217+func hasColumn(db *sql.DB, table, column string) bool {
218+ rows, err := db.Query(fmt.Sprintf("PRAGMA table_info(%s)", table)) //nolint:gosec // table name is hardcoded by caller
219+ if err != nil {
220+ return false
221+ }
222+ defer rows.Close()
223+ for rows.Next() {
224+ var cid int
225+ var name, typ string
226+ var notNull, pk int
227+ var dflt sql.NullString
228+ if err := rows.Scan(&cid, &name, &typ, ¬Null, &dflt, &pk); err != nil {
229+ continue
230+ }
231+ if name == column {
232+ return true
233+ }
234+ }
235+ return false
236+}
237+
238+// --- Helpers ---
239+
240+// purgeStale removes entries older than the TTL.
241+func (c *Cache) purgeStale() {
242+ cutoff := time.Now().Add(-c.ttl).Unix()
243+ for _, table := range []string{"fetch_cache", "search_cache"} {
244+ res, err := c.db.Exec(
245+ fmt.Sprintf("DELETE FROM %s WHERE created <= ?", table), //nolint:gosec // table name is hardcoded
246+ cutoff,
247+ )
248+ if err != nil {
249+ log.Warn().Err(err).Str("table", table).Msg("failed to purge stale cache")
250+ continue
251+ }
252+ if n, _ := res.RowsAffected(); n > 0 {
253+ log.Info().Int64("deleted", n).Str("table", table).Msg("purged stale cache entries")
254+ }
255+ }
256+}
257+
258+// normalizeQuery lowercases and trims a query string.
259+func normalizeQuery(q string) string {
260+ return strings.ToLower(strings.TrimSpace(q))
261+}
262+
263+// stopWords are common words filtered out during tokenization.
264+var stopWords = map[string]bool{
265+ "a": true, "an": true, "the": true, "is": true, "are": true,
266+ "was": true, "were": true, "be": true, "been": true, "being": true,
267+ "have": true, "has": true, "had": true, "do": true, "does": true,
268+ "did": true, "will": true, "would": true, "could": true, "should": true,
269+ "may": true, "might": true, "shall": true, "can": true,
270+ "for": true, "and": true, "but": true, "or": true, "nor": true,
271+ "not": true, "so": true, "yet": true, "to": true, "of": true,
272+ "in": true, "on": true, "at": true, "by": true, "with": true,
273+ "from": true, "as": true, "into": true, "about": true, "between": true,
274+ "through": true, "during": true, "before": true, "after": true,
275+ "above": true, "below": true, "up": true, "down": true, "out": true,
276+ "off": true, "over": true, "under": true, "again": true, "then": true,
277+ "once": true, "here": true, "there": true, "when": true, "where": true,
278+ "why": true, "how": true, "what": true, "which": true, "who": true,
279+ "whom": true, "this": true, "that": true, "these": true, "those": true,
280+ "i": true, "me": true, "my": true, "we": true, "our": true,
281+ "you": true, "your": true, "he": true, "him": true, "his": true,
282+ "she": true, "her": true, "it": true, "its": true, "they": true,
283+ "them": true, "their": true,
284+}
285+
286+// tokenize splits a query into lowercase keywords, filtering stop words and short tokens.
287+func tokenize(query string) []string {
288+ words := strings.Fields(strings.ToLower(strings.TrimSpace(query)))
289+ var out []string
290+ for _, w := range words {
291+ w = strings.Trim(w, ".,;:!?\"'()-[]{}") // strip punctuation
292+ if len(w) < 2 || stopWords[w] {
293+ continue
294+ }
295+ out = append(out, w)
296+ }
297+ return out
298+}
299+
300+// contentOverlap checks how many of the query keywords appear anywhere in the
301+// lowercased result text. Returns fraction and count.
302+func contentOverlap(queryKw []string, resultLower string) (float64, int) {
303+ if len(queryKw) == 0 {
304+ return 0, 0
305+ }
306+ hits := 0
307+ for _, kw := range queryKw {
308+ if strings.Contains(resultLower, kw) {
309+ hits++
310+ }
311+ }
312+ return float64(hits) / float64(len(queryKw)), hits
313+}