enhance: prefer ldb/transact! than d/transact!

This commit is contained in:
Tienson Qin
2025-10-13 19:03:11 +08:00
parent 787372c174
commit 7ed7fb4f31
17 changed files with 58 additions and 52 deletions

View File

@@ -162,7 +162,7 @@
(testing "insert block with properties"
(let [page "insert-block-properties-test"
_ (page/new-page page)
;; :checkbox, :number, :url, :json can be infered and default to :default, but not for :page
;; :checkbox, :number, :url, :json can be inferred and default to :default, but not for :page
b1 (ls-api-call! :editor.insertBlock page "b1" {:properties {"x1" true
"x2" "https://logseq.com"
"x3" 1

View File

@@ -5,7 +5,7 @@
["path" :as node-path]
[babashka.cli :as cli]
[clojure.edn :as edn]
[datascript.core :as d]
[logseq.db :as ldb]
[logseq.db.common.sqlite-cli :as sqlite-cli]
[logseq.db.sqlite.export :as sqlite-export]
[logseq.outliner.cli :as outliner-cli]
@@ -51,9 +51,9 @@
(count (filter :block/title init-tx)) "blocks ...")
;; (fs/writeFileSync "txs.edn" (with-out-str (cljs.pprint/pprint _txs)))
;; (cljs.pprint/pprint _txs)
(d/transact! conn init-tx)
(when (seq block-props-tx) (d/transact! conn block-props-tx))
(when (seq misc-tx) (d/transact! conn misc-tx))
(ldb/transact! conn init-tx)
(when (seq block-props-tx) (ldb/transact! conn block-props-tx))
(when (seq misc-tx) (ldb/transact! conn misc-tx))
(println (if graph-exists? "Updated graph" "Created graph") (str db-name "!"))
(when (:validate options)
(validate-db/validate-db @conn db-name {:group-errors true :closed-maps true :humanize true}))))

View File

@@ -12,6 +12,7 @@
[datascript.core :as d]
[logseq.common.config :as common-config]
[logseq.common.graph :as common-graph]
[logseq.db :as ldb]
[logseq.db.common.sqlite-cli :as sqlite-cli]
[logseq.db.frontend.asset :as db-asset]
[logseq.graph-parser.exporter :as gp-exporter]
@@ -22,7 +23,7 @@
[promesa.core :as p]))
(def tx-queue (atom cljs.core/PersistentQueue.EMPTY))
(def original-transact! d/transact!)
(def original-transact! ldb/transact!)
(defn dev-transact! [conn tx-data tx-meta]
(swap! tx-queue (fn [queue]
(let [new-queue (conj queue {:tx-data tx-data :tx-meta tx-meta})]

View File

@@ -1791,7 +1791,7 @@
(split-pages-and-properties-tx pages-tx old-properties existing-pages (:import-state options) @(:upstream-properties tx-options))
;; _ (when (seq property-pages-tx) (cljs.pprint/pprint {:property-pages-tx property-pages-tx}))
;; Necessary to transact new property entities first so that block+page properties can be transacted next
main-props-tx-report (d/transact! conn property-pages-tx {::new-graph? true ::path file})
main-props-tx-report (ldb/transact! conn property-pages-tx {::new-graph? true ::path file})
_ (save-from-tx property-pages-tx options)
classes-tx @(:classes-tx tx-options)
@@ -1817,13 +1817,13 @@
;; [:whiteboard-pages :pages-index :page-properties-tx :property-page-properties-tx :pages-tx' :classes-tx :blocks-index :blocks-tx]
;; [whiteboard-pages pages-index page-properties-tx property-page-properties-tx pages-tx' classes-tx blocks-index blocks-tx]))
;; _ (when (not (seq whiteboard-pages)) (cljs.pprint/pprint {#_:property-pages-tx #_property-pages-tx :pages-tx pages-tx :tx tx'}))
main-tx-report (d/transact! conn tx' {::new-graph? true ::path file})
main-tx-report (ldb/transact! conn tx' {::new-graph? true ::path file})
_ (save-from-tx tx' options)
upstream-properties-tx
(build-upstream-properties-tx @conn @(:upstream-properties tx-options) (:import-state options) log-fn)
;; _ (when (seq upstream-properties-tx) (cljs.pprint/pprint {:upstream-properties-tx upstream-properties-tx}))
upstream-tx-report (when (seq upstream-properties-tx) (d/transact! conn upstream-properties-tx {::new-graph? true ::path file}))
upstream-tx-report (when (seq upstream-properties-tx) (ldb/transact! conn upstream-properties-tx {::new-graph? true ::path file}))
_ (save-from-tx upstream-properties-tx options)]
;; Return all tx-reports that occurred in this fn as UI needs to know what changed

View File

@@ -2,6 +2,7 @@
"This script generically runs transactions against the queried blocks"
(:require [clojure.edn :as edn]
[datascript.core :as d]
[logseq.db :as ldb]
[logseq.db.common.sqlite-cli :as sqlite-cli]
[logseq.db.frontend.rules :as rules]
[logseq.outliner.db-pipeline :as db-pipeline]
@@ -30,7 +31,7 @@
(prn (map #(select-keys (d/entity @conn %) [:block/name :block/title]) blocks-to-update)))
(do
(db-pipeline/add-listener conn)
(d/transact! conn update-tx)
(ldb/transact! conn update-tx)
(println "Updated" (count update-tx) "block(s) for graph" (str db-name "!"))))))
(when (= nbb/*file* (nbb/invoked-file))

View File

@@ -5,8 +5,8 @@
["path" :as node-path]
[borkdude.rewrite-edn :as rewrite]
[clojure.string :as string]
[datascript.core :as d]
[logseq.common.config :as common-config]
[logseq.db :as ldb]
[logseq.db.common.sqlite-cli :as sqlite-cli]
[logseq.db.sqlite.build :as sqlite-build]
[logseq.db.sqlite.create-graph :as sqlite-create-graph]
@@ -48,9 +48,9 @@
additional-config
(pretty-print-merge additional-config))
git-sha (get-git-sha)]
(d/transact! conn (sqlite-create-graph/build-db-initial-data config-content
(merge {:import-type import-type}
(when git-sha {:graph-git-sha git-sha}))))))
(ldb/transact! conn (sqlite-create-graph/build-db-initial-data config-content
(merge {:import-type import-type}
(when git-sha {:graph-git-sha git-sha}))))))
(defn init-conn
"Create sqlite DB, initialize datascript connection and sync listener and then

View File

@@ -308,5 +308,5 @@
[conn title opts]
(let [{:keys [tx-meta tx-data title' page-uuid]} (create @conn title opts)]
(when (seq tx-data)
(d/transact! conn tx-data tx-meta)
(ldb/transact! conn tx-data tx-meta)
[title' page-uuid])))

View File

@@ -156,7 +156,7 @@
default-value-tx-data [default-value-block
{:db/id (:db/id property)
:logseq.property/default-value [:block/uuid (:block/uuid default-value-block)]}]]
(d/transact! conn default-value-tx-data)
(ldb/transact! conn default-value-tx-data)
1))
unit (:logseq.property.repeat/recur-unit entity)
property (d/entity db property-ident)

View File

@@ -3,6 +3,7 @@
(:require [datascript.core :as d]
[frontend.common.thread-api :refer [def-thread-api]]
[frontend.worker.state :as worker-state]
[logseq.db :as ldb]
[promesa.core :as p]))
(defonce ^:private encoder (new js/TextEncoder "utf-8"))
@@ -115,7 +116,7 @@
(assert (some? conn) repo)
(let [aes-key-datom (first (d/datoms @conn :avet :aes-key-jwk))]
(assert (nil? aes-key-datom) aes-key-datom)
(d/transact! conn [[:db/add "e1" :aes-key-jwk aes-key-jwk]]))))
(ldb/transact! conn [[:db/add "e1" :aes-key-jwk aes-key-jwk]]))))
(defn get-graph-keys-jwk
[repo]

View File

@@ -65,9 +65,9 @@
(map (fn [id] [:db/add id :logseq.property.embedding/hnsw-label-updated-at 0])))
tx-data (concat remove-old-hnsw-tx-data mark-embedding-tx-data)]
(when (seq tx-data)
(d/transact! conn tx-data
{:skip-refresh? true
:pipeline-replace? true})))))
(ldb/transact! conn tx-data
{:skip-refresh? true
:pipeline-replace? true})))))
(defn listen-db-changes!
[repo conn & {:keys [handler-keys]}]

View File

@@ -255,8 +255,8 @@
(doseq [db (if @*publishing? [sqlite-db] [sqlite-db client-ops-db])]
(sqlite-gc/gc-kvs-table! db {:full-gc? full-gc?})
(.exec db "VACUUM"))
(d/transact! datascript-conn [{:db/ident :logseq.kv/graph-last-gc-at
:kv/value (common-util/time-ms)}]))))
(ldb/transact! datascript-conn [{:db/ident :logseq.kv/graph-last-gc-at
:kv/value (common-util/time-ms)}]))))
(defn- create-or-open-db!
[repo {:keys [config datoms] :as opts}]
@@ -295,7 +295,7 @@
(let [config (or config "")
initial-data (sqlite-create-graph/build-db-initial-data
config (select-keys opts [:import-type :graph-git-sha]))]
(d/transact! conn initial-data {:initial-db? true})))
(ldb/transact! conn initial-data {:initial-db? true})))
(gc-sqlite-dbs! db client-ops-db conn {})

View File

@@ -179,7 +179,7 @@
(into-array (map :db/id stale-block-chunk))
false))
tx-data (labels-update-tx-data @conn e+updated-at-coll)]
(d/transact! conn tx-data {:skip-refresh? true})
(ldb/transact! conn tx-data {:skip-refresh? true})
(m/? (task--update-index-info!* repo infer-worker true))
(c.m/<? (.write-index! infer-worker repo))))
(m/? (task--update-index-info!* repo infer-worker false))))))))
@@ -207,7 +207,7 @@
(d/datoms @conn :avet :block/title)
(map (fn [d]
[:db/add (:e d) :logseq.property.embedding/hnsw-label-updated-at 0])))]
(d/transact! conn mark-embedding-tx-data {:skip-refresh? true})))
(ldb/transact! conn mark-embedding-tx-data {:skip-refresh? true})))
(embedding-stale-blocks! repo reset-embedding?)))))
@@ -236,7 +236,7 @@
(when-let [^js infer-worker @worker-state/*infer-worker]
(let [conn (worker-state/get-datascript-conn repo)]
(when (c.m/<? (.load-model infer-worker model-name))
(d/transact! conn [(ldb/kv :logseq.kv/graph-text-embedding-model-name model-name)])
(ldb/transact! conn [(ldb/kv :logseq.kv/graph-text-embedding-model-name model-name)])
(log/info :loaded-model model-name))))))
(defn task--search

View File

@@ -14,6 +14,7 @@
[frontend.worker.rtc.ws-util :as ws-util]
[frontend.worker.state :as worker-state]
[logseq.common.path :as path]
[logseq.db :as ldb]
[malli.core :as ma]
[missionary.core :as m]))
@@ -144,11 +145,11 @@
(throw (ex-info "upload asset failed" r)))
;; asset might be deleted by the user before uploaded successfully
(when (d/entity @conn [:block/uuid asset-uuid])
(d/transact! conn
[{:block/uuid asset-uuid
:logseq.property.asset/remote-metadata {:checksum checksum :type asset-type}}]
(ldb/transact! conn
[{:block/uuid asset-uuid
:logseq.property.asset/remote-metadata {:checksum checksum :type asset-type}}]
;; Don't generate rtc ops again, (block-ops & asset-ops)
{:persist-op? false}))
{:persist-op? false}))
(client-op/remove-asset-op repo asset-uuid))))
(c.m/concurrent-exec-flow 3 (m/seed asset-uuid->url))
(m/reduce (constantly nil))))

View File

@@ -5,6 +5,7 @@
[frontend.worker.rtc.malli-schema :as rtc-schema]
[frontend.worker.state :as worker-state]
[lambdaisland.glogi :as log]
[logseq.db :as ldb]
[logseq.db.sqlite.util :as sqlite-util]
[malli.core :as ma]
[malli.transform :as mt]
@@ -101,7 +102,7 @@
[repo graph-uuid]
{:pre [(some? graph-uuid)]}
(when-let [conn (worker-state/get-client-ops-conn repo)]
(d/transact! conn [[:db/add "e" :graph-uuid graph-uuid]])))
(ldb/transact! conn [[:db/add "e" :graph-uuid graph-uuid]])))
(defn get-graph-uuid
[repo]
@@ -117,13 +118,13 @@
(if-let [datom (first (d/datoms @conn :avet :local-tx))]
[:db/add (:e datom) :local-tx t]
[:db/add "e" :local-tx t])]
(d/transact! conn [tx-data]))))
(ldb/transact! conn [tx-data]))))
(defn remove-local-tx
[repo]
(when-let [conn (worker-state/get-client-ops-conn repo)]
(when-let [datom (first (d/datoms @conn :avet :local-tx))]
(d/transact! conn [[:db/retract (:e datom) :local-tx]]))))
(ldb/transact! conn [[:db/retract (:e datom) :local-tx]]))))
(defn get-local-tx
[repo]
@@ -291,7 +292,7 @@
tx-data2 (when (seq update-kv-value-ops) (generate-ident-kv-ops-tx-data @conn update-kv-value-ops))
tx-data3 (when (seq rename-db-ident-ops) (generate-rename-db-ident-ops-tx-data rename-db-ident-ops))]
(when-let [tx-data (not-empty (concat tx-data1 tx-data2 tx-data3))]
(d/transact! conn tx-data)))))
(ldb/transact! conn tx-data)))))
(defn- get-all-block-ops*
"Return e->op-map"
@@ -352,7 +353,7 @@
(let [e->op-map (get-all-block-ops* @conn)
retract-all-tx-data (mapcat (fn [e] (map (fn [a] [:db.fn/retractAttribute e a]) block-op-types))
(keys e->op-map))]
(d/transact! conn retract-all-tx-data)
(ldb/transact! conn retract-all-tx-data)
(vals e->op-map)))
(defn- get&remove-all-update-kv-value-ops*
@@ -360,7 +361,7 @@
(let [e->op-map (get-all-update-kv-value-ops* @conn)
retract-all-tx-data (mapcat (fn [e] (map (fn [a] [:db.fn/retractAttribute e a]) update-kv-value-op-types))
(keys e->op-map))]
(d/transact! conn retract-all-tx-data)
(ldb/transact! conn retract-all-tx-data)
(vals e->op-map)))
(defn- get&remove-all-rename-db-ident-ops*
@@ -368,7 +369,7 @@
(let [e->op-map (get-all-rename-db-ident-ops* @conn)
retract-all-tx-data (mapcat (fn [e] (map (fn [a] [:db.fn/retractAttribute e a]) db-ident-rename-op-types))
(keys e->op-map))]
(d/transact! conn retract-all-tx-data)
(ldb/transact! conn retract-all-tx-data)
(vals e->op-map)))
(defn get-all-block-ops
@@ -468,7 +469,7 @@
(cond-> [{:block/uuid block-uuid
:remove-asset op}]
update-asset-op (conj [:db.fn/retractAttribute e :update-asset]))))))]
(d/transact! conn tx-data)))))))
(ldb/transact! conn tx-data)))))))
(defn add-all-exists-asset-as-ops
[repo]
@@ -516,7 +517,7 @@
(when-let [conn (worker-state/get-client-ops-conn repo)]
(let [ent (d/entity @conn [:block/uuid asset-uuid])]
(when-let [e (:db/id ent)]
(d/transact! conn (map (fn [a] [:db.fn/retractAttribute e a]) asset-op-types))))))
(ldb/transact! conn (map (fn [a] [:db.fn/retractAttribute e a]) asset-op-types))))))
(defn create-pending-asset-ops-count-flow
[repo]

View File

@@ -156,9 +156,9 @@
(defn- update-remote-schema-version!
[conn server-schema-version]
(when server-schema-version
(d/transact! conn [(ldb/kv :logseq.kv/remote-schema-version server-schema-version)]
{:gen-undo-ops? false
:persist-op? false})))
(ldb/transact! conn [(ldb/kv :logseq.kv/remote-schema-version server-schema-version)]
{:gen-undo-ops? false
:persist-op? false})))
(defonce ^:private *rtc-lock (atom nil))
(defn- holding-rtc-lock

View File

@@ -1,14 +1,15 @@
(ns frontend.worker.rtc.db
"rtc db ops"
(:require [datascript.core :as d]
[frontend.worker.state :as worker-state]))
[frontend.worker.state :as worker-state]
[logseq.db :as ldb]))
(defn remove-rtc-data-from-local-db!
[repo]
(when-let [conn (worker-state/get-datascript-conn repo)]
(d/transact! conn [[:db/retractEntity :logseq.kv/graph-uuid]
[:db/retractEntity :logseq.kv/graph-local-tx]
[:db/retractEntity :logseq.kv/remote-schema-version]])))
(ldb/transact! conn [[:db/retractEntity :logseq.kv/graph-uuid]
[:db/retractEntity :logseq.kv/graph-local-tx]
[:db/retractEntity :logseq.kv/remote-schema-version]])))
(defn reset-client-op-conn
[repo]
@@ -18,7 +19,7 @@
(d/datoms @conn :avet :aes-key-jwk)
(d/datoms @conn :avet :block/uuid))
(map (fn [datom] [:db/retractEntity (:e datom)])))]
(d/transact! conn tx-data))))
(ldb/transact! conn tx-data))))
(defn remove-rtc-data-in-conn!
[repo]

View File

@@ -215,11 +215,11 @@
(when-let [conn (worker-state/get-datascript-conn repo)]
(let [db @conn]
(when-let [schema-version (:kv/value (d/entity db :logseq.kv/schema-version))]
(d/transact! conn
[(ldb/kv :logseq.kv/remote-schema-version schema-version)]
{:rtc-download-graph? true
:gen-undo-ops? false
:persist-op? false})))))
(ldb/transact! conn
[(ldb/kv :logseq.kv/remote-schema-version schema-version)]
{:rtc-download-graph? true
:gen-undo-ops? false
:persist-op? false})))))
(defn- <transact-block-refs!
[repo graph-uuid]