Improve core sample startup time

This commit is contained in:
crimean 2018-09-29 12:38:41 +03:00
parent 1dc793521c
commit 620d1eeec1
2 changed files with 27 additions and 4 deletions

View file

@ -8,6 +8,7 @@ import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher; import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader; import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion; import net.osmand.binary.BinaryMapRouteReaderAdapter.RouteRegion;
import net.osmand.binary.CachedOsmandIndexes;
import net.osmand.binary.GeocodingUtilities; import net.osmand.binary.GeocodingUtilities;
import net.osmand.binary.GeocodingUtilities.GeocodingResult; import net.osmand.binary.GeocodingUtilities.GeocodingResult;
import net.osmand.binary.RouteDataObject; import net.osmand.binary.RouteDataObject;
@ -48,16 +49,30 @@ public class CurrentPositionHelper {
File appPath = app.getAppPath(null); File appPath = app.getAppPath(null);
SampleUtils.collectFiles(appPath, IndexConstants.BINARY_MAP_INDEX_EXT, files); SampleUtils.collectFiles(appPath, IndexConstants.BINARY_MAP_INDEX_EXT, files);
CachedOsmandIndexes cachedOsmandIndexes = new CachedOsmandIndexes();
File indCache = app.getAppPath("ind_core.cache");
if (indCache.exists()) {
try {
cachedOsmandIndexes.readFromFile(indCache, CachedOsmandIndexes.VERSION);
} catch (Exception e) {
}
}
readers.clear(); readers.clear();
for (File f : files) { for (File f : files) {
try { try {
RandomAccessFile mf = new RandomAccessFile(f.getPath(), "r"); BinaryMapIndexReader reader = cachedOsmandIndexes.getReader(f);
BinaryMapIndexReader reader = new BinaryMapIndexReader(mf, f);
readers.add(reader); readers.add(reader);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
if (files.size() > 0 && (!indCache.exists() || indCache.canWrite())) {
try {
cachedOsmandIndexes.writeToFile(indCache);
} catch (Exception e) {
log.error("Index file could not be written", e);
}
}
} }
public Location getLastAskedLocation() { public Location getLastAskedLocation() {

View file

@ -2,6 +2,7 @@ package net.osmand.core.samples.android.sample1.search;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.binary.BinaryMapIndexReader; import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.CachedOsmandIndexes;
import net.osmand.core.samples.android.sample1.SampleApplication; import net.osmand.core.samples.android.sample1.SampleApplication;
import net.osmand.core.samples.android.sample1.SampleUtils; import net.osmand.core.samples.android.sample1.SampleUtils;
import net.osmand.search.SearchUICore; import net.osmand.search.SearchUICore;
@ -51,11 +52,18 @@ public class QuickSearchHelper {
SampleUtils.collectFiles(appPath, IndexConstants.BINARY_MAP_INDEX_EXT, files); SampleUtils.collectFiles(appPath, IndexConstants.BINARY_MAP_INDEX_EXT, files);
SampleUtils.collectFiles(app.getAppPath(IndexConstants.WIKI_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files); SampleUtils.collectFiles(app.getAppPath(IndexConstants.WIKI_INDEX_DIR), IndexConstants.BINARY_MAP_INDEX_EXT, files);
CachedOsmandIndexes cachedOsmandIndexes = new CachedOsmandIndexes();
File indCache = app.getAppPath("ind_core.cache");
if (indCache.exists()) {
try {
cachedOsmandIndexes.readFromFile(indCache, CachedOsmandIndexes.VERSION);
} catch (Exception e) {
}
}
List<BinaryMapIndexReader> readers = new ArrayList<>(); List<BinaryMapIndexReader> readers = new ArrayList<>();
for (File f : files) { for (File f : files) {
try { try {
RandomAccessFile mf = new RandomAccessFile(f.getPath(), "r"); BinaryMapIndexReader reader = cachedOsmandIndexes.getReader(f);
BinaryMapIndexReader reader = new BinaryMapIndexReader(mf, f);
readers.add(reader); readers.add(reader);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();