Skip to content

Commit 2d2ad3b

Browse files
committed
Move encode functions into encode namespace
1 parent ddf766e commit 2d2ad3b

File tree

8 files changed

+138
-66
lines changed

8 files changed

+138
-66
lines changed

examples/ldbc.clj

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns clj-3df.ldbc
22
(:require
3-
[clj-3df.core :as df :refer [exec!]]))
3+
[clj-3df.core :as df :refer [exec!]]
4+
[clj-3df.binding :as binding]))
45

56
(def schema
67
{:system/time {:db/valueType :Number}
@@ -17,10 +18,14 @@
1718

1819
:post/creation-date {:db/valueType :String}
1920

20-
:comment/ip {:db/valueType :String}
21-
:comment/browser {:db/valueType :String}
22-
:comment/content {:db/valueType :String}
23-
:comment/creator {:db/valueType :Eid}
21+
:comment/creation-date {:db/valueType :String}
22+
:comment/ip {:db/valueType :String}
23+
:comment/browser {:db/valueType :String}
24+
:comment/content {:db/valueType :String}
25+
:comment/creator {:db/valueType :Eid}
26+
:comment/place {:db/valueType :Eid}
27+
:comment/parent-post {:db/valueType :Eid}
28+
:comment/parent-comment {:db/valueType :Eid}
2429
})
2530

2631
(def db (df/create-db schema))
@@ -64,24 +69,43 @@
6469

6570
(exec! conn
6671
(df/register-source
67-
[#_:eid #_timestamp :comment/ip :comment/browser :comment/content]
72+
[#_:eid :comment/creation-date :comment/ip :comment/browser :comment/content]
6873
{:CsvFile (merge ldbc-defaults
69-
{:path "/Users/niko/data/1k-users/comment.csv"
70-
:eid_offset 0
71-
:timestamp_offset 1
72-
:schema [[2 {:String ""}]
73-
[3 {:String ""}]
74-
[4 {:String ""}]]})}))
74+
{:path "/Users/niko/data/1k-users/comment.csv"
75+
:eid_offset 0
76+
;; :timestamp_offset 1
77+
:schema [[1 {:String ""}]
78+
[2 {:String ""}]
79+
[3 {:String ""}]
80+
[4 {:String ""}]]})}))
7581

7682
(exec! conn
7783
(df/register-source
7884
[#_:eid :comment/creator]
7985
{:CsvFile (merge ldbc-defaults
8086
{:path "/Users/niko/data/1k-users/comment_hasCreator_person.csv"
87+
:eid_offset 0
88+
:schema [[1 {:Eid 0}]]})})
89+
(df/register-source
90+
[#_:eid :comment/place]
91+
{:CsvFile (merge ldbc-defaults
92+
{:path "/Users/niko/data/1k-users/comment_isLocatedIn_place.csv"
93+
:eid_offset 0
94+
:schema [[1 {:Eid 0}]]})})
95+
(df/register-source
96+
[#_:eid :comment/parent-comment]
97+
{:CsvFile (merge ldbc-defaults
98+
{:path "/Users/niko/data/1k-users/comment_replyOf_comment.csv"
99+
:eid_offset 0
100+
:schema [[1 {:Eid 0}]]})})
101+
(df/register-source
102+
[#_:eid :comment/parent-post]
103+
{:CsvFile (merge ldbc-defaults
104+
{:path "/Users/niko/data/1k-users/comment_replyOf_post.csv"
81105
:eid_offset 0
82106
:schema [[1 {:Eid 0}]]})}))
83107

84-
(exec! conn
108+
#_(exec! conn
85109
(df/register-source
86110
[#_:eid :person/likes-post #_:timestamp]
87111
{:CsvFile (merge ldbc-defaults
@@ -97,9 +121,9 @@
97121

98122
;; advance to now
99123
(exec! conn
100-
(df/advance-domain 1550325946))
124+
(df/advance-domain 1650360833))
101125

102-
(exec! conn
126+
#_(exec! conn
103127
(df/query
104128
db "person-count"
105129
'[:find (count ?person)
@@ -109,6 +133,12 @@
109133
[?person :person/firstname "Chengdong"])]))
110134

111135
(exec! conn
136+
(df/query
137+
db "person-count"
138+
'[:find (count ?person)
139+
:where [?person :person/firstname "Chengdong"]]))
140+
141+
#_(exec! conn
112142
(df/query
113143
db "likes"
114144
'[:find ?person (count ?post)
@@ -117,17 +147,20 @@
117147
[?post :post/creation-date ?creation-date]]))
118148

119149
(exec! conn
120-
(df/register-query
150+
(df/register
121151
db "comment_event_stream"
122-
'[:find
123-
#_?id ?person #_?creationDate #_?ip #_?browser
124-
#_?content #_?reply_to_postId #_?reply_to_commentId #_?placeId
125-
:where
126-
[?person :person/firstname "Chengdong"]
127-
;; [?comment :comment/creator ?person]
128-
;; [?comment :comment/ip ?ip]
129-
;; [?comment :comment/browser ?browser]
130-
#_[?comment :comment/content ?content]])
152+
{:Hector
153+
{:variables '[?comment ?person ?creationDate ?ip ?browser
154+
?content ?parent-post ?parent-comment ?place]
155+
:bindings [(binding/attribute '[?comment :comment/creation-date ?creationDate])
156+
(binding/attribute '[?comment :comment/creator ?person])
157+
(binding/attribute '[?comment :comment/ip ?ip])
158+
(binding/attribute '[?comment :comment/browser ?browser])
159+
(binding/attribute '[?comment :comment/content ?content])
160+
(binding/attribute '[?comment :comment/place ?place])
161+
(binding/optional-attribute '[?comment :comment/parent-post ?parent-post ""])
162+
(binding/optional-attribute '[?comment :comment/parent-comment ?parent-comment ""])]}}
163+
[])
131164
(df/flow "comment_event_stream" "ldbc.sinks/csv"))
132165

133166
)

examples/lww.clj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
(ns lww
22
(:require
33
[clojure.pprint :as pprint]
4-
[clj-3df.core :refer [create-conn create-db exec!
5-
register-plan query transact] :as df])
4+
[clj-3df.core :refer [create-conn create-db exec! query transact] :as df])
65
(:gen-class))
76

87
;; LWW Register

examples/rga.clj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
(ns rga
22
(:require
33
[clojure.pprint :as pprint]
4-
[clj-3df.core :refer [create-conn create-db exec!
5-
register-plan query transact]]
4+
[clj-3df.core :refer [create-conn create-db exec! query transact]]
65
[manifold.stream :as stream]
76
[manifold.bus :as bus])
87
(:gen-class))

src/clj_3df/binding.cljc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(ns clj-3df.binding
2+
(:require
3+
[clj-3df.encode :as encode]))
4+
5+
(defn attribute [[e name v]]
6+
{:Attribute
7+
{:symbols [e v]
8+
:source_attribute name}})
9+
10+
(defn optional-attribute [[e name v default]]
11+
{:Attribute
12+
{:symbols [e v]
13+
:source_attribute name
14+
:default (encode/encode-value default)}})
15+
16+
(defn constant [[symbol value]]
17+
{:Constant
18+
{:symbol symbol
19+
:value (encode/encode-value value)}})
20+
21+
(defn binary-predicate [[predicate x y]]
22+
{:BinaryPredicate
23+
{:symbols [x y]
24+
:predicate (encode/encode-predicate predicate)}})
25+
26+
(comment
27+
28+
(attribute '[?e :name ?n])
29+
(optional-attribute '[?e :admin? ?admin false])
30+
(constant '[?c 123])
31+
(binary-predicate '(<= ?v1 ?v2))
32+
33+
)

src/clj_3df/compiler.cljc

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
[clojure.string :as str]
44
[clojure.set :as set]
55
#?(:clj [clojure.spec.alpha :as s]
6-
:cljs [cljs.spec.alpha :as s])))
6+
:cljs [cljs.spec.alpha :as s])
7+
[clj-3df.encode :as encode]))
78

89
;; UTIL
910

@@ -32,13 +33,6 @@
3233

3334
(def ^{:arglists '([pred] [pred coll])} separate (juxt filter remove))
3435

35-
(defn- encode-value [v]
36-
(cond
37-
(string? v) {:String v}
38-
(number? v) {:Number v}
39-
(keyword? v) {:Aid (subs (str v) 1)}
40-
(boolean? v) {:Bool v}))
41-
4236
;; GRAMMAR
4337

4438
(s/def ::query (s/keys :req-un [::find ::where]
@@ -194,7 +188,7 @@
194188
::lookup (let [[e a sym-v] pattern] {:MatchEA [e a sym-v]})
195189
::entity (let [[e sym-a sym-v] pattern] {:MatchE [e sym-a sym-v]})
196190
::hasattr (let [[sym-e a sym-v] pattern] {:MatchA [sym-e a sym-v]})
197-
::filter (let [[sym-e a [_ v]] pattern] {:MatchAV [sym-e a (encode-value v)]}))))
191+
::filter (let [[sym-e a [_ v]] pattern] {:MatchAV [sym-e a (encode/encode-value v)]}))))
198192

199193
(defrecord NameExpr [rule-name symbols]
200194
;; RuleExpr and NameExpr are equivalent
@@ -215,12 +209,11 @@
215209
(bound-symbols [this]
216210
(if (some? binding) (bound-symbols binding) args))
217211
(plan [this]
218-
(let [encode-predicate {'< "LT" '<= "LTE" '> "GT" '>= "GTE" '= "EQ" 'not= "NEQ"}
219-
symbols (bound-symbols this)]
212+
(let [symbols (bound-symbols this)]
220213
(if (some? binding)
221-
{:Filter [args (encode-predicate predicate) (plan binding) (offset-map->vec 2 offset->const)]}
214+
{:Filter [args (encode/encode-predicate predicate) (plan binding) (offset-map->vec 2 offset->const)]}
222215
(if debug?
223-
{:Filter [args (encode-predicate predicate) :_ offset->const]}
216+
{:Filter [args (encode/encode-predicate predicate) :_ offset->const]}
224217
(throw (ex-info "All predicate inputs must be bound in a single relation." {:binding this})))))))
225218

226219
(defrecord Aggregation [fn-symbols args key-symbols binding symbols with]
@@ -257,12 +250,11 @@
257250
(bound-symbols [this]
258251
(if (some? binding) (conj (bound-symbols binding) result-sym) (conj args result-sym)))
259252
(plan [this]
260-
(let [encode-fn (comp str/upper-case name)]
261-
(if (some? binding)
262-
{:Transform [args result-sym (plan binding) (encode-fn fn) (offset-map->vec 2 offset->const)]}
263-
(if debug?
264-
{:Transform [args result-sym (encode-fn fn) :_ offset->const]}
265-
(throw (ex-info "All function inputs must be bound in a single relation." {:binding this})))))))
253+
(if (some? binding)
254+
{:Transform [args result-sym (plan binding) (encode/encode-fn fn) (offset-map->vec 2 offset->const)]}
255+
(if debug?
256+
{:Transform [args result-sym (encode/encode-fn fn) :_ offset->const]}
257+
(throw (ex-info "All function inputs must be bound in a single relation." {:binding this}))))))
266258

267259
;; In the first pass the tree of (potentially) nested,
268260
;; context-modifying operators is navigated and all bindings are
@@ -279,7 +271,7 @@
279271
(fn [state [offset [typ arg]]]
280272
(case typ
281273
:var (update state :normalized-args conj arg)
282-
:const (update state :offset->const assoc offset (encode-value (second arg)))))
274+
:const (update state :offset->const assoc offset (encode/encode-value (second arg)))))
283275
{:offset->const {} :normalized-args []})))
284276

285277
(defmulti normalize (fn [ctx clause] (first clause)))

src/clj_3df/core.cljc

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,13 @@
7676
(defn flow [source-name sink-name]
7777
[{:Flow [source-name sink-name]}])
7878

79-
(defn register-plan [^DB db name plan rules]
79+
(defn register [^DB db name plan rules]
8080
(let [;; @TODO expose this directly?
8181
;; the top-level plan is just another rule...
8282
top-rule {:name name :plan plan}]
83-
(concat
84-
[{:Register
85-
{:publish [name]
86-
:rules (encode/encode-rules (conj rules top-rule))}}]
87-
;; @TODO split this off
88-
(interest name))))
83+
[{:Register
84+
{:publish [name]
85+
:rules (encode/encode-rules (conj rules top-rule))}}]))
8986

9087
(defn register-query
9188
([^DB db name query] (register-query db name query []))
@@ -101,10 +98,10 @@
10198
:rules (encode/encode-rules (conj compiled-rules top-rule))}}])))
10299

103100
(defn query
104-
([^DB db name query] (query db name query []))
105-
([^DB db name query rules]
101+
([^DB db name q] (query db name q []))
102+
([^DB db name q rules]
106103
(concat
107-
(register-query db name query rules)
104+
(register-query db name q rules)
108105
(interest name))))
109106

110107
(defn register-source [names source]

src/clj_3df/encode.cljc

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
(ns clj-3df.encode
22
"Utilties for encoding query plans to something that the backend can
33
understand. In particular, attributes and symbols will be encoded as
4-
integers.")
4+
integers."
5+
(:require [clojure.string :as str]))
56

67
(def nextID (atom 0))
78

89
(def encode-symbol (memoize (fn [sym] #?(:clj (clojure.lang.RT/nextID)
910
:cljs (swap! nextID inc)))))
1011

12+
(defn encode-value [v]
13+
(cond
14+
(string? v) {:String v}
15+
(number? v) {:Number v}
16+
(keyword? v) {:Aid (subs (str v) 1)}
17+
(boolean? v) {:Bool v}))
18+
1119
(defn encode-keyword [kw]
1220
(subs (str kw) 1))
1321

22+
(def encode-predicate
23+
{'< "LT"
24+
'<= "LTE"
25+
'> "GT"
26+
'>= "GTE"
27+
'= "EQ"
28+
'not= "NEQ"})
29+
30+
(def encode-fn (comp str/upper-case name))
31+
1432
(def encode-semantics
1533
{:db.semantics/raw "Raw"
1634
:db.semantics.cardinality/one "CardinalityOne"

test/clj_3df/core_test.cljc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
:cljs [cljs.test :refer-macros [deftest is testing run-tests]])
55
#?(:clj [clojure.core.async :as async :refer [<! >! go-loop]]
66
:cljs [cljs.core.async :as async :refer [<! >!]])
7-
[clj-3df.core :as df :refer [exec! create-db-inputs create-debug-conn query register-plan transact]])
7+
[clj-3df.core :as df :refer [exec! create-db-inputs create-debug-conn query register interest transact]])
88
#?(:cljs (:require-macros [clj-3df.core :refer [exec!]]
99
[cljs.core.async :refer [go-loop]])))
1010

@@ -100,11 +100,12 @@
100100
(exec! (debug-conn)
101101
(create-db-inputs db)
102102
;; (query db name '[:find ?e1 ?e2 :where [?e1 :name ?n1] [?e2 :name ?n2]])
103-
(register-plan db name '{:Project
104-
[[?e1 ?e2]
105-
{:Join [[]
106-
{:MatchA [?e1 :name ?n1]}
107-
{:MatchA [?e2 :name ?n2]}]}]} [])
103+
(register db name '{:Project
104+
[[?e1 ?e2]
105+
{:Join [[]
106+
{:MatchA [?e1 :name ?n1]}
107+
{:MatchA [?e2 :name ?n2]}]}]} [])
108+
(interest name)
108109
(transact db [[:db/add 1 :name "Dipper"]
109110
[:db/add 2 :name "Mabel"]])
110111
(expect-> out (is (= [name [[[1 1] 0 1] [[1 2] 0 1] [[2 1] 0 1] [[2 2] 0 1]]] out)))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy