Add read-only option for files/get-file

This commit is contained in:
Andrey Antukh 2025-05-08 19:12:50 +02:00
parent 9be569c54c
commit 9de8ebb52c

View file

@ -208,7 +208,7 @@
[:project-id {:optional true} ::sm/uuid]]) [:project-id {:optional true} ::sm/uuid]])
(defn- migrate-file (defn- migrate-file
[{:keys [::db/conn] :as cfg} {:keys [id] :as file}] [{:keys [::db/conn] :as cfg} {:keys [id] :as file} {:keys [read-only?]}]
(binding [pmap/*load-fn* (partial feat.fdata/load-pointer cfg id) (binding [pmap/*load-fn* (partial feat.fdata/load-pointer cfg id)
pmap/*tracked* (pmap/create-tracked)] pmap/*tracked* (pmap/create-tracked)]
(let [;; For avoid unnecesary overhead of creating multiple pointers and (let [;; For avoid unnecesary overhead of creating multiple pointers and
@ -219,43 +219,45 @@
file (-> file file (-> file
(update :data feat.fdata/process-pointers deref) (update :data feat.fdata/process-pointers deref)
(update :data feat.fdata/process-objects (partial into {})) (update :data feat.fdata/process-objects (partial into {}))
(fmg/migrate-file)) (fmg/migrate-file))]
;; When file is migrated, we break the rule of no perform (if (or read-only? (db/read-only? conn))
;; mutations on get operations and update the file with all file
;; migrations applied (let [;; When file is migrated, we break the rule of no perform
;; ;; mutations on get operations and update the file with all
;; WARN: he following code will not work on read-only mode, ;; migrations applied
;; it is a known issue; we keep is not implemented until we file (if (contains? (:features file) "fdata/objects-map")
;; really need this. (feat.fdata/enable-objects-map file)
file (if (contains? (:features file) "fdata/objects-map") file)
(feat.fdata/enable-objects-map file) file (if (contains? (:features file) "fdata/pointer-map")
file) (feat.fdata/enable-pointer-map file)
file (if (contains? (:features file) "fdata/pointer-map") file)]
(feat.fdata/enable-pointer-map file)
file)]
(db/update! conn :file (db/update! conn :file
{:data (blob/encode (:data file)) {:data (blob/encode (:data file))
:version (:version file) :version (:version file)
:features (db/create-array conn "text" (:features file))} :features (db/create-array conn "text" (:features file))}
{:id id}) {:id id}
{::db/return-keys false})
(when (contains? (:features file) "fdata/pointer-map") (when (contains? (:features file) "fdata/pointer-map")
(feat.fdata/persist-pointers! cfg id)) (feat.fdata/persist-pointers! cfg id))
(feat.fmigr/upsert-migrations! conn file) (feat.fmigr/upsert-migrations! conn file)
(feat.fmigr/resolve-applied-migrations cfg file)))) (feat.fmigr/resolve-applied-migrations cfg file))))))
(defn get-file (defn get-file
[{:keys [::db/conn ::wrk/executor] :as cfg} id [{:keys [::db/conn ::wrk/executor] :as cfg} id
& {:keys [project-id & {:keys [project-id
migrate? migrate?
include-deleted? include-deleted?
lock-for-update?] lock-for-update?
preload-pointers?]
:or {include-deleted? false :or {include-deleted? false
lock-for-update? false lock-for-update? false
migrate? true}}] migrate? true
preload-pointers? false}
:as options}]
(assert (db/connection? conn) "expected cfg with valid connection") (assert (db/connection? conn) "expected cfg with valid connection")
@ -273,10 +275,16 @@
;; because it has heavy and synchronous operations for ;; because it has heavy and synchronous operations for
;; decoding file body that are not very friendly with virtual ;; decoding file body that are not very friendly with virtual
;; threads. ;; threads.
file (px/invoke! executor #(decode-row file))] file (px/invoke! executor #(decode-row file))
file (if (and migrate? (fmg/need-migration? file))
(migrate-file cfg file options)
file)]
(if preload-pointers?
(binding [pmap/*load-fn* (partial feat.fdata/load-pointer cfg id)]
(update file :data feat.fdata/process-pointers deref))
(if (and migrate? (fmg/need-migration? file))
(migrate-file cfg file)
file))) file)))
(defn get-minimal-file (defn get-minimal-file