Tienson Qin 1 неделя назад
Родитель
Сommit
5620af9a4e

+ 1 - 1
deps/db-sync/src/logseq/db_sync/order.cljs

@@ -7,7 +7,7 @@
         updates (->> tx-data
                      (filter (fn [[e a v _tx added]]
                                (and (= a :block/order) added e v
-                                    (:block/uuid (d/entity @conn e))))))
+                                    (:block/uuid (d/entity db e))))))
         groups (group-by (fn [{:keys [e v]}]
                            (let [parent (:block/parent (d/entity db e))]
                              [(:db/id parent) v]))

+ 42 - 17
deps/db-sync/src/logseq/db_sync/storage.cljs

@@ -97,6 +97,24 @@
                    created-at
                    (outliner-op->sql outliner-op)))
 
+(defn- with-sql-transaction!
+  [sql transaction-sync-f f]
+  (if (fn? transaction-sync-f)
+    (transaction-sync-f f)
+    (do
+      ;; Node sqlite wrapper supports SQL transaction statements directly.
+      ;; Durable Objects sqlite must use state.storage.transactionSync().
+      (common/sql-exec sql "BEGIN IMMEDIATE")
+      (try
+        (let [result (f)]
+          (common/sql-exec sql "COMMIT")
+          result)
+        (catch :default error
+          (try
+            (common/sql-exec sql "ROLLBACK")
+            (catch :default _))
+          (throw error))))))
+
 (defn fetch-tx-since [sql since-t]
   (let [rows (common/get-sql-rows
               (common/sql-exec sql
@@ -144,29 +162,36 @@
       (restore-data-from-addr sql addr))))
 
 (defn- append-tx-for-tx-report
-  [sql {:keys [db-after db-before tx-data tx-meta] :as tx-report}]
-  (let [new-t (next-t! sql)
-        created-at (common/now-ms)
-        prev-checksum (get-checksum sql)
-        checksum (sync-checksum/update-checksum prev-checksum tx-report)
+  [sql transaction-sync-f {:keys [db-after db-before tx-data tx-meta] :as tx-report}]
+  (let [created-at (common/now-ms)
         normalized-data (->> tx-data
                              (db-normalize/normalize-tx-data db-after db-before))
         ;; _ (prn :debug :tx-data tx-data)
         ;; _ (prn :debug :normalized-data normalized-data)
         tx-str (common/write-transit normalized-data)]
-    (set-checksum! sql checksum)
-    (append-tx! sql new-t tx-str created-at (:outliner-op tx-meta))))
+    (with-sql-transaction!
+      sql
+      transaction-sync-f
+      (fn []
+        (let [new-t (next-t! sql)
+              prev-checksum (get-checksum sql)
+              checksum (sync-checksum/update-checksum prev-checksum tx-report)]
+          (set-checksum! sql checksum)
+          (append-tx! sql new-t tx-str created-at (:outliner-op tx-meta)))))))
 
 (defn- listen-db-updates!
-  [sql conn]
+  [sql conn transaction-sync-f]
   (d/listen! conn ::listen-db-updates
              (fn [tx-report]
-               (append-tx-for-tx-report sql tx-report))))
-
-(defn open-conn [sql]
-  (init-schema! sql)
-  (let [storage (new-sqlite-storage sql)
-        schema db-schema/schema
-        conn (common-sqlite/get-storage-conn storage schema)]
-    (listen-db-updates! sql conn)
-    conn))
+               (append-tx-for-tx-report sql transaction-sync-f tx-report))))
+
+(defn open-conn
+  ([sql]
+   (open-conn sql nil))
+  ([sql {:keys [transaction-sync-f]}]
+   (init-schema! sql)
+   (let [storage (new-sqlite-storage sql)
+         schema db-schema/schema
+         conn (common-sqlite/get-storage-conn storage schema)]
+     (listen-db-updates! sql conn transaction-sync-f)
+     conn)))

+ 7 - 1
deps/db-sync/src/logseq/db_sync/worker/handler/sync.cljs

@@ -48,7 +48,13 @@
 (defn- ensure-conn! [^js self]
   (ensure-schema! self)
   (when-not (.-conn self)
-    (set! (.-conn self) (storage/open-conn (.-sql self)))))
+    (let [storage (some-> self .-state .-storage)
+          transaction-sync-f (when (fn? (some-> storage .-transactionSync))
+                               (fn [f]
+                                 (.transactionSync storage f)))]
+      (set! (.-conn self)
+            (storage/open-conn (.-sql self)
+                               {:transaction-sync-f transaction-sync-f})))))
 
 (defn t-now [^js self]
   (ensure-schema! self)

+ 7 - 7
deps/db/src/logseq/db.cljs

@@ -211,14 +211,14 @@
     (try
       (batch-tx-fn temp-conn *batch-tx-data)
       (vreset! *complete? true)
-      (finally
-        (let [tx-data @*batch-tx-data]
-          (d/unlisten! temp-conn ::temp-conn-batch-tx)
-          (reset! temp-conn nil)
-          (vreset! *batch-tx-data nil)
-          (when (and @*complete? (seq tx-data))
+      (let [tx-data @*batch-tx-data]
+        (when (and @*complete? (seq tx-data))
             ;; transact tx-data to `conn` and validate db
-            (transact! conn tx-data tx-meta)))))))
+          (transact! conn tx-data tx-meta)))
+      (finally
+        (d/unlisten! temp-conn ::temp-conn-batch-tx)
+        (reset! temp-conn nil)
+        (vreset! *batch-tx-data nil)))))
 
 (defn batch-transact!
   "Store once for a batch transaction, notice that this fn doesn't support nest `batch-transact` calls"

+ 3 - 1
src/main/frontend/worker/pipeline.cljs

@@ -437,7 +437,9 @@
                                         (toggle-page-and-block db tx-report))
         display-blocks-tx-data (add-missing-properties-to-typed-display-blocks db-after tx-data tx-meta)
         ensure-query-tx-data (ensure-query-property-on-tag-additions tx-report)
-        commands-tx (when-not (or (:undo? tx-meta) (rtc-tx-or-download-graph? tx-meta))
+        commands-tx (when-not (or (:undo? tx-meta)
+                                  (= :rebase (:outliner-op tx-meta))
+                                  (rtc-tx-or-download-graph? tx-meta))
                       (commands/run-commands tx-report))
         insert-templates-tx (when-not (rtc-tx-or-download-graph? tx-meta)
                               (insert-tag-templates tx-report))

+ 16 - 14
src/main/frontend/worker/sync/apply_txs.cljs

@@ -616,7 +616,9 @@
           block-uuid (:block/uuid block)
           block-ent (when block-uuid
                       (d/entity db [:block/uuid block-uuid]))
-          block-base (dissoc block :db/id)
+          block-base (if (:block/order block-ent)
+                       (dissoc block :db/id :block/order)
+                       (dissoc block :db/id))
           block' (merge block-base
                         (op-construct/rewrite-block-title-with-retracted-refs db block-base))]
       (if (some? block-ent)
@@ -895,21 +897,21 @@
         *rebase-tx-reports (atom [])]
     ;; (prn :debug :apply-remote-tx (first remote-txs))
     (try
-      (ldb/batch-transact!
-       conn
-       tx-meta
-       (fn [conn]
-         (reverse-local-txs! conn local-txs)
+      (let [tx-report (ldb/batch-transact!
+                       conn
+                       tx-meta
+                       (fn [conn]
+                         (reverse-local-txs! conn local-txs)
 
-         (transact-remote-txs! conn remote-txs)
+                         (transact-remote-txs! conn remote-txs)
 
-         (let [rebase-tx-report (rebase-local-txs! repo conn local-txs)]
-           (fix-tx! conn rebase-tx-report {:outliner-op :rebase})))
+                         (rebase-local-txs! repo conn local-txs))
 
-       {:listen-db (fn [{:keys [tx-meta tx-data] :as tx-report}]
-                     (when (and (= :rebase (:outliner-op tx-meta))
-                                (seq tx-data))
-                       (swap! *rebase-tx-reports conj tx-report)))})
+                       {:listen-db (fn [{:keys [tx-meta tx-data] :as tx-report}]
+                                     (when (and (= :rebase (:outliner-op tx-meta))
+                                                (seq tx-data))
+                                       (swap! *rebase-tx-reports conj tx-report)))})]
+        (fix-tx! conn tx-report {:outliner-op :fix}))
 
       (doseq [tx-report @*rebase-tx-reports]
         (handle-local-tx! repo tx-report))
@@ -960,7 +962,7 @@
                                           {:t t
                                            :outliner-op outliner-op
                                            :tx-data-count (count tx-data)
-                                           :tx-data-preview (take 12 tx-data)})
+                                           :tx-data tx-data})
                                         remote-txs)
                       :local-txs (mapv (fn [{:keys [tx-id outliner-op tx reversed-tx]}]
                                          {:tx-id tx-id