Skip to content

Commit

Permalink
Can put datoms into transaction directly (closes tonsky#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jan 17, 2016
1 parent d1ff71d commit d50c255
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- Better error messages for upsert conflicts
- Backtracking of tempids when redefining them later with upserts (issue #76)
- Upsert works in vector form too (issue #99, #109)
- Can specify transaction number in `:db/add`
- Can put datoms into transaction directly (issue #121: supports both addition and retration datoms, keeps tx number)

# 0.14.0

Expand Down
18 changes: 13 additions & 5 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,12 @@
true (update-in [:aevt] btset/btset-conj datom cmp-datoms-aevt-quick)
indexing? (update-in [:avet] btset/btset-conj datom cmp-datoms-avet-quick)
true (advance-max-eid (.-e datom)))
(let [removing (first (-search db [(.-e datom) (.-a datom) (.-v datom)]))]
(if-let [removing (first (-search db [(.-e datom) (.-a datom) (.-v datom)]))]
(cond-> db
true (update-in [:eavt] btset/btset-disj removing cmp-datoms-eavt-quick)
true (update-in [:aevt] btset/btset-disj removing cmp-datoms-aevt-quick)
indexing? (update-in [:avet] btset/btset-disj removing cmp-datoms-avet-quick))))))
indexing? (update-in [:avet] btset/btset-disj removing cmp-datoms-avet-quick))
db))))

(defn- transact-report [report datom]
(-> report
Expand Down Expand Up @@ -932,10 +933,10 @@
[:db/add v straight-a eid]
[:db/add eid straight-a v])))))

(defn- transact-add [report [_ e a v :as ent]]
(defn- transact-add [report [_ e a v tx :as ent]]
(validate-attr a ent)
(validate-val v ent)
(let [tx (current-tx report)
(let [tx (or tx (current-tx report))
db (:db-after report)
e (entid-strict db e)
v (if (ref? db a) (entid-strict db v) v)
Expand Down Expand Up @@ -994,7 +995,7 @@
(-> report
(assoc-in [:tempids :db/current-tx] (current-tx report))
(update-in [:db-after :max-tx] inc))

(map? entity)
(let [old-eid (:db/id entity)]
(cond-let
Expand Down Expand Up @@ -1118,6 +1119,13 @@
: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
(recur (transact-add report [:db/add e a v tx]) entities)
(recur report (cons [:db/retract e a v] entities))))

:else
(raise "Bad entity type at " entity ", expected map or vector"
{:error :transact/syntax, :tx-data entity})
Expand Down
21 changes: 21 additions & 0 deletions test/datascript/test/transact.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@
:where [1 :name ?v]] db)
#{["Petr"]}))))))

(deftest test-with-datoms
(testing "keeps tx number"
(let [db (-> (d/empty-db)
(d/db-with [(d/datom 1 :name "Oleg")
(d/datom 1 :age 17 (+ 1 d/tx0))
[:db/add 1 :aka "x" (+ 2 d/tx0)]]))]
(is (= [[1 :age 17 (+ 1 d/tx0)]
[1 :aka "x" (+ 2 d/tx0)]
[1 :name "Oleg" d/tx0 ]]
(map (juxt :e :a :v :tx)
(d/datoms db :eavt))))))

(testing "retraction"
(let [db (-> (d/empty-db)
(d/db-with [(d/datom 1 :name "Oleg")
(d/datom 1 :age 17)
(d/datom 1 :name "Oleg" d/tx0 false)]))]
(is (= [[1 :age 17 d/tx0]]
(map (juxt :e :a :v :tx)
(d/datoms db :eavt)))))))

(deftest test-retract-fns
(let [db (-> (d/empty-db {:aka { :db/cardinality :db.cardinality/many }
:friend { :db/valueType :db.type/ref }})
Expand Down

0 comments on commit d50c255

Please sign in to comment.