-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.clj
More file actions
351 lines (285 loc) · 12.8 KB
/
Copy pathtutorial.clj
File metadata and controls
351 lines (285 loc) · 12.8 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
(ns tutorial
(:require [xyz.triplox.api :as tc]
[clojure.core.async :as async])
(:import (java.io Closeable)))
(def conn (tc/connect "localhost" 5490))
;; --------------------------------------------------------------------------
;; Schema definition
;; --------------------------------------------------------------------------
;; We are going to consider a minimal issue tracker with the entity types `user`,
;; `team`, `issue` and `status`.
(def schema
;; users
[{:db/ident :user/handle
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
{:db/ident :user/name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :user/team
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many}
;; teams
{:db/ident :team/name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
;; issues
{:db/ident :issue/key
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
{:db/ident :issue/title
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}
{:db/ident :issue/status
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :issue/priority
:db/valueType :db.type/long
:db/cardinality :db.cardinality/one}
{:db/ident :issue/assignee
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/one}
{:db/ident :issue/label
:db/valueType :db.type/string
:db/cardinality :db.cardinality/many}
;; status enum
{:db/ident :status/open}
{:db/ident :status/in-progress}
{:db/ident :status/closed}])
;; commit the schema
(tc/transact conn schema)
;; --------------------------------------------------------------------------
;; Queries
;; --------------------------------------------------------------------------
;; *JOINS and UNIFICATION*
;; Let us first add some users and teams. We are going to add 4 users and 2 teams, with
;; one user being in both teams of sizes 3 and 2 respectively. Let us first subscribe to
;; and incremental query that gives us user team pairs.
(def !user+team-sub
(tc/subscribe conn '{:find [?user-name ?team-name]
:where [[?user :user/name ?user-name]
[?user :user/team ?team]
[?team :team/name ?team-name]]}))
(tc/transact conn
;; teams
[{:db/id "team-frontend" :team/name "Frontend"}
{:db/id "team-backend" :team/name "Backend"}
;; users
{:user/handle "ada"
:user/name "Ada Lovelace"
:user/team "team-frontend"}
{:user/handle "alan"
:user/name "Alan Turing"
:user/team "team-frontend"}
{:db/id "grace-tmpid"
:user/handle "grace"
:user/name "Grace Hopper"}
;; TODO: We don't support vector syntax for cardinality/many attributes yet.
[:db/add "grace-tmpid" :user/team "team-frontend"]
[:db/add "grace-tmpid" :user/team "team-backend"]
{:user/handle "edsger"
:user/name "Edsger Dijkstra"
:user/team "team-backend"}])
;; Ada Lovelace and Alan Turing are working on the Frontend team and
;; Dijkstra on the Backend team. Grace Hopper works on both teams.
;; Let us first run the standard query to see what the result would be.
(tc/q (tc/db conn)
'{:find [?user-name ?team-name]
:where [[?user :user/name ?user-name]
[?user :user/team ?team]
[?team :team/name ?team-name]]})
;; => [["Grace Hopper" "Frontend"]
;; ["Grace Hopper" "Backend"]
;; ["Edsger Dijkstra" "Backend"]
;; ["Alan Turing" "Frontend"]
;; ["Ada Lovelace" "Frontend"]]
;; As expected Grace Hopper appears twice. Let us now inspect initialization delta of the incremental query.
(tc/take! !user+team-sub 500)
;; => [[["Ada Lovelace" "Frontend"] 1]
;; [["Alan Turing" "Frontend"] 1]
;; [["Edsger Dijkstra" "Backend"] 1]
;; [["Grace Hopper" "Backend"] 1]
;; [["Grace Hopper" "Frontend"] 1]]
;; The result matches the standard query. Let's say we are now removing the frontend team as we
;; decided to build a database (no frontend required 😉).
(tc/transact conn [[:db/retract [:team/name "Frontend"] :team/name "Frontend"]])
(tc/take! !user+team-sub 500)
;; => [[["Ada Lovelace" "Frontend"] -1]
;; [["Alan Turing" "Frontend"] -1]
;; [["Grace Hopper" "Frontend"] -1]]
;; All person + team pairs that were part of the frontend team were removed.
;; *PREDICATES and FUNCTIONS*
;; Let us now create a query that uses predicates and functions. It gets the issues
;; with a high priority, their assignee and the corresponding SLA response time in hours.
(def !urgent-issues-sub
(tc/subscribe
conn
'{:find [?title ?assignee-name ?sla-hours]
:where [[?issue :issue/title ?title]
[?issue :issue/priority ?priority]
[(<= ?priority 2)]
[?issue :issue/assignee ?assignee]
[?assignee :user/name ?assignee-name]
[(* ?priority 24) ?sla-hours]]}))
(tc/transact conn
[{:issue/key "TPX-1"
:issue/title "Sync engine drops updates on reconnect"
:issue/priority 1
:issue/assignee [:user/handle "ada"]}
{:issue/key "TPX-2"
:issue/title "Add dark mode to the dashboard"
:issue/priority 4
:issue/assignee [:user/handle "alan"]}
{:issue/key "TPX-3"
:issue/title "Incremental joins allocate per delta"
:issue/priority 2
:issue/assignee [:user/handle "grace"]}
{:issue/key "TPX-4"
:issue/title "Document the tutorial schema"
:issue/priority 5
:issue/assignee [:user/handle "edsger"]}
{:issue/key "TPX-5"
:issue/title "WAL replay is quadratic"
:issue/priority 1
:issue/assignee [:user/handle "grace"]}
{:issue/key "TPX-6"
:issue/title "Flaky test in the bid pipeline"
:issue/priority 3
:issue/assignee [:user/handle "alan"]}])
(tc/take! !urgent-issues-sub)
;; => [[["Incremental joins allocate per delta" "Grace Hopper" 48] 1]
;; [["Sync engine drops updates on reconnect" "Ada Lovelace" 24] 1]
;; [["WAL replay is quadratic" "Grace Hopper" 24] 1]]
;; The inc query initialization gives us the same results at the standard query. Let's say we
;; now prioritize the flake test and deprioritize the incremental join allocations.
(tc/transact conn [[:db/add [:issue/key "issue-6"] :issue/priority 2]
[:db/add [:issue/key "issue-3"] :issue/priority 4]])
(tc/take! !urgent-issues-sub)
;; => [[["Flaky test in the bid pipeline" "Alan Turing" 48] 1]
;; [["Incremental joins allocate per delta" "Grace Hopper" 48] -1]]
;; The flaky tests enters the and "incremental join allocations" exists the urgent tickets.
;; *OR*
;; Let us add the status for the issues and subscribe to the active issues.
(tc/transact conn
[[:db/add [:issue/key "TPX-1"] :issue/status :status/open]
[:db/add [:issue/key "TPX-2"] :issue/status :status/open]
[:db/add [:issue/key "TPX-3"] :issue/status :status/open]
[:db/add [:issue/key "TPX-4"] :issue/status :status/closed]
[:db/add [:issue/key "TPX-5"] :issue/status :status/in-progress]
[:db/add [:issue/key "TPX-6"] :issue/status :status/in-progress]])
(def !active-sub
(tc/subscribe conn '{:find [?title]
:where [[?i :issue/title ?title]
;; TODO replace once https://github.com/FiV0/triplox/issues/313 goes in
#_
(or [?i :issue/status :status/open]
[?i :issue/status :status/in-progress])
[?i :issue/status ?status]
(or [?status :db/ident :status/open]
[?status :db/ident :status/in-progress])]}))
(tc/take! !active-sub)
;; => [[["Add dark mode to the dashboard"] 1]
;; [["Flaky test in the bid pipeline"] 1]
;; [["Incremental joins allocate per delta"] 1]
;; [["Sync engine drops updates on reconnect"] 1]
;; [["WAL replay is quadratic"] 1]]
;; First we all active queries. Let us now change an open to in-progress and a in-progress to closed.
(tc/transact conn [[:db/add [:issue/key "TPX-1"] :issue/status :status/in-progress]
[:db/add [:issue/key "TPX-5"] :issue/status :status/closed]])
(tc/take! !active-sub)
;; => [[["WAL replay is quadratic"] -1]]
;; "WAL replay" was closed hence exists the active set where as "Sync engine drops updates on reconnect" changes status, but
;; doesn't leave or enter the active set.
;; *NOT*
;; To illustrate the `not` clause, let us observe tickets that are not clsoed yet.
(def !not-closed-sub
(tc/subscribe conn '{:find [?title]
:where [[?i :issue/title ?title]
[?i :issue/status ?status]
(not [?status :db/ident :status/closed])]}))
(tc/take! !not-closed-sub)
;; => [[["Add dark mode to the dashboard"] 1]
;; [["Flaky test in the bid pipeline"] 1]
;; [["Incremental joins allocate per delta"] 1]
;; [["Sync engine drops updates on reconnect"] 1]]
(tc/transact conn [[:db/add [:issue/key "TPX-6"] :issue/status :status/closed]])
(tc/take! !not-closed-sub)
;; => [[["Flaky test in the bid pipeline"] -1]]
;; When closing the flaky ticket, it leaves the "not-closed" set.
;; TODO: replace with untriaged below
#_
(def untriaged-sub
(tc/subscribe conn '{:find [?title]
:where [[?i :issue/title ?title]
(not [?i :issue/label ?l])]}))
;; *AGGREGATES + OR-JOIN + NOT-JOIN + RULES*
;; TODO
;; We currently don't support aggregates, or-join, not-join and rules but they will come to Triplox.
;; --------------------------------------------------------------------------
;; Views
;; --------------------------------------------------------------------------
;; We currently don't implement views server side. If there is a big demand for it
;; we might add them some day server side. Here is an example of how you might implement
;; them client side.
(defn update-view [view-map inc-res]
(reduce (fn [view-map [tuple value]]
(let [new-val (+ (get view-map tuple 0) value)]
(if-not (zero? new-val)
(assoc view-map tuple new-val)
(dissoc view-map tuple))))
view-map
inc-res))
(defn update-view! [!view inc-res]
(swap! !view update-view inc-res))
;; A worker that checks the subscription every 500ms and updates the view.
(defn start-worker [sub !view]
(let [stop (async/chan)
done (async/chan)]
(async/go-loop []
(let [[_ ch] (async/alts! [stop (async/timeout 500)])]
(if (= ch stop)
(async/close! done)
(let [res (tc/take! sub 10)]
(when (not= res ::tc/timeout)
(update-view! !view res))
(recur)))))
{:stop stop :done done}))
(defrecord View [sub view stop-chan done-chan]
Closeable
(close [_this]
(async/close! stop-chan)
(async/<!! done-chan)
(.close sub)))
(defn ->view [conn q]
(let [sub (tc/subscribe conn q)
!view (atom {})
{:keys [stop done]} (start-worker sub !view)]
(->View sub !view stop done)))
(defn get-view [{:keys [view]}]
(vec (keys @view)))
;; creating a new view to get a new db
(def conn (tc/connect "localhost" 5490))
(tc/transact conn [{:db/ident :person/name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
{:db/ident :person/residence
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}])
(tc/transact conn [{:person/name "Ada Lovelace"
:person/residence "12 St. James's Square"}
{:person/name "Alan Turing"
:person/residence "Bletchley Park"}])
(def residence-view (->view conn '{:find [?name ?residence]
:where [[?p :person/name ?name]
[?p :person/residence ?residence]]}))
(get-view residence-view)
;; => [["Ada Lovelace" "12 St. James's Square"]
;; ["Alan Turing" "Bletchley Park"]]
(tc/transact conn [[:db/add [:person/name "Ada Lovelace"] :person/residence "Buckingham Palace"]])
(get-view residence-view)
;; => [["Alan Turing" "Bletchley Park"]
;; ["Ada Lovelace" "Buckingham Palace"]]