@@ -60,7 +60,9 @@ void updateLocation(Location location, byte type, boolean sync) throws ClosedJou
60
60
threadLock .lock ();
61
61
try {
62
62
if (opened ) {
63
- journal .sync ();
63
+ if (journal .getInflightWrites ().containsKey (location )) {
64
+ journal .sync ();
65
+ }
64
66
//
65
67
RandomAccessFile raf = getOrCreateRaf (Thread .currentThread (), location .getDataFileId ());
66
68
if (seekToLocation (raf , location , false )) {
@@ -278,39 +280,52 @@ private boolean seekToLocation(RandomAccessFile raf, Location destination, boole
278
280
length = raf .readInt ();
279
281
type = raf .readByte ();
280
282
}
281
- // Else seek:
283
+ // If pointer is wrong, seek by trying the saved file position :
282
284
if (pointer != destination .getPointer ()) {
283
- Entry <Location , Long > hint = journal .getHints ().lowerEntry (destination );
284
- if (hint != null && hint .getKey ().getDataFileId () == destination .getDataFileId ()) {
285
- position = hint .getValue ();
286
- } else {
287
- position = Journal .FILE_HEADER_SIZE ;
288
- }
289
- raf .seek (position );
290
- if (raf .length () - position > Journal .RECORD_HEADER_SIZE ) {
285
+ position = destination .getThisFilePosition ();
286
+ if (position != Location .NOT_SET && raf .length () - position > Journal .RECORD_HEADER_SIZE ) {
287
+ raf .seek (position );
291
288
pointer = raf .readInt ();
292
289
length = raf .readInt ();
293
290
type = raf .readByte ();
294
- while (pointer != destination .getPointer ()) {
295
- IOHelper .skipBytes (raf , length - Journal .RECORD_HEADER_SIZE );
296
- position = raf .getFilePointer ();
297
- if (raf .length () - position > Journal .RECORD_HEADER_SIZE ) {
298
- pointer = raf .readInt ();
299
- length = raf .readInt ();
300
- type = raf .readByte ();
301
- } else {
302
- return false ;
291
+ }
292
+ // Else seek by using hints:
293
+ if (pointer != destination .getPointer ()) {
294
+ Entry <Location , Long > hint = journal .getHints ().lowerEntry (destination );
295
+ if (hint != null && hint .getKey ().getDataFileId () == destination .getDataFileId ()) {
296
+ position = hint .getValue ();
297
+ } else {
298
+ position = Journal .FILE_HEADER_SIZE ;
299
+ }
300
+ raf .seek (position );
301
+ if (raf .length () - position > Journal .RECORD_HEADER_SIZE ) {
302
+ pointer = raf .readInt ();
303
+ length = raf .readInt ();
304
+ type = raf .readByte ();
305
+ while (pointer != destination .getPointer ()) {
306
+ IOHelper .skipBytes (raf , length - Journal .RECORD_HEADER_SIZE );
307
+ position = raf .getFilePointer ();
308
+ if (raf .length () - position > Journal .RECORD_HEADER_SIZE ) {
309
+ pointer = raf .readInt ();
310
+ length = raf .readInt ();
311
+ type = raf .readByte ();
312
+ } else {
313
+ return false ;
314
+ }
303
315
}
316
+ } else {
317
+ return false ;
304
318
}
305
- } else {
306
- return false ;
307
319
}
308
320
}
309
321
if (fillLocation ) {
322
+ // If we have to fill the location, do so and do not seek back to this position:
310
323
destination .setThisFilePosition (position );
311
324
destination .setSize (length );
312
325
destination .setType (type );
313
326
} else {
327
+ // Otherwise seek back to this position as caller will probably read
328
+ // things by itself:
314
329
raf .seek (position );
315
330
}
316
331
return true ;
@@ -351,6 +366,7 @@ private RandomAccessFile getOrCreateRaf(Thread thread, Integer file) throws Comp
351
366
RandomAccessFile raf = rafs .get (file );
352
367
if (raf == null ) {
353
368
raf = journal .getDataFile (file ).openRandomAccessFile ();
369
+ IOHelper .skipBytes (raf , journal .FILE_HEADER_SIZE );
354
370
rafs .put (file , raf );
355
371
}
356
372
return raf ;
0 commit comments