Răsfoiți Sursa

Merge branch 'refactor/pipeline-worker' of https://github.com/logseq/logseq into refactor/pipeline-worker

charlie 2 ani în urmă
părinte
comite
9ec1da8735

+ 9 - 2
deps/db/src/logseq/db/frontend/malli_schema.cljs

@@ -273,6 +273,11 @@
    [:file/path :string]
    [:file/last-modified-at inst?]])
 
+(def asset-block
+  [:map
+   [:asset/uuid :uuid]
+   [:asset/meta :map]])
+
 (def schema-version
   [:map
    [:db/ident :keyword]
@@ -306,7 +311,8 @@
     file-block
     schema-version
     db-ident
-    macro]])
+    macro
+    asset-block]])
 
 ;; Keep malli schema in sync with db schema
 ;; ========================================
@@ -330,7 +336,8 @@
                     {}))))
 
 (let [malli-non-ref-attrs (->> (concat page-attrs block-attrs page-or-block-attrs (rest normal-page))
-                               (concat (rest file-block) (rest db-ident) (rest schema-version) (rest class-page))
+                               (concat (rest file-block) (rest asset-block)
+                                       (rest db-ident) (rest schema-version) (rest class-page))
                                (remove #(= (last %) [:set :int]))
                                (map first)
                                set)

+ 37 - 7
src/main/frontend/worker/rtc/db_listener.cljs

@@ -56,10 +56,10 @@
       (let [updated-key-set (set (keys attr->datom))
             e (some-> attr->datom first second first)
             {[_e _a block-uuid _t add1?] :block/uuid
-             [_e _a _v _t add2?]  :block/name
-             [_e _a _v _t add3?]  :block/parent
-             [_e _a _v _t add4?]  :block/left
-             [_e _a _v _t add5?]  :block/original-name} attr->datom
+             [_e _a _v _t add2?]         :block/name
+             [_e _a _v _t add3?]         :block/parent
+             [_e _a _v _t add4?]         :block/left
+             [_e _a _v _t add5?]         :block/original-name} attr->datom
             ops (cond
                   (and (not add1?) block-uuid
                        (not add2?) (contains? updated-key-set :block/name))
@@ -142,18 +142,48 @@
                        ops)]
         ops*))))
 
+(defn- entity-datoms=>asset-op
+  [db-after entity-datoms]
+  (let [attr->datom (entity-datoms=>attr->datom entity-datoms)]
+    (when (seq attr->datom)
+      (let [e (some-> attr->datom first second first)
+            {[_e _a asset-uuid _t add1?] :asset/uuid
+             [_e _a asset-meta _t add2?] :asset/meta}
+            attr->datom
+            op (cond
+                 (or (and add1? asset-uuid)
+                     (and add2? asset-meta))
+                 [:update-asset]
+
+                 (and (not add1?) asset-uuid)
+                 [:remove-asset asset-uuid])]
+        (when op
+          (let [asset-uuid (some-> (d/entity db-after e) :asset/uuid str)]
+            (case (first op)
+              :update-asset (when asset-uuid ["update-asset" {:asset-uuid asset-uuid}])
+              :remove-asset ["remove-asset" {:asset-uuid (str (second op))}])))))))
+
+
 (defn generate-rtc-ops
   [repo db-before db-after datoms]
   (let [datom-vec-coll (map vec datoms)
         id->same-entity-datoms (group-by first datom-vec-coll)
         id-order (distinct (map first datom-vec-coll))
         same-entity-datoms-coll (map id->same-entity-datoms id-order)
-        ops (mapcat (partial entity-datoms=>ops db-before db-after) same-entity-datoms-coll)
+        asset-ops (keep (partial entity-datoms=>asset-op db-after) same-entity-datoms-coll)
+        ops (when (empty asset-ops)
+              (mapcat (partial entity-datoms=>ops db-before db-after) same-entity-datoms-coll))
         now-epoch*1000 (* 1000 (tc/to-long (t/now)))
         ops* (map-indexed (fn [idx op]
-                            [(first op) (assoc (second op) :epoch (+ idx now-epoch*1000))]) ops)]
+                            [(first op) (assoc (second op) :epoch (+ idx now-epoch*1000))]) ops)
+        epoch2 (+ now-epoch*1000 (count ops))
+        asset-ops* (map-indexed (fn [idx op]
+                                  [(first op) (assoc (second op) :epoch (+ idx epoch2))]) asset-ops)]
     (when (seq ops*)
-      (op-mem-layer/add-ops! repo ops*))))
+      (op-mem-layer/add-ops! repo ops*))
+    (when (seq asset-ops*)
+      (op-mem-layer/add-asset-ops! repo asset-ops*))))
+
 
 
 (defn listen-db-to-generate-ops

+ 30 - 31
src/main/frontend/worker/rtc/op_mem_layer.cljs

@@ -325,37 +325,36 @@
                              :block-uuid->ops old-branch-block-uuid->ops
                              :epoch->block-uuid-sorted-map old-epoch->block-uuid-sorted-map)))))))
 
-(comment
-  (defn add-asset-ops!
-   [repo ops]
-   (assert (contains? (@*ops-store repo) :current-branch) (@*ops-store repo))
-   (let [ops (ops-coercer ops)
-         {{old-branch-block-uuid->ops :block-uuid->ops
-           old-epoch->block-uuid-sorted-map :epoch->block-uuid-sorted-map
-           old-branch-asset-uuid->ops :asset-uuid->ops
-           old-epoch->asset-uuid-sorted-map :epoch->asset-uuid-sorted-map
-           :as old-branch} :old-branch
-          {:keys [block-uuid->ops epoch->block-uuid-sorted-map
-                  asset-uuid->ops epoch->asset-uuid-sorted-map]} :current-branch}
-         (get @*ops-store repo)
-         {:keys [asset-uuid->ops epoch->asset-uuid-sorted-map]}
-         (add-ops-aux ops block-uuid->ops epoch->block-uuid-sorted-map
-                      asset-uuid->ops epoch->asset-uuid-sorted-map)
-         {old-branch-asset-uuid->ops :asset-uuid->ops old-epoch->asset-uuid-sorted-map :epoch->asset-uuid-sorted-map}
-         (when old-branch
-           (add-ops-aux ops old-branch-block-uuid->ops old-epoch->block-uuid-sorted-map
-                        old-branch-asset-uuid->ops old-epoch->asset-uuid-sorted-map))]
-     (swap! *ops-store update repo
-            (fn [{:keys [current-branch old-branch]}]
-              (cond-> {:current-branch
-                       (assoc current-branch
-                              :asset-uuid->ops asset-uuid->ops
-                              :epoch->asset-uuid-sorted-map epoch->asset-uuid-sorted-map)}
-                old-branch
-                (assoc :old-branch
-                       (assoc old-branch
-                              :asset-uuid->ops old-branch-asset-uuid->ops
-                              :epoch->asset-uuid-sorted-map old-epoch->asset-uuid-sorted-map))))))))
+(defn add-asset-ops!
+  [repo ops]
+  (assert (contains? (@*ops-store repo) :current-branch) (@*ops-store repo))
+  (let [ops (ops-coercer ops)
+        {{old-branch-block-uuid->ops :block-uuid->ops
+          old-epoch->block-uuid-sorted-map :epoch->block-uuid-sorted-map
+          old-branch-asset-uuid->ops :asset-uuid->ops
+          old-epoch->asset-uuid-sorted-map :epoch->asset-uuid-sorted-map
+          :as old-branch} :old-branch
+         {:keys [block-uuid->ops epoch->block-uuid-sorted-map
+                 asset-uuid->ops epoch->asset-uuid-sorted-map]} :current-branch}
+        (get @*ops-store repo)
+        {:keys [asset-uuid->ops epoch->asset-uuid-sorted-map]}
+        (add-ops-aux ops block-uuid->ops epoch->block-uuid-sorted-map
+                     asset-uuid->ops epoch->asset-uuid-sorted-map)
+        {old-branch-asset-uuid->ops :asset-uuid->ops old-epoch->asset-uuid-sorted-map :epoch->asset-uuid-sorted-map}
+        (when old-branch
+          (add-ops-aux ops old-branch-block-uuid->ops old-epoch->block-uuid-sorted-map
+                       old-branch-asset-uuid->ops old-epoch->asset-uuid-sorted-map))]
+    (swap! *ops-store update repo
+           (fn [{:keys [current-branch old-branch]}]
+             (cond-> {:current-branch
+                      (assoc current-branch
+                             :asset-uuid->ops asset-uuid->ops
+                             :epoch->asset-uuid-sorted-map epoch->asset-uuid-sorted-map)}
+               old-branch
+               (assoc :old-branch
+                      (assoc old-branch
+                             :asset-uuid->ops old-branch-asset-uuid->ops
+                             :epoch->asset-uuid-sorted-map old-epoch->asset-uuid-sorted-map)))))))
 
 (defn update-local-tx!
   [repo t]