Browse Source

enhance: generate db-ident block's uuid like:

00000002-<hash-of-db-ident>-<fill-with-0>
rcmerci 1 year ago
parent
commit
04e7d2c17b

+ 25 - 2
deps/common/src/logseq/common/uuid.cljs

@@ -7,12 +7,35 @@
 first 8 chars as type, currently only '00000001' for journal-day-page.
 the remaining chars for data of this type"
   [journal-day]
-  {:pre [(int? journal-day)]}
+  {:pre [(pos-int? journal-day)
+         (> 1 (/ journal-day 100000000))]}
   (let [journal-day-str  (str journal-day)
         part1 (subs journal-day-str 0 4)
         part2 (subs journal-day-str 4 8)]
     (uuid (str "00000001-" part1 "-" part2 "-0000-000000000000"))))
 
+(defn- fill-with-0
+  [s length]
+  (let [s-length (count s)]
+    (apply str s (repeat (- length s-length) "0"))))
+
+(defn- gen-db-ident-block-uuid
+  "00000002-<hash-of-db-ident>-<padding-with-0>"
+  [db-ident]
+  {:pre [(keyword? db-ident)]}
+  (let [hash-num (str (abs (hash db-ident)))
+        part1 (fill-with-0 (subs hash-num 0 4) 4)
+        part2 (fill-with-0 (subs hash-num 4 8) 4)
+        part3 (fill-with-0 (subs hash-num 8 12) 4)
+        part4 (fill-with-0 (subs hash-num 12) 12)]
+    (uuid (str "00000002-" part1 "-" part2 "-" part3 "-" part4))))
+
 (defn gen-uuid
+  "supported type:
+  - :journal-page-uuid
+  - :db-ident-block-uuid"
   ([] (d/squuid))
-  ([journal-day] (gen-journal-page-uuid journal-day)))
+  ([type v]
+   (case type
+     :journal-page-uuid (gen-journal-page-uuid v)
+     :db-ident-block-uuid (gen-db-ident-block-uuid v))))

+ 5 - 4
deps/db/src/logseq/db/frontend/property.cljs

@@ -1,8 +1,9 @@
 (ns logseq.db.frontend.property
   "Property related fns for DB graphs and frontend/datascript usage"
-  (:require [datascript.core :as d]
-            [clojure.string :as string]
+  (:require [clojure.string :as string]
+            [datascript.core :as d]
             [flatland.ordered.map :refer [ordered-map]]
+            [logseq.common.uuid :as common-uuid]
             [logseq.db.frontend.db-ident :as db-ident]))
 
 ;; Main property vars
@@ -105,7 +106,7 @@
     (mapv (fn [[db-ident value icon]]
             {:db-ident db-ident
              :value value
-             :uuid (random-uuid)
+             :uuid (common-uuid/gen-uuid :db-ident-block-uuid db-ident)
              :icon {:type :tabler-icon :id icon}})
           [[:logseq.task/status.backlog "Backlog" "Backlog"]
            [:logseq.task/status.todo "Todo" "Todo"]
@@ -123,7 +124,7 @@
     (mapv (fn [[db-ident value icon]]
             {:db-ident db-ident
              :value value
-             :uuid (random-uuid)
+             :uuid (common-uuid/gen-uuid :db-ident-block-uuid db-ident)
              :icon {:type :tabler-icon :id icon}})
           [[:logseq.task/priority.urgent "Urgent" "priorityLvlUrgent"]
            [:logseq.task/priority.high "High" "priorityLvlHigh"]

+ 8 - 5
deps/db/src/logseq/db/sqlite/build.cljs

@@ -183,12 +183,14 @@
         classes-tx (vec
                     (mapcat
                      (fn [[class-name {:build/keys [class-parent schema-properties] :as class-m}]]
-                       (let [new-block
+                       (let [db-ident (get-ident all-idents class-name)
+                             new-block
                              (sqlite-util/build-new-class
                               {:block/name (common-util/page-name-sanity-lc (name class-name))
                                :block/original-name (name class-name)
-                               :block/uuid (or (:block/uuid class-m) (d/squuid))
-                               :db/ident (get-ident all-idents class-name)
+                               :block/uuid (or (:block/uuid class-m)
+                                               (common-uuid/gen-uuid :db-ident-block-uuid db-ident))
+                               :db/ident db-ident
                                :db/id (or (class-db-ids class-name)
                                           (throw (ex-info "No :db/id for class" {:class class-name})))})
                              pvalue-tx-m (->property-value-tx-m new-block (:build/properties class-m) properties-config all-idents)]
@@ -461,7 +463,8 @@
                                        (-> (dissoc page :build/journal)
                                            (merge {:block/journal-day date-int
                                                    :block/original-name page-name
-                                                   :block/uuid (common-uuid/gen-uuid date-int)
+                                                   :block/uuid
+                                                   (common-uuid/gen-uuid :journal-page-uuid date-int)
                                                    :block/type #{"journal" "page"}})))))
                            m))]
     ;; Order matters as some steps depend on previous step having prepared blocks or pages in a certain way
@@ -598,4 +601,4 @@
         {:keys [init-tx block-props-tx]} (build-blocks-tx options')]
     (d/transact! conn init-tx)
     (when (seq block-props-tx)
-      (d/transact! conn block-props-tx))))
+      (d/transact! conn block-props-tx))))

+ 12 - 7
deps/db/src/logseq/db/sqlite/create_graph.cljs

@@ -1,12 +1,14 @@
 (ns logseq.db.sqlite.create-graph
   "Helper fns for creating a DB graph"
-  (:require [logseq.db.sqlite.util :as sqlite-util]
-            [logseq.db.frontend.schema :as db-schema]
-            [logseq.db.frontend.property :as db-property]
+  (:require [clojure.string :as string]
+            [datascript.core :as d]
+            [logseq.common.util :as common-util]
+            [logseq.common.uuid :as common-uuid]
             [logseq.db.frontend.class :as db-class]
+            [logseq.db.frontend.property :as db-property]
             [logseq.db.frontend.property.build :as db-property-build]
-            [logseq.common.util :as common-util]
-            [datascript.core :as d]))
+            [logseq.db.frontend.schema :as db-schema]
+            [logseq.db.sqlite.util :as sqlite-util]))
 
 (defn- mark-block-as-built-in [block built-in-prop-value]
   (assoc block :logseq.property/built-in? [:block/uuid (:block/uuid built-in-prop-value)]))
@@ -55,11 +57,14 @@
                    (map mark-block-as-built-in' properties)
                    (keep #(when (= #{"closed value"} (:block/type %)) (mark-block-as-built-in' %))
                          properties))]
+    (doseq [m tx]
+      (when-let [block-uuid (and (:db/ident m) (:block/uuid m))]
+        (assert (string/starts-with? (str block-uuid) "00000002") m)))
+
     {:tx tx
      :properties (filter #(contains? (:block/type %) "property") properties)
      :built-in-prop-value built-in-prop-value}))
 
-
 (defn kv
   "Creates a key-value pair tx with the key and value respectively stored under
   :db/ident and :kv/value.  The key must be under the namespace :logseq.kv"
@@ -98,7 +103,7 @@
             {:block/original-name original-name'
              :block/name (common-util/page-name-sanity-lc original-name')
              :db/ident db-ident
-             :block/uuid (d/squuid)}
+             :block/uuid (common-uuid/gen-uuid :db-ident-block-uuid db-ident)}
              (seq properties)
              (assoc :class/schema.properties properties))))
         built-in-prop-value)))

+ 10 - 9
deps/db/src/logseq/db/sqlite/util.cljs

@@ -1,16 +1,17 @@
 (ns logseq.db.sqlite.util
   "Utils fns for backend sqlite db"
-  (:require [clojure.string :as string]
-            [logseq.db.frontend.schema :as db-schema]
-            [logseq.common.util :as common-util]
+  (:require [cljs-bean.transit]
+            [clojure.string :as string]
             [cognitect.transit :as transit]
-            [datascript.transit :as dt]
-            [datascript.impl.entity :as de]
             [datascript.core :as d]
-            [cljs-bean.transit]
-            [logseq.db.frontend.property.type :as db-property-type]
+            [datascript.impl.entity :as de]
+            [datascript.transit :as dt]
+            [logseq.common.util :as common-util]
+            [logseq.common.uuid :as common-uuid]
+            [logseq.db.frontend.order :as db-order]
             [logseq.db.frontend.property :as db-property]
-            [logseq.db.frontend.order :as db-order]))
+            [logseq.db.frontend.property.type :as db-property-type]
+            [logseq.db.frontend.schema :as db-schema]))
 
 (defonce db-version-prefix "logseq_db_")
 (defonce file-version-prefix "logseq_local_")
@@ -93,7 +94,7 @@
         :block/format :markdown
         :block/schema (merge {:type :default} (dissoc prop-schema :classes :cardinality))
         :block/name (common-util/page-name-sanity-lc (name prop-name))
-        :block/uuid (or block-uuid (d/squuid))
+        :block/uuid (or block-uuid (common-uuid/gen-uuid :db-ident-block-uuid db-ident'))
         :block/original-name (name prop-name)
         :db/index true
         :db/cardinality (if (= :many (:cardinality prop-schema))

+ 3 - 1
deps/graph-parser/src/logseq/graph_parser/block.cljs

@@ -324,7 +324,9 @@
                    :block/original-name original-page-name}
                   (let [new-uuid* (if (uuid? page-uuid)
                                     page-uuid
-                                    (if journal-day (common-uuid/gen-uuid journal-day) (common-uuid/gen-uuid)))
+                                    (if journal-day
+                                      (common-uuid/gen-uuid :journal-page-uuid journal-day)
+                                      (common-uuid/gen-uuid)))
                         new-uuid (if skip-existing-page-check?
                                    new-uuid*
                                    (or

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser/exporter.cljs

@@ -207,7 +207,7 @@
                             (let [page-m (sqlite-util/build-new-page
                                           (date-time-util/int->journal-title date-int (common-config/get-date-formatter user-config)))]
                               (assoc page-m
-                                     :block/uuid (common-uuid/gen-uuid date-int)
+                                     :block/uuid (common-uuid/gen-uuid :journal-page-uuid date-int)
                                      :block/type (conj (:block/type page-m) "journal")
                                      :block/journal-day date-int)))]
       {:block