|
151 | 151 | :size nil})
|
152 | 152 | (extract-bytes res db-type))))
|
153 | 153 |
|
| 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 | + |
154 | 161 | (extend-protocol PBackingLock
|
155 | 162 | Boolean
|
156 | 163 | (-release [_ env]
|
157 | 164 | (if (:sync? env) nil (go-try- nil))))
|
158 | 165 |
|
159 |
| -(defrecord JDBCRow [table key data] |
| 166 | +(defrecord JDBCRow [table key data cache] |
160 | 167 | PBackingBlob
|
161 | 168 | (-sync [_ env]
|
162 | 169 | (async+sync (:sync? env) *default-sync-translation*
|
|
172 | 179 | (if (:sync? env) true (go-try- true))) ;; May not return nil, otherwise eternal retries
|
173 | 180 | (-read-header [_ env]
|
174 | 181 | (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)))) |
176 | 186 | (-read-meta [_ _meta-size env]
|
177 | 187 | (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)))) |
179 | 189 | (-read-value [_ _meta-size env]
|
180 | 190 | (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)))) |
182 | 192 | (-read-binary [_ _meta-size locked-cb env]
|
183 | 193 | (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})))) |
186 | 196 | (-write-header [_ header env]
|
187 | 197 | (async+sync (:sync? env) *default-sync-translation*
|
188 | 198 | (go-try- (swap! data assoc :header header))))
|
|
200 | 210 | PBackingStore
|
201 | 211 | (-create-blob [this store-key env]
|
202 | 212 | (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))))) |
204 | 214 | (-delete-blob [_ store-key env]
|
205 | 215 | (async+sync (:sync? env) *default-sync-translation*
|
206 | 216 | (go-try- (jdbc/execute! connection
|
|
0 commit comments