From 018f954af19dbf73b21234f2e8e79a73deb2c34d Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Wed, 18 Jun 2025 09:36:03 +0800 Subject: [PATCH] enhance: export sqlite every 30 second and run VACUUM when gc --- src/main/frontend/persist_db.cljs | 34 +++++++++++++++++-------- src/main/frontend/worker/db_worker.cljs | 3 ++- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/frontend/persist_db.cljs b/src/main/frontend/persist_db.cljs index 2b1741d4ac..c6ec2305b0 100644 --- a/src/main/frontend/persist_db.cljs +++ b/src/main/frontend/persist_db.cljs @@ -1,23 +1,24 @@ (ns frontend.persist-db "Backend of DB based graph" - (:require [frontend.persist-db.browser :as browser] + (:require [frontend.config :as config] + [frontend.db :as db] + [frontend.persist-db.browser :as browser] [frontend.persist-db.protocol :as protocol] - [promesa.core :as p] [frontend.state :as state] - [frontend.config :as config] - [frontend.util :as util])) + [frontend.util :as util] + [promesa.core :as p])) (defonce opfs-db (browser/->InBrowser)) - (defn- get-impl +(defn- get-impl "Get the actual implementation of PersistentDB" [] opfs-db) - (defn max-tx +(defonce *last-synced-graph->tx (atom {})) + +(defn- graph-has-changed? + [repo] + (let [tx (@*last-synced-graph->tx repo) + db (db/get-db repo)] + (or (nil? tx) + (> tx (:max-tx db))))) + (defn export-current-graph! [& {:keys [succ-notification? force-save?]}] (when (util/electron?) (when-let [repo (state/get-current-repo)] - (when (or (config/db-based-graph? repo) force-save?) + (when (or (and (config/db-based-graph? repo) (graph-has-changed? repo)) force-save?) (println :debug :save-db-to-disk repo) (-> (p/do! (tx assoc repo (:max-tx (db/get-db repo))) (when succ-notification? (state/pub-event! [:notification/show {:content "The current db has been saved successfully to the disk." @@ -64,5 +76,5 @@ (defn run-export-periodically! [] (js/setInterval export-current-graph! - ;; every 3 minutes - (* 3 60 1000))) + ;; every 30 seconds + (* 30 1000))) diff --git a/src/main/frontend/worker/db_worker.cljs b/src/main/frontend/worker/db_worker.cljs index df49220bd1..5a9a45b7cb 100644 --- a/src/main/frontend/worker/db_worker.cljs +++ b/src/main/frontend/worker/db_worker.cljs @@ -251,7 +251,8 @@ (> (- (common-util/time-ms) last-gc-at) (* 3 24 3600 1000))) ; 3 days ago (println :debug "gc current graph") (doseq [db [sqlite-db client-ops-db]] - (sqlite-gc/gc-kvs-table! db {:full-gc? full-gc?})) + (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)}]))))