|
438 | 438 | (defprotocol ISearch |
439 | 439 | (-search [data pattern])) |
440 | 440 |
|
| 441 | +(defn- ^Datom fsearch [data pattern] |
| 442 | + (first (-search data pattern))) |
| 443 | + |
441 | 444 | (defprotocol IIndexAccess |
442 | 445 | (-datoms [db index components]) |
443 | 446 | (-seek-datoms [db index components]) |
|
1018 | 1021 | indexing? (update :avet set/conj datom cmp-datoms-avet-quick) |
1019 | 1022 | true (advance-max-eid (.-e datom)) |
1020 | 1023 | true (assoc :hash (atom 0))) |
1021 | | - (if-some [removing (first (-search db [(.-e datom) (.-a datom) (.-v datom)]))] |
| 1024 | + (if-some [removing (fsearch db [(.-e datom) (.-a datom) (.-v datom)])] |
1022 | 1025 | (cond-> db |
1023 | 1026 | true (update :eavt set/disj removing cmp-datoms-eavt-quick) |
1024 | 1027 | true (update :aevt set/disj removing cmp-datoms-aevt-quick) |
|
1204 | 1207 | db (:db-after report) |
1205 | 1208 | e (entid-strict db e) |
1206 | 1209 | v (if (ref? db a) (entid-strict db v) v) |
1207 | | - new-datom (datom e a v tx)] |
1208 | | - (if (multival? db a) |
1209 | | - (if (empty? (-search db [e a v])) |
| 1210 | + new-datom (datom e a v tx) |
| 1211 | + multival? (multival? db a) |
| 1212 | + old-datom ^Datom (if multival? |
| 1213 | + (fsearch db [e a v]) |
| 1214 | + (fsearch db [e a]))] |
| 1215 | + (cond |
| 1216 | + (nil? old-datom) |
1210 | 1217 | (transact-report report new-datom) |
1211 | | - report) |
1212 | | - (if-some [^Datom old-datom (first (-search db [e a]))] |
1213 | | - (if (= (.-v old-datom) v) |
1214 | | - report |
1215 | | - (-> report |
1216 | | - (transact-report (datom e a (.-v old-datom) tx false)) |
1217 | | - (transact-report new-datom))) |
1218 | | - (transact-report report new-datom))))) |
| 1218 | + |
| 1219 | + (= (.-v old-datom) v) |
| 1220 | + (update report ::tx-redundant conjv new-datom) |
| 1221 | + |
| 1222 | + :else |
| 1223 | + (-> report |
| 1224 | + (transact-report (datom e a (.-v old-datom) tx false)) |
| 1225 | + (transact-report new-datom))))) |
1219 | 1226 |
|
1220 | 1227 | (defn- transact-retract-datom [report ^Datom d] |
1221 | 1228 | (let [tx (current-tx report)] |
|
1232 | 1239 | (if (contains? (:tempids initial-report) tempid) |
1233 | 1240 | (raise "Conflicting upsert: " tempid " resolves" |
1234 | 1241 | " both to " upserted-eid " and " (get-in initial-report [:tempids tempid]) |
1235 | | - { :error :transact/upsert }) |
| 1242 | + {:error :transact/upsert}) |
1236 | 1243 | ;; try to re-run from the beginning |
1237 | 1244 | ;; but remembering that `tempid` will resolve to `upserted-eid` |
1238 | 1245 | (let [tempids' (-> (:tempids report) |
|
1275 | 1282 | (if (datom-added datom) |
1276 | 1283 | (dissoc tempids (:e datom)) |
1277 | 1284 | tempids)) |
1278 | | - unused (reduce reduce-fn all-tempids (:tx-data report))] |
| 1285 | + unused (reduce reduce-fn all-tempids (concat (:tx-data report) (::tx-redundant report)))] |
1279 | 1286 | (if (empty? unused) |
1280 | | - (dissoc report ::value-tempids) |
| 1287 | + (dissoc report ::value-tempids ::tx-redundant) |
1281 | 1288 | (raise "Tempids used only as value in transaction: " (sort (vals unused)) |
1282 | 1289 | {:error :transact/syntax, :tempids unused})))) |
1283 | 1290 |
|
|
1377 | 1384 | (and (keyword? op) |
1378 | 1385 | (not (builtin-fn? op))) |
1379 | 1386 | (if-some [ident (entid db op)] |
1380 | | - (let [fun (-> (-search db [ident :db/fn]) first :v) |
| 1387 | + (let [fun (:v (fsearch db [ident :db/fn])) |
1381 | 1388 | args (next entity)] |
1382 | 1389 | (if (fn? fun) |
1383 | 1390 | (recur report (concat (apply fun db args) entities)) |
|
1441 | 1448 | (raise "Can’t modify tuple attrs directly: " entity |
1442 | 1449 | {:error :transact/syntax, :tx-data entity}) |
1443 | 1450 |
|
1444 | | - |
1445 | 1451 | (= op :db/add) |
1446 | 1452 | (recur (transact-add report entity) entities) |
1447 | 1453 |
|
|
1450 | 1456 | (let [v (if (ref? db a) (entid-strict db v) v)] |
1451 | 1457 | (validate-attr a entity) |
1452 | 1458 | (validate-val v entity) |
1453 | | - (if-some [old-datom (first (-search db [e a v]))] |
| 1459 | + (if-some [old-datom (fsearch db [e a v])] |
1454 | 1460 | (recur (transact-retract-datom report old-datom) entities) |
1455 | 1461 | (recur report entities))) |
1456 | 1462 | (recur report entities)) |
|
0 commit comments