Browse Source

refactor: batch-tx

rcmerci 1 year ago
parent
commit
bffd1b5c8e

+ 1 - 2
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -287,8 +287,7 @@
   [[:db/type :string]
    [:schema/version :int]
    [:graph/uuid :string]
-   [:graph/local-tx :string]
-   [:batch-tx/counter :int]])
+   [:graph/local-tx :string]])
 
 (def db-ident-key-val
   "A key-val map consists of a :db/ident and a specific key val"

+ 1 - 3
deps/db/src/logseq/db/sqlite/common_db.cljs

@@ -161,9 +161,7 @@
                          (when id
                            [{:db/id (:db/id e)
                              :db/ident :logseq.kv/graph-uuid
-                             :graph/uuid id}
-                            {:db/ident :logseq.kv/tx-batch-counter
-                             :batch-tx/counter 0}])))
+                             :graph/uuid id}])))
         favorites (get-favorites db)
         latest-journals (get-latest-journals db 3)
         all-files (get-all-files db)

+ 8 - 13
src/main/frontend/worker/batch_tx.clj

@@ -6,16 +6,11 @@
   2. run body
   3. exit batch-tx mode"
   [conn {:keys [additional-tx] :as opts} & body]
-  `(let [tx-batch-counter# (get (d/entity @~conn :logseq.kv/tx-batch-counter) :batch-tx/counter 0)
-         outside-batch?# (zero? tx-batch-counter#)
-         tx-meta# (dissoc ~opts :additional-tx :transact-opts)]
-     (logseq.db/transact! ~conn
-                          [{:db/ident :logseq.kv/tx-batch-counter :batch-tx/counter (inc tx-batch-counter#)}]
-                          {:batch-tx-begin? true})
-     (when outside-batch?# (frontend.worker.batch-tx/set-batch-db-before! @~conn))
-     ~@body
-     (when (seq ~additional-tx)
-       (logseq.db/transact! ~conn ~additional-tx {}))
-     (logseq.db/transact! ~conn [{:db/ident :logseq.kv/tx-batch-counter :batch-tx/counter tx-batch-counter#}]
-       (assoc tx-meta# :batch-tx-end? true))
-     (when outside-batch?# (frontend.worker.batch-tx/exit-batch-txs-mode!))))
+  `(if (some? (frontend.worker.batch-tx/get-batch-db-before))
+     (do ~@body)
+     (let [tx-meta# (dissoc ~opts :additional-tx :transact-opts)]
+       (frontend.worker.batch-tx/set-batch-db-before! @~conn)
+       ~@body
+       (when (seq ~additional-tx)
+         (logseq.db/transact! ~conn ~additional-tx {}))
+       (frontend.worker.batch-tx/exit-batch-txs-mode!))))

+ 4 - 0
src/main/frontend/worker/batch_tx.cljs

@@ -8,6 +8,10 @@
 (sr/defkeyword :batch/txs
   "store all tx-data when batch-processing")
 
+(sr/defkeyword :batch/db-before
+  "store db before batch-tx.
+It can be used to judge if it is batch-processing.")
+
 (defn get-batch-txs
   []
   (->> (:batch/txs @worker-state/*state)

+ 11 - 9
src/main/frontend/worker/db_listener.cljs

@@ -67,19 +67,21 @@
     (prn :listen-db-changes! (keys handlers))
     (d/unlisten! conn ::listen-db-changes!)
     (d/listen! conn ::listen-db-changes!
-               (fn [{:keys [tx-data db-before db-after tx-meta] :as tx-report}]
+               (fn [{:keys [tx-data _db-before _db-after tx-meta] :as tx-report}]
                  (let [pipeline-replace? (:pipeline-replace? tx-meta)
-                       batch-processing? (> (:batch-tx/counter (d/entity db-after :logseq.kv/tx-batch-counter)) 0)]
+                       batch-processing? (some? (batch-tx/get-batch-db-before))]
                    (when-not pipeline-replace?
                      (if batch-processing?
                        (batch-tx/conj-batch-txs! tx-data)
-                       (let [exiting-batch-mode? (> (:batch-tx/counter (d/entity db-before :logseq.kv/tx-batch-counter)) 0)
-                             db-before (if exiting-batch-mode?
-                                         (batch-tx/get-batch-db-before)
-                                         (:db-before tx-report))
-                             tx-data (if exiting-batch-mode?
-                                       (batch-tx/get-batch-txs)
-                                       (concat (:tx-data tx-report) tx-data))
+                       (let [;; exiting-batch-mode? (> (:batch-tx/counter (d/entity db-before :logseq.kv/tx-batch-counter)) 0)
+                             db-before (batch-tx/get-batch-db-before)
+                             ;; (if exiting-batch-mode?
+
+                             ;;   (:db-before tx-report))
+
+                             tx-data (batch-tx/get-batch-txs);; (if exiting-batch-mode?
+
+                                       ;; (concat (:tx-data tx-report) tx-data))
                              tx-report (assoc tx-report
                                               :db-before db-before
                                               :tx-data tx-data)

+ 45 - 44
src/main/frontend/worker/undo_redo.cljs

@@ -246,30 +246,30 @@ when undo this op, this original entity-map will be transacted back into db")
         block-entity (d/entity @conn [:block/uuid block-uuid])]
     (when-not block-entity ;; this block shouldn't exist now
       (when-let [left-entity (d/entity @conn [:block/uuid (:block/left block-entity-map)])]
-        (let [sibling? (not= (:block/left block-entity-map) (:block/parent block-entity-map))
-              r (outliner-tx/transact!
-                 {:gen-undo-ops? false
-                  :outliner-op :insert-blocks
-                  :transact-opts {:repo repo
-                                  :conn conn}}
-                 (outliner-core/insert-blocks! repo conn
-                                               [(cond-> {:block/uuid block-uuid
-                                                         :block/content (:block/content block-entity-map)
-                                                         :block/format :markdown}
-                                                  (:block/created-at block-entity-map)
-                                                  (assoc :block/created-at (:block/created-at block-entity-map))
-
-                                                  (:block/updated-at block-entity-map)
-                                                  (assoc :block/updated-at (:block/updated-at block-entity-map))
-
-                                                  (seq (:block/tags block-entity-map))
-                                                  (assoc :block/tags (some->> (:block/tags block-entity-map)
-                                                                              (map (partial vector :block/uuid))
-                                                                              (d/pull-many @conn [:db/id])
-                                                                              (keep :db/id))))]
-                                               left-entity {:sibling? sibling? :keep-uuid? true}))]
+        (let [sibling? (not= (:block/left block-entity-map) (:block/parent block-entity-map))]
+          (outliner-tx/transact!
+           {:gen-undo-ops? false
+            :outliner-op :insert-blocks
+            :transact-opts {:repo repo
+                            :conn conn}}
+           (outliner-core/insert-blocks! repo conn
+                                         [(cond-> {:block/uuid block-uuid
+                                                   :block/content (:block/content block-entity-map)
+                                                   :block/format :markdown}
+                                            (:block/created-at block-entity-map)
+                                            (assoc :block/created-at (:block/created-at block-entity-map))
+
+                                            (:block/updated-at block-entity-map)
+                                            (assoc :block/updated-at (:block/updated-at block-entity-map))
+
+                                            (seq (:block/tags block-entity-map))
+                                            (assoc :block/tags (some->> (:block/tags block-entity-map)
+                                                                        (map (partial vector :block/uuid))
+                                                                        (d/pull-many @conn [:db/id])
+                                                                        (keep :db/id))))]
+                                         left-entity {:sibling? sibling? :keep-uuid? true}))
           (when (d/entity @conn [:block/uuid block-uuid])
-            [:push-undo-redo r]))))))
+            [:push-undo-redo {}]))))))
 
 (defn- other-children-exist?
   "return true if there are other children existing(not included in `block-entities`)"
@@ -310,14 +310,14 @@ when undo this op, this original entity-map will be transacted back into db")
   (let [[_ {:keys [block-uuid block-origin-left block-origin-parent]}] op]
     (when-let [block-entity (d/entity @conn [:block/uuid block-uuid])]
       (when-let [left-entity (d/entity @conn [:block/uuid block-origin-left])]
-        (let [sibling? (not= block-origin-left block-origin-parent)
-              r (outliner-tx/transact!
-                    {:gen-undo-ops? false
-                     :outliner-op :move-blocks
-                     :transact-opts {:repo repo
-                                     :conn conn}}
-                    (outliner-core/move-blocks! repo conn [block-entity] left-entity sibling?))]
-          (when r [:push-undo-redo r]))))))
+        (let [sibling? (not= block-origin-left block-origin-parent)]
+          (outliner-tx/transact!
+           {:gen-undo-ops? false
+            :outliner-op :move-blocks
+            :transact-opts {:repo repo
+                            :conn conn}}
+           (outliner-core/move-blocks! repo conn [block-entity] left-entity sibling?))
+          [:push-undo-redo {}])))))
 
 (defmethod reverse-apply-op ::update-block
   [op conn repo]
@@ -349,16 +349,16 @@ when undo this op, this original entity-map will be transacted back into db")
                           (assoc :block/collapsed? (boolean block-origin-collapsed))
                           (some? block-origin-link)
                           (assoc :block/link [:block/uuid block-origin-link]))
-              r2 (outliner-tx/transact!
-                  {:gen-undo-ops? false
-                   :outliner-op :save-block
-                   :transact-opts {:repo repo
-                                   :conn conn}}
-                  (outliner-core/save-block! repo conn
-                                             (common-config/get-date-formatter (worker-state/get-config repo))
-                                             new-block))]
+              _ (outliner-tx/transact!
+                 {:gen-undo-ops? false
+                  :outliner-op :save-block
+                  :transact-opts {:repo repo
+                                  :conn conn}}
+                 (outliner-core/save-block! repo conn
+                                            (common-config/get-date-formatter (worker-state/get-config repo))
+                                            new-block))]
 
-          (when r2 [:push-undo-redo r2]))))))
+          [:push-undo-redo {}])))))
 
 (defn- sort&merge-ops
   [ops]
@@ -385,7 +385,8 @@ when undo this op, this original entity-map will be transacted back into db")
   [repo page-block-uuid conn]
   (if-let [ops (not-empty (pop-undo-ops repo page-block-uuid))]
     (let [redo-ops-to-push (transient [])]
-      (batch-tx/with-batch-tx-mode conn {:undo? true}
+      (batch-tx/with-batch-tx-mode conn {:gen-undo-ops? false
+                                         :undo? true}
         (doseq [op ops]
           (let [rev-ops (reverse-op @conn op)
                 r (reverse-apply-op op conn repo)]
@@ -404,7 +405,8 @@ when undo this op, this original entity-map will be transacted back into db")
   [repo page-block-uuid conn]
   (if-let [ops (not-empty (pop-redo-ops repo page-block-uuid))]
     (let [undo-ops-to-push (transient [])]
-      (batch-tx/with-batch-tx-mode conn {:redo? true}
+      (batch-tx/with-batch-tx-mode conn {:gen-undo-ops? false
+                                         :redo? true}
         (doseq [op ops]
           (let [rev-ops (reverse-op @conn op)
                 r (reverse-apply-op op conn repo)]
@@ -510,8 +512,7 @@ when undo this op, this original entity-map will be transacted back into db")
 (defmethod db-listener/listen-db-changes :gen-undo-ops
   [_ {:keys [_tx-data tx-meta db-before db-after
              repo id->attr->datom same-entity-datoms-coll]}]
-  (when (and (:gen-undo-ops? tx-meta true)
-             (not (or (:undo? tx-meta) (:redo? tx-meta))))
+  (when (:gen-undo-ops? tx-meta true)
     (generate-undo-ops repo db-before db-after same-entity-datoms-coll id->attr->datom
                        (:gen-undo-boundary-op? tx-meta true))))