From 84fd952471cf506ee020da8f711daf4ad1566744 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 27 Dec 2022 14:31:32 +0100 Subject: [PATCH] :sparkles: Improve storage/* features support on srepl helpers --- backend/src/app/srepl/helpers.clj | 48 ++++++++++++++++++++----------- backend/src/app/srepl/main.clj | 23 ++++++--------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/backend/src/app/srepl/helpers.clj b/backend/src/app/srepl/helpers.clj index d37d0f520c..38c943a08d 100644 --- a/backend/src/app/srepl/helpers.clj +++ b/backend/src/app/srepl/helpers.clj @@ -11,6 +11,7 @@ [app.auth :refer [derive-password]] [app.common.data :as d] [app.common.exceptions :as ex] + [app.common.files.features :as ffeat] [app.common.logging :as l] [app.common.pages :as cp] [app.common.pages.migrations :as pmg] @@ -21,8 +22,11 @@ [app.db :as db] [app.db.sql :as sql] [app.main :refer [system]] + [app.rpc.commands.files :as files] [app.rpc.queries.profile :as prof] [app.util.blob :as blob] + [app.util.objects-map :as omap] + [app.util.pointer-map :as pmap] [app.util.time :as dt] [clojure.spec.alpha :as s] [clojure.stacktrace :as strace] @@ -66,23 +70,33 @@ [system & {:keys [update-fn id save? migrate? inc-revn?] :or {save? false migrate? true inc-revn? true}}] (db/with-atomic [conn (:app.db/pool system)] - (let [file (db/get-by-id conn :file id {:for-update true}) - file (-> file - (update :features db/decode-pgarray #{}) - (update :data blob/decode) - (cond-> migrate? (update :data pmg/migrate-data))) - file (binding [*conn* conn] - (-> (update-fn file) - (cond-> inc-revn? (update :revn inc))))] - (when save? - (let [features (db/create-array conn "text" (:features file)) - data (blob/encode (:data file))] - (db/update! conn :file - {:data data - :revn (:revn file) - :features features} - {:id id}))) - file))) + (let [file (-> (db/get-by-id conn :file id {:for-update true}) + (update :features db/decode-pgarray #{}))] + (binding [*conn* conn + pmap/*tracked* (atom {}) + pmap/*load-fn* (partial files/load-pointer conn id) + ffeat/*wrap-with-pointer-map-fn* + (if (contains? (:features file) "storage/pointer-map") pmap/wrap identity) + ffeat/*wrap-with-objects-map-fn* + (if (contains? (:features file) "storage/objectd-map") omap/wrap identity)] + (let [file (-> file + (update :data blob/decode) + (cond-> migrate? (update :data pmg/migrate-data)) + (update-fn) + (cond-> inc-revn? (update :revn inc)))] + (when save? + (let [features (db/create-array conn "text" (:features file)) + data (blob/encode (:data file))] + (db/update! conn :file + {:data data + :revn (:revn file) + :features features} + {:id id}) + + (when (contains? (:features file) "storage/pointer-map") + (files/persist-pointers! conn id)))) + + (dissoc file :data)))))) (def ^:private sql:retrieve-files-chunk "SELECT id, name, created_at, data FROM file diff --git a/backend/src/app/srepl/main.clj b/backend/src/app/srepl/main.clj index 80d5a7035b..ae09aa2ff7 100644 --- a/backend/src/app/srepl/main.clj +++ b/backend/src/app/srepl/main.clj @@ -110,10 +110,10 @@ (if (contains? features "storage/objects-map") file (-> file - (update :data migrate-to-omap) + (update :data migrate) (update :features conj "storage/objects-map")))) - (migrate-to-omap [data] + (migrate [data] (-> data (update :pages-index update-vals #(update % :objects omap/wrap)) (update :components update-vals #(update % :objects omap/wrap))))] @@ -125,24 +125,17 @@ (defn enable-pointer-map-feature-on-file! [system & {:keys [save? id]}] - (letfn [(update-file [{:keys [features id] :as file}] + (letfn [(update-file [{:keys [features] :as file}] (if (contains? features "storage/pointer-map") file (-> file - (update :data migrate-to-omap id) + (update :data migrate) (update :features conj "storage/pointer-map")))) - (migrate-to-omap [data file-id] - (binding [pmap/*tracked* (atom {})] - (let [data (-> data - (update :pages-index update-vals pmap/wrap) - (update :components pmap/wrap))] - (doseq [[id item] @pmap/*tracked*] - (db/insert! h/*conn* :file-data-fragment - {:id id - :file-id file-id - :content (-> item deref blob/encode)})) - data)))] + (migrate [data] + (-> data + (update :pages-index update-vals pmap/wrap) + (update :components pmap/wrap)))] (h/update-file! system :id id