Skip to content

Commit db3f692

Browse files
authored
Fetch everything during a read in one go (#20)
* read data in one shot * fix formatting
1 parent faaf200 commit db3f692

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/konserve_jdbc/core.clj

+17-7
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,19 @@
151151
:size nil})
152152
(extract-bytes res db-type))))
153153

154+
(defn read-all [db-type connection table id]
155+
(let [res (-> (jdbc/execute! connection
156+
[(str "SELECT id, header, meta, val FROM " table " WHERE id = '" id "';")]
157+
{:builder-fn rs/as-unqualified-lower-maps})
158+
first)]
159+
(into {} (for [[k v] res] [k (if (= k :id) v (extract-bytes v db-type))]))))
160+
154161
(extend-protocol PBackingLock
155162
Boolean
156163
(-release [_ env]
157164
(if (:sync? env) nil (go-try- nil))))
158165

159-
(defrecord JDBCRow [table key data]
166+
(defrecord JDBCRow [table key data cache]
160167
PBackingBlob
161168
(-sync [_ env]
162169
(async+sync (:sync? env) *default-sync-translation*
@@ -172,17 +179,20 @@
172179
(if (:sync? env) true (go-try- true))) ;; May not return nil, otherwise eternal retries
173180
(-read-header [_ env]
174181
(async+sync (:sync? env) *default-sync-translation*
175-
(go-try- (read-field (:dbtype (:db-spec table)) (:connection table) (:table table) key :header))))
182+
(go-try-
183+
(when-not @cache
184+
(reset! cache (read-all (:dbtype (:db-spec table)) (:connection table) (:table table) key)))
185+
(-> @cache :header))))
176186
(-read-meta [_ _meta-size env]
177187
(async+sync (:sync? env) *default-sync-translation*
178-
(go-try- (read-field (:dbtype (:db-spec table)) (:connection table) (:table table) key :meta))))
188+
(go-try- (-> @cache :meta))))
179189
(-read-value [_ _meta-size env]
180190
(async+sync (:sync? env) *default-sync-translation*
181-
(go-try- (read-field (:dbtype (:db-spec table)) (:connection table) (:table table) key :val))))
191+
(go-try- (-> @cache :val))))
182192
(-read-binary [_ _meta-size locked-cb env]
183193
(async+sync (:sync? env) *default-sync-translation*
184-
(go-try- (read-field (:dbtype (:db-spec table)) (:connection table) (:table table) key :val
185-
:binary? true :locked-cb locked-cb))))
194+
(go-try- (locked-cb {:input-stream (when (-> @cache :val) (ByteArrayInputStream. (-> @cache :val)))
195+
:size nil}))))
186196
(-write-header [_ header env]
187197
(async+sync (:sync? env) *default-sync-translation*
188198
(go-try- (swap! data assoc :header header))))
@@ -200,7 +210,7 @@
200210
PBackingStore
201211
(-create-blob [this store-key env]
202212
(async+sync (:sync? env) *default-sync-translation*
203-
(go-try- (JDBCRow. this store-key (atom {})))))
213+
(go-try- (JDBCRow. this store-key (atom {}) (atom nil)))))
204214
(-delete-blob [_ store-key env]
205215
(async+sync (:sync? env) *default-sync-translation*
206216
(go-try- (jdbc/execute! connection

0 commit comments

Comments
 (0)