-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
interpret.cljc
202 lines (175 loc) · 6.43 KB
/
interpret.cljc
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
(ns datalevin.interpret
"Code interpreter, including functions and macros useful for command line
and query/transaction functions."
(:require [clojure.walk :as w]
[clojure.set :as set]
[clojure.pprint :as p]
[clojure.java.io :as io]
[sci.core :as sci]
[taoensso.nippy :as nippy]
[datalevin.query :as q]
[datalevin.util :as u]
[datalevin.core]
[datalevin.search]
[datalevin.stem]
[datalevin.client]
[datalevin.constants]
[clojure.string :as s])
(:import [clojure.lang AFn]
[datalevin.datom Datom]
[org.tartarus.snowball SnowballStemmer]
[java.text Normalizer Normalizer$Form]
[java.io DataInput DataOutput Writer]))
(if (u/graal?)
(require 'datalevin.binding.graal)
(require 'datalevin.binding.java))
(def ^:no-doc user-facing-ns
#{'datalevin.core 'datalevin.client 'datalevin.interpret 'datalevin.constants})
(defn- user-facing? [v]
(let [m (meta v)
d (m :doc)]
(and d
(if-let [p (:protocol m)]
(and (not (:no-doc (meta p)))
(not (:no-doc m)))
(not (:no-doc m)))
(not (s/starts-with? d "Positional factory function for class")))))
(defn ^:no-doc user-facing-map [ns var-map]
(let [sci-ns (sci/create-ns ns)]
(reduce
(fn [m [k v]]
(assoc m k (sci/new-var (symbol v) v (assoc (meta v)
:sci.impl/built-in true
:ns sci-ns))))
{}
(select-keys var-map
(keep (fn [[k v]] (when (user-facing? v) k)) var-map)))))
(defn- user-facing-vars []
(reduce
(fn [m ns]
(assoc m ns (user-facing-map ns (ns-publics ns))))
{}
user-facing-ns))
(defn ^:no-doc resolve-var [s]
(when (symbol? s)
(some #(ns-resolve % s) (conj user-facing-ns *ns*))))
(defn- qualify-fn [x]
(if (list? x)
(let [[f & args] x]
(if-let [var (when-not (q/rule-head f) (resolve-var f))]
(apply list (symbol var) args)
x))
x))
(declare ctx)
(defn ^:no-doc eval-fn [ctx form]
(sci/eval-form ctx (if (coll? form)
(w/postwalk qualify-fn form)
form)))
(defn load-edn
"Same as [`clojure.core/load-file`](https://clojuredocs.org/clojure.core/load-file),
useful for e.g. loading schema from a file"
[f]
(let [f (io/file f)
s (slurp f)]
(sci/with-bindings {sci/ns @sci/ns
sci/file (.getAbsolutePath f)}
(sci/eval-string* ctx s))))
(defn exec-code
"Execute code and print results. `code` is a string. Acceptable code includes
Datalevin functions and some Clojure core functions."
[code]
(let [reader (sci/reader code)]
(sci/with-bindings {sci/ns @sci/ns}
(loop []
(let [next-form (sci/parse-next ctx reader)]
(when-not (= ::sci/eof next-form)
(prn (eval-fn ctx next-form))
(recur)))))))
;; inter-fn
(defn- filter-used
"Only keep referred locals in the form"
[locals [args & body]]
(let [args (set args)
used (reduce (fn [coll s]
(if-not (or (qualified-symbol? s) (args s))
(conj coll s)
coll))
#{}
(filter symbol? (flatten body)))]
(set/intersection (set locals) used)))
(defn- save-env
"Borrowed some pieces from https://github.com/technomancy/serializable-fn"
[locals form]
(let [form (cons 'fn (w/postwalk qualify-fn (rest form)))
quoted-form `(quote ~form)]
(if locals
`(list `let [~@(for [local (filter-used locals (rest form)),
let-arg [`(quote ~local)
`(list `quote ~local)]]
let-arg)]
~quoted-form)
quoted-form)))
(defmacro inter-fn
"Same signature as `fn`. Create a function that can be used as an input in
Datalevin queries or transactions, e.g. as a filtering predicate or as a
transaction function.
This function will be sent over the wire if the database is on a remote
server or in a babashka pod. It runs in a sandboxed interpreter whether
the database is remote or local.
The symbols used in the inter-fn definition needs to be fully-qualified
to be resolved correctly."
[args & body]
`(with-meta
(sci/eval-form ctx (fn ~args (do ~@body)))
{:type :datalevin/inter-fn
:source ~(save-env (keys &env) &form)}))
(defn inter-fn?
"Return true if `x` is an `inter-fn`"
[x]
(= (:type (meta x)) :datalevin/inter-fn))
(defmacro definterfn
"Create a named `inter-fn`"
[fn-name args & body]
`(def ~fn-name (inter-fn ~args ~@body)))
(defn- source->inter-fn
"Convert a source form to get an inter-fn"
[src]
(with-meta
(sci/eval-form ctx src)
{:type :datalevin/inter-fn
:source src}))
(nippy/extend-freeze AFn :datalevin/inter-fn
[^AFn x ^DataOutput out]
(if (inter-fn? x)
(nippy/freeze-to-out! out (:source (meta x)))
(u/raise "Can only freeze an inter-fn" {:x x})))
(nippy/extend-thaw :datalevin/inter-fn
[^DataInput in]
(let [src (nippy/thaw-from-in! in)]
(source->inter-fn src)))
(defmethod print-method :datalevin/inter-fn [f, ^Writer w]
(.write w "#datalevin/inter-fn ")
(binding [*out* w] (p/pprint (:source (meta f)))))
(defn inter-fn-from-reader
"Read a printed `inter-fn` back in."
[x]
(source->inter-fn x))
(defn ^:no-doc additional-vars
[]
{'datalevin.search
(user-facing-map 'datalevin.search
{'en-analyzer #'datalevin.search/en-analyzer})
'datalevin.stem
(user-facing-map 'datalevin.stem
{'get-stemmer #'datalevin.stem/get-stemmer})
'datalevin.constants
(user-facing-map 'datalevin.constants
{'en-stop-words? #'datalevin.constants/en-stop-words?})})
(def ^:no-doc sci-opts
{:namespaces (merge (user-facing-vars) (additional-vars))
:classes {:allow :all
'java.text.Normalizer java.text.Normalizer
'java.text.Normalizer$Form java.text.Normalizer$Form
'org.tartarus.snowball.SnowballStemmer
org.tartarus.snowball.SnowballStemmer}})
(def ^:no-doc ctx (sci/init sci-opts))