Merge pull request #47 from mapbox/zxystream-use-index
Use map table + map index if present for zxystream
This commit is contained in:
@@ -15,6 +15,5 @@ install:
|
|||||||
|
|
||||||
build: OFF
|
build: OFF
|
||||||
test: OFF
|
test: OFF
|
||||||
test_script: OFF
|
|
||||||
deploy: OFF
|
deploy: OFF
|
||||||
|
|
||||||
|
|||||||
+11
-1
@@ -28,7 +28,17 @@ function ZXYStream(source, options) {
|
|||||||
|
|
||||||
ZXYStream.prototype._read = function() {
|
ZXYStream.prototype._read = function() {
|
||||||
var stream = this;
|
var stream = this;
|
||||||
this.source._db.all('SELECT zoom_level AS z, tile_column AS x, tile_row AS y FROM tiles LIMIT ' + this.batch + ' OFFSET ' + this.offset, function(err, rows) {
|
|
||||||
|
// Check for the existence of a map table that is indexed.
|
||||||
|
if (!stream.table) {
|
||||||
|
return this.source._db.get("select count(1) as count from sqlite_master where type = 'index' and tbl_name = 'map';", function(err, row) {
|
||||||
|
if (err) return stream.emit('error', err);
|
||||||
|
stream.table = row.count === 1 ? 'map' : 'tiles';
|
||||||
|
return stream._read();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.source._db.all('SELECT zoom_level AS z, tile_column AS x, tile_row AS y FROM ' + this.table + ' LIMIT ' + this.batch + ' OFFSET ' + this.offset, function(err, rows) {
|
||||||
if (err && err.code === 'SQLITE_ERROR' && /no such table/.test(err.message)) return stream.push(null);
|
if (err && err.code === 'SQLITE_ERROR' && /no such table/.test(err.message)) return stream.push(null);
|
||||||
if (err) return stream.emit('error', err);
|
if (err) return stream.emit('error', err);
|
||||||
if (!rows.length) return stream.push(null);
|
if (!rows.length) return stream.push(null);
|
||||||
|
|||||||
Vendored
BIN
Binary file not shown.
@@ -20,6 +20,7 @@ tape('zxystream default batch', function(assert) {
|
|||||||
assert.deepEqual(stream.offset, 0, 'sets stream.offset = 0');
|
assert.deepEqual(stream.offset, 0, 'sets stream.offset = 0');
|
||||||
|
|
||||||
stream.on('data', function(lines) {
|
stream.on('data', function(lines) {
|
||||||
|
assert.equal(stream.table, 'map');
|
||||||
output += lines;
|
output += lines;
|
||||||
called++;
|
called++;
|
||||||
});
|
});
|
||||||
@@ -52,6 +53,7 @@ tape('zxystream batch = 10', function(assert) {
|
|||||||
assert.deepEqual(stream.offset, 0, 'sets stream.offset = 0');
|
assert.deepEqual(stream.offset, 0, 'sets stream.offset = 0');
|
||||||
|
|
||||||
stream.on('data', function(lines) {
|
stream.on('data', function(lines) {
|
||||||
|
assert.equal(stream.table, 'map');
|
||||||
output += lines;
|
output += lines;
|
||||||
called++;
|
called++;
|
||||||
});
|
});
|
||||||
@@ -73,6 +75,48 @@ tape('zxystream batch = 10', function(assert) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tape('zxystream unindexed', function(assert) {
|
||||||
|
new MBTiles(__dirname + '/fixtures/unindexed.mbtiles', function(err, s) {
|
||||||
|
assert.ifError(err);
|
||||||
|
source = s;
|
||||||
|
assert.end();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
tape('zxystream unindexed zxystream', function(assert) {
|
||||||
|
var stream = source.createZXYStream();
|
||||||
|
var output = '';
|
||||||
|
var called = 0;
|
||||||
|
|
||||||
|
assert.deepEqual(stream.source, source, 'sets stream.source');
|
||||||
|
assert.deepEqual(stream.batch, 1000, 'sets stream.batch = 1000');
|
||||||
|
assert.deepEqual(stream.offset, 0, 'sets stream.offset = 0');
|
||||||
|
|
||||||
|
stream.on('data', function(lines) {
|
||||||
|
assert.equal(stream.table, 'tiles');
|
||||||
|
output += lines;
|
||||||
|
called++;
|
||||||
|
});
|
||||||
|
stream.on('end', function() {
|
||||||
|
var queue = output.toString().split('\n');
|
||||||
|
assert.equal(queue.length, 286);
|
||||||
|
assert.equal(called, 1, 'emitted data x27 times');
|
||||||
|
checkTile(queue);
|
||||||
|
function checkTile(queue) {
|
||||||
|
if (!queue.length) return assert.end();
|
||||||
|
var zxy = queue.shift();
|
||||||
|
if (!zxy) return checkTile(queue);
|
||||||
|
zxy = zxy.split('/');
|
||||||
|
source.getTile(zxy[0], zxy[1], zxy[2], function(err, buffer, headers) {
|
||||||
|
assert.equal(!err && (buffer instanceof Buffer), true, zxy.join('/') + ' exists');
|
||||||
|
checkTile(queue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
tape('zxystream empty', function(assert) {
|
tape('zxystream empty', function(assert) {
|
||||||
new MBTiles(__dirname + '/fixtures/non_existent.mbtiles', function(err, s) {
|
new MBTiles(__dirname + '/fixtures/non_existent.mbtiles', function(err, s) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
|
|||||||
Reference in New Issue
Block a user