From bcd04dba05be5090b45ff7e2212cad46e586e8b5 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Thu, 25 Apr 2024 15:57:14 +0800 Subject: [PATCH] fix: file graph issues --- deps/db/src/logseq/db/sqlite/common_db.cljs | 93 +++++++++++---------- src/main/frontend/components/editor.cljs | 5 +- src/main/frontend/search/browser.cljs | 3 +- src/main/frontend/worker/search.cljs | 29 ++++--- 4 files changed, 70 insertions(+), 60 deletions(-) diff --git a/deps/db/src/logseq/db/sqlite/common_db.cljs b/deps/db/src/logseq/db/sqlite/common_db.cljs index f4dd54129..659ad6d79 100644 --- a/deps/db/src/logseq/db/sqlite/common_db.cljs +++ b/deps/db/src/logseq/db/sqlite/common_db.cljs @@ -6,7 +6,8 @@ [logseq.db.sqlite.util :as sqlite-util] [logseq.common.util.date-time :as date-time-util] [logseq.common.util :as common-util] - [logseq.common.config :as common-config])) + [logseq.common.config :as common-config] + [logseq.db.frontend.entity-plus :as entity-plus])) (defn- get-pages-by-name [db page-name] @@ -88,50 +89,51 @@ (defn- property-with-values [db block] - (let [block (d/entity db (:db/id block)) - class-properties (when (contains? (:block/type block) "class") - (let [property-ids (map :db/id (:class/schema.properties block))] - (when (seq property-ids) - (d/pull-many db '[*] property-ids)))) - block-properties (when (seq (:block/raw-properties block)) - (let [pairs (d/pull-many db '[*] (map :db/id (:block/raw-properties block)))] - (mapcat - (fn [pair] - (let [property (d/entity db (:db/id (:property/pair-property pair))) - property-values (get pair (:db/ident property)) - values (if (and (coll? property-values) - (map? (first property-values))) - property-values - #{property-values}) - value-ids (when (every? map? values) - (->> (map :db/id values) - (filter (fn [id] (or (int? id) (keyword? id)))))) - value-blocks (->> - (when (seq value-ids) - (mapcat - (fn [id] - (let [b (d/entity db id)] - (cons (d/pull db '[*] id) - (let [ids (map :db/id (:block/raw-properties b))] - (when (seq ids) - (d/pull-many db '[*] ids)))))) - value-ids)) + (when (entity-plus/db-based-graph? db) + (let [block (d/entity db (:db/id block)) + class-properties (when (contains? (:block/type block) "class") + (let [property-ids (map :db/id (:class/schema.properties block))] + (when (seq property-ids) + (d/pull-many db '[*] property-ids)))) + block-properties (when (seq (:block/raw-properties block)) + (let [pairs (d/pull-many db '[*] (map :db/id (:block/raw-properties block)))] + (mapcat + (fn [pair] + (let [property (d/entity db (:db/id (:property/pair-property pair))) + property-values (get pair (:db/ident property)) + values (if (and (coll? property-values) + (map? (first property-values))) + property-values + #{property-values}) + value-ids (when (every? map? values) + (->> (map :db/id values) + (filter (fn [id] (or (int? id) (keyword? id)))))) + value-blocks (->> + (when (seq value-ids) + (mapcat + (fn [id] + (let [b (d/entity db id)] + (cons (d/pull db '[*] id) + (let [ids (map :db/id (:block/raw-properties b))] + (when (seq ids) + (d/pull-many db '[*] ids)))))) + value-ids)) ;; FIXME: why d/pull returns {:db/id db-ident} instead of {:db/id number-eid}? - (map (fn [block] - (let [from-property-id (get-in block [:logseq.property/created-from-property :db/id])] - (if (keyword? from-property-id) - (assoc-in block [:logseq.property/created-from-property :db/id] (:db/id (d/entity db from-property-id))) - block))))) - page (when (seq values) - (when-let [page-id (:db/id (:block/page (d/entity db (:db/id (first values)))))] - (d/pull db '[*] page-id))) - property' (d/pull db '[*] (:db/id property))] - (remove nil? (concat [page] - [property'] - value-blocks - [pair])))) - pairs)))] - (concat class-properties block-properties))) + (map (fn [block] + (let [from-property-id (get-in block [:logseq.property/created-from-property :db/id])] + (if (keyword? from-property-id) + (assoc-in block [:logseq.property/created-from-property :db/id] (:db/id (d/entity db from-property-id))) + block))))) + page (when (seq values) + (when-let [page-id (:db/id (:block/page (d/entity db (:db/id (first values)))))] + (d/pull db '[*] page-id))) + property' (d/pull db '[*] (:db/id property))] + (remove nil? (concat [page] + [property'] + value-blocks + [pair])))) + pairs)))] + (concat class-properties block-properties)))) (defn get-block-and-children [db id children?] @@ -261,7 +263,8 @@ latest-journals (get-latest-journals db 3) all-files (get-all-files db) home-page-data (get-home-page db all-files) - structured-blocks (get-structured-blocks db) + structured-blocks (when (entity-plus/db-based-graph? db) + (get-structured-blocks db)) data (concat idents structured-blocks favorites latest-journals all-files home-page-data)] {:schema schema :initial-data data})) diff --git a/src/main/frontend/components/editor.cljs b/src/main/frontend/components/editor.cljs index dbdd8c645..808ab7a47 100644 --- a/src/main/frontend/components/editor.cljs +++ b/src/main/frontend/components/editor.cljs @@ -248,10 +248,9 @@ :on-enter non-exist-block-handler :empty-placeholder [:div.text-gray-500.text-sm.px-4.py-2 (t :editor/block-search)] :item-render (fn [{:block/keys [page uuid]}] ;; content returned from search engine is normalized - (let [page (or (:block/original-name page) - (:block/name page)) + (let [page-entity (db/entity [:block/uuid page]) repo (state/sub :git/current-repo) - format (db/get-page-format page) + format (get page-entity :block/format :markdown) block (db-model/query-block-by-uuid uuid) content (:block/content block)] (when-not (string/blank? content) diff --git a/src/main/frontend/search/browser.cljs b/src/main/frontend/search/browser.cljs index e9bd1ea14..1fcac4215 100644 --- a/src/main/frontend/search/browser.cljs +++ b/src/main/frontend/search/browser.cljs @@ -17,7 +17,8 @@ (p/let [result (.search-blocks sqlite (state/get-current-repo) q (bean/->js option)) result (bean/->clj result)] (keep (fn [{:keys [content page] :as block}] - {:block/uuid (uuid (:uuid block)) + {:page? (= (:uuid block) page) + :block/uuid (uuid (:uuid block)) :block/content content :block/page (uuid page)}) result)) (p/resolved nil))) diff --git a/src/main/frontend/worker/search.cljs b/src/main/frontend/worker/search.cljs index 104b3ca95..d04840c42 100644 --- a/src/main/frontend/worker/search.cljs +++ b/src/main/frontend/worker/search.cljs @@ -10,7 +10,8 @@ [frontend.worker.util :as worker-util] [logseq.db.sqlite.util :as sqlite-util] [logseq.common.util :as common-util] - [logseq.db :as ldb])) + [logseq.db :as ldb] + [cljs-bean.core :as bean])) ;; TODO: use sqlite for fuzzy search (defonce indices (atom nil)) @@ -81,10 +82,14 @@ [^Object db blocks] (.transaction db (fn [tx] (doseq [item blocks] - (.exec tx #js {:sql "INSERT INTO blocks (id, content, page) VALUES ($id, $content, $page) ON CONFLICT (id) DO UPDATE SET (content, page) = ($content, $page)" - :bind #js {:$id (.-id item) - :$content (.-content item) - :$page (.-page item)}}))))) + (if (and (common-util/uuid-string? (.-id item)) + (common-util/uuid-string? (.-page item))) + (.exec tx #js {:sql "INSERT INTO blocks (id, content, page) VALUES ($id, $content, $page) ON CONFLICT (id) DO UPDATE SET (content, page) = ($content, $page)" + :bind #js {:$id (.-id item) + :$content (.-content item) + :$page (.-page item)}}) + (throw (ex-info "Search upsert-blocks wrong data: " + (bean/->clj item)))))))) (defn delete-blocks! [db ids] @@ -158,10 +163,11 @@ matched-result (search-blocks-aux db match-sql match-input page limit) non-match-result (search-blocks-aux db non-match-sql non-match-input page limit) all-result (->> (concat matched-result non-match-result) - (map (fn [[id page _content snippet]] - {:uuid id - :content snippet - :page page})))] + (map (fn [result] + (let [[id page _content snippet] result] + {:uuid id + :content snippet + :page page}))))] (->> all-result (common-util/distinct-by :uuid) @@ -252,8 +258,9 @@ (str content (when (not= content "") "\n") (get-db-properties-str db properties)) content)] (when-not (string/blank? content') + (assert (or (:block/uuid page) uuid)) {:id (str uuid) - :page (str (:block/uuid page)) + :page (str (or (:block/uuid page) uuid)) :content (sanitize content') :format format}))))) @@ -264,7 +271,7 @@ {:db/id (:db/id e) :block/name (:block/name e) :block/uuid id - :block/page (:db/id (:block/page e)) + :block/page (:block/page e) :block/content (:block/content e) :block/format (:block/format e) :block/properties (:block/properties e)})))