Skip to content

Commit 10d871c

Browse files
author
Peter
committed
Merge pull request graphhopper#275 from duongnt/master
incCapacity takes # of bytes required, not the delta
2 parents 6224b05 + cc4ad69 commit 10d871c

17 files changed

+86
-44
lines changed

core/src/main/java/com/graphhopper/coll/OSMIDMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ public int put( long key, int value )
7575
return oldValue;
7676
}
7777

78-
values.incCapacity(size + 4);
78+
values.ensureCapacity(size + 4);
7979
values.setInt(size, value);
8080
long doubleSize = size * 2;
81-
keys.incCapacity(doubleSize + 8);
81+
keys.ensureCapacity(doubleSize + 8);
8282

8383
// store long => double of the orig size
8484
byte[] longBytes = bitUtil.fromLong(key);

core/src/main/java/com/graphhopper/reader/PillarInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void setNode( int id, double lat, double lon, double ele )
7676
private void _setNode( int id, double lat, double lon, double ele )
7777
{
7878
long tmp = (long) id * rowSizeInBytes;
79-
da.incCapacity(tmp + rowSizeInBytes);
79+
da.ensureCapacity(tmp + rowSizeInBytes);
8080
da.setInt(tmp + LAT, Helper.degreeToInt(lat));
8181
da.setInt(tmp + LON, Helper.degreeToInt(lon));
8282

core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,15 +766,15 @@ public final boolean accept( EdgeIteratorState iter )
766766
private void setOrigEdgeCount( int index, int value )
767767
{
768768
long tmp = (long) index * 4;
769-
originalEdges.incCapacity(tmp + 4);
769+
originalEdges.ensureCapacity(tmp + 4);
770770
originalEdges.setInt(tmp, value);
771771
}
772772

773773
private int getOrigEdgeCount( int index )
774774
{
775775
// TODO possible memory usage improvement: avoid storing the value 1 for normal edges (does not change)!
776776
long tmp = (long) index * 4;
777-
originalEdges.incCapacity(tmp + 4);
777+
originalEdges.ensureCapacity(tmp + 4);
778778
return originalEdges.getInt(tmp);
779779
}
780780

core/src/main/java/com/graphhopper/search/NameIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public int put( String name )
7777
}
7878
byte[] bytes = getBytes(name);
7979
int oldPointer = bytePointer;
80-
names.incCapacity(bytePointer + 1 + bytes.length);
80+
names.ensureCapacity(bytePointer + 1 + bytes.length);
8181
byte[] sizeBytes = new byte[]
8282
{
8383
(byte) bytes.length

core/src/main/java/com/graphhopper/storage/AbstractDataAccess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ protected void copyHeader( DataAccess da )
138138
public DataAccess copyTo( DataAccess da )
139139
{
140140
copyHeader(da);
141-
da.incCapacity(getCapacity());
141+
da.ensureCapacity(getCapacity());
142142
long cap = getCapacity();
143143
// currently get/setBytes does not support copying more bytes then segmentSize
144144
int segSize = Math.min(da.getSegmentSize(), getSegmentSize());

core/src/main/java/com/graphhopper/storage/DataAccess.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Directory.create. Current implementations are RAM and memory mapped access.
2424
* <p/>
2525
* Life cycle: (1) object creation, (2) configuration (e.g. segment size), (3) create or
26-
* loadExisting, (4) usage and calling incCapacity if necessary, (5) close
26+
* loadExisting, (4) usage and calling ensureCapacity if necessary, (5) close
2727
* <p/>
2828
* @author Peter Karich
2929
*/
@@ -86,7 +86,7 @@ public interface DataAccess extends Storable<DataAccess>
8686

8787
/**
8888
* The first time you use a DataAccess object after configuring it you need to call this. After
89-
* that first call you have to use incCapacity to ensure that enough space is reserved.
89+
* that first call you have to use ensureCapacity to ensure that enough space is reserved.
9090
*/
9191
@Override
9292
DataAccess create( long bytes );
@@ -98,7 +98,7 @@ public interface DataAccess extends Storable<DataAccess>
9898
* @see #create(long)
9999
* @return true if size was increased
100100
*/
101-
boolean incCapacity( long bytes );
101+
boolean ensureCapacity(long bytes);
102102

103103
/**
104104
* Reduces the allocate space to the specified bytes. Warning: it'll free the space even if it

core/src/main/java/com/graphhopper/storage/GraphHopperStorage.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@
2424
import com.graphhopper.routing.util.EdgeFilter;
2525
import com.graphhopper.routing.util.EncodingManager;
2626
import com.graphhopper.search.NameIndex;
27-
import com.graphhopper.util.*;
28-
import static com.graphhopper.util.Helper.nf;
27+
import com.graphhopper.util.BitUtil;
28+
import com.graphhopper.util.EdgeExplorer;
29+
import com.graphhopper.util.EdgeIterator;
30+
import com.graphhopper.util.EdgeIteratorState;
31+
import com.graphhopper.util.GHUtility;
32+
import com.graphhopper.util.Helper;
33+
import com.graphhopper.util.PointList;
2934
import com.graphhopper.util.shapes.BBox;
3035

36+
import static com.graphhopper.util.Helper.nf;
37+
3138
/**
3239
* The main implementation which handles nodes and edges file format. It can be used with different
3340
* Directory implementations like RAMDirectory for fast access or via MMapDirectory for
@@ -259,7 +266,7 @@ final void ensureNodeIndex( int nodeIndex )
259266

260267
long oldNodes = nodeCount;
261268
nodeCount = nodeIndex + 1;
262-
boolean capacityIncreased = nodes.incCapacity((long) nodeCount * nodeEntryBytes);
269+
boolean capacityIncreased = nodes.ensureCapacity((long)nodeCount * nodeEntryBytes);
263270
if (capacityIncreased)
264271
{
265272
long newBytesCapacity = nodes.getCapacity();
@@ -290,12 +297,12 @@ private void initNodeRefs( long oldCapacity, long newCapacity )
290297

291298
private void ensureEdgeIndex( int edgeIndex )
292299
{
293-
edges.incCapacity(((long) edgeIndex + 1) * edgeEntryBytes);
300+
edges.ensureCapacity(((long)edgeIndex + 1) * edgeEntryBytes);
294301
}
295302

296303
private void ensureGeometry( long bytePos, int byteLength )
297304
{
298-
wayGeometry.incCapacity(bytePos + byteLength);
305+
wayGeometry.ensureCapacity(bytePos + byteLength);
299306
}
300307

301308
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public MMapDataAccess create( long bytes )
8181
initRandomAccessFile();
8282
bytes = Math.max(10 * 4, bytes);
8383
setSegmentSize(segmentSizeInBytes);
84-
incCapacity(bytes);
84+
ensureCapacity(bytes);
8585
return this;
8686
}
8787

@@ -97,7 +97,7 @@ public DataAccess copyTo( DataAccess da )
9797
}
9898

9999
@Override
100-
public boolean incCapacity( long bytes )
100+
public boolean ensureCapacity(long bytes)
101101
{
102102
return mapIt(HEADER_OFFSET, bytes, true);
103103
}

core/src/main/java/com/graphhopper/storage/RAMDataAccess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ public RAMDataAccess create( long bytes )
8787

8888
// initialize transient values
8989
setSegmentSize(segmentSizeInBytes);
90-
incCapacity(Math.max(10 * 4, bytes));
90+
ensureCapacity(Math.max(10 * 4, bytes));
9191
return this;
9292
}
9393

9494
@Override
95-
public boolean incCapacity( long bytes )
95+
public boolean ensureCapacity(long bytes)
9696
{
9797
if (bytes < 0)
9898
throw new IllegalArgumentException("new capacity has to be strictly positive");

core/src/main/java/com/graphhopper/storage/RAMIntDataAccess.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ public RAMIntDataAccess create( long bytes )
9090

9191
// initialize transient values
9292
setSegmentSize(segmentSizeInBytes);
93-
incCapacity(Math.max(10 * 4, bytes));
93+
ensureCapacity(Math.max(10 * 4, bytes));
9494
return this;
9595
}
9696

9797
@Override
98-
public boolean incCapacity( long bytes )
98+
public boolean ensureCapacity(long bytes)
9999
{
100100
if (bytes < 0)
101101
throw new IllegalArgumentException("new capacity has to be strictly positive");

core/src/main/java/com/graphhopper/storage/Storable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* interface</li>
3131
* <li>if(!storable.loadExisting()) storable.create()</li>
3232
* <li>usage storable and optional flush() calls in-between. Keep in mind that some data structure
33-
* could require a call to increase memory while usage. E.g. DataAccess.incCapacity()</li>
33+
* could require a call to increase memory while usage. E.g. DataAccess.ensureCapacity()</li>
3434
* <li>Finally do close() which does no flush()</li>
3535
* </ol>
3636
* <p/>

core/src/main/java/com/graphhopper/storage/SynchedDAWrapper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ public synchronized DataAccess create( long bytes )
100100
}
101101

102102
@Override
103-
public synchronized boolean incCapacity( long bytes )
103+
public synchronized boolean ensureCapacity(long bytes)
104104
{
105-
return inner.incCapacity(bytes);
105+
return inner.ensureCapacity(bytes);
106106
}
107107

108108
@Override

core/src/main/java/com/graphhopper/storage/TurnCostStorage.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,7 @@ private long nextCostFlags( int node, int edgeFrom, int edgeTo )
204204

205205
private void ensureTurnCostIndex( int nodeIndex )
206206
{
207-
long deltaCap = ((long) nodeIndex + 4) * turnCostsEntryBytes - turnCosts.getCapacity();
208-
if (deltaCap <= 0)
209-
{
210-
return;
211-
}
212-
turnCosts.incCapacity(deltaCap);
207+
turnCosts.ensureCapacity(((long) nodeIndex + 4) * turnCostsEntryBytes);
213208
}
214209

215210
@Override

core/src/main/java/com/graphhopper/storage/UnsafeDataAccess.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
package com.graphhopper.storage;
1919

20-
import static com.graphhopper.storage.AbstractDataAccess.HEADER_OFFSET;
2120
import com.graphhopper.util.NotThreadSafe;
2221
import java.io.File;
2322
import java.io.IOException;
@@ -74,12 +73,12 @@ public UnsafeDataAccess create( long bytes )
7473
// TODO use unsafe.pageSize() instead segmentSizeInBytes?
7574
// e.g. on my system pageSize is only 4096
7675
setSegmentSize(segmentSizeInBytes);
77-
incCapacity(bytes);
76+
ensureCapacity(bytes);
7877
return this;
7978
}
8079

8180
@Override
82-
public final boolean incCapacity( long bytes )
81+
public final boolean ensureCapacity(long bytes)
8382
{
8483
return ensureCapacity(bytes, true);
8584
}

core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ int store( InMemEntry entry, int intIndex )
525525
size += len;
526526
intIndex++;
527527
leafs++;
528-
dataAccess.incCapacity((long) (intIndex + len + 1) * 4);
528+
dataAccess.ensureCapacity((long)(intIndex + len + 1) * 4);
529529
if (len == 1)
530530
{
531531
// less disc space for single entries
@@ -550,7 +550,7 @@ int store( InMemEntry entry, int intIndex )
550550
{
551551
continue;
552552
}
553-
dataAccess.incCapacity((long) (intIndex + 1) * 4);
553+
dataAccess.ensureCapacity((long)(intIndex + 1) * 4);
554554
int beforeIntIndex = intIndex;
555555
intIndex = store(subEntry, beforeIntIndex);
556556
if (intIndex == beforeIntIndex)

core/src/test/java/com/graphhopper/storage/DataAccessTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void testExceptionIfNoEnsureCapacityWasCalled()
9595
{
9696
DataAccess da = createDataAccess(name);
9797
assertFalse(da.loadExisting());
98-
// throw some undefined exception if no incCapacity was called
98+
// throw some undefined exception if no ensureCapacity was called
9999
try
100100
{
101101
da.setInt(2 * 4, 321);
@@ -149,7 +149,7 @@ public void testEnsureCapacity()
149149
da.setInt(31 * 4, 200);
150150

151151
assertEquals(200, da.getInt(31 * 4));
152-
da.incCapacity(2 * 128);
152+
da.ensureCapacity(2 * 128);
153153
assertEquals(200, da.getInt(31 * 4));
154154
// now it shouldn't fail
155155
da.setInt(32 * 4, 220);
@@ -159,7 +159,7 @@ public void testEnsureCapacity()
159159
// ensure some bigger area
160160
da = createDataAccess(name);
161161
da.create(200 * 4);
162-
da.incCapacity(600 * 4);
162+
da.ensureCapacity(600 * 4);
163163
da.close();
164164
}
165165

@@ -198,7 +198,7 @@ public void testSegments()
198198
da.setSegmentSize(128);
199199
da.create(10);
200200
assertEquals(1, da.getSegments());
201-
da.incCapacity(500);
201+
da.ensureCapacity(500);
202202
int olds = da.getSegments();
203203
assertTrue(olds > 3);
204204

core/src/test/java/com/graphhopper/storage/GraphHopperStorageWithTurnCostsTest.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
*/
1818
package com.graphhopper.storage;
1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertTrue;
22-
2320
import java.io.IOException;
24-
25-
import org.junit.Test;
21+
import java.util.Random;
2622

2723
import com.graphhopper.util.EdgeIteratorState;
2824
import com.graphhopper.util.Helper;
25+
import org.junit.Test;
26+
27+
import static org.junit.Assert.assertEquals;
28+
import static org.junit.Assert.assertTrue;
2929

3030
/**
3131
*
@@ -93,4 +93,45 @@ public void testSave_and_fileFormat_withTurnCostEntries() throws IOException
9393
graph.edge(3, 4, 123, true).setWayGeometry(Helper.createPointList(4.4, 5.5, 6.6, 7.7));
9494
checkGraph(graph);
9595
}
96+
97+
@Test
98+
public void testEnsureCapacity() throws IOException {
99+
graph = newGraph(new MMapDirectory(defaultGraphLoc), false);
100+
graph.setSegmentSize(128);
101+
graph.create(100); // 100 is the minimum size
102+
103+
// assert that turnCostStorage can hold 104 turn cost entries at the beginning
104+
assertEquals(104, turnCostStorage.getCapacity() / 16);
105+
106+
Random r = new Random();
107+
108+
NodeAccess na = graph.getNodeAccess();
109+
for (int i = 0; i < 100; i++) {
110+
double randomLat = 90 * r.nextDouble();
111+
double randomLon = 180 * r.nextDouble();
112+
113+
na.setNode(i, randomLat, randomLon);
114+
}
115+
116+
// Make node 50 the 'center' node
117+
for (int nodeId = 51; nodeId < 100; nodeId++) {
118+
graph.edge(50, nodeId, r.nextDouble(), true);
119+
}
120+
for (int nodeId = 0; nodeId < 50; nodeId++) {
121+
graph.edge(nodeId, 50, r.nextDouble(), true);
122+
}
123+
124+
// add 100 turn cost entries around node 50
125+
for (int edgeId = 0; edgeId < 50; edgeId++) {
126+
turnCostStorage.addTurnInfo(50, edgeId, edgeId + 50, 1337);
127+
turnCostStorage.addTurnInfo(50, edgeId + 50, edgeId, 1337);
128+
}
129+
130+
turnCostStorage.addTurnInfo(50, 0, 1, 1337);
131+
assertEquals(104, turnCostStorage.getCapacity() / 16); // we are still good here
132+
133+
turnCostStorage.addTurnInfo(50, 0, 2, 1337);
134+
// A new segment should be added, which will support 128 / 16 = 8 more entries.
135+
assertEquals(112, turnCostStorage.getCapacity() / 16);
136+
}
96137
}

0 commit comments

Comments
 (0)