cache mbtiles info

This commit is contained in:
Konstantin Käfer
2011-09-20 17:33:26 +02:00
parent fde3538493
commit 1b5b66e760

View File

@@ -89,10 +89,10 @@ MBTiles.prototype._open = function(uri) {
mbtiles._setup(this); mbtiles._setup(this);
}, function(err) { }, function(err) {
if (err) return error(err); if (err) return error(err);
fs.stat(mbtiles.filename, this); mbtiles._getInfo(this);
}, function(err, stat) { }, function(err, info) {
if (err) return error(err); if (err) return error(err);
mbtiles._stat = stat; mbtiles._info = info;
fs.watchFile(mbtiles.filename, { interval: 1000 }, function(cur, prev) { fs.watchFile(mbtiles.filename, { interval: 1000 }, function(cur, prev) {
if (cur.mtime != prev.mtime) { if (cur.mtime != prev.mtime) {
fs.unwatchFile(mbtiles.filename); fs.unwatchFile(mbtiles.filename);
@@ -276,23 +276,21 @@ MBTiles.prototype._metadata = function(key, callback) {
}); });
}; };
// Obtain metadata from the database. Performing fallback queries if certain MBTiles.prototype._getInfo = function(callback) {
// keys(like `bounds`, `minzoom`, `maxzoom`) have not been provided. var mbtiles = this;
//
// - @param {Function(err, data)} callback
MBTiles.prototype.getInfo = function(callback) {
if (typeof callback !== 'function') throw new Error('Callback needed');
if (!this.open) return callback(new Error('MBTiles not yet loaded'));
var that = this;
var info = {}; var info = {};
info.filesize = this._stat.size;
info.scheme = 'tms'; info.scheme = 'tms';
info.basename = path.basename(that.filename); info.basename = path.basename(mbtiles.filename);
info.id = path.basename(that.filename, path.extname(that.filename)); info.id = path.basename(mbtiles.filename, path.extname(mbtiles.filename));
Step(function() { Step(function() {
fs.stat(mbtiles.filename, this);
}, function(err, stat) {
if (err) return callback(err);
mbtiles._stat = stat;
info.filesize = stat.size;
var end = this; var end = this;
that._db.all('SELECT name, value FROM metadata', function(err, rows) { mbtiles._db.all('SELECT name, value FROM metadata', function(err, rows) {
if (rows) for (var i = 0; i < rows.length; i++) { if (rows) for (var i = 0; i < rows.length; i++) {
info[rows[i].name] = rows[i].value; info[rows[i].name] = rows[i].value;
} }
@@ -306,7 +304,7 @@ MBTiles.prototype.getInfo = function(callback) {
&& info.minzoom !== undefined) return this(); && info.minzoom !== undefined) return this();
var step = this; var step = this;
var zoomquery = that._db.prepare('SELECT zoom_level FROM tiles ' + var zoomquery = mbtiles._db.prepare('SELECT zoom_level FROM tiles ' +
'WHERE zoom_level = ? LIMIT 1', function(err) { 'WHERE zoom_level = ? LIMIT 1', function(err) {
if (err) { if (err) {
if (err.errno === 1) step(); if (err.errno === 1) step();
@@ -341,7 +339,7 @@ MBTiles.prototype.getInfo = function(callback) {
var next = this; var next = this;
Step( Step(
function() { function() {
that._db.get( mbtiles._db.get(
'SELECT MAX(tile_column) AS maxx, ' + 'SELECT MAX(tile_column) AS maxx, ' +
'MIN(tile_column) AS minx, MAX(tile_row) AS maxy, ' + 'MIN(tile_column) AS minx, MAX(tile_row) AS maxy, ' +
'MIN(tile_row) AS miny FROM tiles ' + 'MIN(tile_row) AS miny FROM tiles ' +
@@ -394,6 +392,16 @@ MBTiles.prototype.getInfo = function(callback) {
return callback(null, info); return callback(null, info);
}); });
}
// Obtain metadata from the database. Performing fallback queries if certain
// keys(like `bounds`, `minzoom`, `maxzoom`) have not been provided.
//
// - @param {Function(err, data)} callback
MBTiles.prototype.getInfo = function(callback) {
if (typeof callback !== 'function') throw new Error('Callback needed');
if (!this._info || !this.open) return callback(new Error('MBTiles not yet loaded'));
callback(null, this._info);
}; };
// Puts the MBTiles tilestore into write mode. // Puts the MBTiles tilestore into write mode.
@@ -616,5 +624,14 @@ MBTiles.prototype.putInfo = function(data, callback) {
for (var key in data) { for (var key in data) {
if (keys.indexOf(key) !== -1) stmt.run(key, String(data[key])); if (keys.indexOf(key) !== -1) stmt.run(key, String(data[key]));
} }
stmt.finalize(callback);
var mbtiles = this;
stmt.finalize(function(err) {
if (err) return callback(err);
mbtiles._getInfo(function(err, info) {
if (err) return callback(err);
mbtiles._info = info;
if (callback) callback(null);
});
});
}; };