From 7319a235c0c778c089c9b6b935d7e27f1ea2ffb8 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sat, 16 May 2020 17:12:34 +0200 Subject: [PATCH] Lazy loading reading --- .../osmand/binary/BinaryMapIndexReader.java | 26 +++++++++++-------- .../BinaryMapTransportReaderAdapter.java | 17 +++++------- .../osmand/router/TransportRoutePlanner.java | 4 +-- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java index 3474affe41..ae64660ea0 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapIndexReader.java @@ -3,6 +3,7 @@ package net.osmand.binary; import com.google.protobuf.CodedInputStream; import com.google.protobuf.CodedOutputStream; +import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.WireFormat; import net.osmand.Collator; @@ -26,6 +27,7 @@ import net.osmand.binary.OsmandOdb.OsmAndMapIndex.MapRootLevel; import net.osmand.data.Amenity; import net.osmand.data.Building; import net.osmand.data.City; +import net.osmand.data.IncompleteTransportRoute; import net.osmand.data.LatLon; import net.osmand.data.MapObject; import net.osmand.data.Street; @@ -54,7 +56,6 @@ import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.io.Reader; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -110,9 +111,7 @@ public class BinaryMapIndexReader { /*private*/ List transportIndexes = new ArrayList(); /*private*/ List routingIndexes = new ArrayList(); /*private*/ List indexes = new ArrayList(); - - private final TLongObjectHashMap incompleteRoutes = - new TLongObjectHashMap(); + TLongObjectHashMap incompleteTransportRoutes = null; protected CodedInputStream codedIS; @@ -227,7 +226,7 @@ public class BinaryMapIndexReader { ind.filePointer = codedIS.getTotalBytesRead(); if (transportAdapter != null) { oldLimit = codedIS.pushLimit(ind.length); - transportAdapter.readTransportIndex(ind, incompleteRoutes); + transportAdapter.readTransportIndex(ind); codedIS.popLimit(oldLimit); transportIndexes.add(ind); indexes.add(ind); @@ -2635,12 +2634,17 @@ public class BinaryMapIndexReader { } } - public net.osmand.data.IncompleteTransportRoute getIncompleteRoutePointers(long id) { - return incompleteRoutes.get(id); - } - - public Collection getIncompleteRoutes() { - return incompleteRoutes.valueCollection(); + public TLongObjectHashMap getIncompleteTransportRoutes() throws InvalidProtocolBufferException, IOException { + if (incompleteTransportRoutes == null) { + incompleteTransportRoutes = new TLongObjectHashMap<>(); + for (TransportIndex ti : transportIndexes) { + if (ti.incompleteRoutesLength > 0) { + transportAdapter.readIncompleteRoutesList(incompleteTransportRoutes, ti.incompleteRoutesLength, + ti.incompleteRoutesOffset); + } + } + } + return incompleteTransportRoutes; } diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java index 13d9732f7b..049fc161eb 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapTransportReaderAdapter.java @@ -83,7 +83,7 @@ public class BinaryMapTransportReaderAdapter { } - protected void readTransportIndex(TransportIndex ind, TLongObjectHashMap incompleteRoutes) throws IOException { + protected void readTransportIndex(TransportIndex ind) throws IOException { while(true){ int t = codedIS.readTag(); int tag = WireFormat.getTagFieldNumber(t); @@ -113,13 +113,9 @@ public class BinaryMapTransportReaderAdapter { codedIS.seek(st.length + st.fileOffset); break; case OsmandOdb.OsmAndTransportIndex.INCOMPLETEROUTES_FIELD_NUMBER : - TIntObjectHashMap stab = new TIntObjectHashMap(); ind.incompleteRoutesLength = codedIS.readRawVarint32(); ind.incompleteRoutesOffset = codedIS.getTotalBytesRead(); - int oldl = codedIS.pushLimit(ind.incompleteRoutesLength); - //may be we should start caching stringTable in advance? - readIncompleteRoutesList(incompleteRoutes, ind.incompleteRoutesLength, ind.incompleteRoutesOffset, stab); - codedIS.popLimit(oldl); + codedIS.seek(ind.incompleteRoutesLength + ind.incompleteRoutesOffset); break; default: @@ -128,6 +124,7 @@ public class BinaryMapTransportReaderAdapter { } } } + private void readTransportBounds(TransportIndex ind) throws IOException { while(true){ @@ -254,8 +251,8 @@ public class BinaryMapTransportReaderAdapter { return ((char) i)+""; } - private void readIncompleteRoutesList(TLongObjectHashMap incompleteRoutes, - int length, int offset, TIntObjectHashMap stringTable) throws IOException { + public void readIncompleteRoutesList(TLongObjectHashMap incompleteRoutes, + int length, int offset) throws IOException { codedIS.seek(offset); boolean end = false; while (!end) { @@ -268,7 +265,7 @@ public class BinaryMapTransportReaderAdapter { case OsmandOdb.IncompleteTransportRoutes.ROUTES_FIELD_NUMBER: int l = codedIS.readRawVarint32(); int olds = codedIS.pushLimit(l); - net.osmand.data.IncompleteTransportRoute ir = readIncompleteRoute(stringTable); + net.osmand.data.IncompleteTransportRoute ir = readIncompleteRoute(); net.osmand.data.IncompleteTransportRoute itr = incompleteRoutes.get(ir.getRouteId()); if(itr != null) { itr.setNextLinkedRoute(ir); @@ -286,7 +283,7 @@ public class BinaryMapTransportReaderAdapter { } - public net.osmand.data.IncompleteTransportRoute readIncompleteRoute(TIntObjectHashMap stringTable) throws IOException { + public net.osmand.data.IncompleteTransportRoute readIncompleteRoute() throws IOException { net.osmand.data.IncompleteTransportRoute dataObject = new net.osmand.data.IncompleteTransportRoute(); boolean end = false; while(!end){ diff --git a/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java b/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java index 1548bba2f3..9ca8d2a609 100644 --- a/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java +++ b/OsmAnd-java/src/main/java/net/osmand/router/TransportRoutePlanner.java @@ -1126,9 +1126,9 @@ public class TransportRoutePlanner { private List findIncompleteRouteParts(TransportRoute baseRoute) throws IOException { List allRoutes = null; - // TODO completely irrelevant always reiteration over all maps + // TODO completely irrelevant always reiteration over all maps (especially not in bbox of the route probabl) for (BinaryMapIndexReader bmir : routeMap.keySet()) { - IncompleteTransportRoute ptr = bmir.getIncompleteRoutePointers(baseRoute.getId()); + IncompleteTransportRoute ptr = bmir.getIncompleteTransportRoutes().get(baseRoute.getId()); if (ptr != null) { TIntArrayList lst = new TIntArrayList(); while(ptr != null) {