cache mbtiles info
This commit is contained in:
@@ -89,10 +89,10 @@ MBTiles.prototype._open = function(uri) {
|
||||
mbtiles._setup(this);
|
||||
}, function(err) {
|
||||
if (err) return error(err);
|
||||
fs.stat(mbtiles.filename, this);
|
||||
}, function(err, stat) {
|
||||
mbtiles._getInfo(this);
|
||||
}, function(err, info) {
|
||||
if (err) return error(err);
|
||||
mbtiles._stat = stat;
|
||||
mbtiles._info = info;
|
||||
fs.watchFile(mbtiles.filename, { interval: 1000 }, function(cur, prev) {
|
||||
if (cur.mtime != prev.mtime) {
|
||||
fs.unwatchFile(mbtiles.filename);
|
||||
@@ -276,23 +276,21 @@ MBTiles.prototype._metadata = function(key, callback) {
|
||||
});
|
||||
};
|
||||
|
||||
// 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.open) return callback(new Error('MBTiles not yet loaded'));
|
||||
|
||||
var that = this;
|
||||
MBTiles.prototype._getInfo = function(callback) {
|
||||
var mbtiles = this;
|
||||
var info = {};
|
||||
info.filesize = this._stat.size;
|
||||
info.scheme = 'tms';
|
||||
info.basename = path.basename(that.filename);
|
||||
info.id = path.basename(that.filename, path.extname(that.filename));
|
||||
info.basename = path.basename(mbtiles.filename);
|
||||
info.id = path.basename(mbtiles.filename, path.extname(mbtiles.filename));
|
||||
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;
|
||||
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++) {
|
||||
info[rows[i].name] = rows[i].value;
|
||||
}
|
||||
@@ -306,7 +304,7 @@ MBTiles.prototype.getInfo = function(callback) {
|
||||
&& info.minzoom !== undefined) return 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) {
|
||||
if (err) {
|
||||
if (err.errno === 1) step();
|
||||
@@ -341,7 +339,7 @@ MBTiles.prototype.getInfo = function(callback) {
|
||||
var next = this;
|
||||
Step(
|
||||
function() {
|
||||
that._db.get(
|
||||
mbtiles._db.get(
|
||||
'SELECT MAX(tile_column) AS maxx, ' +
|
||||
'MIN(tile_column) AS minx, MAX(tile_row) AS maxy, ' +
|
||||
'MIN(tile_row) AS miny FROM tiles ' +
|
||||
@@ -394,6 +392,16 @@ MBTiles.prototype.getInfo = function(callback) {
|
||||
|
||||
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.
|
||||
@@ -616,5 +624,14 @@ MBTiles.prototype.putInfo = function(data, callback) {
|
||||
for (var key in data) {
|
||||
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);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user