Skip to content

Commit

Permalink
Speed up rschema calculation (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Apr 9, 2017
1 parent 98600a6 commit 7d2fe9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Added `contains?` to built-ins (#211)
- Fixed handling of false values in entity cache (PR #198, thx [Brandon Bloom](https://github.com/brandonbloom))
- Fixed issue when string values were interpreted as lookup refs (#214)
- Speed up rschema calculation (#192, thx [Andre R.](https://github.com/rauhs))

# 0.15.5

Expand Down
39 changes: 20 additions & 19 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -513,27 +513,28 @@
;; ----------------------------------------------------------------------------

(defn attr->properties [k v]
(cond
(= [k v] [:db/isComponent true]) [:db/isComponent]
(= v :db.type/ref) [:db.type/ref :db/index]
(= v :db.cardinality/many) [:db.cardinality/many]
(= v :db.unique/identity) [:db/unique :db.unique/identity :db/index]
(= v :db.unique/value) [:db/unique :db.unique/value :db/index]
(= [k v] [:db/index true]) [:db/index]))

(defn- multimap [e m]
(reduce
(fn [acc [k v]]
(update-in acc [k] (fnil conj e) v))
{} m))
(case v
:db.unique/identity [:db/unique :db.unique/identity :db/index]
:db.unique/value [:db/unique :db.unique/value :db/index]
:db.cardinality/many [:db.cardinality/many]
:db.type/ref [:db.type/ref :db/index]
(when (true? v)
(case k
:db/isComponent [:db/isComponent]
:db/index [:db/index]
[]))))

(defn- rschema [schema]
(->>
(for [[a kv] schema
[k v] kv
prop (attr->properties k v)]
[prop a])
(multimap #{})))
(reduce-kv
(fn [m attr keys->values]
(reduce-kv
(fn [m key value]
(reduce
(fn [m prop]
(assoc m prop (conj (get m prop #{}) attr)))
m (attr->properties key value)))
m keys->values))
{} schema))

(defn- validate-schema-key [a k v expected]
(when-not (or (nil? v)
Expand Down

0 comments on commit 7d2fe9e

Please sign in to comment.