Skip to content

Commit

Permalink
Preconditions to validate db/conn arguments (closes #101)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jul 19, 2015
1 parent 976bd20 commit 8020986
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 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 @@
- lookup refs in JS API `pull`, `pull_many`, `entity` (#94, thx [Matt Senior](https://github.com/mattsenior))
- fix in Pull API for reverse non-component attributes (#91, thx [Matt Senior](https://github.com/mattsenior))
- Node.js and Browser repls for dev profile (#93)
- Preconditions to validate db/conn arguments (#101)

# 0.11.5

Expand Down
46 changes: 33 additions & 13 deletions src/datascript.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

(def q dq/q)
(def entity de/entity)
(defn entity-db [^Entity entity] (.-db entity))
(defn entity-db [^Entity entity]
{:pre [(de/entity? entity)]}
(.-db entity))

(def datom dc/datom)

Expand All @@ -36,6 +38,7 @@
(instance? FilteredDB x))

(defn filter [db pred]
{:pre [(dc/db? db)]}
(if (is-filtered db)
(let [^FilteredDB fdb db
u (.-unfiltered-db fdb)]
Expand All @@ -45,6 +48,7 @@
(defn with
([db tx-data] (with db tx-data nil))
([db tx-data tx-meta]
{:pre [(dc/db? db)]}
(if (is-filtered db)
(throw (ex-info "Filtered DB cannot be modified" {:error :transaction/filtered}))
(dc/transact-tx-data (dc/map->TxReport
Expand All @@ -55,35 +59,44 @@
:tx-meta tx-meta}) tx-data))))

(defn db-with [db tx-data]
{:pre [(dc/db? db)]}
(:db-after (with db tx-data)))

(defn datoms
([db index] (dc/-datoms db index []))
([db index c1] (dc/-datoms db index [c1]))
([db index c1 c2] (dc/-datoms db index [c1 c2]))
([db index c1 c2 c3] (dc/-datoms db index [c1 c2 c3]))
([db index c1 c2 c3 c4] (dc/-datoms db index [c1 c2 c3 c4])))
([db index] {:pre [(dc/db? db)]} (dc/-datoms db index []))
([db index c1] {:pre [(dc/db? db)]} (dc/-datoms db index [c1]))
([db index c1 c2] {:pre [(dc/db? db)]} (dc/-datoms db index [c1 c2]))
([db index c1 c2 c3] {:pre [(dc/db? db)]} (dc/-datoms db index [c1 c2 c3]))
([db index c1 c2 c3 c4] {:pre [(dc/db? db)]} (dc/-datoms db index [c1 c2 c3 c4])))

(defn seek-datoms
([db index] (dc/-seek-datoms db index []))
([db index c1] (dc/-seek-datoms db index [c1]))
([db index c1 c2] (dc/-seek-datoms db index [c1 c2]))
([db index c1 c2 c3] (dc/-seek-datoms db index [c1 c2 c3]))
([db index c1 c2 c3 c4] (dc/-seek-datoms db index [c1 c2 c3 c4])))
([db index] {:pre [(dc/db? db)]} (dc/-seek-datoms db index []))
([db index c1] {:pre [(dc/db? db)]} (dc/-seek-datoms db index [c1]))
([db index c1 c2] {:pre [(dc/db? db)]} (dc/-seek-datoms db index [c1 c2]))
([db index c1 c2 c3] {:pre [(dc/db? db)]} (dc/-seek-datoms db index [c1 c2 c3]))
([db index c1 c2 c3 c4] {:pre [(dc/db? db)]} (dc/-seek-datoms db index [c1 c2 c3 c4])))

(def index-range dc/-index-range)
(defn index-range [db attr start end]
{:pre [(dc/db? db)]}
(dc/-index-range db attr start end))

(def entid dc/entid)

;; Conn

(defn conn? [conn]
(and (instance? #?(:clj clojure.lang.Atom
:cljs cljs.core/Atom) conn)
(dc/db? @conn)))

(defn create-conn
([] (create-conn dc/default-schema))
([schema]
(atom (empty-db schema)
:meta { :listeners (atom {}) })))

(defn -transact! [conn tx-data tx-meta]
{:pre [(conn? conn)]}
(let [report (atom nil)]
(swap! conn (fn [db]
(let [r (with db tx-data tx-meta)]
Expand All @@ -94,6 +107,7 @@
(defn transact!
([conn tx-data] (transact! conn tx-data nil))
([conn tx-data tx-meta]
{:pre [(conn? conn)]}
(let [report (-transact! conn tx-data tx-meta)]
(doseq [[_ callback] @(:listeners (meta conn))]
(callback report))
Expand All @@ -102,10 +116,12 @@
(defn listen!
([conn callback] (listen! conn (rand) callback))
([conn key callback]
{:pre [(conn? conn)]}
(swap! (:listeners (meta conn)) assoc key callback)
key))

(defn unlisten! [conn key]
{:pre [(conn? conn)]}
(swap! (:listeners (meta conn)) dissoc key))


Expand Down Expand Up @@ -141,11 +157,14 @@
(defn resolve-tempid [_db tempids tempid]
(get tempids tempid))

(def db deref)
(defn db [conn]
{:pre [(conn? conn)]}
@conn)

(defn transact
([conn tx-data] (transact conn tx-data nil))
([conn tx-data tx-meta]
{:pre [(conn? conn)]}
(let [res (transact! conn tx-data tx-meta)]
#?(:cljs
(reify
Expand Down Expand Up @@ -181,6 +200,7 @@
(defn transact-async
([conn tx-data] (transact-async conn tx-data nil))
([conn tx-data tx-meta]
{:pre [(conn? conn)]}
(future-call #(transact! conn tx-data tx-meta))))

(defn- rand-bits [pow]
Expand Down
7 changes: 6 additions & 1 deletion src/datascript/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,10 @@
(btset/slice (.-avet db) (resolve-datom db nil attr start nil)
(resolve-datom db nil attr end nil))))

(defn db? [x] (and (instance? ISearch x) (instance? IIndexAccess x) (instance? IDB x)))
(defn db? [x]
(and (satisfies? ISearch x)
(satisfies? IIndexAccess x)
(satisfies? IDB x)))

;; ----------------------------------------------------------------------------
(defrecord-updatable FilteredDB [unfiltered-db pred #?(:clj __hash)]
Expand Down Expand Up @@ -516,6 +519,7 @@
(defn ^DB empty-db
([] (empty-db default-schema))
([schema]
{:pre [(or (nil? schema) (map? schema))]}
(map->DB {
:schema (validate-schema schema)
:eavt (btset/btset-by cmp-datoms-eavt)
Expand Down Expand Up @@ -658,6 +662,7 @@
(is-attr? db attr :db/isComponent))

(defn entid [db eid]
{:pre [(db? db)]}
(cond
(number? eid) eid
(sequential? eid)
Expand Down
5 changes: 2 additions & 3 deletions src/datascript/impl/entity.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
(dc/entid db eid)))

(defn entity [db eid]
{:pre [(satisfies? dc/IDB db)
(satisfies? dc/ISearch db)
(satisfies? dc/IIndexAccess db)]}
{:pre [(dc/db? db)]}
(when-let [e (entid db eid)]
(->Entity db e (volatile! false) (volatile! {}))))

Expand Down Expand Up @@ -189,6 +187,7 @@
{} (partition-by :a datoms)))

(defn touch [^Entity e]
{:pre [(entity? e)]}
(when-not @(.-touched e)
(when-let [datoms (not-empty (dc/-search (.-db e) [(.-eid e)]))]
(vreset! (.-cache e) (->> datoms
Expand Down
8 changes: 4 additions & 4 deletions src/datascript/pull_api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@
(let [eids (into [] (map #(dc/entid-strict db %)) eids)]
(pull-pattern db (list (initial-frame pattern eids multi?)))))

(defn pull
[db selector eid]
(defn pull [db selector eid]
{:pre [(dc/db? db)]}
(pull-spec db (dpp/parse-pull selector) [eid] false))

(defn pull-many
[db selector eids]
(defn pull-many [db selector eids]
{:pre [(dc/db? db)]}
(pull-spec db (dpp/parse-pull selector) eids true))

0 comments on commit 8020986

Please sign in to comment.