From a6828c51adfce3fdea2ba26f3de37e4bf9541bb0 Mon Sep 17 00:00:00 2001 From: PaulStets Date: Wed, 13 Sep 2017 17:23:57 +0300 Subject: [PATCH] Sorting files for search improvements --- .../net/osmand/search/core/SearchPhrase.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/search/core/SearchPhrase.java b/OsmAnd-java/src/net/osmand/search/core/SearchPhrase.java index d788f6033b..f1d0effd08 100644 --- a/OsmAnd-java/src/net/osmand/search/core/SearchPhrase.java +++ b/OsmAnd-java/src/net/osmand/search/core/SearchPhrase.java @@ -13,6 +13,7 @@ import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -535,6 +536,7 @@ public class SearchPhrase { if(indexes == null) { indexes = new ArrayList<>(getOfflineIndexes()); } + Map> diffsByRegion = getDiffsByRegion(); final LatLon ll = getLastTokenLocation(); if(ll != null) { Collections.sort(indexes, new Comparator() { @@ -542,9 +544,6 @@ public class SearchPhrase { @Override public int compare(BinaryMapIndexReader o1, BinaryMapIndexReader o2) { - if (o1.getRegionName().equals(o2.getRegionName())) { - return o1.getFile().getName().compareTo(o2.getFile().getName()); - } LatLon rc1 = o1 == null ? null : getLocation(o1); LatLon rc2 = o2 == null ? null : getLocation(o2); double d1 = rc1 == null ? 10000000d : MapUtils.getDistance(rc1, ll); @@ -566,9 +565,34 @@ public class SearchPhrase { return rc1; } }); + List finalSort = new ArrayList<>(); + for (int i = 0; i < indexes.size(); i++) { + BinaryMapIndexReader currFile = indexes.get(i); + finalSort.addAll(diffsByRegion.get(currFile.getRegionName())); + finalSort.add(currFile); + } + indexes.clear(); + indexes.addAll(finalSort); } } + private Map> getDiffsByRegion() { + Map> result = new HashMap<>(); + for (BinaryMapIndexReader r : indexes) { + if (r.getFile().getName().matches("[a-zA-Z_-]+([0-9]+_*{3}).+[a-z]+")) { + String currRegionName = r.getRegionName(); + if (result.containsKey(currRegionName)) { + result.get(currRegionName).add(r); + } else { + result.put(currRegionName, Arrays.asList(r)); + } + indexes.remove(r); + } + + } + return result; + } + public static class NameStringMatcher implements StringMatcher { private CollatorStringMatcher sm;