Make getReader nullable

This commit is contained in:
max-klaus 2020-02-24 16:51:06 +03:00
parent 4eec8bbbca
commit ea9126ad38
9 changed files with 169 additions and 109 deletions

View file

@ -219,10 +219,13 @@ public class CurrentPositionHelper {
int y31 = MapUtils.get31TileNumberY(lat);
int x31 = MapUtils.get31TileNumberX(lon);
for(BinaryMapReaderResource r : app.getResourceManager().getFileReaders()) {
if(!r.isClosed() && r.getShallowReader().containsRouteData(x31, y31, x31, y31, 15)) {
if(!res.contains(r)) {
res = new ArrayList<>(res);
res.add(r);
if (!r.isClosed()) {
BinaryMapIndexReader shallowReader = r.getShallowReader();
if (shallowReader != null && shallowReader.containsRouteData(x31, y31, x31, y31, 15)) {
if (!res.contains(r)) {
res = new ArrayList<>(res);
res.add(r);
}
}
}
}
@ -241,10 +244,12 @@ public class CurrentPositionHelper {
continue;
}
BinaryMapIndexReader reader = rt.getReader(BinaryMapReaderResourceType.STREET_LOOKUP);
for (RouteRegion rb : reader.getRoutingIndexes()) {
if (r.regionFP == rb.getFilePointer() && r.regionLen == rb.getLength()) {
foundRepo = reader;
break;
if (reader != null) {
for (RouteRegion rb : reader.getRoutingIndexes()) {
if (r.regionFP == rb.getFilePointer() && r.regionLen == rb.getLength()) {
foundRepo = reader;
break;
}
}
}
}

View file

@ -12,6 +12,7 @@ import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Spannable;
import android.text.TextWatcher;
@ -198,7 +199,8 @@ public abstract class SearchByNameAbstractActivity<T> extends OsmandListActivity
protected int getZoomToDisplay(T item){
return 15;
}
@Nullable
protected LatLon getLocation(T item) {
if (item instanceof MapObject) {
return ((MapObject) item).getLocation();

View file

@ -22,6 +22,7 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.resources.RegionAddressRepository;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import java.util.Collection;

View file

@ -10,6 +10,7 @@ import net.osmand.plus.R;
import net.osmand.plus.helpers.FileNameTranslationHelper;
import net.osmand.plus.resources.RegionAddressRepository;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
@ -36,6 +37,7 @@ public class SearchRegionByNameActivity extends SearchByNameAbstractActivity<Reg
super.reset();
}
@Nullable
@Override
protected LatLon getLocation(RegionAddressRepository item) {
return item.getEstimatedRegionCenter();

View file

@ -1,5 +1,7 @@
package net.osmand.plus.resources;
import android.support.annotation.Nullable;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
@ -17,6 +19,7 @@ import org.apache.commons.logging.Log;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -29,6 +32,7 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
this.resource = resource;
}
@Nullable
private BinaryMapIndexReader getOpenFile() {
return resource.getReader(BinaryMapReaderResourceType.POI);
}
@ -41,18 +45,21 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
public boolean checkContains(double latitude, double longitude) {
int x31 = MapUtils.get31TileNumberX(longitude);
int y31 = MapUtils.get31TileNumberY(latitude);
return getOpenFile().containsPoiData(x31, y31, x31, y31);
BinaryMapIndexReader reader = getOpenFile();
return reader != null && reader.containsPoiData(x31, y31, x31, y31);
}
@Override
public boolean checkContainsInt(int top31, int left31, int bottom31, int right31) {
return getOpenFile().containsPoiData(left31, top31, right31, bottom31);
BinaryMapIndexReader reader = getOpenFile();
return reader != null && reader.containsPoiData(left31, top31, right31, bottom31);
}
public synchronized Map<PoiCategory, List<String>> searchAmenityCategoriesByName(String query, Map<PoiCategory, List<String>> map) {
try {
return getOpenFile().searchPoiCategoriesByName(query, map);
BinaryMapIndexReader reader = getOpenFile();
return reader != null ? reader.searchPoiCategoriesByName(query, map) : new HashMap<PoiCategory, List<String>>();
} catch (IOException e) {
log.error("Error searching amenities", e); //$NON-NLS-1$
}
@ -66,20 +73,21 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(x, y, query, l, r, t, b,resulMatcher);
try {
BinaryMapIndexReader index = getOpenFile();
amenities = index.searchPoiByName(req);
if (log.isDebugEnabled()) {
String nm = "";
List<MapIndex> mi = index.getMapIndexes();
if(mi.size() > 0) {
nm = mi.get(0).getName();
if (index != null) {
amenities = index.searchPoiByName(req);
if (log.isDebugEnabled()) {
String nm = "";
List<MapIndex> mi = index.getMapIndexes();
if (mi.size() > 0) {
nm = mi.get(0).getName();
}
log.debug(String.format("Search for %s done in %s ms found %s (%s) %s.", //$NON-NLS-1$
query, System.currentTimeMillis() - now, amenities.size(), nm, index.getFile().getName())); //$NON-NLS-1$
}
log.debug(String.format("Search for %s done in %s ms found %s (%s) %s.", //$NON-NLS-1$
query, System.currentTimeMillis() - now, amenities.size(), nm, index.getFile().getName())); //$NON-NLS-1$
}
} catch (IOException e) {
log.error("Error searching amenities", e); //$NON-NLS-1$
}
return amenities;
}
@ -91,7 +99,10 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
filter, matcher);
List<Amenity> result = null;
try {
result = getOpenFile().searchPoi(req);
BinaryMapIndexReader reader = getOpenFile();
if (reader != null) {
result = reader.searchPoi(req);
}
} catch (IOException e) {
log.error("Error searching amenities", e); //$NON-NLS-1$
}
@ -109,7 +120,10 @@ public class AmenityIndexRepositoryBinary implements AmenityIndexRepository {
SearchRequest<Amenity> req = BinaryMapIndexReader.buildSearchPoiRequest(locations, radius,
filter, matcher );
try {
result = getOpenFile().searchPoi(req);
BinaryMapIndexReader reader = getOpenFile();
if (reader != null) {
result = reader.searchPoi(req);
}
} catch (IOException e) {
log.error("Error searching amenities", e); //$NON-NLS-1$
return result;

View file

@ -1,5 +1,7 @@
package net.osmand.plus.resources;
import android.support.annotation.Nullable;
import java.text.Collator;
import java.util.Comparator;
import java.util.List;
@ -25,7 +27,7 @@ public interface RegionAddressRepository {
public boolean isTransliterateNames();
@Nullable
public LatLon getEstimatedRegionCenter();
// is called on low memory
@ -33,15 +35,13 @@ public interface RegionAddressRepository {
// called to close resources
public void close();
public void preloadCities(ResultMatcher<City> resultMatcher);
public void preloadBuildings(Street street, ResultMatcher<Building> resultMatcher);
public void preloadStreets(City o, ResultMatcher<Street> resultMatcher);
public List<City> getLoadedCities();
// Returns city or postcode (if id < 0)

View file

@ -1,14 +1,7 @@
package net.osmand.plus.resources;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import net.osmand.Collator;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
@ -18,8 +11,6 @@ import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapAddressReaderAdapter;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.BinaryMapIndexReader.SearchRequest;
import net.osmand.binary.GeocodingUtilities;
import net.osmand.binary.GeocodingUtilities.GeocodingResult;
import net.osmand.data.Building;
import net.osmand.data.City;
import net.osmand.data.LatLon;
@ -34,6 +25,14 @@ import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class RegionAddressRepositoryBinary implements RegionAddressRepository {
private static final Log log = PlatformUtil.getLog(RegionAddressRepositoryBinary.class);
@ -67,26 +66,30 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
public synchronized void preloadCities(ResultMatcher<City> resultMatcher) {
if (cities.isEmpty()) {
try {
List<City> cs = getOpenFile().getCities(BinaryMapIndexReader.buildAddressRequest(resultMatcher),
BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE);
LinkedHashMap<Long, City> ncities = new LinkedHashMap<Long, City>();
for (City c : cs) {
ncities.put(c.getId(), c);
LatLon loc = c.getLocation();
if (loc != null) {
int y31 = MapUtils.get31TileNumberY(loc.getLatitude());
int x31 = MapUtils.get31TileNumberX(loc.getLongitude());
int dz = (31 - ZOOM_QTREE);
citiesQtree.insert(c, new QuadRect((x31 >> dz) - 1, (y31 >> dz) - 1, (x31 >> dz) + 1, (y31 >> dz) + 1));
BinaryMapIndexReader reader = getOpenFile();
if (reader != null) {
List<City> cs = reader.getCities(BinaryMapIndexReader.buildAddressRequest(resultMatcher),
BinaryMapAddressReaderAdapter.CITY_TOWN_TYPE);
LinkedHashMap<Long, City> ncities = new LinkedHashMap<Long, City>();
for (City c : cs) {
ncities.put(c.getId(), c);
LatLon loc = c.getLocation();
if (loc != null) {
int y31 = MapUtils.get31TileNumberY(loc.getLatitude());
int x31 = MapUtils.get31TileNumberX(loc.getLongitude());
int dz = (31 - ZOOM_QTREE);
citiesQtree.insert(c, new QuadRect((x31 >> dz) - 1, (y31 >> dz) - 1, (x31 >> dz) + 1, (y31 >> dz) + 1));
}
}
cities = ncities;
}
cities = ncities;
} catch (IOException e) {
log.error("Disk operation failed", e); //$NON-NLS-1$
}
}
}
@Nullable
private BinaryMapIndexReader getOpenFile() {
return resource.getReader(BinaryMapReaderResourceType.ADDRESS);
}
@ -121,8 +124,11 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
public synchronized void preloadBuildings(Street street, ResultMatcher<Building> resultMatcher) {
if (street.getBuildings().isEmpty() && street.getIntersectedStreets().isEmpty()) {
try {
getOpenFile().preloadBuildings(street, BinaryMapIndexReader.buildAddressRequest(resultMatcher));
street.sortBuildings();
BinaryMapIndexReader reader = getOpenFile();
if (reader != null) {
reader.preloadBuildings(street, BinaryMapIndexReader.buildAddressRequest(resultMatcher));
street.sortBuildings();
}
} catch (IOException e) {
log.error("Disk operation failed", e); //$NON-NLS-1$
}
@ -150,7 +156,10 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
return;
}
try {
getOpenFile().preloadStreets(o, BinaryMapIndexReader.buildAddressRequest(resultMatcher));
BinaryMapIndexReader reader = getOpenFile();
if (reader != null) {
reader.preloadStreets(o, BinaryMapIndexReader.buildAddressRequest(resultMatcher));
}
} catch (IOException e) {
log.error("Disk operation failed", e); //$NON-NLS-1$
}
@ -164,7 +173,10 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
SearchRequest<MapObject> req = BinaryMapIndexReader.buildAddressByNameRequest(resultMatcher, name,
StringMatcherMode.CHECK_STARTS_FROM_SPACE);
try {
getOpenFile().searchAddressDataByName(req, typeFilter);
BinaryMapIndexReader reader = getOpenFile();
if (reader != null) {
reader.searchAddressDataByName(req, typeFilter);
}
} catch (IOException e) {
log.error("Disk operation failed", e); //$NON-NLS-1$
}
@ -274,7 +286,8 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
@Override
public String getCountryName() {
return resource.getShallowReader().getCountryName();
BinaryMapIndexReader shallowReader = resource.getShallowReader();
return shallowReader != null ? shallowReader.getCountryName() : "";
}
@Override
@ -282,6 +295,7 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
return resource.getFileName();
}
@NonNull
@Override
public String toString() {
return getName() + " repository";
@ -300,28 +314,31 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
preloadCities(null);
if (!cities.containsKey(id)) {
try {
getOpenFile().getCities(BinaryMapIndexReader.buildAddressRequest(new ResultMatcher<City>() {
boolean canceled = false;
BinaryMapIndexReader reader = getOpenFile();
if (reader != null) {
reader.getCities(BinaryMapIndexReader.buildAddressRequest(new ResultMatcher<City>() {
boolean canceled = false;
@Override
public boolean isCancelled() {
return canceled;
}
@Override
public boolean isCancelled() {
return canceled;
}
@Override
public boolean publish(City object) {
if (id < -1) {
if (object.getName().toUpperCase().equals(cmpName)) {
addCityToPreloadedList(object);
@Override
public boolean publish(City object) {
if (id < -1) {
if (object.getName().toUpperCase().equals(cmpName)) {
addCityToPreloadedList(object);
canceled = true;
}
} else if (object.getId() != null && object.getId().longValue() == id) {
addCityToPreloadedList((City) object);
canceled = true;
}
} else if (object.getId() != null && object.getId().longValue() == id) {
addCityToPreloadedList((City) object);
canceled = true;
return false;
}
return false;
}
}), id < -1 ? BinaryMapAddressReaderAdapter.POSTCODES_TYPE : BinaryMapAddressReaderAdapter.VILLAGES_TYPE);
}), id < -1 ? BinaryMapAddressReaderAdapter.POSTCODES_TYPE : BinaryMapAddressReaderAdapter.VILLAGES_TYPE);
}
} catch (IOException e) {
log.error("Disk operation failed", e); //$NON-NLS-1$
}
@ -354,9 +371,11 @@ public class RegionAddressRepositoryBinary implements RegionAddressRepository {
}
@Nullable
@Override
public LatLon getEstimatedRegionCenter() {
return resource.getShallowReader().getRegionCenter();
BinaryMapIndexReader shallowReader = resource.getShallowReader();
return shallowReader != null ? shallowReader.getRegionCenter() : null;
}
}

View file

@ -130,17 +130,18 @@ public class ResourceManager {
readers.add(null);
}
}
@Nullable
public BinaryMapIndexReader getReader(BinaryMapReaderResourceType type) {
BinaryMapIndexReader r = readers.get(type.ordinal());
if(r == null) {
BinaryMapIndexReader initialReader = this.initialReader;
if (r == null && initialReader != null) {
try {
RandomAccessFile raf = new RandomAccessFile(filename, "r");
r = new BinaryMapIndexReader(raf, initialReader);
readers.set(type.ordinal(), r);
} catch (IOException e) {
log.error("Fail to initialize " + filename.getName(), e);
e.printStackTrace();
}
}
return r;
@ -150,16 +151,16 @@ public class ResourceManager {
return filename.getName();
}
// should not use methods to read from file!
@Nullable
public BinaryMapIndexReader getShallowReader() {
return initialReader;
}
public void close() {
close(initialReader);
for(BinaryMapIndexReader rr : readers) {
if(rr != null) {
for (BinaryMapIndexReader rr : readers) {
if (rr != null) {
close(rr);
}
}
@ -175,7 +176,6 @@ public class ResourceManager {
r.close();
} catch (IOException e) {
log.error("Fail to close " + filename.getName(), e);
e.printStackTrace();
}
}
@ -994,7 +994,9 @@ public class ResourceManager {
List<TransportStop> stops = new ArrayList<>();
r.searchTransportStops(topLat, leftLon, bottomLat, rightLon, -1, stops, matcher);
BinaryMapIndexReader reader = ((TransportIndexRepositoryBinary) r).getOpenFile();
TransportRoutingContext.mergeTransportStops(reader, loadedTransportStops, stops, null, null);
if (reader != null) {
TransportRoutingContext.mergeTransportStops(reader, loadedTransportStops, stops, null, null);
}
}
}
List<TransportStop> stops = new ArrayList<>();
@ -1107,9 +1109,12 @@ public class ResourceManager {
Collection<BinaryMapReaderResource> fileReaders = getFileReaders();
List<BinaryMapIndexReader> readers = new ArrayList<>(fileReaders.size());
for (BinaryMapReaderResource r : fileReaders) {
if (r.getShallowReader().containsPoiData() ||
r.getShallowReader().containsAddressData()) {
readers.add(r.getReader(BinaryMapReaderResourceType.QUICK_SEARCH));
BinaryMapIndexReader shallowReader = r.getShallowReader();
if (shallowReader != null && (shallowReader.containsPoiData() || shallowReader.containsAddressData())) {
BinaryMapIndexReader reader = r.getReader(BinaryMapReaderResourceType.QUICK_SEARCH);
if (reader != null) {
readers.add(reader);
}
}
}
return readers.toArray(new BinaryMapIndexReader[readers.size()]);

View file

@ -1,5 +1,7 @@
package net.osmand.plus.resources;
import android.support.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
@ -26,18 +28,21 @@ public class TransportIndexRepositoryBinary implements TransportIndexRepository
public TransportIndexRepositoryBinary(BinaryMapReaderResource resource) {
this.resource = resource;
}
@Nullable
public BinaryMapIndexReader getOpenFile() {
return resource.getReader(BinaryMapReaderResourceType.TRANSPORT);
}
@Override
public boolean checkContains(double latitude, double longitude) {
return resource.getShallowReader().containTransportData(latitude, longitude);
BinaryMapIndexReader shallowReader = resource.getShallowReader();
return shallowReader != null && shallowReader.containTransportData(latitude, longitude);
}
@Override
public boolean checkContains(double topLatitude, double leftLongitude, double bottomLatitude, double rightLongitude) {
return resource.getShallowReader().containTransportData(topLatitude, leftLongitude, bottomLatitude, rightLongitude);
BinaryMapIndexReader shallowReader = resource.getShallowReader();
return shallowReader != null && shallowReader.containTransportData(topLatitude, leftLongitude, bottomLatitude, rightLongitude);
}
@Override
@ -45,12 +50,15 @@ public class TransportIndexRepositoryBinary implements TransportIndexRepository
int limit, List<TransportStop> stops, ResultMatcher<TransportStop> matcher) {
long now = System.currentTimeMillis();
try {
getOpenFile().searchTransportIndex(BinaryMapIndexReader.buildSearchTransportRequest(MapUtils.get31TileNumberX(leftLongitude),
MapUtils.get31TileNumberX(rightLongitude), MapUtils.get31TileNumberY(topLatitude),
MapUtils.get31TileNumberY(bottomLatitude), limit, stops));
if (log.isDebugEnabled()) {
log.debug(String.format("Search for %s done in %s ms found %s.", //$NON-NLS-1$
topLatitude + " " + leftLongitude, System.currentTimeMillis() - now, stops.size())); //$NON-NLS-1$
BinaryMapIndexReader reader = getOpenFile();
if (reader != null) {
reader.searchTransportIndex(BinaryMapIndexReader.buildSearchTransportRequest(MapUtils.get31TileNumberX(leftLongitude),
MapUtils.get31TileNumberX(rightLongitude), MapUtils.get31TileNumberY(topLatitude),
MapUtils.get31TileNumberY(bottomLatitude), limit, stops));
if (log.isDebugEnabled()) {
log.debug(String.format("Search for %s done in %s ms found %s.", //$NON-NLS-1$
topLatitude + " " + leftLongitude, System.currentTimeMillis() - now, stops.size())); //$NON-NLS-1$
}
}
} catch (IOException e) {
log.error("Disk error ", e); //$NON-NLS-1$
@ -65,22 +73,25 @@ public class TransportIndexRepositoryBinary implements TransportIndexRepository
@Override
public List<TransportRoute> getRoutesForReferences(int[] referencesToRoutes) {
try {
Collection<TransportRoute> res = getOpenFile().getTransportRoutes(referencesToRoutes).valueCollection();
if (res != null) {
List<TransportRoute> lst = new ArrayList<>(res);
Collections.sort(lst, new Comparator<TransportRoute>() {
@Override
public int compare(TransportRoute o1, TransportRoute o2) {
int i1 = Algorithms.extractFirstIntegerNumber(o1.getRef());
int i2 = Algorithms.extractFirstIntegerNumber(o2.getRef());
int r = Algorithms.compare(i1, i2);
if (r == 0) {
r = Algorithms.compare(o1.getName(), o2.getName());
BinaryMapIndexReader reader = getOpenFile();
if (reader != null) {
Collection<TransportRoute> res = reader.getTransportRoutes(referencesToRoutes).valueCollection();
if (res != null) {
List<TransportRoute> lst = new ArrayList<>(res);
Collections.sort(lst, new Comparator<TransportRoute>() {
@Override
public int compare(TransportRoute o1, TransportRoute o2) {
int i1 = Algorithms.extractFirstIntegerNumber(o1.getRef());
int i2 = Algorithms.extractFirstIntegerNumber(o2.getRef());
int r = Algorithms.compare(i1, i2);
if (r == 0) {
r = Algorithms.compare(o1.getName(), o2.getName());
}
return r;
}
return r;
}
});
return lst;
});
return lst;
}
}
} catch (IOException e) {
log.error("Disk error ", e); //$NON-NLS-1$
@ -90,7 +101,8 @@ public class TransportIndexRepositoryBinary implements TransportIndexRepository
@Override
public boolean acceptTransportStop(TransportStop stop) {
return resource.getShallowReader().transportStopBelongsTo(stop);
BinaryMapIndexReader shallowReader = resource.getShallowReader();
return shallowReader != null && shallowReader.transportStopBelongsTo(stop);
}
@Override