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; module.exports = MBTiles;
MBTiles.utils = require('./utils'); MBTiles.utils = require('./utils');
var cache = {};
// Provides access to an mbtiles database file. // Provides access to an mbtiles database file.
// - uri: A parsed URL hash, the only relevant part is `pathname`. // - uri: A parsed URL hash, the only relevant part is `pathname`.
// - callback: Will be called when the resources have been acquired // - callback: Will be called when the resources have been acquired
@@ -51,61 +49,23 @@ function MBTiles(uri, callback) {
uri.query = uri.query || {}; uri.query = uri.query || {};
if (!uri.query.batch) uri.query.batch = 100; 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; var mbtiles = this;
function error(err) { this.setMaxListeners(0);
process.nextTick(function() {
mbtiles.error = err;
mbtiles.emit('open', err);
});
}
var key = url.format(uri);
this.filename = uri.pathname; this.filename = uri.pathname;
this._batchSize = +uri.query.batch; this._batchSize = +uri.query.batch;
Step(function() { Step(function() {
mbtiles._db = new sqlite3.Database(mbtiles.filename, this); mbtiles._db = new sqlite3.Database(mbtiles.filename, this);
}, function(err) { }, function(err) {
if (err) return error(err); if (err) throw err;
mbtiles._setup(this); mbtiles._setup(this);
}, function(err) { }, function(err) {
if (err) return error(err); if (err) throw err;
fs.stat(mbtiles.filename, this); fs.stat(mbtiles.filename, this);
}, function(err, stat) { }, function(err, stat) {
if (err) return error(err); if (err) return callback(err);
mbtiles._stat = stat; 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.open = true;
mbtiles.emit('open', null, mbtiles); callback(null, mbtiles);
}); });
return undefined; return undefined;
@@ -161,10 +121,6 @@ MBTiles.prototype._exists = function(table, callback) {
} }
}; };
MBTiles.prototype._close = function() {
fs.unwatchFile(this.filename);
};
// DB integrity check. // DB integrity check.
// //
// - @param {Function(err)} callback // - @param {Function(err)} callback

View File

@@ -53,7 +53,6 @@ exports['test file reloading during copying'] = function(beforeExit) {
var returned = 0; var returned = 0;
tiles.forEach(function(c) { tiles.forEach(function(c) {
mbtiles.getTile(c[0], c[1], c[2], function(err, tile) { mbtiles.getTile(c[0], c[1], c[2], function(err, tile) {
if (++returned === tiles.length) mbtiles._close();
if (err) assert.ok(false, "Couldn't load tile " + c[0] + '/' + c[1] + '/' + c[2]); if (err) assert.ok(false, "Couldn't load tile " + c[0] + '/' + c[1] + '/' + c[2]);
else status.success++; else status.success++;
}); });
@@ -106,4 +105,4 @@ exports['test file reloading during copying'] = function(beforeExit) {
assert.equal(status.error, 11); assert.equal(status.error, 11);
assert.equal(status.success, 11); assert.equal(status.success, 11);
}); });
}; };