|
81 | 81 | (^:private append-to-slab! [_ descriptor])
|
82 | 82 | (^:private read-write-lock [_]))
|
83 | 83 |
|
| 84 | +(defmacro ^:private with-buffer [[buf slab] & body] |
| 85 | + `(with-lock (read-write-lock ~slab) |
| 86 | + (when-let [~buf (buffer ~slab)] |
| 87 | + ~@body))) |
| 88 | + |
84 | 89 | ;;;
|
85 | 90 |
|
86 | 91 | (defn create-buffer [filename size]
|
|
157 | 162 | (deserializer))
|
158 | 163 | ITask
|
159 | 164 | (status [_]
|
160 |
| - (or @status |
161 |
| - (let [s (case (.get ^ByteBuffer (buffer slab) (p/+ offset 1)) |
162 |
| - 0 :incomplete |
163 |
| - 1 :in-progress |
164 |
| - 2 :complete)] |
165 |
| - (reset! status s) |
166 |
| - s))) |
| 165 | + (with-buffer [buf slab] |
| 166 | + (or @status |
| 167 | + (let [s (case (.get buf (p/+ offset 1)) |
| 168 | + 0 :incomplete |
| 169 | + 1 :in-progress |
| 170 | + 2 :complete)] |
| 171 | + (reset! status s) |
| 172 | + s)))) |
167 | 173 | (status! [_ s]
|
168 |
| - (reset! status s) |
169 |
| - (.put ^ByteBuffer (buffer slab) (p/+ offset 1) |
170 |
| - (case s |
171 |
| - :incomplete 0 |
172 |
| - :in-progress 1 |
173 |
| - :complete 2)) |
174 |
| - (invalidate slab (p/+ offset 1) 1) |
175 |
| - nil)) |
| 174 | + (with-buffer [buf slab] |
| 175 | + (reset! status s) |
| 176 | + (.put buf (p/+ offset 1) |
| 177 | + (case s |
| 178 | + :incomplete 0 |
| 179 | + :in-progress 1 |
| 180 | + :complete 2)) |
| 181 | + (invalidate slab (p/+ offset 1) 1) |
| 182 | + nil))) |
176 | 183 |
|
177 | 184 | (defn- task [slab offset len lock]
|
178 | 185 | (Task.
|
|
181 | 188 | len
|
182 | 189 | (atom nil)
|
183 | 190 | (fn []
|
184 |
| - (with-lock lock |
185 |
| - (let [^ByteBuffer buf (-> (buffer slab) |
| 191 | + (with-buffer [buf slab] |
| 192 | + (let [^ByteBuffer buf (-> buf |
186 | 193 | (.position offset)
|
187 | 194 | ^ByteBuffer
|
188 | 195 | (.limit (+ offset len))
|
|
212 | 219 | ([slab]
|
213 | 220 | (slab->task-seq slab 0))
|
214 | 221 | ([slab pos]
|
215 |
| - (let [lock (read-write-lock slab)] |
216 |
| - (with-lock lock |
217 |
| - (try |
218 |
| - (let [^ByteBuffer |
219 |
| - buf' (-> (buffer slab) |
220 |
| - (.position pos))] |
221 |
| - |
222 |
| - ;; is there a next task, and is there space left in the buffer? |
223 |
| - (when (and |
224 |
| - (< header-size (.remaining buf')) |
225 |
| - (== 1 (.get buf'))) |
226 |
| - |
227 |
| - (lazy-seq |
228 |
| - (with-lock lock |
229 |
| - (let [status (.get buf') |
230 |
| - checksum (.getLong buf') |
231 |
| - size (.getInt buf')] |
232 |
| - |
233 |
| - ;; this shouldn't be necessary, but let's not gratuitously |
234 |
| - ;; overreach our bounds |
235 |
| - (when (< size (.remaining buf')) |
236 |
| - (cons |
237 |
| - |
238 |
| - (task |
239 |
| - slab |
240 |
| - pos |
241 |
| - (+ header-size size) |
242 |
| - lock) |
243 |
| - |
244 |
| - (slab->task-seq |
245 |
| - slab |
246 |
| - (+ pos header-size size))))))))) |
247 |
| - (catch Throwable e |
248 |
| - ;; this implies unrecoverable corruption |
249 |
| - nil |
250 |
| - )))))) |
| 222 | + (with-buffer [buf slab] |
| 223 | + (try |
| 224 | + (let [^ByteBuffer |
| 225 | + buf' (.position buf pos)] |
| 226 | + |
| 227 | + ;; is there a next task, and is there space left in the buffer? |
| 228 | + (when (and |
| 229 | + (< header-size (.remaining buf')) |
| 230 | + (== 1 (.get buf'))) |
| 231 | + |
| 232 | + (lazy-seq |
| 233 | + (with-buffer [buf slab] |
| 234 | + (let [status (.get buf') |
| 235 | + checksum (.getLong buf') |
| 236 | + size (.getInt buf')] |
| 237 | + |
| 238 | + ;; this shouldn't be necessary, but let's not gratuitously |
| 239 | + ;; overreach our bounds |
| 240 | + (when (< size (.remaining buf')) |
| 241 | + (cons |
| 242 | + |
| 243 | + (task |
| 244 | + slab |
| 245 | + pos |
| 246 | + (+ header-size size) |
| 247 | + (read-write-lock slab)) |
| 248 | + |
| 249 | + (slab->task-seq |
| 250 | + slab |
| 251 | + (+ pos header-size size))))))))) |
| 252 | + (catch Throwable e |
| 253 | + ;; this implies unrecoverable corruption |
| 254 | + nil |
| 255 | + ))))) |
251 | 256 |
|
252 | 257 | (deftype TaskSlab
|
253 | 258 | [filename
|
|
287 | 292 | (fn [[start end]]
|
288 | 293 | [(min start start') (max end end')]))))
|
289 | 294 |
|
290 |
| - (sync! [_] |
| 295 | + (sync! [this] |
291 | 296 | (let [[start end] @dirty]
|
292 | 297 | (when (< start end)
|
293 |
| - (when-let [^MappedByteBuffer buf @buf] |
294 |
| - (force-buffer buf start (- end start)) |
295 |
| - (compare-and-set! dirty [start end] [Integer/MAX_VALUE 0]) |
296 |
| - nil)))) |
| 298 | + (with-buffer [_ this] |
| 299 | + (let [buf @buf] |
| 300 | + (force-buffer buf start (- end start)) |
| 301 | + (compare-and-set! dirty [start end] [Integer/MAX_VALUE 0]) |
| 302 | + nil))))) |
297 | 303 |
|
298 | 304 | (append-to-slab! [this descriptor]
|
299 |
| - (with-exclusive-lock lock |
| 305 | + (with-buffer [buf this] |
300 | 306 | (let [ary (nippy/freeze descriptor)
|
301 | 307 | cnt (count ary)
|
302 | 308 | pos @position
|
303 |
| - |
304 |
| - ^ByteBuffer |
305 |
| - buf (-> (buffer this) |
306 |
| - (.position pos))] |
| 309 | + ^ByteBuffer buf (.position buf pos)] |
307 | 310 |
|
308 | 311 | (when (> (.remaining buf) (+ (count ary) header-size))
|
309 | 312 | ;; write to the buffer
|
|
0 commit comments