Merge pull request #1071 from ilasica/sqlitedbLoad

Saving tiles into sqlitedb
This commit is contained in:
vshcherb 2015-02-14 01:47:40 +01:00
commit 60e0c1953b
4 changed files with 73 additions and 29 deletions

View file

@ -4,6 +4,8 @@ import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
@ -107,6 +109,19 @@ public class MapTileDownloader {
public void setError(boolean error){
this.error = error;
}
public void saveTile(InputStream inputStream) throws IOException {
fileToSave.getParentFile().mkdirs();
OutputStream stream = null;
try {
stream = new FileOutputStream(fileToSave);
Algorithms.streamCopy(inputStream, stream);
stream.flush();
} finally {
Algorithms.closeStream(inputStream);
Algorithms.closeStream(stream);
}
}
}
@ -218,23 +233,15 @@ public class MapTileDownloader {
log.debug("Start downloading tile : " + request.url); //$NON-NLS-1$
}
long time = System.currentTimeMillis();
request.setError(false);
try {
request.fileToSave.getParentFile().mkdirs();
URL url = new URL(request.url);
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.setReadTimeout(CONNECTION_TIMEOUT);
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
FileOutputStream stream = null;
try {
stream = new FileOutputStream(request.fileToSave);
Algorithms.streamCopy(inputStream, stream);
stream.flush();
} finally {
Algorithms.closeStream(inputStream);
Algorithms.closeStream(stream);
}
request.saveTile(inputStream);
if (log.isDebugEnabled()) {
log.debug("Downloading tile : " + request.url + " successfull " + (System.currentTimeMillis() - time) + " ms"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}

View file

@ -394,18 +394,7 @@ public class SQLiteTileSource implements ITileSource {
private static final int BUF_SIZE = 1024;
/**
* Makes method synchronized to give a little more time for get methods and
* let all writing attempts to wait outside of this method
*/
public synchronized void insertImage(int x, int y, int zoom, File fileToSave) throws IOException {
SQLiteConnection db = getDatabase();
if (db == null || db.isReadOnly() || onlyReadonlyAvailable) {
return;
}
if (exists(x, y, zoom)) {
return;
}
public void insertImage(int x, int y, int zoom, File fileToSave) throws IOException {
ByteBuffer buf = ByteBuffer.allocate((int) fileToSave.length());
FileInputStream is = new FileInputStream(fileToSave);
int i = 0;
@ -414,21 +403,36 @@ public class SQLiteTileSource implements ITileSource {
buf.put(b, 0, i);
}
insertImage(x, y, zoom, buf.array());
is.close();
}
/**
* Makes method synchronized to give a little more time for get methods and
* let all writing attempts to wait outside of this method
*/
public /*synchronized*/ void insertImage(int x, int y, int zoom, byte[] dataToSave) throws IOException {
SQLiteConnection db = getDatabase();
if (db == null || db.isReadOnly() || onlyReadonlyAvailable) {
return;
}
/*There is no sense to downoad and do not save. If needed, check should perform before downlad
if (exists(x, y, zoom)) {
return;
}*/
String query = timeSupported ? "INSERT INTO tiles(x,y,z,s,image,time) VALUES(?, ?, ?, ?, ?, ?)"
: "INSERT INTO tiles(x,y,z,s,image) VALUES(?, ?, ?, ?, ?)";
String query = timeSupported ? "INSERT OR REPLACE INTO tiles(x,y,z,s,image,time) VALUES(?, ?, ?, ?, ?, ?)"
: "INSERT OR REPLACE INTO tiles(x,y,z,s,image) VALUES(?, ?, ?, ?, ?)";
net.osmand.plus.api.SQLiteAPI.SQLiteStatement statement = db.compileStatement(query); //$NON-NLS-1$
statement.bindLong(1, x);
statement.bindLong(2, y);
statement.bindLong(3, getFileZoom(zoom));
statement.bindLong(4, 0);
statement.bindBlob(5, buf.array());
statement.bindBlob(5, dataToSave);
if (timeSupported) {
statement.bindLong(6, System.currentTimeMillis());
}
statement.execute();
statement.close();
is.close();
}

View file

@ -1,7 +1,14 @@
package net.osmand.plus.resources;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
@ -14,6 +21,8 @@ import net.osmand.map.ITileSource;
import net.osmand.map.MapTileDownloader.DownloadRequest;
import net.osmand.map.MapTileDownloader.IMapDownloaderCallback;
import net.osmand.plus.BusyIndicator;
import net.osmand.plus.SQLiteTileSource;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
@ -166,6 +175,30 @@ public class AsyncLoadingThread extends Thread {
this.tileSource = source;
this.tileId = tileId;
}
public void saveTile(InputStream inputStream) throws IOException {
if(tileSource instanceof SQLiteTileSource){
ByteArrayOutputStream stream = null;
try {
stream = new ByteArrayOutputStream(inputStream.available());
Algorithms.streamCopy(inputStream, stream);
stream.flush();
try {
((SQLiteTileSource) tileSource).insertImage(xTile, yTile, zoom, stream.toByteArray());
} catch (IOException e) {
log.warn("Tile x="+xTile +" y="+ yTile+" z="+ zoom+" couldn't be read", e); //$NON-NLS-1$//$NON-NLS-2$
}
} finally {
Algorithms.closeStream(inputStream);
Algorithms.closeStream(stream);
}
}
else {
super.saveTile(inputStream);
}
}
}
protected class MapObjectLoadRequest<T> implements ResultMatcher<T> {

View file

@ -210,7 +210,7 @@ public class ResourceManager {
if(request instanceof TileLoadDownloadRequest){
TileLoadDownloadRequest req = ((TileLoadDownloadRequest) request);
imagesOnFS.put(req.tileId, Boolean.TRUE);
if(req.fileToSave != null && req.tileSource instanceof SQLiteTileSource){
/* if(req.fileToSave != null && req.tileSource instanceof SQLiteTileSource){
try {
((SQLiteTileSource) req.tileSource).insertImage(req.xTile, req.yTile, req.zoom, req.fileToSave);
} catch (IOException e) {
@ -225,7 +225,7 @@ public class ResourceManager {
req.fileToSave.getParentFile().getParentFile().delete();
}
}
}
}*/
}
}
@ -437,7 +437,7 @@ public class ResourceManager {
Algorithms.streamCopy(OsmandRegions.class.getResourceAsStream("regions.ocbf"),
new FileOutputStream(file));
}
}
}
regions.prepareFile(file.getAbsolutePath());
} catch (IOException e) {
log.error(e.getMessage(), e);