Skip to content

Commit

Permalink
Fetch everything during a read in one go (#20)
Browse files Browse the repository at this point in the history
* read data in one shot

* fix formatting
  • Loading branch information
alekcz authored Oct 19, 2023
1 parent faaf200 commit db3f692
Showing 1 changed file with 17 additions and 7 deletions.
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

0 comments on commit db3f692

Please sign in to comment.