enhance: Use path-refs calculation in CLI namespaces

Also moved path-refs truncation into outliner so that it applies consistently
across environments
pull/10438/head
Gabriel Horner 2023-09-08 11:51:49 -04:00
parent 779b7417fb
commit 5f66ca027e
5 changed files with 39 additions and 36 deletions

View File

@ -90,7 +90,7 @@
;; Steps:
;; 1. For each changed block, new-refs = its page + :block/refs + parents :block/refs
;; 2. Its children' block/path-refs might need to be updated too.
(defn compute-block-path-refs
(defn- compute-block-path-refs
[{:keys [db-before db-after]} blocks*]
(let [blocks (remove :block/name blocks*)
*computed-ids (atom #{})]
@ -143,3 +143,9 @@
:block/path-refs new-refs}])
children-refs))))
blocks)))
(defn compute-block-path-refs-tx
[tx-report blocks]
(let [refs-tx (compute-block-path-refs tx-report blocks)
truncate-refs-tx (map (fn [m] [:db/retract (:db/id m) :block/path-refs]) refs-tx)]
(concat truncate-refs-tx refs-tx)))

View File

@ -109,21 +109,16 @@
#(swap! current-db-id dec))
(defn- ->block-tx [m uuid-maps property-db-ids page-id last-block]
(let [property-refs (when (seq (:properties m))
(build-property-refs (:properties m) property-db-ids))]
(merge (dissoc m :properties)
(sqlite-util/block-with-timestamps
{:db/id (new-db-id)
:block/format :markdown
:block/path-refs (cond-> [{:db/id page-id}]
(seq (:properties m))
(into property-refs))
:block/page {:db/id page-id}
:block/left {:db/id (or (:db/id last-block) page-id)}
:block/parent {:db/id page-id}})
(when (seq (:properties m))
{:block/properties (->block-properties-tx (:properties m) uuid-maps)
:block/refs property-refs}))))
(merge (dissoc m :properties)
(sqlite-util/block-with-timestamps
{:db/id (new-db-id)
:block/format :markdown
:block/page {:db/id page-id}
:block/left {:db/id (or (:db/id last-block) page-id)}
:block/parent {:db/id page-id}})
(when (seq (:properties m))
{:block/properties (->block-properties-tx (:properties m) uuid-maps)
:block/refs (build-property-refs (:properties m) property-db-ids)})))
(defn create-blocks-tx
"Given an EDN map for defining pages, blocks and properties, this creates a

View File

@ -2,7 +2,7 @@
"This ns allows DB graphs to persist datascript changes to their respective
sqlite db. Since changes are persisted, this can be used to create or update graphs.
Known limitations:
* Changes to block references don't update :block/path-refs"
* Deleted blocks don't update effected :block/tx-id"
(:require [datascript.core :as d]
[logseq.db.sqlite.db :as sqlite-db]
[logseq.db.sqlite.util :as sqlite-util]
@ -11,14 +11,19 @@
[logseq.outliner.pipeline :as outliner-pipeline]))
(defn- invoke-hooks
"Modified copy frontend.modules.outliner.pipeline/invoke-hooks that doesn't
handle :block/path-refs recalculation"
[{:keys [db-after] :as tx-report}]
(let [{:keys [blocks]} (ds-report/get-blocks-and-pages tx-report)
deleted-block-uuids (set (outliner-pipeline/filter-deleted-blocks (:tx-data tx-report)))
upsert-blocks (outliner-pipeline/build-upsert-blocks blocks deleted-block-uuids db-after)]
{:blocks upsert-blocks
:deleted-block-uuids deleted-block-uuids}))
"Modified copy of frontend.modules.outliner.pipeline/invoke-hooks that doesn't
handle :block/tx-id"
[conn {:keys [db-after] :as tx-report}]
(when (not (get-in tx-report [:tx-meta :replace?]))
(let [{:keys [blocks]} (ds-report/get-blocks-and-pages tx-report)
block-path-refs-tx (outliner-pipeline/compute-block-path-refs-tx tx-report blocks)
db-after' (if (seq block-path-refs-tx)
(:db-after (d/transact! conn block-path-refs-tx {:replace? true}))
db-after)
deleted-block-uuids (set (outliner-pipeline/filter-deleted-blocks (:tx-data tx-report)))
upsert-blocks (outliner-pipeline/build-upsert-blocks blocks deleted-block-uuids db-after')]
{:blocks upsert-blocks
:deleted-block-uuids deleted-block-uuids})))
(defn- update-sqlite-db
"Modified copy of :db-transact-data defmethod in electron.handler"
@ -34,4 +39,4 @@
sqlite db name"
[conn db-name]
(d/listen! conn :persist-to-sqlite (fn persist-to-sqlite [tx-report]
(update-sqlite-db db-name (invoke-hooks tx-report)))))
(update-sqlite-db db-name (invoke-hooks conn tx-report)))))

View File

@ -17,10 +17,10 @@
(not (get-in tx-report [:tx-meta :created-from-journal-template?])))
(file/sync-to-file page (:outliner-op (:tx-meta tx-report)))))
(defn compute-block-path-refs
(defn compute-block-path-refs-tx
[{:keys [tx-meta] :as tx-report} blocks]
(when (and (:outliner-op tx-meta) (react/path-refs-need-recalculated? tx-meta))
(outliner-pipeline/compute-block-path-refs tx-report blocks)))
(outliner-pipeline/compute-block-path-refs-tx tx-report blocks)))
(defn invoke-hooks
[tx-report]
@ -30,11 +30,9 @@
(not (:replace? tx-meta)))
(let [{:keys [pages blocks]} (ds-report/get-blocks-and-pages tx-report)
repo (state/get-current-repo)
refs-tx (util/profile
tx (util/profile
"Compute path refs: "
(set (compute-block-path-refs tx-report blocks)))
truncate-refs-tx (map (fn [m] [:db/retract (:db/id m) :block/path-refs]) refs-tx)
tx (util/concat-without-nil truncate-refs-tx refs-tx)
(set (compute-block-path-refs-tx tx-report blocks)))
tx-report' (if (seq tx)
(let [refs-tx-data' (:tx-data (db/transact! repo tx {:outliner/transact? true
:replace? true}))]

View File

@ -15,7 +15,7 @@
(map first)))
;; TODO: Move this test to outliner dep when there is a load-test-files helper for deps
(deftest compute-block-path-refs
(deftest compute-block-path-refs-tx
(load-test-files [{:file/path "pages/page1.md"
:file/content "prop:: #bar
- parent #foo
@ -32,9 +32,8 @@
:block/path-refs [{:db/id new-tag-id}])
%)
blocks)
refs-tx (pipeline/compute-block-path-refs {:tx-meta {:outliner-op :save-block} :db-after @conn} modified-blocks)
_ (d/transact! conn (concat (map (fn [m] [:db/retract (:db/id m) :block/path-refs]) refs-tx)
refs-tx))
refs-tx (pipeline/compute-block-path-refs-tx {:tx-meta {:outliner-op :save-block} :db-after @conn} modified-blocks)
_ (d/transact! conn refs-tx)
updated-blocks (->> (get-blocks @conn)
(map #(hash-map :block/content (:block/content %)
:path-ref-names (mapv :block/name (:block/path-refs %)))))]