Small refactoring

This commit is contained in:
Victor Shcherb 2020-05-18 15:20:30 +02:00
parent 3a800d2520
commit c906fcab5b
3 changed files with 49 additions and 39 deletions

View file

@ -70,11 +70,4 @@ public class PoiFilter extends AbstractPoiType {
return poiTypes; return poiTypes;
} }
public List<String> getPoiTypesKeys() {
List<String> filters = new ArrayList<>();
for (PoiType p : poiTypes) {
filters.add(p.keyName);
}
return filters;
}
} }

View file

@ -26,17 +26,15 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
public class AmenityIndexRepositoryBinary implements AmenityIndexRepository { public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
private final static Log log = PlatformUtil.getLog(AmenityIndexRepositoryBinary.class); private final static Log log = PlatformUtil.getLog(AmenityIndexRepositoryBinary.class);
private BinaryMapReaderResource resource; private BinaryMapReaderResource resource;
private MapPoiTypes poiTypes; private MapPoiTypes poiTypes;
private Map<String, List<String>> poiCategories = new HashMap<>(); private Map<String, List<String>> deltaPoiCategories = new HashMap<>();
public AmenityIndexRepositoryBinary(BinaryMapReaderResource resource, OsmandApplication app) { public AmenityIndexRepositoryBinary(BinaryMapReaderResource resource, OsmandApplication app) {
this.resource = resource; this.resource = resource;
@ -44,8 +42,8 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
checkCachedCategories(app.getPoiFilters()); checkCachedCategories(app.getPoiFilters());
} }
public Map<String, List<String>> getPoiCategories() { public Map<String, List<String>> getDeltaPoiCategories() {
return poiCategories; return deltaPoiCategories;
} }
private void checkCachedCategories(PoiFiltersHelper poiFiltersHelper) { private void checkCachedCategories(PoiFiltersHelper poiFiltersHelper) {
@ -53,35 +51,54 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
long lastModified = resource.getFileLastModified(); long lastModified = resource.getFileLastModified();
Pair<Long, Map<String, List<String>>> cache = poiFiltersHelper.getCacheByResourceName(fileName); Pair<Long, Map<String, List<String>>> cache = poiFiltersHelper.getCacheByResourceName(fileName);
if (cache == null || cache.first != null && cache.first != lastModified) { if (cache == null || cache.first != null && cache.first != lastModified) {
deltaPoiCategories = new HashMap<>();
try { try {
BinaryMapIndexReader reader = getOpenFile(); BinaryMapIndexReader reader = getOpenFile();
if (reader != null) { if (reader != null) {
reader.initCategories(); reader.initCategories();
List<BinaryMapPoiReaderAdapter.PoiRegion> regions = reader.getPoiIndexes(); List<BinaryMapPoiReaderAdapter.PoiRegion> regions = reader.getPoiIndexes();
for (BinaryMapPoiReaderAdapter.PoiRegion region : regions) { for (BinaryMapPoiReaderAdapter.PoiRegion region : regions) {
List<String> categories = region.getCategories(); calculateDeltaSubcategories(region);
List<List<String>> subCategories = region.getSubcategories();
for (int i = 0; i < categories.size(); i++) {
PoiCategory poiCategory = poiTypes.getPoiCategoryByName(categories.get(i));
Set<String> filters = new HashSet<>(subCategories.get(i));
List<String> keys = poiCategory.getPoiTypesKeys();
filters.removeAll(keys);
if (!filters.isEmpty()) {
poiCategories.put(categories.get(i), new ArrayList<>(filters));
}
}
} }
if (cache == null) { if (cache == null) {
poiFiltersHelper.insertCacheForResource(fileName, lastModified, poiCategories); poiFiltersHelper.insertCacheForResource(fileName, lastModified, deltaPoiCategories);
} else { } else {
poiFiltersHelper.updateCacheForResource(fileName, lastModified, poiCategories); poiFiltersHelper.updateCacheForResource(fileName, lastModified, deltaPoiCategories);
} }
} }
} catch (IOException e) { } catch (IOException e) {
log.error("Error initializing categories ", e); log.error("Error initializing categories ", e);
} }
} else if (cache.second != null) { } else if (cache.second != null) {
poiCategories = cache.second; deltaPoiCategories = cache.second;
}
}
private void calculateDeltaSubcategories(BinaryMapPoiReaderAdapter.PoiRegion region) {
List<String> categories = region.getCategories();
List<List<String>> subCategories = region.getSubcategories();
for (int i = 0; i < categories.size(); i++) {
String categoryName = categories.get(i);
PoiCategory poiCategory = poiTypes.getPoiCategoryByName(categoryName);
List<String> deltaSubCategories = null;
for (List<String> subList : subCategories) {
for (String subCategory : subList) {
if (poiCategory.getPoiTypeByKeyName(subCategory) != null) {
if (deltaSubCategories == null) {
deltaSubCategories = new ArrayList<>();
}
deltaSubCategories.add(subCategory);
}
}
}
if (deltaSubCategories != null) {
if(deltaPoiCategories.containsKey(categoryName)) {
deltaPoiCategories.get(categoryName).addAll(deltaSubCategories);
} else {
deltaPoiCategories.put(categoryName, deltaSubCategories);
}
}
} }
} }
@ -123,7 +140,7 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
public synchronized List<Amenity> searchAmenitiesByName(int x, int y, int l, int t, int r, int b, String query, ResultMatcher<Amenity> resulMatcher) { public synchronized List<Amenity> searchAmenitiesByName(int x, int y, int l, int t, int r, int b, String query, ResultMatcher<Amenity> resulMatcher) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
List<Amenity> amenities = Collections.emptyList(); List<Amenity> amenities = Collections.emptyList();
SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(x, y, query, l, r, t, b,resulMatcher); SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(x, y, query, l, r, t, b, resulMatcher);
try { try {
BinaryMapIndexReader index = getOpenFile(); BinaryMapIndexReader index = getOpenFile();
if (index != null) { if (index != null) {
@ -146,7 +163,7 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
@Override @Override
public synchronized List<Amenity> searchAmenities(int stop, int sleft, int sbottom, int sright, int zoom, public synchronized List<Amenity> searchAmenities(int stop, int sleft, int sbottom, int sright, int zoom,
final SearchPoiTypeFilter filter, ResultMatcher<Amenity> matcher) { final SearchPoiTypeFilter filter, ResultMatcher<Amenity> matcher) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(sleft, sright, stop, sbottom, zoom, SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(sleft, sright, stop, sbottom, zoom,
filter, matcher); filter, matcher);
@ -171,7 +188,7 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
List<Amenity> result = null; List<Amenity> result = null;
SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(locations, radius, SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(locations, radius,
filter, matcher ); filter, matcher);
try { try {
BinaryMapIndexReader reader = getOpenFile(); BinaryMapIndexReader reader = getOpenFile();
if (reader != null) { if (reader != null) {
@ -182,7 +199,7 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
return result; return result;
} }
if (log.isDebugEnabled() && result != null) { if (log.isDebugEnabled() && result != null) {
log.debug(String.format("Search done in %s ms found %s.", (System.currentTimeMillis() - now), result.size())); //$NON-NLS-1$ log.debug(String.format("Search done in %s ms found %s.", (System.currentTimeMillis() - now), result.size())); //$NON-NLS-1$
} }
return result; return result;

View file

@ -752,7 +752,7 @@ public class ResourceManager {
} }
} }
for (AmenityIndexRepository repo : amenityRepositories.values()) { for (AmenityIndexRepository repo : amenityRepositories.values()) {
Map<String, List<String>> categories = ((AmenityIndexRepositoryBinary) repo).getPoiCategories(); Map<String, List<String>> categories = ((AmenityIndexRepositoryBinary) repo).getDeltaPoiCategories();
if (!categories.isEmpty()) { if (!categories.isEmpty()) {
for (Map.Entry<String, List<String>> entry : categories.entrySet()) { for (Map.Entry<String, List<String>> entry : categories.entrySet()) {
PoiCategory poiCategory = context.getPoiTypes().getPoiCategoryByName(entry.getKey(), true); PoiCategory poiCategory = context.getPoiTypes().getPoiCategoryByName(entry.getKey(), true);