Skip to content

Commit fec258e

Browse files
committed
throw if writing value from another db
1 parent b3b1a44 commit fec258e

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/xitdb/db.clj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,22 @@
4646
nil)))
4747
(.count history))
4848

49+
(defn- write-value! [^WriteCursor cursor new-value]
50+
(if (satisfies? common/ISlot new-value)
51+
(let [db (.db cursor)
52+
coll (common/-unwrap new-value)
53+
coll-db (-> coll .cursor .db)]
54+
(when (not= db coll-db)
55+
(throw (IllegalArgumentException. "Cannot write value from a different database")))
56+
(.write cursor (common/-slot new-value)))
57+
(conversion/v->slot! cursor new-value)))
58+
4959
(defn xitdb-reset!
5060
"Sets the value of the database to `new-value`.
5161
Returns new history index."
5262
[^WriteArrayList history new-value]
5363
(append-context! history nil (fn [^WriteCursor cursor]
54-
(if (satisfies? common/ISlot new-value)
55-
(.write cursor (common/-slot new-value))
56-
(conversion/v->slot! cursor new-value)))))
64+
(write-value! cursor new-value))))
5765

5866
(defn v->slot!
5967
"Converts a value to a slot which can be written to a cursor.
@@ -81,7 +89,7 @@
8189
(let [cursor (conversion/keypath-cursor cursor base-keypath)
8290
obj (xtypes/read-from-cursor cursor true)]
8391
(let [retval (apply f (into [obj] args))]
84-
(.write cursor (v->slot! cursor retval))))))))
92+
(write-value! cursor retval)))))))
8593

8694
(defn xitdb-swap-with-lock!
8795
"Performs the 'swap!' operation while locking `db.lock`.

0 commit comments

Comments
 (0)