public class SqlTileWriter extends Object implements IFilesystemCache, SplashScreenable
IFilesystemCache
based on the original TileWriter. It writes tiles to a sqlite database cache.
It supports expiration timestamps if provided by the server from which the tile was downloaded. Trimming
of expired
If the database exceeds Configuration#getInstance()#getTileFileSystemCacheTrimBytes()
cache exceeds 600 Mb then it will be trimmed to 500 Mb by deleting files that expire first.
DatabaseFileArchive
,
SqliteArchiveTileWriter
Modifier and Type | Field and Description |
---|---|
static String |
COLUMN_EXPIRES |
static String |
COLUMN_EXPIRES_INDEX |
static String |
DATABASE_FILENAME |
protected static File |
db_file |
(package private) static boolean |
hasInited |
protected long |
lastSizeCheck |
protected static SQLiteDatabase |
mDb |
Constructor and Description |
---|
SqlTileWriter() |
Modifier and Type | Method and Description |
---|---|
protected void |
catchException(Exception pException) |
long |
delete(String pTileSourceName,
int pZoom,
Collection<Rect> pInclude,
Collection<Rect> pExclude)
Delete cache tiles
|
boolean |
exists(ITileSource pTileSource,
long pMapTileIndex)
Returns true if the given tile source and tile coordinates exist in the cache
|
boolean |
exists(String pTileSource,
long pMapTileIndex)
Returns true if the given tile source and tile coordinates exist in the cache
|
protected static String |
extractXFromKeyInSQL(int pZoom) |
protected static String |
extractYFromKeyInSQL(int pZoom) |
protected SQLiteDatabase |
getDb() |
Long |
getExpirationTimestamp(ITileSource pTileSource,
long pMapTileIndex)
Gets the cache expiration timestamp of a tile
|
long |
getFirstExpiry()
Returns the expiry time of the tile that expires first.
|
static long |
getIndex(long pMapTileIndex)
Gets the single column index value for a map tile
Unluckily, "map tile index" and "sql pk" don't match
|
static long |
getIndex(long pX,
long pY,
long pZ) |
static String |
getPrimaryKey() |
static String[] |
getPrimaryKeyParameters(long pIndex,
ITileSource pTileSourceInfo) |
static String[] |
getPrimaryKeyParameters(long pIndex,
String pTileSourceInfo) |
long |
getRowCount(String tileSourceName)
Returns the number of tiles in the cache for the specified tile source name
|
long |
getRowCount(String pTileSourceName,
int pZoom,
Collection<Rect> pInclude,
Collection<Rect> pExclude)
Count cache tiles
|
protected long |
getRowCount(String pWhereClause,
String[] pWhereClauseArgs)
Count cache tiles: helper method
|
long |
getSize()
Returns the size of the database file in bytes.
|
Cursor |
getTileCursor(String[] pPrimaryKeyParameters,
String[] pColumns) |
protected StringBuilder |
getWhereClause(int pZoom,
Collection<Rect> pInclude,
Collection<Rect> pExclude) |
protected StringBuilder |
getWhereClause(int pZoom,
Rect pRect) |
int[] |
importFromFileCache(boolean removeFromFileSystem)
a helper method to import file system stored map tiles into the sql tile cache
on successful import, the tiles are removed from the file system.
|
static boolean |
isFunctionalException(SQLiteException pSQLiteException) |
Drawable |
loadTile(ITileSource pTileSource,
long pMapTileIndex)
Gets the tile drawable
|
void |
onDetach()
Now we use only one static instance of database, which should never be closed
|
boolean |
purgeCache()
purges and deletes everything from the cache database
|
boolean |
purgeCache(String mTileSourceName)
purges and deletes all tiles from the given tile source name from the cache database
|
void |
refreshDb() |
boolean |
remove(ITileSource pTileSourceInfo,
long pMapTileIndex)
Removes a specific tile from the cache
|
void |
runCleanupOperation()
this could be a long running operation, don't run on the UI thread unless necessary.
|
void |
runCleanupOperation(long pToBeDeleted,
int pBulkSize,
long pPauseMillis,
boolean pIncludeUnexpired) |
void |
runDuringSplashScreen() |
boolean |
saveFile(ITileSource pTileSourceInfo,
long pMapTileIndex,
InputStream pStream,
Long pExpirationTime)
Save an InputStream as the specified tile in the file system cache for the specified tile
source.
|
static void |
setCleanupOnStart(boolean value) |
public static final String DATABASE_FILENAME
public static final String COLUMN_EXPIRES
public static final String COLUMN_EXPIRES_INDEX
protected static File db_file
protected static SQLiteDatabase mDb
protected long lastSizeCheck
static boolean hasInited
public static void setCleanupOnStart(boolean value)
public void runCleanupOperation()
public boolean saveFile(ITileSource pTileSourceInfo, long pMapTileIndex, InputStream pStream, Long pExpirationTime)
IFilesystemCache
saveFile
in interface IFilesystemCache
pTileSourceInfo
- a tile sourcepMapTileIndex
- a tilepStream
- an InputStreampublic boolean exists(String pTileSource, long pMapTileIndex)
public boolean exists(ITileSource pTileSource, long pMapTileIndex)
exists
in interface IFilesystemCache
public void onDetach()
onDetach
in interface IFilesystemCache
public boolean purgeCache()
public boolean purgeCache(String mTileSourceName)
public int[] importFromFileCache(boolean removeFromFileSystem)
This can take a long time, so consider running this off of the main thread.
public boolean remove(ITileSource pTileSourceInfo, long pMapTileIndex)
remove
in interface IFilesystemCache
public long getRowCount(String tileSourceName)
tileSourceName
- protected long getRowCount(String pWhereClause, String[] pWhereClauseArgs)
public long getRowCount(String pTileSourceName, int pZoom, Collection<Rect> pInclude, Collection<Rect> pExclude)
pTileSourceName
- the tile source name (possibly null)pZoom
- the zoom levelpInclude
- a collection of bounding boxes to include (possibly null/empty)pExclude
- a collection of bounding boxes to exclude (possibly null/empty)public long getSize()
public long getFirstExpiry()
public static long getIndex(long pX, long pY, long pZ)
extractXFromKeyInSQL(int)
,
extractYFromKeyInSQL(int)
protected static String extractXFromKeyInSQL(int pZoom)
getIndex(long, long, long)
protected static String extractYFromKeyInSQL(int pZoom)
getIndex(long, long, long)
public static long getIndex(long pMapTileIndex)
pMapTileIndex
- public Long getExpirationTimestamp(ITileSource pTileSource, long pMapTileIndex)
IFilesystemCache
getExpirationTimestamp
in interface IFilesystemCache
public static String getPrimaryKey()
public static String[] getPrimaryKeyParameters(long pIndex, ITileSource pTileSourceInfo)
pIndex
- pTileSourceInfo
- public static String[] getPrimaryKeyParameters(long pIndex, String pTileSourceInfo)
pIndex
- pTileSourceInfo
- public Cursor getTileCursor(String[] pPrimaryKeyParameters, String[] pColumns)
pPrimaryKeyParameters
- pColumns
- public Drawable loadTile(ITileSource pTileSource, long pMapTileIndex) throws Exception
IFilesystemCache
loadTile
in interface IFilesystemCache
Exception
public void runCleanupOperation(long pToBeDeleted, int pBulkSize, long pPauseMillis, boolean pIncludeUnexpired)
pToBeDeleted
- Amount of bytes to delete (as tile blob size)pBulkSize
- Number of tiles to delete in bulkpPauseMillis
- Pause between bulk actions, in order not to play it not aggressive on the CPUpIncludeUnexpired
- Should we also delete tiles that are not expired?protected SQLiteDatabase getDb()
public void refreshDb()
protected void catchException(Exception pException)
public static boolean isFunctionalException(SQLiteException pSQLiteException)
public void runDuringSplashScreen()
runDuringSplashScreen
in interface SplashScreenable
protected StringBuilder getWhereClause(int pZoom, Rect pRect)
protected StringBuilder getWhereClause(int pZoom, Collection<Rect> pInclude, Collection<Rect> pExclude)
public long delete(String pTileSourceName, int pZoom, Collection<Rect> pInclude, Collection<Rect> pExclude)
pTileSourceName
- the tile source name (possibly null)pZoom
- the zoom levelpInclude
- a collection of bounding boxes to include (possibly null/empty)pExclude
- a collection of bounding boxes to exclude (possibly null/empty)