Skip to content

Commit 79f05cc

Browse files
author
Peter
committed
Merge branch 'mmapfix'
2 parents 95a38c6 + 4b11dcc commit 79f05cc

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/main/java/com/graphhopper/storage/MMapDataAccess.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,20 @@ protected void mapIt(long offset, long byteCount, boolean clearNew) {
112112
if (byteCount <= capacity())
113113
return;
114114

115-
int i = 0;
116-
int segmentsToMap = (int) (byteCount / segmentSizeInBytes);
115+
long longSegmentSize = segmentSizeInBytes;
116+
int segmentsToMap = (int) (byteCount / longSegmentSize);
117117
if (segmentsToMap < 0)
118118
throw new IllegalStateException("Too many segments needs to be allocated. Increase segmentSize.");
119119

120-
if (byteCount % segmentSizeInBytes != 0)
120+
if (byteCount % longSegmentSize != 0)
121121
segmentsToMap++;
122122
if (segmentsToMap == 0)
123123
throw new IllegalStateException("0 segments are not allowed.");
124124

125125
long bufferStart = offset;
126126
int newSegments;
127+
int i = 0;
128+
long newFileLength = offset + segmentsToMap * longSegmentSize;
127129
try {
128130
// ugly remapping
129131
// http://stackoverflow.com/q/14011919/194609
@@ -135,19 +137,20 @@ protected void mapIt(long offset, long byteCount, boolean clearNew) {
135137
// This approach is probably problematic but a bit faster if done often.
136138
// Here we rely on the OS+file system that increasing the file
137139
// size has no effect on the old mappings!
138-
bufferStart += segments.size() * segmentSizeInBytes;
140+
bufferStart += segments.size() * longSegmentSize;
139141
newSegments = segmentsToMap - segments.size();
140142
}
141-
raFile.setLength(offset + segmentsToMap * segmentSizeInBytes);
143+
raFile.setLength(newFileLength);
142144
for (; i < newSegments; i++) {
143-
segments.add(newByteBuffer(bufferStart, segmentSizeInBytes));
144-
bufferStart += segmentSizeInBytes;
145+
segments.add(newByteBuffer(bufferStart, longSegmentSize));
146+
bufferStart += longSegmentSize;
145147
}
146148
} catch (IOException ex) {
147149
// we could get an exception here if buffer is too small and area too large
148150
// e.g. I got an exception for the 65421th buffer (probably around 2**16 == 65536)
149151
throw new RuntimeException("Couldn't map buffer " + i + " of " + segmentsToMap
150-
+ " at position " + bufferStart + " for " + byteCount + " bytes with offset " + offset, ex);
152+
+ " at position " + bufferStart + " for " + byteCount
153+
+ " bytes with offset " + offset + ", new fileLength:" + newFileLength, ex);
151154
}
152155
}
153156

@@ -206,7 +209,7 @@ public boolean loadExisting() {
206209
try {
207210
long byteCount = readHeader(raFile);
208211
if (byteCount < 0)
209-
return false;
212+
return false;
210213
mapIt(HEADER_OFFSET, byteCount - HEADER_OFFSET, false);
211214
return true;
212215
} catch (IOException ex) {

0 commit comments

Comments
 (0)