@@ -461,26 +461,39 @@ public boolean commit(final int commitLeastPages) {
461
461
*/
462
462
public MappedFile findMappedFileByOffset (final long offset , final boolean returnFirstOnNotFound ) {
463
463
try {
464
- MappedFile mappedFile = this .getFirstMappedFile ();
465
- if (mappedFile != null ) {
466
- int index = (int ) ((offset / this .mappedFileSize ) - (mappedFile .getFileFromOffset () / this .mappedFileSize ));
467
- if (index < 0 || index >= this .mappedFiles .size ()) {
468
- LOG_ERROR .warn ("Offset for {} not matched. Request offset: {}, index: {}, " +
469
- "mappedFileSize: {}, mappedFiles count: {}" ,
470
- mappedFile ,
464
+ MappedFile firstMappedFile = this .getFirstMappedFile ();
465
+ MappedFile lastMappedFile = this .getLastMappedFile ();
466
+ if (firstMappedFile != null && lastMappedFile != null ) {
467
+ if (offset < firstMappedFile .getFileFromOffset () || offset >= lastMappedFile .getFileFromOffset () + this .mappedFileSize ) {
468
+ LOG_ERROR .warn ("Offset not matched. Request offset: {}, firstOffset: {}, lastOffset: {}, mappedFileSize: {}, mappedFiles count: {}" ,
471
469
offset ,
472
- index ,
470
+ firstMappedFile .getFileFromOffset (),
471
+ lastMappedFile .getFileFromOffset () + this .mappedFileSize ,
473
472
this .mappedFileSize ,
474
473
this .mappedFiles .size ());
475
- }
474
+ } else {
475
+ int index = (int ) ((offset / this .mappedFileSize ) - (firstMappedFile .getFileFromOffset () / this .mappedFileSize ));
476
+ MappedFile targetFile = null ;
477
+ try {
478
+ targetFile = this .mappedFiles .get (index );
479
+ } catch (Exception ignored ) {
480
+ }
476
481
477
- try {
478
- return this .mappedFiles .get (index );
479
- } catch (Exception e ) {
480
- if (returnFirstOnNotFound ) {
481
- return mappedFile ;
482
+ if (targetFile != null && offset >= targetFile .getFileFromOffset ()
483
+ && offset < targetFile .getFileFromOffset () + this .mappedFileSize ) {
484
+ return targetFile ;
482
485
}
483
- LOG_ERROR .warn ("findMappedFileByOffset failure. " , e );
486
+
487
+ for (MappedFile tmpMappedFile : this .mappedFiles ) {
488
+ if (offset >= tmpMappedFile .getFileFromOffset ()
489
+ && offset < tmpMappedFile .getFileFromOffset () + this .mappedFileSize ) {
490
+ return tmpMappedFile ;
491
+ }
492
+ }
493
+ }
494
+
495
+ if (returnFirstOnNotFound ) {
496
+ return firstMappedFile ;
484
497
}
485
498
}
486
499
} catch (Exception e ) {
0 commit comments