/** * Efficient cache or osmnodes * * @author ab */ package btools.mapaccess; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public final class NodesCache { private String segmentDir; private OsmNodesMap nodesMap; private int lookupVersion; private boolean carMode; private String currentFileName; private HashMap fileCache; private byte[] iobuffer; private OsmFile[][] fileRows; private ArrayList segmentList = new ArrayList(); public DistanceChecker distanceChecker; public boolean oom_carsubset_hint = false; private long cacheSum = 0; private boolean garbageCollectionEnabled = false; public NodesCache( String segmentDir, OsmNodesMap nodesMap, int lookupVersion, boolean carMode, NodesCache oldCache ) { this.segmentDir = segmentDir; this.nodesMap = nodesMap; this.lookupVersion = lookupVersion; this.carMode = carMode; if ( oldCache != null ) { fileCache = oldCache.fileCache; iobuffer = oldCache.iobuffer; oom_carsubset_hint = oldCache.oom_carsubset_hint; // re-use old, virgin caches fileRows = oldCache.fileRows; for( OsmFile[] fileRow : fileRows ) { if ( fileRow == null ) continue; for( OsmFile osmf : fileRow ) { cacheSum += osmf.setGhostState(); } } } else { fileCache = new HashMap(4); fileRows = new OsmFile[180][]; iobuffer = new byte[65636]; } } // if the cache sum exceeded a threshold, // clean all ghosts and enable garbage collection private void checkEnableCacheCleaning() { if ( cacheSum < 200000 || garbageCollectionEnabled ) return; for( int i=0; i getAllNodes() { List all = new ArrayList(); for( MicroCache segment : segmentList ) { List positions = segment.getPositions( nodesMap ); all.addAll( positions ); } return all; } public void close() { for( PhysicalFile f: fileCache.values() ) { try { if ( f != null ) f.ra.close(); } catch( IOException ioe ) { // ignore } } } }