Browse Source

perf: instant result for node search

Tienson Qin 7 months ago
parent
commit
7be6381636

+ 1 - 1
deps/db/src/logseq/db/common/sqlite.cljs

@@ -182,7 +182,7 @@
                                              (:block/_parent block))))
                                        (remove (fn [e] (or (:logseq.property/created-from-property e)
                                                            (:block/closed-value-property e)))))
-                             children-props (or children-props [:db/id :block/uuid :block/parent :block/order :block/collapsed? :block/title])]
+                             children-props (or children-props [:db/id :block/uuid :block/parent :block/order :block/collapsed? :block/title :logseq.task/status])]
                          (map
                           (fn [block]
                             (if (= children-props '[*])

+ 5 - 5
src/main/frontend/components/editor.cljs

@@ -147,7 +147,7 @@
                                       (editor-handler/get-matched-classes q)
                                       (editor-handler/<get-matched-blocks q {:nlp-pages? true}))]
                        (set-matched-pages! result))))]
-    (hooks/use-effect! search-f [(hooks/use-debounced-value q 50)])
+    (hooks/use-effect! search-f [(hooks/use-debounced-value q 150)])
 
     (let [matched-pages' (if (string/blank? q)
                            (if db-tag?
@@ -183,11 +183,11 @@
          :on-enter    (fn []
                         (page-handler/page-not-exists-handler input id q current-pos))
          :item-render (fn [block _chosen?]
-                        (let [block (if (and (:block/uuid block) (not (de/entity? block)))
-                                      (db/entity [:block/uuid (:block/uuid block)])
+                        (let [block (if-let [id (:block/uuid block)]
+                                      (or (db/entity [:block/uuid id]) block)
                                       block)]
                           [:div.flex.flex-col
-                           (when-not (db/page? block)
+                           (when (and (not (:page? block)) (:block/uuid block))
                              (when-let [breadcrumb (state/get-component :block/breadcrumb)]
                                [:div.text-xs.opacity-70.mb-1 {:style {:margin-left 3}}
                                 (breadcrumb {:search? true} (state/get-current-repo) (:block/uuid block) {})]))
@@ -207,7 +207,7 @@
                                  (db-model/whiteboard-page? block)
                                  (ui/icon "whiteboard" {:extension? true})
 
-                                 (db/page? block)
+                                 (:page? block)
                                  (ui/icon "page" {:extension? true})
 
                                  (or (string/starts-with? (str (:block/title block)) (t :new-tag))

+ 3 - 13
src/main/frontend/handler/editor.cljs

@@ -1752,19 +1752,9 @@
   "Return matched blocks that are not built-in"
   [q & [{:keys [nlp-pages?]}]]
   (p/let [block (state/get-edit-block)
-          nodes (search/block-search (state/get-current-repo) q {:built-in? false
-                                                                 :enable-snippet? false})
-          matched (p/all
-                   (keep (fn [b]
-                           (when-let [id (:block/uuid b)]
-                             (when-not (= id (:block/uuid block)) ; avoid block self-reference
-                               (p/let [e (db/entity [:block/uuid id])
-                                       e' (or e
-                                              (p/let [result (db-async/<get-block (state/get-current-repo) id {:children? false})]
-                                                result))]
-                                 (when e'
-                                   (assoc e' :block/title (:block/title b)))))))
-                         nodes))]
+          result (search/block-search (state/get-current-repo) q {:built-in? false
+                                                                  :enable-snippet? false})
+          matched (remove (fn [b] (= (:block/uuid b) (:block/uuid block))) result)]
     (-> (concat matched
                 (when nlp-pages?
                   (map (fn [title] {:block/title title :nlp-date? true})

+ 4 - 1
src/main/frontend/worker/search.cljs

@@ -277,6 +277,8 @@ DROP TRIGGER IF EXISTS blocks_au;
                           :or {enable-snippet? true}}]
   (when-not (string/blank? q)
     (let [match-input (get-match-input q)
+          page-count (count (d/datoms @conn :avet :block/name))
+          large-graph? (> page-count 2500)
           non-match-input (when (<= (count q) 2)
                             (str "%" (string/replace q #"\s+" "%") "%"))
           limit  (or limit 100)
@@ -296,7 +298,8 @@ DROP TRIGGER IF EXISTS blocks_au;
           matched-result (search-blocks-aux search-db match-sql q match-input page limit enable-snippet?)
           non-match-result (when non-match-input
                              (search-blocks-aux search-db non-match-sql q non-match-input page limit enable-snippet?))
-          fuzzy-result (when-not page (fuzzy-search repo @conn q option))
+           ;; fuzzy is too slow for large graphs
+          fuzzy-result (when-not (or page large-graph?) (fuzzy-search repo @conn q option))
           result (->> (concat fuzzy-result matched-result non-match-result)
                       (common-util/distinct-by :id)
                       (keep (fn [result]