File size: 9,956 Bytes
34dcea4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""
database.sqlite_db β€” Production SQLite metadata store with FTS5 full-text search.

Features:
  - WAL journal mode for concurrent read/write
  - FTS5 virtual table for keyword search
  - Hash-based deduplication (UNIQUE constraint on hash column)
  - Time-range queries via indexed timestamp column
  - Tag filtering
  - Batch insert via executemany
  - Preserves ordered retrieval by maintaining original ID sequence
"""

import sqlite3
import sys
from pathlib import Path
from typing import Dict, List, Optional, Set

# Import from root config
sys.path.insert(0, str(Path(__file__).parent.parent))
from config import SQLITE_DB_PATH


class MemoryDB:
    """
    SQLite-backed metadata store for memory chunks.
    Each chunk gets a monotonically increasing integer ID that maps
    exactly to its FAISS vector index position.
    """

    def __init__(self, db_path: Optional[Path] = None, readonly: bool = False):
        path = str(db_path or SQLITE_DB_PATH)
        if readonly:
            # Read-only URI connection β€” safe for concurrent MCP server reads
            uri = f"file:{path}?mode=ro"
            self.conn = sqlite3.connect(uri, uri=True, check_same_thread=False)
        else:
            self.conn = sqlite3.connect(path, check_same_thread=False)
            # WAL mode: allows concurrent readers while writing
            self.conn.execute("PRAGMA journal_mode=WAL;")
            self.conn.execute("PRAGMA synchronous=NORMAL;")

        self.conn.row_factory = sqlite3.Row
        self._init_tables()

    def _init_tables(self):
        """Create core tables and FTS5 index if they don't exist."""
        with self.conn:
            # Core metadata table
            self.conn.execute("""
                CREATE TABLE IF NOT EXISTS memories (
                    id        INTEGER PRIMARY KEY AUTOINCREMENT,
                    hash      TEXT    UNIQUE NOT NULL,
                    text      TEXT    NOT NULL,
                    source    TEXT    NOT NULL DEFAULT '',
                    timestamp TEXT    NOT NULL DEFAULT '',
                    tags      TEXT    NOT NULL DEFAULT '',
                    created   TEXT    NOT NULL DEFAULT (datetime('now'))
                )
            """)

            # Indices for fast lookups
            self.conn.execute(
                "CREATE INDEX IF NOT EXISTS idx_hash ON memories(hash);"
            )
            self.conn.execute(
                "CREATE INDEX IF NOT EXISTS idx_timestamp ON memories(timestamp);"
            )
            self.conn.execute(
                "CREATE INDEX IF NOT EXISTS idx_tags ON memories(tags);"
            )

            # FTS5 virtual table for full-text keyword search
            # content= makes it an external-content FTS table (no data duplication)
            self.conn.execute("""
                CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts
                USING fts5(text, source, tags, content=memories, content_rowid=id)
            """)

            # Triggers to keep FTS5 in sync with the main table
            self.conn.executescript("""
                CREATE TRIGGER IF NOT EXISTS memories_ai AFTER INSERT ON memories BEGIN
                    INSERT INTO memories_fts(rowid, text, source, tags)
                    VALUES (new.id, new.text, new.source, new.tags);
                END;

                CREATE TRIGGER IF NOT EXISTS memories_ad AFTER DELETE ON memories BEGIN
                    INSERT INTO memories_fts(memories_fts, rowid, text, source, tags)
                    VALUES ('delete', old.id, old.text, old.source, old.tags);
                END;

                CREATE TRIGGER IF NOT EXISTS memories_au AFTER UPDATE ON memories BEGIN
                    INSERT INTO memories_fts(memories_fts, rowid, text, source, tags)
                    VALUES ('delete', old.id, old.text, old.source, old.tags);
                    INSERT INTO memories_fts(rowid, text, source, tags)
                    VALUES (new.id, new.text, new.source, new.tags);
                END;
            """)

    # ─────────────────────────────────────────────
    # Deduplication
    # ─────────────────────────────────────────────

    def get_existing_hashes(self) -> Set[str]:
        """
        Fetch all known content hashes.
        Used by update.py to skip already-processed chunks.
        """
        cur = self.conn.execute("SELECT hash FROM memories")
        return {row["hash"] for row in cur.fetchall()}

    # ─────────────────────────────────────────────
    # Batch Insert
    # ─────────────────────────────────────────────

    def insert_memories(self, data: List[Dict]) -> List[int]:
        """
        Insert a batch of chunks. Returns list of SQLite row IDs
        that map exactly to FAISS vector positions.

        Uses INSERT OR IGNORE to safely skip duplicates within
        the same batch.
        """
        ids: List[int] = []
        with self.conn:
            for item in data:
                cur = self.conn.execute(
                    """
                    INSERT OR IGNORE INTO memories (hash, text, source, timestamp, tags)
                    VALUES (?, ?, ?, ?, ?)
                    """,
                    (
                        item.get("hash", ""),
                        item.get("text", ""),
                        item.get("source", ""),
                        item.get("timestamp", ""),
                        item.get("tags", ""),
                    ),
                )

                if cur.lastrowid and cur.rowcount > 0:
                    ids.append(cur.lastrowid)
                else:
                    # Retrieve existing ID for this hash (it was a duplicate)
                    existing = self.conn.execute(
                        "SELECT id FROM memories WHERE hash=?",
                        (item["hash"],),
                    ).fetchone()
                    if existing:
                        ids.append(existing["id"])

        return ids

    # ─────────────────────────────────────────────
    # Retrieval
    # ─────────────────────────────────────────────

    def get_memories_by_ids(self, ids: List[int]) -> List[Dict]:
        """
        Retrieve full metadata for a list of FAISS-matched IDs.
        Returns results in the SAME ORDER as the input IDs.
        """
        if not ids:
            return []

        placeholders = ",".join(["?"] * len(ids))
        query = f"SELECT * FROM memories WHERE id IN ({placeholders})"
        cur = self.conn.execute(query, ids)

        row_dict = {row["id"]: dict(row) for row in cur.fetchall()}
        return [row_dict[i] for i in ids if i in row_dict]

    def search_by_time_range(
        self, start_iso: str, end_iso: str, limit: int = 100
    ) -> List[Dict]:
        """
        Retrieve memories within a time range (ISO 8601 strings).
        """
        cur = self.conn.execute(
            """
            SELECT id FROM memories
            WHERE timestamp >= ? AND timestamp <= ?
            ORDER BY timestamp DESC
            LIMIT ?
            """,
            (start_iso, end_iso, limit),
        )
        return [row["id"] for row in cur.fetchall()]

    def search_by_tags(self, tag: str, limit: int = 100) -> List[int]:
        """Return IDs of memories matching a tag substring."""
        cur = self.conn.execute(
            "SELECT id FROM memories WHERE tags LIKE ? LIMIT ?",
            (f"%{tag}%", limit),
        )
        return [row["id"] for row in cur.fetchall()]

    def keyword_search(self, query: str, limit: int = 20) -> List[int]:
        """
        Full-text keyword search via FTS5.
        Returns matching memory IDs ranked by BM25 relevance.
        """
        try:
            cur = self.conn.execute(
                """
                SELECT rowid FROM memories_fts
                WHERE memories_fts MATCH ?
                ORDER BY rank
                LIMIT ?
                """,
                (query, limit),
            )
            return [row["rowid"] for row in cur.fetchall()]
        except Exception:
            # FTS match syntax can fail on certain special characters
            return []

    # ─────────────────────────────────────────────
    # Stats
    # ─────────────────────────────────────────────

    def count(self) -> int:
        """Total number of stored memory chunks."""
        return self.conn.execute("SELECT COUNT(*) FROM memories").fetchone()[0]

    def get_stats(self) -> Dict:
        """Return diagnostic statistics."""
        row = self.conn.execute(
            """
            SELECT
                COUNT(*) as total,
                MIN(timestamp) as earliest,
                MAX(timestamp) as latest,
                COUNT(DISTINCT source) as sources
            FROM memories
            """
        ).fetchone()
        return dict(row) if row else {}

    # ─────────────────────────────────────────────
    # Lifecycle
    # ─────────────────────────────────────────────

    def close(self):
        """Close the database connection."""
        self.conn.close()