use of net.osmand.binary.OsmandIndex.FileIndex in project Osmand by osmandapp.
the class CachedOsmandIndexes method getReader.
public BinaryMapIndexReader getReader(File f) throws IOException {
RandomAccessFile mf = new RandomAccessFile(f.getPath(), "r");
FileIndex found = null;
if (storedIndex != null) {
for (int i = 0; i < storedIndex.getFileIndexCount(); i++) {
FileIndex fi = storedIndex.getFileIndex(i);
if (f.length() == fi.getSize() && f.getName().equals(fi.getFileName())) {
// f.lastModified() == fi.getDateModified()
found = fi;
break;
}
}
}
BinaryMapIndexReader reader = null;
if (found == null) {
long val = System.currentTimeMillis();
reader = new BinaryMapIndexReader(mf, f);
addToCache(reader, f);
if (log.isDebugEnabled()) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
log.debug("Initializing db " + f.getAbsolutePath() + " " + (System.currentTimeMillis() - val) + "ms");
}
} else {
reader = initFileIndex(found, mf, f);
}
return reader;
}
use of net.osmand.binary.OsmandIndex.FileIndex in project Osmand by osmandapp.
the class CachedOsmandIndexes method addToCache.
public void addToCache(BinaryMapIndexReader reader, File f) {
hasChanged = true;
if (storedIndexBuilder == null) {
storedIndexBuilder = OsmandIndex.OsmAndStoredIndex.newBuilder();
storedIndexBuilder.setVersion(VERSION);
storedIndexBuilder.setDateCreated(System.currentTimeMillis());
if (storedIndex != null) {
for (FileIndex ex : storedIndex.getFileIndexList()) {
storedIndexBuilder.addFileIndex(ex);
}
}
}
FileIndex.Builder fileIndex = OsmandIndex.FileIndex.newBuilder();
long d = reader.getDateCreated();
fileIndex.setDateModified(d == 0 ? f.lastModified() : d);
fileIndex.setSize(f.length());
fileIndex.setVersion(reader.getVersion());
fileIndex.setFileName(f.getName());
for (MapIndex index : reader.getMapIndexes()) {
MapPart.Builder map = OsmandIndex.MapPart.newBuilder();
map.setSize(index.getLength());
map.setOffset(index.getFilePointer());
if (index.getName() != null) {
map.setName(index.getName());
}
for (MapRoot mr : index.getRoots()) {
MapLevel.Builder lev = OsmandIndex.MapLevel.newBuilder();
lev.setSize(mr.length);
lev.setOffset(mr.filePointer);
lev.setLeft(mr.left);
lev.setRight(mr.right);
lev.setTop(mr.top);
lev.setBottom(mr.bottom);
lev.setMinzoom(mr.minZoom);
lev.setMaxzoom(mr.maxZoom);
map.addLevels(lev);
}
fileIndex.addMapIndex(map);
}
for (AddressRegion index : reader.getAddressIndexes()) {
AddressPart.Builder addr = OsmandIndex.AddressPart.newBuilder();
addr.setSize(index.getLength());
addr.setOffset(index.getFilePointer());
if (index.getName() != null) {
addr.setName(index.getName());
}
if (index.getEnName() != null) {
addr.setNameEn(index.getEnName());
}
addr.setIndexNameOffset(index.getIndexNameOffset());
for (CitiesBlock mr : index.getCities()) {
CityBlock.Builder cblock = OsmandIndex.CityBlock.newBuilder();
cblock.setSize(mr.length);
cblock.setOffset(mr.filePointer);
cblock.setType(mr.type);
addr.addCities(cblock);
}
for (String s : index.getAttributeTagsTable()) {
addr.addAdditionalTags(s);
}
fileIndex.addAddressIndex(addr);
}
for (PoiRegion index : reader.getPoiIndexes()) {
PoiPart.Builder poi = OsmandIndex.PoiPart.newBuilder();
poi.setSize(index.getLength());
poi.setOffset(index.getFilePointer());
if (index.getName() != null) {
poi.setName(index.getName());
}
poi.setLeft(index.left31);
poi.setRight(index.right31);
poi.setTop(index.top31);
poi.setBottom(index.bottom31);
fileIndex.addPoiIndex(poi.build());
}
for (TransportIndex index : reader.getTransportIndexes()) {
TransportPart.Builder transport = OsmandIndex.TransportPart.newBuilder();
transport.setSize(index.getLength());
transport.setOffset(index.getFilePointer());
if (index.getName() != null) {
transport.setName(index.getName());
}
transport.setLeft(index.getLeft());
transport.setRight(index.getRight());
transport.setTop(index.getTop());
transport.setBottom(index.getBottom());
transport.setStopsTableLength(index.stopsFileLength);
transport.setStopsTableOffset(index.stopsFileOffset);
transport.setStringTableLength(index.stringTable.length);
transport.setStringTableOffset(index.stringTable.fileOffset);
fileIndex.addTransportIndex(transport);
}
for (RouteRegion index : reader.getRoutingIndexes()) {
RoutingPart.Builder routing = OsmandIndex.RoutingPart.newBuilder();
routing.setSize(index.getLength());
routing.setOffset(index.getFilePointer());
if (index.getName() != null) {
routing.setName(index.getName());
}
for (RouteSubregion sub : index.getSubregions()) {
addRouteSubregion(routing, sub, false);
}
for (RouteSubregion sub : index.getBaseSubregions()) {
addRouteSubregion(routing, sub, true);
}
fileIndex.addRoutingIndex(routing);
}
storedIndexBuilder.addFileIndex(fileIndex);
}
Aggregations