-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
285 lines (241 loc) · 6.97 KB
/
db.py
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from typing import Iterator
import time
import sqlite3
import json
from dataclasses import dataclass
from functools import cache
from typing import Optional, Mapping
from collections.abc import MutableMapping
# Reduce repetition
TABLE = "CREATE TABLE IF NOT EXISTS"
INT = "INTEGER NOT NULL"
TEXT = "TEXT NOT NULL"
IDENT = f"id {INT} PRIMARY KEY AUTOINCREMENT"
FNOW = f"{INT} DEFAULT (strftime('%s', 'now'))"
TIMER = f"ctime {FNOW}, atime {FNOW}, access {INT} DEFAULT 0"
MEMORY = f"{IDENT}, {TIMER}"
# Database schema
SCHEMA = f"""
{TABLE} log ({IDENT},
time {INT},
level {INT},
message {TEXT}
);
{TABLE} state ( -- Basically a JSON object
key {TEXT} PRIMARY KEY,
value {TEXT}
);
{TABLE} origins ({IDENT},
name {TEXT} UNIQUE
);
{TABLE} explicit ({MEMORY},
origin {INT} REFERENCES origins(id),
message {TEXT},
--embedding BLOB NOT NULL,
importance INTEGER
);
"""
@cache
def sanitize(name: str) -> str:
'''Sanitize an identifier, raise an error if it's invalid'''
if not isinstance(name, str):
raise TypeError(f"Expected str, got {type(name).__name__}")
if not name.isidentifier():
raise ValueError(f"Invalid identifier: {name}")
return name.lower()
@cache
def LIST(count: int) -> str:
return f"({', '.join('?' * count)})"
@cache
def INSERT(table: str, fields: tuple[str, ...]) -> str:
values = LIST(len(fields))
fields = ', '.join(map(sanitize, fields))
return f"INSERT INTO {sanitize(table)} ({fields}) VALUES {values}"
@cache
def SELECT(col: str|tuple[str, ...], table: str, fields: tuple[str, ...]) -> str:
if isinstance(col, str):
col = (sanitize(col),)
col = ', '.join(map(sanitize, col))
pred = ' AND '.join(f"{sanitize(k)} = ?" for k in fields)
return f"SELECT {col} FROM {sanitize(table)} WHERE {pred}"
@cache
def DELETE(table: str, where: Optional[str]=None) -> str:
s = f"DELETE FROM {sanitize(table)}"
return s if where is None else f"{s} WHERE {where}"
@cache
def UPDATE(table: str, fields: tuple[str, ...], where: Optional[str]=None) -> str:
fields = ', '.join(f"{sanitize(k)} = ?" for k in fields)
s = f"UPDATE {sanitize(table)} SET {fields}"
return s if where is None else f"{s} WHERE {where}"
@cache
def IN(name: str, count: int) -> str:
return f"{name} IN {LIST(count)}"
@dataclass
class Identified:
id: int
@dataclass
class MemoryEntry(Identified):
'''Purposefully nasty name to avoid instantiation'''
ctime: float
atime: float
access: int
@dataclass
class Log(Identified):
time: float
level: int
message: str
@dataclass
class Origin(Identified):
name: str
@dataclass
class ExplicitMemory(MemoryEntry):
origin: Origin
message: str
importance: Optional[int]
@dataclass
class FeaturalMemory(MemoryEntry):
embedding: bytes
@dataclass
class AssociativeMemory(MemoryEntry):
key: bytes
value: bytes
class StateProxy(MutableMapping):
'''Proxy for the state table.'''
def __init__(self, conn):
# Store connection not db to avoid ref loops
self._conn = conn
self._cache = {}
def get(self, k: str, default=None):
'''Get a value from the state dict or an optional default.'''
if k in self._cache:
return self._cache[k]
cur = self._conn.execute("SELECT value FROM state WHERE key = ?", (k,))
if row := cur.fetchone():
self._cache[k] = val = json.loads(row[0])
return val
return default
def update(self, mapping: Mapping[str, object]):
'''Mapping update.'''
self._cache.update(mapping)
self._conn.executemany(
"INSERT OR REPLACE INTO state (key, value) VALUES (?, ?)",
mapping.items()
)
self._conn.commit()
def reload(self):
'''Reload the cache.'''
self._cache.clear()
cur = self._conn.execute("SELECT key, value FROM state")
for k, v in cur:
self._cache[k] = json.loads(v)
def load_defaults(self, **kwargs):
'''Insert a default value if the key is not present.'''
self._conn.executemany(
"INSERT OR IGNORE INTO state (key, value) VALUES (?, ?)",
((k, json.dumps(v)) for k, v in kwargs.items())
)
self._conn.commit()
self.reload()
def __contains__(self, k: str):
return self.get(k, ...) is ...
def __getitem__(self, k: str):
return self.get(k)
def __setitem__(self, x: str, y):
self._cache[x] = y
self._conn.execute(
"INSERT OR REPLACE INTO state (key, value) VALUES (?, ?)",
(x, json.dumps(y))
)
self._conn.commit()
def __getattr__(self, name: str):
if name.startswith("_"):
return super().__getattr__(name)
return self[name]
def __setattr__(self, name: str, value):
if name.startswith("_"):
return super().__setattr__(name, value)
self[name] = value
def __repr__(self):
return repr(self._cache)
class Database:
def __init__(self, conn):
if isinstance(conn, str):
conn = sqlite3.connect(conn)
self.conn = conn
self.conn.row_factory = sqlite3.Row
self.conn.executescript(SCHEMA)
self.conn.commit()
self.origins = {}
self.state = StateProxy(self.conn)
# Specific database methods
def origin(self, ident: int|str|Origin) -> Origin:
'''Convert an id or name to an origin, may insert a new one.'''
# Check if the origin needs work
if isinstance(ident, Origin):
if ident.id is None:
ident = origin.name
elif ident.name is None:
ident = origin.id
else:
return ident
# Figure out what kind of origin we have
origin = self.origins.get(ident, None)
if isinstance(name := ident, str):
if origin is None:
row = self.conn.execute(
"SELECT id FROM origins WHERE name = ?", (ident,)
).fetchone()
if row:
id = row[0]
else:
# Brand new origin
cur = self.conn.execute(
"INSERT INTO origins (name) VALUES (?)", (ident,)
)
self.commit()
id = cur.lastrowid
else:
id = origin
elif isinstance(id := ident, int):
if origin is None:
row = self.conn.execute(
"SELECT name FROM origins WHERE id = ?", (ident,)
).fetchone()
if row:
name = row[0]
else:
raise ValueError("No such origin")
else:
name = origin
else:
raise TypeError("origin must be str|int|Origin(str|int)")
# Update the cache and return
self.origins[name] = id
self.origins[id] = name
return Origin(id, name)
def recent(self, lines: int) -> Iterator[ExplicitMemory]:
'''Get the `lines` most recent explicit memories.'''
# Sanitization
if not isinstance(lines, int):
raise TypeError("lines must be int")
recent = self.conn.execute(
f"SELECT * FROM explicit ORDER BY ctime DESC LIMIT {lines}"
).fetchall()[::-1]
for row in recent:
yield ExplicitMemory(**row, origin=self.origin(row.origin))
def log(self, level: int, msg: str) -> int:
'''Insert a new log entry. Returns the id.'''
cur = self.conn.execute(
"INSERT INTO log (time, level, message) VALUES (?, ?, ?)",
(time.time(), level, msg)
)
self.conn.commit()
return cur.lastrowid
def insert_explicit(self, memory: ExplicitMemory) -> int:
'''Insert an explicit memory. Returns the id.'''
cur = self.conn.execute(
"INSERT INTO explicit (origin, message, importance) VALUES (?, ?, ?)",
(memory.origin.id, memory.message, memory.importance)
)
self.conn.commit()
return cur.lastrowid