-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.clj
604 lines (533 loc) · 20.6 KB
/
main.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
(ns datalevin.main
"Functions and vars to support the `dtlv` command line tool"
(:refer-clojure :exclude [drop load])
(:require
[clojure.tools.cli :refer [parse-opts]]
[clojure.string :as s]
[clojure.pprint :as p]
[clojure.java.io :as io]
[clojure.edn :as edn]
[clojure.stacktrace :as st]
[sci.core :as sci]
[datalevin.core :as d]
[datalevin.datom :as dd]
[datalevin.util :as u :refer [raise]]
[datalevin.interpret :as i]
[datalevin.lmdb :as l]
[datalevin.db :as db]
[datalevin.bits :as b]
[datalevin.server :as srv]
[pod.huahaiy.datalevin :as pod]
[datalevin.constants :as c])
(:import
[java.io BufferedReader PushbackReader IOException]
[java.lang RuntimeException]
[datalevin.datom Datom])
(:gen-class))
(if (u/graal?)
(require 'datalevin.binding.graal)
(require 'datalevin.binding.java))
(def ^:private version "0.8.12")
(def ^:private version-str
(str
"
Datalevin (version: " version ")"))
(defn- parse-version
"return [major minor non-breaking] version numbers"
[s]
(let [[major minor non-breaking] (s/split s #"\.")]
[(Integer/parseInt major)
(Integer/parseInt minor)
(Integer/parseInt non-breaking)]))
;; Data Readers
(def datalevin-data-readers
"Built-in data readers."
{'datalevin/Datom dd/datom-from-reader
'datalevin/DB db/db-from-reader
'datalevin/bytes b/bytes-from-reader
'datalevin/regex b/regex-from-reader
'datalevin/bigint b/bigint-from-reader
'datalevin/inter-fn i/inter-fn-from-reader
})
(def ^:dynamic *datalevin-data-readers*
"Can be bound to support reading custom tag literals."
datalevin-data-readers)
(def ^:private commands
#{"copy" "drop" "dump" "exec" "help" "load" "repl" "serv" "stat"})
(def ^:private serv-help
"
Command serv - run as a server.
Optional options:
-p --port Listening port, default is 8898
-r --root Root data directory, default is /var/lib/datalevin
-v --verbose Show detailed logging messages
Examples:
dtlv -p 8899 -v serv
dtlv -r /data/dtlv serv")
(def ^:private stat-help
"
Command stat - show statistics of the main database or sub-database(s).
Required option:
-d --dir PATH Path to the database directory
Optional options:
-a --all All of the sub-databases
Optional arguments:
name(s) of sub-database(s)
Examples:
dtlv -d /data/companydb stat
dtlv -d /data/companydb -a stat
dtlv -d /data/companydb stat sales products")
(def ^:private dump-help
"
Command dump - dump the content of the database or sub-database(s).
Required option:
-d --dir PATH Path to the source database directory
Optional options:
-a --all All of the sub-databases
-f --file PATH Write to the specified target file instead of stdout
-g --datalog Dump as a Datalog database
-l --list List the names of sub-databases instead of the content
Optional arguments:
Name(s) of sub-database(s)
Examples:
dtlv -d /data/companydb -l dump
dtlv -d /data/companydb -g dump
dtlv -d /data/companydb -f ~/sales-data dump sales
dtlv -d /data/companydb -f ~/company-data -a dump")
(def ^:private load-help
"
Command load - load data into the database or a sub-database.
Required option:
-d --dir PATH Path to the target database directory
Optional option:
-f --file PATH Load from the specified source file instead of stdin
-g --datalog Load a Datalog database
Optional argument:
Name of the single sub-database to load the data into, useful when loading
data into a sub-database with a name different from the original name
Examples:
dtlv -d /data/companydb -f ~/sales-data load new-sales
dtlv -d /data/companydb -f ~/sales-data -g load")
(def ^:private copy-help
"
Command copy - Copy the database. This can be done regardless of whether it is
currently in use.
Required option:
-d --dir PATH Path to the source database directory
Optional option:
-c --compact Compact while copying. Only pages in use will be copied.
Required argument:
Path to the destination directory.
Examples:
dtlv -d /data/companydb -c copy /backup/companydb-2021-02-14")
(def ^:private drop-help
"
Command drop - Drop or clear the content of sub-database(s).
Required option:
-d --dir PATH Path to the database directory
Optional option:
-D --delete Delete the sub-database, not just empty it.
Required argument:
Name(s) of the sub-database(s)
Examples:
dtlv -d /data/companydb -D drop sales")
(def ^:private exec-help
"
Command exec - Execute database transactions or queries.
Required argument:
The code to be executed. The code needs to be wrapped in single quotes,
so that the shell passes them through to Datalevin. Replace ' in query
with (quote ...). Escape \" with \\.
Examples:
dtlv exec '(def conn (get-conn \"/data/companydb\")) \\
(transact! conn [{:name \"Datalevin\"}]) \\
(q (quote [:find ?e ?n :where [?e :name ?n]]) @conn) \\
(close conn)'")
(defn- repl-help []
(println "")
(println "In addition to some Clojure core functions, the following functions are available:")
(doseq [ns i/user-facing-ns
:let [fs (->> ns
ns-publics
(i/user-facing-map ns)
keys
(sort-by name)
(partition 4 4 nil))]]
(println "")
(println "In namespace" ns)
(println "")
(doseq [f4 fs]
(doseq [f f4]
(printf "%-22s" (name f)))
(println "")))
(println "")
(println "Can call function without namespace: (<function name> <arguments>)")
(println "")
(println "Type (doc <function name>) to read documentation of the function")
"")
(def ^:private repl-header
"
Type (help) to see available functions. some Clojure core functions are also available.
Type (exit) to exit.
")
(defn- usage [options-summary]
(->> [version-str
""
"Usage: dtlv [options] [command] [arguments]"
""
"Commands:"
" copy Copy a database, regardless of whether it is now in use"
" drop Drop or clear a database"
" dump Dump the content of a database to standard output"
" exec Execute database transactions or queries"
" help Show help messages"
" load Load data from standard input into a database"
" repl Enter an interactive shell"
" serv Run as a server"
" stat Display statistics of database"
""
"Options:"
options-summary
""
"Type 'dtlv help <command>' to read about a specific command."
""
]
(s/join \newline)))
(defn- error-msg [errors]
(s/join \newline ["The following errors occurred while parsing your command:"
(s/join \newline errors)]))
(def default-root-dir
(if (u/windows?)
"C:\\ProgramData\\Datalevin"
"/var/lib/datalevin"))
(def ^:private cli-opts
[["-a" "--all" "Include all of the sub-databases"]
["-c" "--compact" "Compact while copying"]
["-d" "--dir PATH" "Path to the database directory"]
["-D" "--delete" "Delete the sub-database, not just empty it"]
["-f" "--file PATH" "Path to the specified file"]
["-g" "--datalog" "Dump/load as a Datalog database"]
["-h" "--help" "Show usage"]
["-l" "--list" "List the names of sub-databases instead of the content"]
["-p" "--port PORT" "Server listening port number"
:default c/default-port
:parse-fn #(Integer/parseInt %)
:validate [#(< 0 % 0x10000) "Must be a number between 0 and 65536"]]
["-r" "--root ROOT" "Server root data directory"
:default default-root-dir]
["-v" "--verbose" "Show verbose server debug log"]
["-V" "--version" "Show Datalevin version and exit"]])
(defn ^:no-doc validate-args
"Validate command line arguments. Either return a map indicating the program
should exit (with a error message, and optional ok status), or a map
indicating the action the program should take and the options provided."
[args]
(let [{:keys [options arguments errors summary]}
(parse-opts args cli-opts)
command (first arguments)
pod? (= "true" (System/getenv "BABASHKA_POD"))]
(cond
pod? {:command "pods"}
(:version options) {:exit-message version-str :ok? true}
(:help options) {:exit-message (usage summary) :ok? true}
errors {:exit-message (str (error-msg errors)
\newline
(usage summary))}
(commands command) {:command command
:options options
:arguments (rest arguments)
:summary summary}
(nil? command) {:command "repl"}
:else {:exit-message (usage summary)})))
(defn- exit
([]
(exit 0))
([status]
(System/exit status))
([status msg]
(println msg)
(System/exit status)))
(defn- dtlv-help [arguments summary]
(if (seq arguments)
(let [command (s/lower-case (first arguments))]
(exit 0 (case command
"repl" (repl-help)
"exec" exec-help
"copy" copy-help
"drop" drop-help
"dump" dump-help
"load" load-help
"stat" stat-help
(str "Unknown command: " command))))
(exit 0 (usage summary))))
(defn exec
"Execuate code that passed in as a seq of strings."
[arguments]
(i/exec-code (s/join (if (seq arguments)
arguments
(doall (line-seq (BufferedReader. *in*)))))))
(defn- dtlv-exec [arguments]
(try
(exec arguments)
(catch Throwable e
(st/print-cause-trace e)
(exit 1 (str "Execution error: " (.getMessage e)))))
(exit 0))
(defn copy
"Copy a database. `src-dir` is the source data directory path. `dest-dir` is
the destination data directory path. Will compact while copying if
`compact?` is true."
[src-dir dest-dir compact?]
(let [lmdb (l/open-kv src-dir)]
(println "Opened database, copying...")
(l/copy lmdb dest-dir compact?)
(l/close-kv lmdb)
(println "Copied database.")))
(defn- dtlv-copy [{:keys [dir compact]} arguments]
(assert dir (s/join \newline
["Missing source data directory path." copy-help]))
(assert (seq arguments)
(s/join \newline
["Missing destination data directory path." copy-help]))
(try
(copy dir (first arguments) compact)
(catch Throwable e
(st/print-cause-trace e)
(exit 1 (str "Copy error: " (.getMessage e)))))
(exit 0))
(defn drop
"Drop (when `delete` is true) or clear (when `delete` is false) the list of
sub-database(s) named by `dbis` from the database at the data directory path
`dir`."
[dir dbis delete]
(let [lmdb (l/open-kv dir)]
(if delete
(doseq [dbi dbis]
(l/drop-dbi lmdb dbi)
(println (str "Dropped " dbi)))
(doseq [dbi dbis]
(l/clear-dbi lmdb dbi)
(println (str "Cleared " dbi))))
(l/close-kv lmdb)))
(defn- dtlv-drop [{:keys [dir delete]} arguments]
(assert dir (s/join \newline ["Missing data directory path." drop-help]))
(assert (seq arguments)
(s/join \newline ["Missing sub-database name." drop-help]))
(try
(drop dir arguments delete)
(catch Throwable e
(st/print-cause-trace e)
(exit 1 (str "Drop error: " (.getMessage e)))))
(exit 0))
(defn- dump-dbi [lmdb dbi]
(p/pprint {:dbi dbi :entries (l/entries lmdb dbi) :ver version})
(doseq [[k v] (l/get-range lmdb dbi [:all] :raw :raw)]
(p/pprint [(b/encode-base64 k) (b/encode-base64 v)])))
(defn- dump-all [lmdb]
(doseq [dbi (set (l/list-dbis lmdb)) ] (dump-dbi lmdb dbi)))
(defn- dump-datalog [dir]
(let [conn (d/create-conn dir)]
(p/pprint (d/opts conn))
(p/pprint (d/schema conn))
(doseq [^Datom datom (d/datoms @conn :eav)]
(prn [(.-e datom) (.-a datom) (.-v datom)]))
(d/close conn)))
(defn dump
"Dump database content. `src-dir` is the database directory path.
The content will be written to `dest-file` if given, or to stdout.
If `list?` is true, will list the names of the sub-databases only, not the
content.
If `datalog?` is true, will dump the whole database as a Datalog store,
including the schema and all the datoms.
If `all?` is true, will dump raw data of all the sub-databases.
If `dbis` is not empty, will dump raw data of only the named sub-databases."
[src-dir dest-file dbis list? datalog? all?]
(let [f (when dest-file (io/writer dest-file))]
(binding [*out* (or f *out*)]
(cond
list? (let [lmdb (l/open-kv src-dir)]
(p/pprint (set (l/list-dbis lmdb)))
(l/close-kv lmdb))
datalog? (dump-datalog src-dir)
all? (let [lmdb (l/open-kv src-dir)]
(dump-all lmdb)
(l/close-kv lmdb))
(seq dbis) (let [lmdb (l/open-kv src-dir)]
(doseq [dbi dbis] (dump-dbi lmdb dbi))
(l/close-kv lmdb))
:else (println dump-help)))
(when f (.flush f) (.close f))))
(defn- dtlv-dump [{:keys [dir all file datalog list]} arguments]
(assert dir (s/join \newline ["Missing data directory path." dump-help]))
(try
(dump dir file arguments list datalog all)
(catch Throwable e
(st/print-cause-trace e)
(exit 1 (str "Dump error: " (.getMessage e)))))
(exit 0))
(defn- load-datalog [dir in]
(try
(with-open [^PushbackReader r in]
(let [read-form #(edn/read {:eof ::EOF
:readers *datalevin-data-readers*} r)
read-maps #(let [m1 (read-form)]
(if (:db/ident m1)
[nil m1]
[m1 (read-form)]))
[opts schema] (read-maps)
datoms (->> (repeatedly read-form)
(take-while #(not= ::EOF %))
(map #(apply d/datom %)))
db (d/init-db datoms dir schema opts)]
(d/close-db db)))
(catch IOException e
(raise "IO error while loading Datalog data: " (ex-message e) {}))
(catch RuntimeException e
(raise "Parse error while loading Datalog data: " (ex-message e) {}))
(catch Exception e
(raise "Error loading Datalog data: " (ex-message e) {}))))
(defn- load-kv [dbi [k v]]
[:put dbi (b/decode-base64 k) (b/decode-base64 v) :raw :raw])
(defn- load-dbi [lmdb dbi in]
(try
(with-open [^PushbackReader r in]
(let [read-form #(edn/read {:eof ::EOF} r)
{:keys [entries]} (read-form)]
(l/open-dbi lmdb dbi)
(l/transact-kv lmdb (->> (repeatedly read-form)
(take-while #(not= ::EOF %))
(take entries)
(map (partial load-kv dbi))))))
(catch IOException e
(raise "IO error while loading raw data: " (ex-message e) {}))
(catch RuntimeException e
(raise "Parse error while loading raw data: " (ex-message e) {}))
(catch Exception e
(raise "Error loading raw data: " (ex-message e) {}))))
(defn- load-all [lmdb in]
(try
(with-open [^PushbackReader r in]
(let [read-form #(edn/read {:eof ::EOF} r)
load-dbi (fn [[ms vs]]
(doseq [{:keys [dbi]} (butlast ms)]
(l/open-dbi lmdb dbi))
(let [{:keys [dbi entries]} (last ms)]
(l/open-dbi lmdb dbi)
(->> vs
(take entries)
(map (partial load-kv dbi)))))]
(l/transact-kv lmdb (->> (repeatedly read-form)
(take-while #(not= ::EOF %))
(partition-by map?)
(partition 2 2 nil)
(mapcat load-dbi)))))
(catch IOException e
(raise "IO error while loading raw data: " (ex-message e) {}))
(catch RuntimeException e
(raise "Parse error while loading raw data: " (ex-message e) {}))
(catch Exception e
(raise "Error loading raw data: " (ex-message e) {}))))
(defn load
"Load content into the database at data directory path `dir`,
from `src-file` if given, or from stdin.
If `datalog?` is true, the content are schema and datoms, otherwise they are
raw data.
Will load raw data into the named sub-database `dbi` if given. "
[dir src-file dbi datalog?]
(let [f (when src-file (PushbackReader. (io/reader src-file)))
in (or f (PushbackReader. *in*))]
(cond
datalog? (load-datalog dir in)
dbi (let [lmdb (l/open-kv dir)]
(load-dbi lmdb dbi in)
(l/close-kv lmdb))
:else (let [lmdb (l/open-kv dir)]
(load-all lmdb in)
(l/close-kv lmdb)))
(when f (.close f))))
(defn- dtlv-load [{:keys [dir file datalog]} arguments]
(assert dir (s/join \newline ["Missing data directory path." load-help]))
(try
(load dir file (first arguments) datalog)
(catch Throwable e
(st/print-cause-trace e)
(exit 1 (str "Load error: " (.getMessage e)))))
(exit 0))
;; TODO show reader info and free list info as well
(defn- dtlv-stat [{:keys [dir all]} arguments]
(assert dir (s/join \newline ["Missing data directory path." stat-help]))
(try
(let [lmdb (l/open-kv dir)
dbis (if all (l/list-dbis lmdb) arguments)]
(if (seq dbis)
(p/pprint (cond-> []
all (conj {"Main DB" (l/stat lmdb)})
true (into (for [dbi dbis
:let [_ (l/open-dbi lmdb dbi)]]
{dbi (l/stat lmdb dbi)}))))
(p/pprint {"Main DB" (l/stat lmdb)}))
(l/close-kv lmdb))
(catch Throwable e
(st/print-cause-trace e)
(exit 1 (str "Stat error: " (.getMessage e)))))
(exit 0))
(defn- prompt [ctx]
(let [ns-name (sci/eval-string* ctx "(ns-name *ns*)")]
(print (str ns-name "> "))
(flush)))
(defn- handle-error [_ last-error e]
(binding [*out* *err*] (println (ex-message e)))
(sci/set! last-error e))
(defn- document [s]
(when-let [f (i/resolve-var s)]
(let [m (meta f)]
(println " -------------------------")
(println (str (ns-name (:ns m)) "/" (:name m)))
(println (:arglists m))
(println (:doc m)))))
(defn- dtlv-repl []
(println version-str)
(println repl-header)
(let [reader (sci/reader *in*)
last-error (sci/new-dynamic-var '*e nil
{:ns (sci/create-ns 'clojure.core)})
ctx (sci/init (update i/sci-opts :namespaces
merge {'clojure.core {'*e last-error}}))]
(sci/with-bindings {sci/ns @sci/ns
last-error @last-error}
(loop []
(prompt ctx)
(let [next-form (try (sci/parse-next ctx reader)
(catch Throwable e
(handle-error ctx last-error e)
::err))]
(cond
(#{'(exit) '(quit)} next-form ) (exit)
(= next-form '(help)) (do (repl-help) (recur))
(and (list? next-form) (= ((comp name first) next-form) "doc"))
(do (document (first (next next-form))) (recur))
:else (when-not (= ::sci/eof next-form)
(when-not (= ::err next-form)
(let [res (try (i/eval-fn ctx next-form)
(catch Throwable e
(handle-error ctx last-error e)
::err))]
(when-not (= ::err res)
(prn res))))
(recur))))))))
(defn -main [& args]
(let [{:keys [command options arguments summary exit-message ok?]}
(validate-args args)]
(if exit-message
(exit (if ok? 0 1) exit-message)
(case command
"copy" (dtlv-copy options arguments)
"drop" (dtlv-drop options arguments)
"dump" (dtlv-dump options arguments)
"exec" (dtlv-exec arguments)
"help" (dtlv-help arguments summary)
"load" (dtlv-load options arguments)
"pods" (pod/run)
"repl" (dtlv-repl)
"serv" (srv/start (srv/create options))
"stat" (dtlv-stat options arguments)))))