-
Notifications
You must be signed in to change notification settings - Fork 1
/
SqliteKVStore.py
62 lines (55 loc) · 2.15 KB
/
SqliteKVStore.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
import sqlite3
import threading
import copy
class KeyExistError(Exception):
pass
# This class is thread-safe. It serves reads from memory and writes
# persistently to disk before returning.
class SqliteKVStore(object):
def __init__(self, dbPath="Shortener.db", table="short_url_to_url", keyCol="shortUrl", valueCol="fullUrl"):
self.mutex = threading.Lock()
self.dbPath = dbPath
self.table = table
self.keyCol = keyCol
self.valueCol = valueCol
with self.mutex:
self.inMemoryMap = {}
self.db = sqlite3.connect(dbPath, check_same_thread=False)
self.db.row_factory = sqlite3.Row
c = self.db.cursor()
c.execute("CREATE TABLE IF NOT EXISTS {} ({} TEXT PRIMARY KEY, {} TEXT)".format(table, keyCol, valueCol))
# Read inMemoryMap out of the db
rows = c.execute('SELECT "{}" as key, "{}" as value from "{}"'.format(keyCol, valueCol, table))
for row in rows:
self.inMemoryMap[row["key"]] = row["value"]
def __getitem__(self, key):
with self.mutex:
return self.inMemoryMap[key]
def __contains__(self, key):
with self.mutex:
return key in self.inMemoryMap
# This will insert but not update; should raise a KeyExistError if the key
# already exists
def __setitem__(self, key, val):
with self.mutex:
c = self.db.cursor()
try:
c.execute("INSERT INTO {} ({}, {}) VALUES(?, ?)".format(self.table, self.keyCol, self.valueCol), (key, val))
self.db.commit()
except sqlite3.IntegrityError:
raise KeyExistError
# Write to in-memory map only if the db write succeeded.
self.inMemoryMap[key] = val
def __delitem__(self, key):
with self.mutex:
c = self.db.cursor()
c.execute("DELETE FROM {} WHERE {} = ?".format(self.table, self.keyCol), (key,))
self.db.commit()
try:
del self.inMemoryMap[key]
except KeyError:
pass
def snapshot(self):
with self.mutex:
# We make a copy here to avoid racing with a concurrent modification.
return copy.deepcopy(self.inMemoryMap)