This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrick-db.rkt
232 lines (195 loc) · 8.17 KB
/
trick-db.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
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
#lang racket/base
(require
json
racket/contract
(only-in racket/file call-with-atomic-output-file)
(only-in racket/format ~a)
(only-in racket/function const thunk)
(only-in racket/list last)
(only-in racket/string string-suffix? string-trim)
(only-in racket/symbol symbol->immutable-string)
"log.rkt")
; A context id specifies the environment tricks belong to. Most commonly, it is a
; guild id or DM channel id
(define context-id? string?)
(define trick-key? string?)
; A permission check is a way to ensure a trick can be modified or removed.
(define permission-check? (or/c (-> any/c boolean?) #f))
(provide
trickdb?
(contract-out
(make-trickdb (-> path-string? (-> jsexpr? any/c) trickdb?))
(list-tricks (-> trickdb? context-id? (listof trick-key?)))
(all-tricks (-> trickdb? context-id? (listof (cons/c trick-key? any/c))))
(get-trick (-> trickdb? context-id? trick-key? (or/c any/c #f)))
(add-trick! (-> trickdb? context-id? trick-key? (-> any/c) boolean?))
(update-trick! (-> trickdb? context-id? trick-key? (-> any/c any/c) permission-check? boolean?))
(remove-trick! (-> trickdb? context-id? trick-key? permission-check? boolean?))
(commit-db! (-> trickdb? (-> any/c jsexpr?) boolean?))))
; data: context-id -> (trick-key -> trick)
(struct trickdb (data filename (dirty #:mutable) lock))
(define (load-data dir json->trick)
(define (load-tricks path dest)
(define context-id (string-trim (path->string (last (explode-path path)))
".json"
#:left? #f))
(call-with-input-file* path
(lambda (port)
(define js (read-json port))
(unless (hash? js)
(error "Data file was not a json object"))
(hash-set! dest context-id
(make-hash (hash-map js (lambda (name trick)
(cons (symbol->immutable-string name)
(json->trick trick))))))
(log-r16-info "Loaded ~a tricks from ~a" (hash-count (hash-ref dest context-id)) path))))
(define (load-guild path acc)
(let ([path (path->string path)])
(when (string-suffix? path ".json")
(load-tricks path acc)))
acc)
(foldl load-guild (make-hash) (directory-list dir #:build? #t)))
(define (make-trickdb path json->trick)
(trickdb
(load-data path json->trick)
path
#f
(make-semaphore 1)))
(define-syntax-rule (with-db-lock db . body)
(call-with-semaphore (trickdb-lock db) (thunk . body)))
(define (mark-dirty db) (set-trickdb-dirty! db #t))
; Note: db lock must be held
(define (get-submap db context-id)
(and context-id
(hash-ref!
(trickdb-data db)
context-id
(thunk (make-hash)))))
(define (list-tricks db context-id)
(with-db-lock db
(hash-keys (get-submap db context-id))))
(define (all-tricks db context-id)
(with-db-lock db
(hash->list (get-submap db context-id))))
(define (get-trick db context-id name)
(with-db-lock db
(hash-ref (get-submap db context-id) name #f)))
(define (add-trick! db context-id name thunk)
(with-db-lock db
(let* ((table (get-submap db context-id))
(create (not (hash-has-key? table name))))
(when create
(log-r16-info (~a "Trick created: " name))
(mark-dirty db)
(hash-set! table name (thunk)))
create)))
(define (update-trick! db context-id name thunk perm-check)
(with-db-lock db
(let* ((table (get-submap db context-id))
(modify (and (hash-has-key? table name) (perm-check (hash-ref table name)))))
(when modify
(log-r16-info (~a "Trick updated: " name))
(mark-dirty db)
(hash-set! table name (thunk (hash-ref table name))))
modify)))
(define (remove-trick! db context-id name perm-check)
(with-db-lock db
(let* ((table (get-submap db context-id))
(remove (and (hash-has-key? table name) (perm-check (hash-ref table name)))))
(when remove
(log-r16-info (~a "Trick deleted: " name))
(mark-dirty db)
(hash-remove! table name))
remove)))
(define (save data trick->json folder)
(log-r16-debug "Saving data in ~a" folder)
(with-handlers ([exn:fail:filesystem:exists? void])
(make-directory folder))
(hash-for-each
data
(lambda (guild submap)
(define tricks-serialized
(make-immutable-hash (hash-map submap (lambda (name trick)
(cons (string->symbol name) (trick->json trick))))))
(call-with-atomic-output-file
(build-path folder (~a guild ".json"))
(lambda (port _)
(write-json tricks-serialized port))))))
(define (commit-db! db trick->json)
(with-db-lock db
(and (trickdb-dirty db)
(begin
(save (trickdb-data db) trick->json (trickdb-filename db))
(set-trickdb-dirty! db #f)
#t))))
(module* test #f
(require (only-in racket/file
delete-directory/files
make-temporary-file)
rackunit)
(struct
fake-trick
(value)
#:mutable
#:transparent)
(define (fake-trick->json ft)
(hasheq 'value (fake-trick-value ft)))
(define (json->fake-trick js)
(fake-trick (hash-ref js 'value)))
(test-case "CRUD Smoke test"
(define context-id "guild1")
(define trick-id "trick1")
(define path (make-temporary-file "rkttmp~a" 'directory))
(define db (make-trickdb path json->fake-trick))
(after
(check-equal? (list-tricks db context-id) null)
(check-equal? (all-tricks db context-id) null)
(check-true (add-trick! db context-id trick-id (thunk (fake-trick "foo"))))
(check-equal? (fake-trick-value (get-trick db context-id trick-id)) "foo")
(check-equal? (length (list-tricks db context-id)) 1)
(check-equal? (length (all-tricks db context-id)) 1)
(check-true (update-trick! db context-id trick-id
(const (fake-trick "bar"))
(const #t)))
(check-equal? (fake-trick-value (get-trick db context-id trick-id)) "bar")
(check-true (remove-trick! db context-id trick-id (const #t)))
(check-equal? (list-tricks db context-id) null)
(check-equal? (all-tricks db context-id) null)
(delete-directory/files path)))
(test-case "Duplicate and Missing Test"
(define context-id "guild1")
(define trick-id "trick1")
(define path (make-temporary-file "rkttmp~a" 'directory))
(define db (make-trickdb path json->fake-trick))
(after
(check-true (add-trick! db context-id trick-id (thunk (fake-trick "foo"))))
(check-false (add-trick! db context-id trick-id (thunk (fake-trick "foo2"))))
(check-false (update-trick! db context-id "nonexistent-id" values (const #t))
(check-false (remove-trick! db context-id "nonexistent-id" (const #t))))
(delete-directory/files path)))
(test-case "Context Separation Test"
(define context-id-1 "guild1")
(define context-id-2 "guild2")
(define trick-id "trick1")
(define path (make-temporary-file "rkttmp~a" 'directory))
(define db (make-trickdb path json->fake-trick))
(after
(check-true (add-trick! db context-id-1 trick-id (thunk (fake-trick "foo"))))
(check-true (add-trick! db context-id-2 trick-id (thunk (fake-trick "bar"))))
(check-equal? (fake-trick-value (get-trick db context-id-1 trick-id)) "foo")
(check-equal? (fake-trick-value (get-trick db context-id-2 trick-id)) "bar")
(delete-directory/files path)))
(test-case "Saving Test"
(define context-id-1 "guild1")
(define context-id-2 "guild2")
(define trick-id "trick1")
(define path (make-temporary-file "rkttmp~a" 'directory))
(after
(let ([db (make-trickdb path json->fake-trick)])
(check-true (add-trick! db context-id-1 trick-id (thunk (fake-trick "foo"))))
(check-true (add-trick! db context-id-2 trick-id (thunk (fake-trick "bar"))))
(check-true (commit-db! db fake-trick->json)))
(let ([db (make-trickdb path json->fake-trick)])
(check-equal? (fake-trick-value (get-trick db context-id-1 trick-id)) "foo")
(check-equal? (fake-trick-value (get-trick db context-id-2 trick-id)) "bar"))
(delete-directory/files path))))