Browse Source

Refactor/db (#945)

* fix(db): rebuild-page-blocks-children

* refactor(db): template-exists?

* refactor(db): sub-key-value

* refactor(db): get-pages-with-modified-at

* style(page): fix style
Michael Wong 4 years ago
parent
commit
1d4d2ee53e

+ 3 - 2
src/main/frontend/components/content.cljs

@@ -19,7 +19,8 @@
             [frontend.handler.notification :as notification]
             [frontend.components.editor :as editor]
             [frontend.context.i18n :as i18n]
-            [frontend.text :as text]))
+            [frontend.text :as text]
+            [frontend.handler.page :as page-handler]))
 
 (defn- set-format-js-loading!
   [format value]
@@ -89,7 +90,7 @@
                     :on-click (fn []
                                 (let [title (string/trim @input)]
                                   (when (not (string/blank? title))
-                                    (if (db/template-exists? title)
+                                    (if (page-handler/template-exists? title)
                                       (notification/show!
                                        [:p "Template already exists!"]
                                        :error)

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

@@ -451,7 +451,7 @@
       [:div.flex-1
        [:h1.title (t :all-pages)]
        (when current-repo
-         (let [pages (db/get-pages-with-modified-at current-repo)]
+         (let [pages (page-handler/get-pages-with-modified-at current-repo)]
            [:table.table-auto
             [:thead
              [:tr

+ 3 - 4
src/main/frontend/db.cljs

@@ -48,16 +48,15 @@
   get-latest-journals get-marker-blocks get-matched-blocks get-page get-page-alias get-page-alias-names get-page-blocks
   get-page-blocks-count get-page-blocks-no-cache get-page-file get-page-format get-page-name get-page-properties
   get-page-properties-content get-page-referenced-blocks get-page-referenced-pages get-page-unlinked-references
-  get-pages get-pages-relation get-pages-that-mentioned-page get-pages-with-modified-at get-public-pages get-tag-pages
+  get-pages get-pages-relation get-pages-that-mentioned-page get-public-pages get-tag-pages
   journal-page? local-native-fs? mark-repo-as-cloned! page-alias-set page-blocks-transform pull-block
-  rebuild-page-blocks-children  reset-config! set-file-last-modified-at! sub-key-value template-exists?
-  transact-files-db! with-block-refs-count]
+  reset-config! set-file-last-modified-at! transact-files-db! with-block-refs-count get-modified-pages]
 
  [frontend.db.react
   get-current-marker get-current-page get-current-priority get-handler-keys set-file-content! set-key-value
   transact-react! remove-key! remove-q! remove-query-component! add-q! add-query-component! clear-query-state!
   clear-query-state-without-refs-and-embeds! get-block-blocks-cache-atom get-page-blocks-cache-atom kv q
-  query-state query-components query-entity-in-component remove-custom-query! set-new-result!]
+  query-state query-components query-entity-in-component remove-custom-query! set-new-result! sub-key-value]
 
  [frontend.db.query-custom
   custom-query custom-query-result-transform])

+ 7 - 83
src/main/frontend/db/model.cljs

@@ -34,15 +34,6 @@
          (when-let [conn (conn/get-files-conn repo-url)]
            (d/transact! conn (vec tx-data))))))))
 
-(defn sub-key-value
-  ([key]
-   (sub-key-value (state/get-current-repo) key))
-  ([repo-url key]
-   (when (conn/get-conn repo-url)
-     (-> (react/q repo-url [:kv key] {} key key)
-         react
-         key))))
-
 (defn pull-block
   [id]
   (let [repo (state/get-current-repo)]
@@ -103,23 +94,14 @@
         (conn/get-conn repo))
        (map first)))
 
-(defn get-pages-with-modified-at
+(defn get-modified-pages
   [repo]
-  (let [now-long (tc/to-long (t/now))]
-    (->> (d/q
-          '[:find ?page-name ?modified-at
-            :where
-            [?page :page/original-name ?page-name]
-            [(get-else $ ?page :page/last-modified-at 0) ?modified-at]]
-          (conn/get-conn repo))
-         (seq)
-         (sort-by (fn [[page modified-at]]
-                    [modified-at page]))
-         (reverse)
-         (remove (fn [[page modified-at]]
-                   (or (util/file-page? page)
-                       (and modified-at
-                            (> modified-at now-long))))))))
+  (d/q
+    '[:find ?page-name ?modified-at
+      :where
+      [?page :page/original-name ?page-name]
+      [(get-else $ ?page :page/last-modified-at 0) ?modified-at]]
+    (conn/get-conn repo)))
 
 (defn get-page-alias
   [repo page-name]
@@ -1046,56 +1028,6 @@
        db)
       (db-utils/seq-flatten)))
 
-(defn rebuild-page-blocks-children
-  "For performance reason, we can update the :block/children value after every operation,
-  but it's hard to make sure that it's correct, also it needs more time to implement it.
-  We can improve it if the performance is really an issue."
-  [repo page]
-  (let [blocks (->>
-                (get-page-blocks-no-cache repo page {:pull-keys '[:db/id :block/uuid :block/level :block/pre-block? :block/meta]})
-                (remove :block/pre-block?)
-                (map #(select-keys % [:db/id :block/uuid :block/level]))
-                (reverse))
-        original-blocks blocks]
-    (loop [blocks blocks
-           tx []
-           children {}
-           last-level 10000]
-      (if (seq blocks)
-        (let [[{:block/keys [uuid level] :as block} & others] blocks
-              [tx children] (cond
-                              (< level last-level)        ; parent
-                              (let [cur-children (get children last-level)
-                                    tx (if (seq cur-children)
-                                         (vec
-                                          (concat
-                                           tx
-                                           (map
-                                            (fn [child]
-                                              [:db/add (:db/id block) :block/children [:block/uuid child]])
-                                            cur-children)))
-                                         tx)
-                                    children (-> children
-                                                 (dissoc last-level)
-                                                 (update level conj uuid))]
-                                [tx children])
-
-                              (> level last-level)        ; child of sibling
-                              (let [children (update children level conj uuid)]
-                                [tx children])
-
-                              :else                       ; sibling
-                              (let [children (update children last-level conj uuid)]
-                                [tx children]))]
-          (recur others tx children level))
-        ;; TODO: add top-level children to the "Page" block (we might remove the Page from db schema)
-        (when (seq tx)
-          (let [delete-tx (map (fn [block]
-                                 [:db/retract (:db/id block) :block/children])
-                               original-blocks)]
-            (->> (concat delete-tx tx)
-                 (remove nil?))))))))
-
 (defn get-all-templates
   []
   (let [pred (fn [db properties]
@@ -1112,14 +1044,6 @@
                 [(get m "template") e]))
          (into {}))))
 
-(defn template-exists?
-  [title]
-  (when title
-    (let [templates (keys (get-all-templates))]
-      (when (seq templates)
-        (let [templates (map string/lower-case templates)]
-          (contains? (set templates) (string/lower-case title)))))))
-
 (defonce blocks-count-cache (atom nil))
 
 (defn blocks-count

+ 9 - 0
src/main/frontend/db/react.cljs

@@ -336,6 +336,15 @@
                      {:key [:kv key]})
     (remove-key! repo-url key)))
 
+(defn sub-key-value
+  ([key]
+   (sub-key-value (state/get-current-repo) key))
+  ([repo-url key]
+   (when (conn/get-conn repo-url)
+     (-> (q repo-url [:kv key] {} key key)
+         react
+         key))))
+
 (defn set-file-content!
   [repo path content]
   (when (and repo path)

+ 25 - 1
src/main/frontend/handler/page.cljs

@@ -22,7 +22,9 @@
             [frontend.fs :as fs]
             [promesa.core :as p]
             [lambdaisland.glogi :as log]
-            [frontend.format.mldoc :as mldoc]))
+            [frontend.format.mldoc :as mldoc]
+            [cljs-time.core :as t]
+            [cljs-time.coerce :as tc]))
 
 (defn- get-directory
   [journal?]
@@ -467,3 +469,25 @@
                   '())
         new-pages (take 12 (distinct (cons page pages)))]
     (db/set-key-value repo :recent/pages new-pages)))
+
+
+(defn template-exists?
+  [title]
+  (when title
+    (let [templates (keys (db/get-all-templates))]
+      (when (seq templates)
+        (let [templates (map string/lower-case templates)]
+          (contains? (set templates) (string/lower-case title)))))))
+
+(defn get-pages-with-modified-at
+  [repo]
+  (let [now-long (tc/to-long (t/now))]
+    (->> (db/get-modified-pages repo)
+         (seq)
+         (sort-by (fn [[page modified-at]]
+                    [modified-at page]))
+         (reverse)
+         (remove (fn [[page modified-at]]
+                   (or (util/file-page? page)
+                     (and modified-at
+                       (> modified-at now-long))))))))

+ 1 - 1
src/main/frontend/handler/repo.cljs

@@ -313,7 +313,7 @@
      tx
      transact-option)
     (when (seq pages)
-      (let [children-tx (mapcat #(db/rebuild-page-blocks-children repo %) pages)]
+      (let [children-tx (mapcat #(rebuild-page-blocks-children repo %) pages)]
         (when (seq children-tx)
           (db/transact! repo children-tx))))
     (when (seq files)