add sqlite tile cache
This commit is contained in:
@@ -26,6 +26,12 @@ public abstract class TileSource {
|
||||
|
||||
protected final Options options = new Options();
|
||||
|
||||
public ITileCache tileCache;
|
||||
|
||||
public void setCache(ITileCache cache) {
|
||||
tileCache = cache;
|
||||
}
|
||||
|
||||
public TileSource setOption(String key, String value) {
|
||||
options.put(key, value);
|
||||
return this;
|
||||
|
||||
@@ -104,22 +104,40 @@ public class LwHttp {
|
||||
}
|
||||
|
||||
static class Buffer extends BufferedInputStream {
|
||||
public Buffer(InputStream is) {
|
||||
final OutputStream mCache;
|
||||
|
||||
public Buffer(InputStream is, OutputStream cache) {
|
||||
super(is, 4096);
|
||||
mCache = cache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int read() throws IOException {
|
||||
return super.read();
|
||||
int data = super.read();
|
||||
if (data >= 0)
|
||||
mCache.write(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized int read(byte[] buffer, int offset, int byteCount)
|
||||
throws IOException {
|
||||
return super.read(buffer, offset, byteCount);
|
||||
int len = super.read(buffer, offset, byteCount);
|
||||
|
||||
if (len >= 0)
|
||||
mCache.write(buffer, offset, len);
|
||||
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
||||
OutputStream mCacheOutputStream;
|
||||
|
||||
public void setOutputStream(OutputStream outputStream) {
|
||||
mCacheOutputStream = outputStream;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (mSocket != null) {
|
||||
try {
|
||||
@@ -204,6 +222,10 @@ public class LwHttp {
|
||||
is.mark(0);
|
||||
is.skip(end);
|
||||
|
||||
if (mCacheOutputStream != null) {
|
||||
is = new Buffer(is, mCacheOutputStream);
|
||||
}
|
||||
|
||||
if (mInflateContent)
|
||||
return new InflaterInputStream(is);
|
||||
|
||||
|
||||
@@ -14,14 +14,17 @@
|
||||
*/
|
||||
package org.oscim.tiling.source.common;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.SocketException;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.oscim.tiling.MapTile;
|
||||
import org.oscim.tiling.source.ITileCache;
|
||||
import org.oscim.tiling.source.ITileDataSink;
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.utils.IOUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -34,48 +37,76 @@ public abstract class PbfTileDataSource implements ITileDataSource {
|
||||
|
||||
protected LwHttp mConn;
|
||||
protected final PbfDecoder mTileDecoder;
|
||||
protected final ITileCache mTileCache;
|
||||
|
||||
public PbfTileDataSource(PbfDecoder tileDecoder) {
|
||||
public PbfTileDataSource(PbfDecoder tileDecoder, ITileCache tileCache) {
|
||||
mTileDecoder = tileDecoder;
|
||||
mTileCache = tileCache;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryResult executeQuery(MapTile tile, ITileDataSink sink) {
|
||||
QueryResult result = QueryResult.SUCCESS;
|
||||
boolean success = true;
|
||||
|
||||
ITileCache.TileWriter cacheWriter = null;
|
||||
|
||||
if (mTileCache != null) {
|
||||
ITileCache.TileReader c = mTileCache.getTile(tile);
|
||||
if (c == null) {
|
||||
// create new cache entry
|
||||
cacheWriter = mTileCache.writeTile(tile);
|
||||
mConn.setOutputStream(cacheWriter.getOutputStream());
|
||||
} else {
|
||||
try {
|
||||
InputStream is = c.getInputStream();
|
||||
if (mTileDecoder.decode(tile, sink, is, c.getBytes())) {
|
||||
IOUtils.closeQuietly(is);
|
||||
return QueryResult.SUCCESS;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.debug(tile + " Cache read failed");
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
InputStream is;
|
||||
if (!mConn.sendRequest(tile)) {
|
||||
log.debug(tile + " Request Failed");
|
||||
result = QueryResult.FAILED;
|
||||
log.debug(tile + " Request failed");
|
||||
success = false;
|
||||
} else if ((is = mConn.readHeader()) != null) {
|
||||
boolean win = mTileDecoder.decode(tile, sink, is, mConn.getContentLength());
|
||||
if (!win)
|
||||
log.debug(tile + " failed");
|
||||
int bytes = mConn.getContentLength();
|
||||
success = mTileDecoder.decode(tile, sink, is, bytes);
|
||||
if (!success)
|
||||
log.debug(tile + " Decoding failed");
|
||||
} else {
|
||||
log.debug(tile + " Network Error");
|
||||
result = QueryResult.FAILED;
|
||||
success = false;
|
||||
}
|
||||
} catch (SocketException e) {
|
||||
log.debug(tile + " Socket exception: " + e.getMessage());
|
||||
result = QueryResult.FAILED;
|
||||
success = false;
|
||||
} catch (SocketTimeoutException e) {
|
||||
log.debug(tile + " Socket Timeout");
|
||||
result = QueryResult.FAILED;
|
||||
success = false;
|
||||
} catch (UnknownHostException e) {
|
||||
log.debug(tile + " No Network");
|
||||
result = QueryResult.FAILED;
|
||||
success = false;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
result = QueryResult.FAILED;
|
||||
success = false;
|
||||
}
|
||||
|
||||
mConn.requestCompleted();
|
||||
|
||||
if (result != QueryResult.SUCCESS)
|
||||
if (cacheWriter != null)
|
||||
cacheWriter.complete(success);
|
||||
|
||||
if (success)
|
||||
mConn.close();
|
||||
|
||||
return result;
|
||||
return success ? QueryResult.SUCCESS : QueryResult.FAILED;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,6 +18,7 @@ import java.net.URL;
|
||||
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.tiling.source.common.LwHttp;
|
||||
import org.oscim.tiling.source.common.PbfTileDataSource;
|
||||
import org.oscim.tiling.source.common.UrlTileSource;
|
||||
@@ -26,13 +27,13 @@ public class MapnikVectorTileSource extends UrlTileSource {
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
return new TileDataSource(mUrl);
|
||||
return new TileDataSource(this, mUrl);
|
||||
}
|
||||
|
||||
static class TileDataSource extends PbfTileDataSource {
|
||||
|
||||
public TileDataSource(URL url) {
|
||||
super(new TileDecoder());
|
||||
public TileDataSource(TileSource tileSource, URL url) {
|
||||
super(new TileDecoder(), tileSource.tileCache);
|
||||
|
||||
mConn = new LwHttp(url, "image/png", "vector.pbf", true) {
|
||||
@Override
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.oscim.tiling.source.oscimap;
|
||||
import java.net.URL;
|
||||
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.tiling.source.common.LwHttp;
|
||||
import org.oscim.tiling.source.common.PbfTileDataSource;
|
||||
import org.oscim.tiling.source.common.UrlTileSource;
|
||||
@@ -29,12 +30,12 @@ public class OSciMap1TileSource extends UrlTileSource {
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
return new TileDataSource(mUrl);
|
||||
return new TileDataSource(this, mUrl);
|
||||
}
|
||||
|
||||
class TileDataSource extends PbfTileDataSource {
|
||||
public TileDataSource(URL url) {
|
||||
super(new TileDecoder());
|
||||
public TileDataSource(TileSource tileSource, URL url) {
|
||||
super(new TileDecoder(), tileSource.tileCache);
|
||||
mConn = new LwHttp(url, "application/osmtile", "osmtile", false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.oscim.core.TagSet;
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.tiling.source.ITileDataSink;
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.tiling.source.common.LwHttp;
|
||||
import org.oscim.tiling.source.common.PbfDecoder;
|
||||
import org.oscim.tiling.source.common.PbfTileDataSource;
|
||||
@@ -37,12 +38,12 @@ public class OSciMap2TileSource extends UrlTileSource {
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
return new TileDataSource(mUrl);
|
||||
return new TileDataSource(this, mUrl);
|
||||
}
|
||||
|
||||
class TileDataSource extends PbfTileDataSource {
|
||||
public TileDataSource(URL url) {
|
||||
super(new TileDecoder());
|
||||
public TileDataSource(TileSource tileSource, URL url) {
|
||||
super(new TileDecoder(), tileSource.tileCache);
|
||||
mConn = new LwHttp(url, "application/osmtile", "osmtile", false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.oscim.tiling.source.oscimap4;
|
||||
import java.net.URL;
|
||||
|
||||
import org.oscim.tiling.source.ITileDataSource;
|
||||
import org.oscim.tiling.source.TileSource;
|
||||
import org.oscim.tiling.source.common.LwHttp;
|
||||
import org.oscim.tiling.source.common.PbfTileDataSource;
|
||||
import org.oscim.tiling.source.common.UrlTileSource;
|
||||
@@ -25,12 +26,12 @@ public class OSciMap4TileSource extends UrlTileSource {
|
||||
|
||||
@Override
|
||||
public ITileDataSource getDataSource() {
|
||||
return new TileDataSource(mUrl);
|
||||
return new TileDataSource(this, mUrl);
|
||||
}
|
||||
|
||||
class TileDataSource extends PbfTileDataSource {
|
||||
public TileDataSource(URL url) {
|
||||
super(new TileDecoder());
|
||||
public TileDataSource(TileSource tileSource, URL url) {
|
||||
super(new TileDecoder(), tileSource.tileCache);
|
||||
//mConn = new LwHttp(url, "application/x-protobuf", "vtm", false);
|
||||
mConn = new LwHttp(url, "image/png", "vtm", false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user