-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-hash.rkt
118 lines (101 loc) · 3.67 KB
/
db-hash.rkt
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
#lang racket/base
(require
db
net/base64
racket/bool
racket/contract
racket/fasl
racket/hash
racket/serialize
sql)
(define serializable-hash/c
(hash/c serializable? serializable?))
(provide
(contract-out
[make-db-hash (->* (connection?)
(#:table-name string?
#:src-hash (or/c false? serializable-hash/c)
#:serializer (-> serializable? string?)
#:deserializer (-> string? serializable?))
(and/c serializable-hash/c (not/c immutable?)))])
DEFAULT-TABLE-NAME
DEFAULT-SERIALIZER
DEFAULT-DESERIALIZER)
;; Private helpers
(define (hash-from-db db-conn table-name deserializer)
(make-hash
(map
(lambda (row) (cons (deserializer (vector-ref row 0))
(deserializer (vector-ref row 1))))
(query-rows db-conn
(format "SELECT key, value FROM ~a" table-name)))))
(define (make-impersonator db-conn table-name serializer target-hash)
(impersonate-hash
target-hash
;; ref-proc (no-op)
(lambda (hash key)
(values key
(lambda (hash key val) val)))
;; set-proc
; TODO: what if the hash-table rejects the set op after we store in db?
; (I don't think that can happen)
(lambda (hash key val)
(query-exec db-conn
(format
"INSERT INTO ~a (key, value) VALUES ($1, $2) ON CONFLICT (key) DO UPDATE SET value=$2"
table-name)
(serializer key) (serializer val))
(values key val))
;; remove-proc
(lambda (hash key)
(query-exec db-conn
(format "DELETE FROM ~a WHERE key = $1" table-name)
(serializer key))
key)
;; key-proc (no-op)
(lambda (hash key) key)
;; clear-proc (optional)
(lambda (hash)
(query-exec db-conn
(format "DELETE FROM ~a" table-name)))
;; equal-key-proc (optional, no-op)
; TODO: is there a risk of keys which compare unequal having the same
; db key after fasl serialization?
(lambda (hash key) key)))
;; Public
(define DEFAULT-TABLE-NAME "hashtable")
(define DEFAULT-SERIALIZER
(lambda (value)
(bytes->string/latin-1 (base64-encode (s-exp->fasl (serialize value))))))
(define DEFAULT-DESERIALIZER
(lambda (value)
(deserialize (fasl->s-exp (base64-decode (string->bytes/latin-1 value))))))
(define (make-db-hash db-conn #:table-name [table-name DEFAULT-TABLE-NAME]
#:src-hash [src-hash #f]
#:serializer [serializer DEFAULT-SERIALIZER]
#:deserializer [deserializer DEFAULT-DESERIALIZER])
;; mutable hash impersonator backed by db storage
;; (we de/serialize both keys and values)
;; we want to properly quote table-name according to the current db style
(define db-type (dbsystem-name (connection-dbsystem db-conn)))
(set! table-name
(parameterize ((current-sql-dialect db-type))
(sql-ast->string (ident-qq (Ident:AST ,(make-ident-ast table-name))))))
(query-exec db-conn
(format
"CREATE TABLE IF NOT EXISTS ~a (key text NOT NULL, value text NOT NULL, PRIMARY KEY (key))"
table-name))
(define storage-hash
(make-impersonator db-conn
table-name
serializer
(hash-from-db db-conn table-name deserializer)))
;; TODO: we could possibly introspect the contract of src-hash
; > (value-contract make-theory)
; (-> (and/c hash? (not/c immutable?)))
; > (contract-projection (value-contract make-theory))
; #<procedure:...ow-val-first.rkt:1692:5>
(when src-hash
(hash-clear! storage-hash)
(hash-union! storage-hash src-hash))
storage-hash)