-
Notifications
You must be signed in to change notification settings - Fork 4
/
kvs.ss
38 lines (28 loc) · 1.1 KB
/
kvs.ss
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
;;;; Key Value Store Interface
(import
:std/error
:std/db/dbi :std/db/sqlite :std/sugar :std/misc/list-builder
:clan/base)
(export #t)
(def current-db-connection (make-parameter #f))
(def current-db-transaction (make-parameter #f))
(deferror-class DbError ())
(def (raise-db-error where message . irritants)
(raise (make-DbError message where: where irritants: irritants)))
(defstruct Kvs (connection)
constructor: :init!)
(defmethod {:init! Kvs}
(lambda (self e) (struct-instance-init! self e)))
(defmethod {close Kvs}
(lambda (self) {close (Kvs-connection self)}))
;; NB: In the near future, a key value store backed by several remote servers may implement
;; this method by querying its multiple replicas and identifying whichever is correct.
(defmethod {read-decode-check-key Kvs}
(lambda (self key decode check?)
(defvalues (bytes present?) {read-key self key})
(unless present?
(raise-db-error 'read-decode-check-key "kvs key absent" key))
(def value (decode bytes))
(unless (check? value)
(error 'kvs-data-tampering "Database was tampered with" self key))
value))