Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the ability to use keyword refs directly as entity ids #245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 23 additions & 19 deletions src/datascript/db.cljc
Expand Up @@ -26,7 +26,7 @@
data (last fragments)]
`(throw (ex-info (str ~@(map (fn [m#] (if (string? m#) m# (list 'pr-str m#))) msgs)) ~data)))))

(defn #?@(:clj [^Boolean seqable?]
(defn #?@(:clj [^Boolean seqable?]
:cljs [^boolean seqable?])
[x]
(and (not (string? x))
Expand Down Expand Up @@ -143,7 +143,7 @@
IIndexed
(-nth [this i] (nth-datom this i))
(-nth [this i not-found] (nth-datom this i not-found))

IAssociative
(-assoc [d k v] (assoc-datom d k v))

Expand All @@ -168,7 +168,7 @@
(empty [d] (throw (UnsupportedOperationException. "empty is not supported on Datom")))
(count [d] 5)
(cons [d [k v]] (assoc-datom d k v))

clojure.lang.Indexed
(nth [this i] (nth-datom this i))
(nth [this i not-found] (nth-datom this i not-found))
Expand All @@ -187,7 +187,7 @@
([e a v] (Datom. e a v tx0 true))
([e a v tx] (Datom. e a v tx true))
([e a v tx added] (Datom. e a v tx added)))

(defn datom? [x] (instance? Datom x))

(defn- hash-datom [^Datom d]
Expand Down Expand Up @@ -431,7 +431,7 @@
(filter (fn [^Datom d] (= tx (.-tx d)))))
(btset/slice eavt (Datom. e nil nil nil nil)) ;; e _ _ _
(if (indexing? db a) ;; _ a v tx
(->> (btset/slice avet (Datom. nil a v nil nil))
(->> (btset/slice avet (Datom. nil a v nil nil))
(filter (fn [^Datom d] (= tx (.-tx d)))))
(->> (btset/slice aevt (Datom. nil a nil nil nil))
(filter (fn [^Datom d] (and (= v (.-v d))
Expand Down Expand Up @@ -754,14 +754,18 @@
(raise "Lookup ref should contain 2 elements: " eid
{:error :lookup-ref/syntax, :entity-id eid})
(not (is-attr? db (first eid) :db/unique))
(raise "Lookup ref attribute should be marked as :db/unique: " eid
{:error :lookup-ref/unique
:entity-id eid})
(if (= :db/ident (first eid))
(raise "You must have :db/ident marked as :db/unique in your schema to use keyword refs" {:error :lookup-ref/db-ident
:entity-id eid})
(raise "Lookup ref attribute should be marked as :db/unique: " eid
{:error :lookup-ref/unique
:entity-id eid}))
(nil? (second eid))
nil
:else
(:e (first (-datoms db :avet eid))))
#?@(:cljs [(array? eid) (recur db (array-seq eid))])
(keyword? eid) (recur db [:db/ident eid])
:else
(raise "Expected number or lookup ref for entity id, got " eid
{:error :entity-id/syntax
Expand Down Expand Up @@ -864,10 +868,10 @@
(cond
(keyword? attr)
(= \_ (nth (name attr) 0))

(string? attr)
(boolean (re-matches #"(?:([^/]+)/)?_([^/]+)" attr))

:else
(raise "Bad attribute type: " attr ", expected keyword or string"
{:error :transact/syntax, :attribute attr})))
Expand All @@ -884,7 +888,7 @@
(if (= \_ (nth name 0))
(if ns (str ns "/" (subs name 1)) (subs name 1))
(if ns (str ns "/_" name) (str "_" name))))

:else
(raise "Bad attribute type: " attr ", expected keyword or string"
{:error :transact/syntax, :attribute attr})))
Expand Down Expand Up @@ -943,12 +947,12 @@
(not (or (da/array? vs)
(and (coll? vs) (not (map? vs)))))
[vs]

;; probably lookup ref
(and (= (count vs) 2)
(is-attr? db (first vs) :db.unique/identity))
[vs]

:else vs))


Expand Down Expand Up @@ -1051,13 +1055,13 @@
(let [id (current-tx report)]
(recur (allocate-eid report old-eid id)
(cons (assoc entity :db/id id) entities)))

;; lookup-ref => resolved | error
(sequential? old-eid)
(let [id (entid-strict db old-eid)]
(recur report
(cons (assoc entity :db/id id) entities)))

;; upserted => explode | error
[upserted-eid (upsert-eid db entity)]
(if (and (neg-number? old-eid)
Expand All @@ -1066,7 +1070,7 @@
(retry-with-tempid initial-report initial-es old-eid upserted-eid)
(recur (allocate-eid report old-eid upserted-eid)
(concat (explode db (assoc entity :db/id upserted-eid)) entities)))

;; resolved | allocated-tempid | tempid | nil => explode
(or (number? old-eid)
(nil? old-eid))
Expand All @@ -1075,10 +1079,10 @@
(neg? old-eid) (or (get (:tempids report) old-eid)
(next-eid db))
:else old-eid)
new-entity (assoc entity :db/id new-eid)]
new-entity (assoc entity :db/id new-eid)]
(recur (allocate-eid report old-eid new-eid)
(concat (explode db new-entity) entities)))

;; trash => error
:else
(raise "Expected number or lookup ref for :db/id, got " old-eid
Expand Down Expand Up @@ -1166,7 +1170,7 @@
:else
(raise "Unknown operation at " entity ", expected :db/add, :db/retract, :db.fn/call, :db.fn/retractAttribute or :db.fn/retractEntity"
{:error :transact/syntax, :operation op, :tx-data entity})))

(datom? entity)
(let [[e a v tx added] entity]
(if added
Expand Down
3 changes: 2 additions & 1 deletion src/datascript/impl/entity.cljc
Expand Up @@ -12,7 +12,8 @@

(defn- entid [db eid]
(when (or (number? eid)
(sequential? eid))
(sequential? eid)
(keyword? eid))
(db/entid db eid)))

(defn entity [db eid]
Expand Down
22 changes: 11 additions & 11 deletions src/datascript/query.cljc
Expand Up @@ -145,7 +145,7 @@
(defn- and-fn [& args]
(reduce (fn [a b]
(if b b (reduced b))) true args))

(defn- or-fn [& args]
(reduce (fn [a b]
(if b (reduced b) b)) nil args))
Expand All @@ -163,8 +163,8 @@
'str str, 'pr-str pr-str, 'print-str print-str, 'println-str println-str, 'prn-str prn-str, 'subs subs,
're-find re-find, 're-matches re-matches, 're-seq re-seq,
'-differ? -differ?, 'get-else -get-else, 'get-some -get-some, 'missing? -missing?, 'ground identity})
(def built-in-aggregates

(def built-in-aggregates
(letfn [(sum [coll] (reduce + 0 coll))
(avg [coll] (/ (sum coll) (count coll)))
(median
Expand All @@ -183,8 +183,8 @@
:let [delta (- x mean)]]
(* delta delta)))]
(/ sum (count coll))))
(stddev
[coll]
(stddev
[coll]
(#?(:cljs js/Math.sqrt :clj Math/sqrt) (variance coll)))]
{'avg avg
'median median
Expand Down Expand Up @@ -249,11 +249,11 @@
BindIgnore
(in->rel [_ _]
(prod-rel))

BindScalar
(in->rel [binding value]
(Relation. {(get-in binding [:variable :symbol]) 0} [(into-array [value])]))

BindColl
(in->rel [binding coll]
(cond
Expand All @@ -265,7 +265,7 @@
:else
(reduce sum-rel
(map #(in->rel (:binding binding) %) coll))))

BindTuple
(in->rel [binding coll]
(cond
Expand Down Expand Up @@ -430,7 +430,7 @@
tuples-args (da/make-array len)]
(dotimes [i len]
(let [arg (nth args i)]
(if (symbol? arg)
(if (symbol? arg)
(if-let [source (get sources arg)]
(da/aset static-args i source)
(da/aset tuples-args i (get attrs arg)))
Expand Down Expand Up @@ -596,9 +596,9 @@
(if (satisfies? db/IDB source)
(let [[e a v tx] pattern]
(->
[(if (lookup-ref? e) (db/entid-strict source e) e)
[(if (or (lookup-ref? e) (attr? e)) (db/entid-strict source e) e)
a
(if (and v (attr? a) (db/ref? source a) (lookup-ref? v)) (db/entid-strict source v) v)
(if (and v (attr? a) (db/ref? source a) (or (lookup-ref? v) (attr? v))) (db/entid-strict source v) v)
(if (lookup-ref? tx) (db/entid-strict source tx) tx)]
(subvec 0 (count pattern))))
pattern))
Expand Down
3 changes: 2 additions & 1 deletion test/datascript/test/entity.cljc
Expand Up @@ -86,7 +86,8 @@
{:db/id 2, :name "Oleg"}]))]
(is (nil? (d/entity db nil)))
(is (nil? (d/entity db "abc")))
(is (nil? (d/entity db :keyword)))
;;(is (nil? (d/entity db :keyword)))
(is (thrown-with-msg? ExceptionInfo #"You must have :db/ident marked as :db/unique in your schema to use keyword refs" (d/entity db :keyword)))
(is (nil? (d/entity db [:name "Petr"])))
(is (= 777 (:db/id (d/entity db 777))))
(is (thrown-with-msg? ExceptionInfo #"Lookup ref attribute should be marked as :db/unique"
Expand Down