From 599c519447dc759477cf197ec80ac2fea5bececa Mon Sep 17 00:00:00 2001 From: Young Hahn Date: Thu, 10 Nov 2011 15:26:47 -0500 Subject: [PATCH] Remove memory cache, file watcher. --- lib/mbtiles.js | 54 ++++-------------------------------------- test/reloading.test.js | 3 +-- 2 files changed, 6 insertions(+), 51 deletions(-) diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 7d8258b..b5080d5 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -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 diff --git a/test/reloading.test.js b/test/reloading.test.js index 9cda09e..aaef635 100644 --- a/test/reloading.test.js +++ b/test/reloading.test.js @@ -53,7 +53,6 @@ exports['test file reloading during copying'] = function(beforeExit) { var returned = 0; tiles.forEach(function(c) { 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]); else status.success++; }); @@ -106,4 +105,4 @@ exports['test file reloading during copying'] = function(beforeExit) { assert.equal(status.error, 11); assert.equal(status.success, 11); }); -}; \ No newline at end of file +};