refactor: replace :block/left with :block/order

Finally no need to worry about parent-left conflicts and broken chain.
With :block/order, we only need to re-compute new orders for siblings
with same order (it can happens if there're bugs in our code, or
updates from rtc), but it doesn't break UI.

Another huge potential benefit after discussing with Zhiyuan is:
Ee might be able to simplify both RTC and undo/redo, currently, we
need to handle each new op for both of them, with recently
refactorings like properties being db attributes, :block/order
is a string instead of a ref, we can handle most property value
conflicts using last-write-wins, and others (e.g. :block/parent,
property with :default type) specifically.

I haven't fixed the issues of using :block/left in RTC and undo/redo,
because we might change both soon.
This commit is contained in:
Tienson Qin
2024-05-01 03:37:42 +08:00
parent 439d6e6da7
commit 1ec4796eff
65 changed files with 503 additions and 1412 deletions

View File

@@ -15,7 +15,8 @@
["fs" :as fs]
["path" :as node-path]
[nbb.classpath :as cp]
[logseq.db.frontend.property :as db-property]))
[logseq.db.frontend.property :as db-property]
[logseq.db.frontend.order :as db-order]))
(defn- find-on-classpath [rel-path]
(some (fn [dir]
@@ -110,13 +111,13 @@
"Provides the next temp :db/id to use in a create-graph transact!"
#(swap! current-db-id dec))
(defn- ->block-tx [m uuid-maps all-idents page-id last-block block-id-fn]
(defn- ->block-tx [m uuid-maps all-idents page-id]
(merge (dissoc m :properties)
(sqlite-util/block-with-timestamps
{:db/id (new-db-id)
:block/format :markdown
:block/page {:db/id page-id}
:block/left {:db/id (if last-block (block-id-fn last-block) page-id)}
:block/order (db-order/gen-key nil)
:block/parent {:db/id page-id}})
(when (seq (:properties m))
(merge (->block-properties (:properties m) uuid-maps all-idents)
@@ -172,7 +173,7 @@
have the following limitations:
* Only top level blocks can be easily defined. Other level blocks can be
defined but they require explicit setting of attributes like :block/left and :block/parent
defined but they require explicit setting of attributes like :block/order and :block/parent
* Block content containing page refs or tags is not supported yet
The EDN map has the following keys:
@@ -190,8 +191,6 @@
* :graph-namespace - namespace to use for db-ident creation. Useful when importing an ontology
* :page-id-fn - custom fn that returns ent lookup id for page refs e.g. `[:block/uuid X]`
Default is :db/id
* :block-id-fn - custom fn that returns ent lookup id for page refs e.g. `[:block/uuid X]`
Default is :db/id
The :properties in :pages-and-blocks is a map of property names to property
values. Multiple property values for a many cardinality property are defined
@@ -199,8 +198,8 @@
:checkbox, :number, :page and :date. :checkbox and :number values are written
as booleans and integers/floats. :page references are written as
vectors e.g. `[:page \"PAGE NAME\"]`"
[{:keys [pages-and-blocks properties graph-namespace page-id-fn block-id-fn]
:or {page-id-fn :db/id block-id-fn :db/id}
[{:keys [pages-and-blocks properties graph-namespace page-id-fn]
:or {page-id-fn :db/id}
:as options}]
(let [_ (validate-options options)
;; add uuids before tx for refs in :properties
@@ -244,7 +243,7 @@
;; blocks tx
(reduce (fn [acc m]
(conj acc
(->block-tx m uuid-maps all-idents (page-id-fn new-page) (last acc) block-id-fn)))
(->block-tx m uuid-maps all-idents (page-id-fn new-page))))
[]
blocks))))
pages-and-blocks'))]

View File

@@ -60,7 +60,7 @@
[file1 file2 & args]
(let [spec {:ignored-attributes
;; Ignores some attributes by default that are expected to change often
{:alias :i :coerce #{:keyword} :default #{:block/tx-id :block/left :block/updated-at}}}
{:alias :i :coerce #{:keyword} :default #{:block/tx-id :block/order :block/updated-at}}}
{{:keys [ignored-attributes]} :opts} (cli/parse-args args {:spec spec})
datom-filter (fn [[e a _ _ _]] (contains? ignored-attributes a))
data-diff* (apply data/diff (map (fn [x] (->> x slurp edn/read-string (remove datom-filter))) [file1 file2]))