Browse Source

perf: refs reactive query

Tienson Qin 3 years ago
parent
commit
fd5f3b4f60

+ 1 - 1
src/main/frontend/components/reference.cljs

@@ -147,7 +147,7 @@
       :init-collapsed (fn [collapsed-atom]
                         (reset! *collapsed? collapsed-atom))})))
 
-(rum/defcs references* < rum/reactive
+(rum/defcs references* < rum/reactive db-mixins/query
   {:init (fn [state]
            (let [page-name (first (:rum/args state))
                  filters (when page-name

+ 11 - 12
src/main/frontend/db/model.cljs

@@ -1213,7 +1213,7 @@
              aliases (set/difference pages #{page-id})]
          (->>
           (react/q repo
-            [:frontend.db.react/page<-blocks-or-block<-blocks page-id]
+            [:frontend.db.react/refs page-id]
             {:query-fn (fn []
                          (let [entities (mapcat (fn [id]
                                                   (:block/_path-refs (db-utils/entity id))) pages)
@@ -1242,16 +1242,15 @@
           page? (:block/name block)
           result (if page?
                    (let [pages (page-alias-set repo (:block/name block))]
-                     (d/q
-                       '[:find [?block ...]
-                         :in $ [?ref-page ...] ?id
-                         :where
-                         [?block :block/refs ?ref-page]
-                         [?block :block/page ?p]
-                         [(not= ?p ?id)]]
-                       (conn/get-db repo)
-                       pages
-                       id))
+                     @(react/q repo [:frontend.db.react/refs-count id] {}
+                        '[:find [?block ...]
+                          :in $ [?ref-page ...] ?id
+                          :where
+                          [?block :block/refs ?ref-page]
+                          [?block :block/page ?p]
+                          [(not= ?p ?id)]]
+                        pages
+                        id))
                    (:block/_refs block))]
       (count result))))
 
@@ -1322,7 +1321,7 @@
    (when-let [repo (state/get-current-repo)]
      (when (conn/get-db repo)
        (let [block (db-utils/entity [:block/uuid block-uuid])
-             query-result (->> (react/q repo [:frontend.db.react/page<-blocks-or-block<-blocks
+             query-result (->> (react/q repo [:frontend.db.react/refs
                                               (:db/id block)]
                                  {}
                                  '[:find [(pull ?ref-block ?block-attrs) ...]

+ 22 - 14
src/main/frontend/db/react.cljs

@@ -30,10 +30,10 @@
 ;; ::page<-pages
 ;; get PAGES referencing PAGE
 (s/def ::page<-pages (s/tuple #(= ::page<-pages %) int?))
-;; ::page<-blocks-or-block<-blocks
+;; ::refs
 ;; get BLOCKS referencing PAGE or BLOCK
-(s/def ::page<-blocks-or-block<-blocks
-  (s/tuple #(= ::page<-blocks-or-block<-blocks %) int?))
+(s/def ::refs (s/tuple #(= ::refs %) int?))
+(s/def ::refs-count int?)
 ;; FIXME: this react-query has performance issues
 (s/def ::page-unlinked-refs (s/tuple #(= ::page-unlinked-refs %) int?))
 
@@ -45,7 +45,8 @@
                                 :block-and-children ::block-and-children
                                 :journals ::journals
                                 :page<-pages ::page<-pages
-                                :page<-blocks-or-block<-blocks ::page<-blocks-or-block<-blocks
+                                :refs ::refs
+                                :refs-count ::refs-count
                                 :page-unlinked-refs ::page-unlinked-refs
                                 :custom ::custom))
 
@@ -215,10 +216,11 @@
   "Get affected queries through transaction datoms."
   [{:keys [tx-data db-before]}]
   {:post [(s/valid? ::affected-keys %)]}
+  (def debug-tx-data tx-data)
   (let [blocks (->> (filter (fn [datom] (contains? #{:block/left :block/parent :block/page} (:a datom))) tx-data)
                     (map :v)
                     (distinct))
-        refs (->> (filter (fn [datom] (= :block/refs (:a datom))) tx-data)
+        refs (->> (filter (fn [datom] (contains? #{:block/refs :block/path-refs} (:a datom))) tx-data)
                   (map :v)
                   (distinct))
         other-blocks (->> (filter (fn [datom] (= "block" (namespace (:a datom)))) tx-data)
@@ -235,26 +237,32 @@
                                              (when (:block/name block) (:db/id block))
                                              (:db/id (:block/page block)))
                                     blocks [[::block (:db/id block)]]
+                                    path-refs (:block/path-refs block)
+                                    path-refs' (mapcat (fn [ref]
+                                                         [[::refs-count (:db/id ref)]
+                                                          [::refs (:db/id ref)]]) path-refs)
                                     others (when page-id
                                              [[::page-blocks page-id]])]
-                                (concat blocks others)))))
+                                (concat blocks others path-refs')))))
                         blocks)
 
                        (when-let [current-page-id (:db/id (get-current-page))]
                          [[::page<-pages current-page-id]])
 
-                       (map (fn [ref]
-                              (let [entity (db-utils/entity ref)]
-                                (if (:block/name entity) ; page
-                                  [::page-blocks ref]
-                                  [::page-blocks (:db/id (:block/page entity))])))
+                       (mapcat
+                        (fn [ref]
+                          (let [entity (db-utils/entity ref)]
+                            (conj
+                             [[::refs-count (:db/id entity)]
+                              [::refs (:db/id entity)]]
+                             (if (:block/name entity) ; page
+                               [::page-blocks ref]
+                               [::page-blocks (:db/id (:block/page entity))]))))
                          refs))
         others (->>
                 (keys @query-state)
                 (filter (fn [ks]
-                          (contains? #{::block-and-children
-                                       ::page<-blocks-or-block<-blocks}
-                                     (second ks))))
+                          (contains? #{::block-and-children} (second ks))))
                 (map (fn [v] (vec (rest v)))))]
     (->>
      (util/concat-without-nil