Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch everything during a read in one go #20

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/konserve_jdbc/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,19 @@
:size nil})
(extract-bytes res db-type))))

(defn read-all [db-type connection table id]
(let [res (-> (jdbc/execute! connection
[(str "SELECT id, header, meta, val FROM " table " WHERE id = '" id "';")]
{:builder-fn rs/as-unqualified-lower-maps})
first)]
(into {} (for [[k v] res] [k (if (= k :id) v (extract-bytes v db-type))]))))

(extend-protocol PBackingLock
Boolean
(-release [_ env]
(if (:sync? env) nil (go-try- nil))))

(defrecord JDBCRow [table key data]
(defrecord JDBCRow [table key data cache]
PBackingBlob
(-sync [_ env]
(async+sync (:sync? env) *default-sync-translation*
Expand All @@ -172,17 +179,20 @@
(if (:sync? env) true (go-try- true))) ;; May not return nil, otherwise eternal retries
(-read-header [_ env]
(async+sync (:sync? env) *default-sync-translation*
(go-try- (read-field (:dbtype (:db-spec table)) (:connection table) (:table table) key :header))))
(go-try-
(when-not @cache
(reset! cache (read-all (:dbtype (:db-spec table)) (:connection table) (:table table) key)))
(-> @cache :header))))
(-read-meta [_ _meta-size env]
(async+sync (:sync? env) *default-sync-translation*
(go-try- (read-field (:dbtype (:db-spec table)) (:connection table) (:table table) key :meta))))
(go-try- (-> @cache :meta))))
(-read-value [_ _meta-size env]
(async+sync (:sync? env) *default-sync-translation*
(go-try- (read-field (:dbtype (:db-spec table)) (:connection table) (:table table) key :val))))
(go-try- (-> @cache :val))))
(-read-binary [_ _meta-size locked-cb env]
(async+sync (:sync? env) *default-sync-translation*
(go-try- (read-field (:dbtype (:db-spec table)) (:connection table) (:table table) key :val
:binary? true :locked-cb locked-cb))))
(go-try- (locked-cb {:input-stream (when (-> @cache :val) (ByteArrayInputStream. (-> @cache :val)))
:size nil}))))
(-write-header [_ header env]
(async+sync (:sync? env) *default-sync-translation*
(go-try- (swap! data assoc :header header))))
Expand All @@ -200,7 +210,7 @@
PBackingStore
(-create-blob [this store-key env]
(async+sync (:sync? env) *default-sync-translation*
(go-try- (JDBCRow. this store-key (atom {})))))
(go-try- (JDBCRow. this store-key (atom {}) (atom nil)))))
(-delete-blob [_ store-key env]
(async+sync (:sync? env) *default-sync-translation*
(go-try- (jdbc/execute! connection
Expand Down