Browse Source

fix: wrong data in ui db after applying remote tx data

Tienson Qin 1 month ago
parent
commit
7806997f0b

+ 16 - 5
deps/db/src/logseq/db.cljs

@@ -176,14 +176,23 @@
      ;; Because UI assumes that the in-memory db has all the data except the last one transaction
      (when (seq tx-data)
 
-       ;; (prn :debug :transact :sync? (= d/transact! (or @*transact-fn d/transact!)) :tx-meta tx-meta)
-       ;; (cljs.pprint/pprint tx-data)
+       (prn :debug :transact :sync? (= d/transact! (or @*transact-fn d/transact!)) :tx-meta tx-meta)
+       (cljs.pprint/pprint tx-data)
        ;; (js/console.trace)
 
        (if-let [transact-fn @*transact-fn]
          (transact-fn repo-or-conn tx-data tx-meta)
          (transact-sync repo-or-conn tx-data tx-meta))))))
 
+(defn remove-conflict-datoms
+  [datoms]
+  (->> datoms
+       (group-by (fn [d] (take 4 d))) ; group by '(e a v tx)
+       (keep (fn [[_eavt same-eavt-datoms]]
+               (first (rseq same-eavt-datoms))))
+       ;; sort by :tx, use nth to make this fn works on both vector and datom
+       (sort-by #(nth % 3))))
+
 (defn transact-with-temp-conn!
   "Validate db and store once for a batch transaction, the `temp` conn can still load data from disk,
   however it can't write to the disk."
@@ -207,9 +216,11 @@
       (vreset! *batch-tx-data nil)
       (when (seq tx-data)
         ;; transact tx-data to `conn` and validate db
-        (let [tx-data' (if (fn? filter-tx-data)
-                         (filter-tx-data temp-after-db tx-data)
-                         tx-data)]
+        (let [tx-data' (->>
+                        (if (fn? filter-tx-data)
+                          (filter-tx-data temp-after-db tx-data)
+                          tx-data)
+                        remove-conflict-datoms)]
           (transact! conn tx-data' tx-meta))))))
 
 (def page? common-entity-util/page?)

+ 1 - 4
src/main/frontend/worker/db_sync.cljs

@@ -1182,7 +1182,6 @@
                  (delete-nodes! temp-conn deleted-nodes
                                 (assoc temp-tx-meta :op :delete-blocks))))))
           remote-tx-report @*remote-tx-report]
-
       ;; persist rebase tx to client ops
       (when has-local-changes?
         (when-let [tx-data (seq @*rebase-tx-data)]
@@ -1263,9 +1262,7 @@
                                 tx* (if aes-key
                                       (<decrypt-tx-data aes-key tx)
                                       (p/resolved tx))]
-                          (apply-remote-tx! repo client tx*
-                                            :local-tx local-tx
-                                            :remote-tx remote-tx)
+                          (apply-remote-tx! repo client tx*)
                           (client-op/update-local-tx repo remote-tx)
                           (broadcast-rtc-state! client)
                           (flush-pending! repo client)))))

+ 2 - 11
src/main/frontend/worker/pipeline.cljs

@@ -453,19 +453,10 @@
             fix-page-tags-tx-data
             fix-inline-page-tx-data)))
 
-(defn- remove-conflict-datoms
-  [datoms]
-  (->> datoms
-       (group-by (fn [d] (take 4 d))) ; group by '(e a v tx)
-       (keep (fn [[_eavt same-eavt-datoms]]
-               (first (rseq same-eavt-datoms))))
-       ;; sort by :tx, use nth to make this fn works on both vector and datom
-       (sort-by #(nth % 3))))
-
 (defn transact-pipeline
   "Compute extra tx-data and block/refs, should ensure it's a pure function and
   doesn't call `d/transact!` or `ldb/transact!`."
-  [repo {:keys [db-after tx-meta tx-data] :as tx-report}]
+  [repo {:keys [db-after tx-meta _tx-data] :as tx-report}]
   (when-not (:temp-conn? tx-meta)
     (let [extra-tx-data (compute-extra-tx-data tx-report)
           tx-report* (if (seq extra-tx-data)
@@ -495,7 +486,7 @@
           tx-report' (or replace-tx-report tx-report*)
           full-tx-data (-> (concat (:tx-data tx-report*)
                                    (:tx-data replace-tx-report))
-                           remove-conflict-datoms)]
+                           ldb/remove-conflict-datoms)]
       (assoc tx-report'
              :tx-data full-tx-data
              :tx-meta tx-meta

+ 3 - 3
src/test/frontend/worker/pipeline_test.cljs

@@ -15,14 +15,14 @@
       (is (= (set [[1 :a 1 1]
                    [1 :a 2 1]
                    [2 :a 1 1]])
-             (set (#'worker-pipeline/remove-conflict-datoms datoms))))))
+             (set (ldb/remove-conflict-datoms datoms))))))
   (testing "check block/tags"
     (let [datoms [[163 :block/tags 2 536870930 true]
                   [163 :block/tags 136 536870930 true]
                   [163 :block/tags 136 536870930 false]]]
       (is (= (set [[163 :block/tags 2 536870930 true]
                    [163 :block/tags 136 536870930 false]])
-             (set (#'worker-pipeline/remove-conflict-datoms datoms))))))
+             (set (ldb/remove-conflict-datoms datoms))))))
   (testing "check block/refs"
     (let [datoms [[176 :block/refs 177 536871080 true]
                   [158 :block/refs 21 536871082 false]
@@ -39,7 +39,7 @@
                    [176 :block/refs 177 536871082 true]
                    [177 :block/refs 136 536871082 true]
                    [177 :block/refs 21 536871082 true]])
-             (set (#'worker-pipeline/remove-conflict-datoms datoms)))))))
+             (set (ldb/remove-conflict-datoms datoms)))))))
 
 (deftest test-built-in-page-updates-that-should-be-reverted
   (let [graph test-helper/test-db-name-db-version