fix: store db/id in block/children instead of lookup refs

pull/645/head
Tienson Qin 2020-11-12 20:16:00 +08:00
parent 09c6f16079
commit b49f172ce3
3 changed files with 27 additions and 32 deletions

View File

@ -2250,31 +2250,21 @@
[?h :block/collapsed? true]]
(get-conn)))
;; recursive query might be slow, need benchmarks
;; Could replace this with a recursive call or , see below
(defn get-block-parents-rec
[repo block-id depth]
(when-let [conn (get-conn repo)]
(let [ids (->> (d/q
'[:find ?e2
:in $ ?e1 %
:where (parent ?e2 ?e1)]
conn
block-id
;; recursive rules
'[[(parent ?e2 ?e1)
[?e2 :block/children ?e1]]
[(parent ?e2 ?e1)
[?t :block/children ?e1]
(parent ?e2 ?t)]])
(seq-flatten))]
(when (seq ids)
(pull-many repo '[:block/uuid :block/title] ids)))))
(defn get-block-parent
[repo block-id]
(when-let [conn (get-conn repo)]
(d/entity conn [:block/children [:block/uuid block-id]])))
(when-let [id (:db/id (d/entity conn [:block/uuid block-id]))]
(d/entity conn [:block/children id]))))
;; Using reverse lookup, a bit slow compared to get-block-parents
#_(defn get-block-parents-rec
[repo block-id depth]
(when-let [conn (get-conn repo)]
(let [id (:db/id (entity repo [:block/uuid block-id]))]
(d/pull conn
'[:db/id :block/uuid :block/title :block/content
{:block/_children ...}]
id))))
;; non recursive query
(defn get-block-parents
@ -2418,6 +2408,7 @@
last-level 10000]
(if (seq blocks)
(let [[{:block/keys [uuid level] :as block} & others] blocks
db-id (:db/id block)
[tx children] (cond
(< level last-level) ; parent
(let [cur-children (get children last-level)
@ -2427,20 +2418,20 @@
tx
(map
(fn [child]
[:db/add (:db/id block) :block/children [:block/uuid child]])
[:db/add (:db/id block) :block/children child])
cur-children)))
tx)
children (-> children
(dissoc last-level)
(update level conj uuid))]
(update level conj db-id))]
[tx children])
(> level last-level) ; child of sibling
(let [children (update children level conj uuid)]
(let [children (update children level conj db-id)]
[tx children])
:else ; sibling
(let [children (update children last-level conj uuid)]
(let [children (update children last-level conj db-id)]
[tx children]))]
(recur others tx children level))
;; TODO: add top-level children to the "Page" block (we might remove the Page from db schema)

View File

@ -221,7 +221,10 @@
(defn block-keywordize
[block]
(medley/map-keys
(fn [k] (keyword "block" k))
(fn [k]
(if (namespace k)
k
(keyword "block" k)))
block))
(defn safe-blocks
@ -269,23 +272,23 @@
(when (util/uuid-string? custom-id)
(uuid custom-id))))
(d/squuid))
temp-id (d/tempid :db.part/user)
block (second block)
level (:level block)
[children current-block-children]
(cond
(>= level last-level)
[(conj children [id level])
[(conj children [temp-id level])
#{}]
(< level last-level)
(let [current-block-children (set (->> (filter #(< level (second %)) children)
(map first)
(map (fn [id]
[:block/uuid id]))))
(map first)))
others (vec (remove #(< level (second %)) children))]
[(conj others [id level])
[(conj others [temp-id level])
current-block-children]))
block (-> (assoc block
:db/id temp-id
:uuid id
:body (vec (reverse block-body))
:properties (:properties properties)

View File

@ -26,6 +26,7 @@
([repo]
(when (and
repo
(db/cloned? repo)
(gobj/get js/window "workerThread")
(gobj/get js/window.workerThread "getChangedFiles"))
(->