Remove memory cache, file watcher.

This commit is contained in:
Young Hahn
2011-11-10 15:26:47 -05:00
parent 71d683d454
commit 599c519447
2 changed files with 6 additions and 51 deletions

View File

@@ -27,8 +27,6 @@ function hash(z, x, y) {
module.exports = MBTiles;
MBTiles.utils = require('./utils');
var cache = {};
// Provides access to an mbtiles database file.
// - uri: A parsed URL hash, the only relevant part is `pathname`.
// - callback: Will be called when the resources have been acquired
@@ -51,61 +49,23 @@ function MBTiles(uri, callback) {
uri.query = uri.query || {};
if (!uri.query.batch) uri.query.batch = 100;
var key = url.format(uri);
if (!cache[key]) {
this.setMaxListeners(0);
cache[key] = this;
this._open(uri);
}
var mbtiles = cache[key];
if (!mbtiles.open && !mbtiles.error) {
mbtiles.once('open', function(err, mbtiles) {
if (err) delete cache[key];
callback(err, mbtiles);
});
} else {
callback(mbtiles.error || null, mbtiles);
}
return undefined;
}
MBTiles.prototype._open = function(uri) {
var mbtiles = this;
function error(err) {
process.nextTick(function() {
mbtiles.error = err;
mbtiles.emit('open', err);
});
}
var key = url.format(uri);
this.setMaxListeners(0);
this.filename = uri.pathname;
this._batchSize = +uri.query.batch;
Step(function() {
mbtiles._db = new sqlite3.Database(mbtiles.filename, this);
}, function(err) {
if (err) return error(err);
if (err) throw err;
mbtiles._setup(this);
}, function(err) {
if (err) return error(err);
if (err) throw err;
fs.stat(mbtiles.filename, this);
}, function(err, stat) {
if (err) return error(err);
if (err) return callback(err);
mbtiles._stat = stat;
fs.watchFile(mbtiles.filename, {
// Indicates that the node process may exit if the file watcher
// is the only thing in the event loop.
persistent: false,
interval: 1000
}, function(cur, prev) {
if (cur.mtime != prev.mtime) {
fs.unwatchFile(mbtiles.filename);
delete cache[key];
}
});
mbtiles.open = true;
mbtiles.emit('open', null, mbtiles);
callback(null, mbtiles);
});
return undefined;
@@ -161,10 +121,6 @@ MBTiles.prototype._exists = function(table, callback) {
}
};
MBTiles.prototype._close = function() {
fs.unwatchFile(this.filename);
};
// DB integrity check.
//
// - @param {Function(err)} callback