diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 7e7f069..3968eb7 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -5,6 +5,7 @@ var _ = require('underscore'), zlib = require('zlib'), path = require('path'), url = require('url'), + qs = require('querystring'), Buffer = require('buffer').Buffer, sm = new (require('sphericalmercator')), sqlite3 = require('sqlite3'); @@ -34,7 +35,8 @@ var cache = {}; // or acquisition failed. require('util').inherits(MBTiles, require('events').EventEmitter) function MBTiles(uri, callback) { - if (typeof uri === 'string') uri = url.parse(uri); + if (typeof uri === 'string') uri = url.parse(uri, true); + else if (typeof uri.query === 'string') uri.query = qs.parse(uri.query); if (!uri.pathname) { callback(new Error('Invalid URI ' + url.format(uri))); @@ -46,13 +48,16 @@ function MBTiles(uri, callback) { delete uri.hostname; delete uri.host; } + uri.query = uri.query || {}; + if (!uri.query.batch) uri.query.batch = 100; - if (!cache[uri.pathname]) { - cache[uri.pathname] = this; + var key = url.format(uri); + if (!cache[key]) { + cache[key] = this; this._open(uri); } - var mbtiles = cache[uri.pathname]; + var mbtiles = cache[key]; if (!mbtiles.open) { mbtiles.once('open', callback); } else { @@ -69,7 +74,9 @@ MBTiles.prototype._open = function(uri) { }); } + var key = url.format(uri); this.filename = uri.pathname; + this._batchSize = uri.query.batch; Step(function() { mbtiles._db = new sqlite3.Database(mbtiles.filename, this); }, function(err) { @@ -84,7 +91,7 @@ MBTiles.prototype._open = function(uri) { fs.watchFile(mbtiles.filename, { interval: 1000 }, function(cur, prev) { if (cur.mtime != prev.mtime) { fs.unwatchFile(mbtiles.filename); - delete cache[uri.pathname]; + delete cache[key]; } }); mbtiles.open = true; @@ -559,8 +566,8 @@ MBTiles.prototype.putTile = function(z, x, y, data, callback) { if (!this._mapCache[coords]) this._mapCache[coords] = { z: z, x: x, y: y }; this._mapCache[coords].tile_id = id; - // Only commit when we can insert at least 100 rows. - if (++this._pending >= 100) return this._commit(callback); + // Only commit when we can insert at least batchSize rows. + if (++this._pending >= this._batchSize) return this._commit(callback); else return callback(null); }; @@ -598,8 +605,8 @@ MBTiles.prototype.putGrid = function(z, x, y, data, callback) { if (!this._mapCache[coords]) this._mapCache[coords] = { z: z, x: x, y: y }; this._mapCache[coords].grid_id = id; - // Only commit when we can insert at least 100 rows. - if (++this._pending >= 100) return this._commit(callback); + // Only commit when we can insert at least batchSize rows. + if (++this._pending >= this._batchSize) return this._commit(callback); else return callback(null); };