From cfecc3784e0b5c948e7cd6db9d1059402658d1ca Mon Sep 17 00:00:00 2001 From: Roman Inflianskas Date: Thu, 2 Jun 2016 11:53:42 +0300 Subject: [PATCH] Fix #2097 --- .../osmand/binary/BinaryMapIndexReader.java | 8 ++- .../RegionAddressRepositoryBinary.java | 59 ++++++++++--------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/binary/BinaryMapIndexReader.java b/OsmAnd-java/src/net/osmand/binary/BinaryMapIndexReader.java index 45278020f1..6803d16497 100644 --- a/OsmAnd-java/src/net/osmand/binary/BinaryMapIndexReader.java +++ b/OsmAnd-java/src/net/osmand/binary/BinaryMapIndexReader.java @@ -1278,7 +1278,7 @@ public class BinaryMapIndexReader { return dataObject; } - public List searchAddressDataByName(SearchRequest req) throws IOException { + public List searchAddressDataByName(SearchRequest req, int[] typeFilter) throws IOException { if (req.nameQuery == null || req.nameQuery.length() == 0) { throw new IllegalArgumentException(); } @@ -1287,13 +1287,17 @@ public class BinaryMapIndexReader { codedIS.seek(reg.indexNameOffset); int len = readInt(); int old = codedIS.pushLimit(len); - addressAdapter.searchAddressDataByName(reg, req, null); + addressAdapter.searchAddressDataByName(reg, req, typeFilter); codedIS.popLimit(old); } } return req.getSearchResults(); } + public List searchAddressDataByName(SearchRequest req) throws IOException { + return searchAddressDataByName(req, null); + } + public void initCategories(PoiRegion poiIndex) throws IOException { poiAdapter.initCategories(poiIndex); } diff --git a/OsmAnd/src/net/osmand/plus/resources/RegionAddressRepositoryBinary.java b/OsmAnd/src/net/osmand/plus/resources/RegionAddressRepositoryBinary.java index 91a9b2ae64..267ef6a639 100644 --- a/OsmAnd/src/net/osmand/plus/resources/RegionAddressRepositoryBinary.java +++ b/OsmAnd/src/net/osmand/plus/resources/RegionAddressRepositoryBinary.java @@ -168,45 +168,52 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository { } } -// // not use ccontains It is really slow, takes about 10 times more than other steps +// // not use contains It is really slow, takes about 10 times more than other steps // private StringMatcherMode[] streetsCheckMode = new StringMatcherMode[] {StringMatcherMode.CHECK_ONLY_STARTS_WITH, // StringMatcherMode.CHECK_STARTS_FROM_SPACE_NOT_BEGINNING}; - @Override - public synchronized List searchMapObjectsByName(String name, ResultMatcher resultMatcher) { + public synchronized List searchMapObjectsByName(String name, ResultMatcher resultMatcher, int[] typeFilter) { SearchRequest req = BinaryMapIndexReader.buildAddressByNameRequest(resultMatcher, name); try { log.debug("file=" + file + "; req=" + req); - file.searchAddressDataByName(req); + file.searchAddressDataByName(req, typeFilter); } catch (IOException e) { log.error("Disk operation failed", e); //$NON-NLS-1$ } return req.getSearchResults(); } - private List fillWithVillages(String name, String lang, final ResultMatcher resultMatcher) throws IOException { + @Override + public synchronized List searchMapObjectsByName(String name, ResultMatcher resultMatcher) { + return searchMapObjectsByName(name, resultMatcher, null); + } + + private List fillWithCities(String name, final ResultMatcher resultMatcher, final int type) throws IOException { List result = new ArrayList(); - List foundCities = file.getCities(BinaryMapIndexReader.buildAddressRequest(new ResultMatcher() { - List cache = new ArrayList(); + ResultMatcher matcher = new ResultMatcher() { + List cache = new ArrayList(); - @Override - public boolean publish(City c) { - if (c.getLocation() != null) { - City ct = getClosestCity(c.getLocation(), cache); - c.setClosestCity(ct); - } - return resultMatcher.publish(c); + @Override + public boolean publish(MapObject o) { + City c = (City) o; + if (type == BinaryMapAddressReaderAdapter.VILLAGES_TYPE) { + if (c.getLocation() != null) { + City ct = getClosestCity(c.getLocation(), cache); + c.setClosestCity(ct); } + } + return resultMatcher.publish(c); + } - @Override - public boolean isCancelled() { - return resultMatcher.isCancelled(); - } - }), new CollatorStringMatcher(name, StringMatcherMode.CHECK_STARTS_FROM_SPACE), lang, - BinaryMapAddressReaderAdapter.VILLAGES_TYPE); + @Override + public boolean isCancelled() { + return resultMatcher.isCancelled(); + } + }; + List foundCities = searchMapObjectsByName(name, matcher, new int[]{type}); - for (City c : foundCities) { - result.add(c); + for (MapObject o : foundCities) { + result.add((City) o); if (resultMatcher.isCancelled()) { return result; } @@ -234,7 +241,7 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository { resultMatcher.publish(c); } try { - citiesToFill.addAll(fillWithVillages(name, lang, resultMatcher)); + citiesToFill.addAll(fillWithCities(name, resultMatcher, BinaryMapAddressReaderAdapter.VILLAGES_TYPE)); } catch (IOException e) { log.error("Disk operation failed", e); //$NON-NLS-1$ } @@ -248,9 +255,7 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository { if (/*name.length() >= 2 && Algorithms.containsDigit(name) && */searchVillages) { // also try to identify postcodes String uName = name.toUpperCase(); - List foundCities = file.getCities(BinaryMapIndexReader.buildAddressRequest(resultMatcher), - new CollatorStringMatcher(uName, StringMatcherMode.CHECK_CONTAINS), lang, - BinaryMapAddressReaderAdapter.POSTCODES_TYPE); + List foundCities = fillWithCities(uName, resultMatcher, BinaryMapAddressReaderAdapter.POSTCODES_TYPE); for (City code : foundCities) { citiesToFill.add(code); if (resultMatcher.isCancelled()) { @@ -274,7 +279,7 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository { int initialSize = citiesToFill.size(); if (/*name.length() >= 3 && */searchVillages) { - citiesToFill.addAll(fillWithVillages(name, lang, resultMatcher)); + citiesToFill.addAll(fillWithCities(name, resultMatcher, BinaryMapAddressReaderAdapter.VILLAGES_TYPE)); } log.debug("Loaded citites " + (citiesToFill.size() - initialSize)); //$NON-NLS-1$ } catch (IOException e) {