make the batchsize configurable
This commit is contained in:
+16
-9
@@ -5,6 +5,7 @@ var _ = require('underscore'),
|
|||||||
zlib = require('zlib'),
|
zlib = require('zlib'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
url = require('url'),
|
url = require('url'),
|
||||||
|
qs = require('querystring'),
|
||||||
Buffer = require('buffer').Buffer,
|
Buffer = require('buffer').Buffer,
|
||||||
sm = new (require('sphericalmercator')),
|
sm = new (require('sphericalmercator')),
|
||||||
sqlite3 = require('sqlite3');
|
sqlite3 = require('sqlite3');
|
||||||
@@ -34,7 +35,8 @@ var cache = {};
|
|||||||
// or acquisition failed.
|
// or acquisition failed.
|
||||||
require('util').inherits(MBTiles, require('events').EventEmitter)
|
require('util').inherits(MBTiles, require('events').EventEmitter)
|
||||||
function MBTiles(uri, callback) {
|
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) {
|
if (!uri.pathname) {
|
||||||
callback(new Error('Invalid URI ' + url.format(uri)));
|
callback(new Error('Invalid URI ' + url.format(uri)));
|
||||||
@@ -46,13 +48,16 @@ function MBTiles(uri, callback) {
|
|||||||
delete uri.hostname;
|
delete uri.hostname;
|
||||||
delete uri.host;
|
delete uri.host;
|
||||||
}
|
}
|
||||||
|
uri.query = uri.query || {};
|
||||||
|
if (!uri.query.batch) uri.query.batch = 100;
|
||||||
|
|
||||||
if (!cache[uri.pathname]) {
|
var key = url.format(uri);
|
||||||
cache[uri.pathname] = this;
|
if (!cache[key]) {
|
||||||
|
cache[key] = this;
|
||||||
this._open(uri);
|
this._open(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
var mbtiles = cache[uri.pathname];
|
var mbtiles = cache[key];
|
||||||
if (!mbtiles.open) {
|
if (!mbtiles.open) {
|
||||||
mbtiles.once('open', callback);
|
mbtiles.once('open', callback);
|
||||||
} else {
|
} else {
|
||||||
@@ -69,7 +74,9 @@ MBTiles.prototype._open = function(uri) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var key = url.format(uri);
|
||||||
this.filename = uri.pathname;
|
this.filename = uri.pathname;
|
||||||
|
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) {
|
||||||
@@ -84,7 +91,7 @@ MBTiles.prototype._open = function(uri) {
|
|||||||
fs.watchFile(mbtiles.filename, { interval: 1000 }, function(cur, prev) {
|
fs.watchFile(mbtiles.filename, { interval: 1000 }, function(cur, prev) {
|
||||||
if (cur.mtime != prev.mtime) {
|
if (cur.mtime != prev.mtime) {
|
||||||
fs.unwatchFile(mbtiles.filename);
|
fs.unwatchFile(mbtiles.filename);
|
||||||
delete cache[uri.pathname];
|
delete cache[key];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
mbtiles.open = true;
|
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 };
|
if (!this._mapCache[coords]) this._mapCache[coords] = { z: z, x: x, y: y };
|
||||||
this._mapCache[coords].tile_id = id;
|
this._mapCache[coords].tile_id = id;
|
||||||
|
|
||||||
// Only commit when we can insert at least 100 rows.
|
// Only commit when we can insert at least batchSize rows.
|
||||||
if (++this._pending >= 100) return this._commit(callback);
|
if (++this._pending >= this._batchSize) return this._commit(callback);
|
||||||
else return callback(null);
|
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 };
|
if (!this._mapCache[coords]) this._mapCache[coords] = { z: z, x: x, y: y };
|
||||||
this._mapCache[coords].grid_id = id;
|
this._mapCache[coords].grid_id = id;
|
||||||
|
|
||||||
// Only commit when we can insert at least 100 rows.
|
// Only commit when we can insert at least batchSize rows.
|
||||||
if (++this._pending >= 100) return this._commit(callback);
|
if (++this._pending >= this._batchSize) return this._commit(callback);
|
||||||
else return callback(null);
|
else return callback(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user