-
Notifications
You must be signed in to change notification settings - Fork 0
/
crux-tutorial.clj
241 lines (198 loc) · 5.58 KB
/
crux-tutorial.clj
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
(ns crux-tutorial.core
(:require [crux.api :as crux]))
(def node (crux/start-node {}))
(def manifest
{:crux.db/id :manifest
:pilot-name "Johanna"
:id/rocket "SB002-sol"
:id/employee "22910x2"
:badges "SETUP"
:cargo ["stereo" "gold fish" "slippers" "secret note"]})
(crux/submit-tx node [[:crux.tx/put manifest]])
(crux/entity-history (crux/db node) :manifest :asc)
(crux/submit-tx
node
[[:crux.tx/put
{:crux.db/id :commodity/Pu
:common-name "Plutonium"
:type :element/metal
:density 19.816
:radioactive true}]
[:crux.tx/put
{:crux.db/id :commodity/N
:common-name "Nitrogen"
:type :element/gas
:density 1.2506
:radioactive false}]
[:crux.tx/put
{:crux.db/id :commodity/CH4
:common-name "Methane"
:type :molecule/gas
:density 0.717
:radioactive false}]])
(crux/submit-tx
node
[[:crux.tx/put
{:crux.db/id :stock/Pu
:commod :commodity/Pu
:weight-ton 21}
#inst "2115-02-13T18"] ;; valid-time
[:crux.tx/put
{:crux.db/id :stock/Pu
:commod :commodity/Pu
:weight-ton 23}
#inst "2115-02-14T18"]
[:crux.tx/put
{:crux.db/id :stock/Pu
:commod :commodity/Pu
:weight-ton 22.2}
#inst "2115-02-15T18"]
[:crux.tx/put
{:crux.db/id :stock/Pu
:commod :commodity/Pu
:weight-ton 24}
#inst "2115-02-18T18"]
[:crux.tx/put
{:crux.db/id :stock/Pu
:commod :commodity/Pu
:weight-ton 24.9}
#inst "2115-02-19T18"]])
(crux/submit-tx
node
[[:crux.tx/put
{:crux.db/id :stock/N
:commod :commodity/N
:weight-ton 3}
#inst "2115-02-13T18" ;; start valid-time
#inst "2115-02-19T18"] ;; end valid-time
[:crux.tx/put
{:crux.db/id :stock/CH4
:commod :commodity/CH4
:weight-ton 92}
#inst "2115-02-15T18"
#inst "2115-02-19T18"]])
(crux/entity (crux/db node #inst "2115-02-14T18") :stock/Pu)
(defn easy-ingest
"Uses Crux put transaction to add a vector of documents to a node"
[node docs]
(crux/submit-tx node (mapv (fn [doc] [:crux.tx/put doc]) docs)))
(crux/submit-tx
node
[[:crux.tx/put (assoc manifest :badges ["SETUP" "PUT"])]])
(easy-ingest
node
[{:crux.db/id :commodity/Pu
:common-name "Plutonium"
:type :element/metal
:density 19.816
:radioactive true}
{:crux.db/id :commodity/N
:common-name "Nitrogen"
:type :element/gas
:density 1.2506
:radioactive false}
{:crux.db/id :commodity/CH4
:common-name "Methane"
:type :molecule/gas
:density 0.717
:radioactive false}
{:crux.db/id :commodity/Au
:common-name "Gold"
:type :element/metal
:density 19.300
:radioactive false}
{:crux.db/id :commodity/C
:common-name "Carbon"
:type :element/non-metal
:density 2.267
:radioactive false}
{:crux.db/id :commodity/borax
:common-name "Borax"
:IUPAC-name "Sodium tetraborate decahydrate"
:other-names ["Borax decahydrate" "sodium borate"
"sodium tetraborate" "disodium tetraborate"]
:type :mineral/solid
:appearance "white solid"
:density 1.73
:radioactive false}])
(crux/q (crux/db node)
'{:find [element]
:where [[element :type :element/metal]]})
(=
(crux/q (crux/db node)
'{:find [element]
:where [[element :type :element/metal]]})
(crux/q (crux/db node)
{:find '[element]
:where '[[element :type :element/metal]]})
(crux/q (crux/db node)
(quote
{:find [element]
:where [[element :type :element/metal]]})))
(crux/q (crux/db node)
'{:find [name]
:where [[e :type :element/metal]
[e :common-name name]]})
(crux/q (crux/db node)
'{:find [name rho]
:where [[e :density rho]
[e :common-name name]]})
(crux/q (crux/db node)
{:find '[name]
:where '[[e :type t]
[e :common-name name]]
:args [{'t :element/metal}]})
(defn filter-type
[type]
(crux/q (crux/db node)
{:find '[name]
:where '[[e :type t]
[e :common-name name]]
:args [{'t type}]}))
(defn filter-appearance
[description]
(crux/q (crux/db node)
{:find '[name IUPAC]
:where '[[e :common-name name]
[e :IUPAC-name IUPAC]
[e :appearance appearance]]
:args [{'appearance description}]}))
(filter-type :element/metal)
(filter-appearance "white solid")
(crux/submit-tx
node
[[:crux.tx/put
(assoc manifest :badges ["SETUP" "PUT" "DATALOG-QUERIES"])]])
(crux/q (crux/db node)
{:find '[belongings]
:where '[[e :cargo belongings]]
:args [{'belongings "secret note"}]})
(crux/submit-tx
node [[:crux.tx/put {:crux.db/id :kaarlang/clients
:clients [:encompass-trade]}
#inst "2110-01-01T09"
#inst "2111-01-01T09"]
[:crux.tx/put {:crux.db/id :kaarlang/clients
:clients [:encompass-trade :blue-energy]}
#inst "2111-01-01T09"
#inst "2113-01-01T09"]
[:crux.tx/put {:crux.db/id :kaarlang/clients
:clients [:blue-energy]}
#inst "2113-01-01T09"
#inst "2114-01-01T09"]
[:crux.tx/put {:crux.db/id :kaarlang/clients
:clients [:blue-energy :gold-harmony :tombaugh-resources]}
#inst "2114-01-01T09"
#inst "2115-01-01T09"]])
(crux/entity-history
(crux/db node #inst "2116-01-01T09")
:kaarlang/clients
:desc
{:with-docs? true})
(crux/submit-tx
node [[:crux.tx/delete :kaarlang/clients #inst "2110-01-01" #inst "2116-01-01"]])
(crux/entity-history
(crux/db node #inst "2116-01-01T09")
:kaarlang/clients
:desc
{:with-docs? true})