This commit is contained in:
Young Hahn
2012-03-07 04:55:26 -05:00
6 changed files with 65 additions and 61 deletions
+34 -30
View File
@@ -55,9 +55,6 @@ function MBTiles(uri, callback) {
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) {
if (err) throw err;
mbtiles._setup(this);
}, function(err) { }, function(err) {
if (err) throw err; if (err) throw err;
fs.stat(mbtiles.filename, this); fs.stat(mbtiles.filename, this);
@@ -174,7 +171,7 @@ MBTiles.prototype.getTile = function(z, x, y, callback) {
} else { } else {
var options = { var options = {
'Content-Type': MBTiles.utils.getMimeType(row.tile_data), 'Content-Type': MBTiles.utils.getMimeType(row.tile_data),
'Last-Modified': mbtiles._stat.mtime, 'Last-Modified': new Date(mbtiles._stat.mtime).toUTCString(),
'ETag': mbtiles._stat.size + '-' + Number(mbtiles._stat.mtime) 'ETag': mbtiles._stat.size + '-' + Number(mbtiles._stat.mtime)
}; };
return callback(null, row.tile_data, options); return callback(null, row.tile_data, options);
@@ -216,21 +213,22 @@ MBTiles.prototype.getGrid = function(z, x, y, callback) {
if (err) return callback(err); if (err) return callback(err);
zlib.inflate(!Buffer.isBuffer(row.grid) zlib.inflate(!Buffer.isBuffer(row.grid)
? new Buffer(row.grid, 'binary') ? new Buffer(row.grid, 'binary')
: row.grid : row.grid,
, function(err,buffer) { function(err,buffer) {
if (err) { if (err) return callback(new Error('Grid is invalid:' + err.message));
return callback(new Error('Grid is invalid:' + err.message)); var data = rows.reduce(function(memo, r) {
} memo[r.key_name] = JSON.parse(r.key_json);
var data = rows.reduce(function(memo, r) { return memo;
memo[r.key_name] = JSON.parse(r.key_json); }, {});
return memo; var result = _(JSON.parse(buffer.toString())).extend({ data: data });
}, {}); var options = {
var result = _(JSON.parse(buffer.toString())).extend({ data: data }); 'Content-Type': 'text/javascript',
'Last-Modified': new Date(that._stat.mtime).toUTCString(),
callback(null, result); 'ETag': that._stat.size + '-' + Number(that._stat.mtime)
}); };
callback(null, result, options);
});
} }
); );
}; };
@@ -385,18 +383,24 @@ MBTiles.prototype.startWriting = function(callback) {
if (typeof callback !== 'function') throw new Error('Callback needed'); if (typeof callback !== 'function') throw new Error('Callback needed');
if (!this.open) return callback(new Error('MBTiles not yet loaded')); if (!this.open) return callback(new Error('MBTiles not yet loaded'));
// Sets the synchronous flag to OFF for (much) faster inserts.
// See http://www.sqlite3.org/pragma.html#pragma_synchronous
var mbtiles = this; var mbtiles = this;
if (!this._isWritable) { Step(function() {
this._isWritable = 1; mbtiles._setup(this);
this._clearCaches(); }, function(err) {
this._db.run('PRAGMA synchronous=OFF', callback); if (err) throw err;
} else { // Sets the synchronous flag to OFF for (much) faster inserts.
this._isWritable++; // See http://www.sqlite3.org/pragma.html#pragma_synchronous
return callback(null); if (!mbtiles._isWritable) {
} mbtiles._isWritable = 1;
mbtiles._clearCaches();
mbtiles._db.run('PRAGMA synchronous=OFF', this);
} else {
mbtiles._isWritable++;
this();
}
}, function(err) {
return callback(err);
});
}; };
MBTiles.prototype._clearCaches = function() { MBTiles.prototype._clearCaches = function() {
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "mbtiles", "name": "mbtiles",
"version": "0.1.17", "version": "0.1.18",
"description": "Utilities and tilelive integration for the MBTiles format.", "description": "Utilities and tilelive integration for the MBTiles format.",
"url": "http://github.com/mapbox/node-mbtiles", "url": "http://github.com/mapbox/node-mbtiles",
"author": { "author": {
+2 -2
View File
@@ -60,7 +60,7 @@ exports['get/put metadata from empty file'] = function(beforeExit, assert) {
assert.deepEqual({ assert.deepEqual({
basename: "empty.mbtiles", basename: "empty.mbtiles",
filesize: 16384, filesize: 0,
id: "empty", id: "empty",
scheme: "tms" scheme: "tms"
}, data); }, data);
@@ -88,7 +88,7 @@ exports['get/put metadata from empty file'] = function(beforeExit, assert) {
assert.deepEqual({ assert.deepEqual({
basename: "empty.mbtiles", basename: "empty.mbtiles",
filesize: 16384, filesize: 0,
id: "empty", id: "empty",
scheme: "tms", scheme: "tms",
version: "1.0.0" version: "1.0.0"
+8 -2
View File
@@ -38,9 +38,12 @@ exports['get tiles'] = function(beforeExit, assert) {
if (coords) { if (coords) {
// Flip Y coordinate because file names are TMS, but .getTile() expects XYZ. // Flip Y coordinate because file names are TMS, but .getTile() expects XYZ.
coords[2] = Math.pow(2, coords[3]) - 1 - coords[2]; coords[2] = Math.pow(2, coords[3]) - 1 - coords[2];
mbtiles.getTile(coords[3] | 0, coords[1] | 0, coords[2] | 0, function(err, tile) { mbtiles.getTile(coords[3] | 0, coords[1] | 0, coords[2] | 0, function(err, tile, headers) {
if (err) throw err; if (err) throw err;
assert.deepEqual(tile, fs.readFileSync(__dirname + '/fixtures/images/' + file)); assert.deepEqual(tile, fs.readFileSync(__dirname + '/fixtures/images/' + file));
assert.equal(headers['Content-Type'], 'image/png');
assert.ok(!isNaN(Date.parse(headers['Last-Modified'])));
assert.ok(/\d+-\d+/.test(headers['ETag']));
status.success++; status.success++;
}); });
} }
@@ -75,9 +78,12 @@ exports['get grids'] = function(beforeExit, assert) {
if (coords) { if (coords) {
// Flip Y coordinate because file names are TMS, but .getTile() expects XYZ. // Flip Y coordinate because file names are TMS, but .getTile() expects XYZ.
coords[2] = Math.pow(2, coords[3]) - 1 - coords[2]; coords[2] = Math.pow(2, coords[3]) - 1 - coords[2];
mbtiles.getGrid(coords[3] | 0, coords[1] | 0, coords[2] | 0, function(err, grid) { mbtiles.getGrid(coords[3] | 0, coords[1] | 0, coords[2] | 0, function(err, grid, headers) {
if (err) throw err; if (err) throw err;
assert.deepEqual(JSON.stringify(grid), fs.readFileSync(__dirname + '/fixtures/grids/' + file, 'utf8')); assert.deepEqual(JSON.stringify(grid), fs.readFileSync(__dirname + '/fixtures/grids/' + file, 'utf8'));
assert.equal(headers['Content-Type'], 'text/javascript');
assert.ok(!isNaN(Date.parse(headers['Last-Modified'])));
assert.ok(/\d+-\d+/.test(headers['ETag']));
status.success++; status.success++;
}); });
} }
+10 -13
View File
@@ -30,20 +30,17 @@ exports['test mbtiles file creation'] = function(beforeExit, assert) {
// Flip Y coordinate because file names are TMS, but .putTile() expects XYZ. // Flip Y coordinate because file names are TMS, but .putTile() expects XYZ.
coords[2] = Math.pow(2, coords[3]) - 1 - coords[2]; coords[2] = Math.pow(2, coords[3]) - 1 - coords[2];
fs.readFile(__dirname + '/fixtures/images/' + file, function(err, tile) { var tile = fs.readFileSync(__dirname + '/fixtures/images/' + file);
mbtiles.putTile(coords[3] | 0, coords[1] | 0, coords[2] | 0, tile, function(err) {
if (err) throw err; if (err) throw err;
completed.written++;
mbtiles.putTile(coords[3] | 0, coords[1] | 0, coords[2] | 0, tile, function(err) { if (completed.written === 285) {
if (err) throw err; mbtiles.stopWriting(function(err) {
completed.written++; completed.stopped = true;
if (completed.written === 285) { if (err) throw err;
mbtiles.stopWriting(function(err) { verifyWritten();
completed.stopped = true; });
if (err) throw err; }
verifyWritten();
});
}
});
}); });
} }
+10 -13
View File
@@ -29,20 +29,17 @@ exports['test mbtiles file creation'] = function(beforeExit, assert) {
// Flip Y coordinate because file names are TMS, but .putGrid() expects XYZ. // Flip Y coordinate because file names are TMS, but .putGrid() expects XYZ.
coords[2] = Math.pow(2, coords[3]) - 1 - coords[2]; coords[2] = Math.pow(2, coords[3]) - 1 - coords[2];
fs.readFile(__dirname + '/fixtures/grids/' + file, 'utf8', function(err, grid) { var grid = fs.readFileSync(__dirname + '/fixtures/grids/' + file, 'utf8');
mbtiles.putGrid(coords[3] | 0, coords[1] | 0, coords[2] | 0, JSON.parse(grid), function(err) {
if (err) throw err; if (err) throw err;
completed.written++;
mbtiles.putGrid(coords[3] | 0, coords[1] | 0, coords[2] | 0, JSON.parse(grid), function(err) { if (completed.written === 241) {
if (err) throw err; mbtiles.stopWriting(function(err) {
completed.written++; completed.stopped = true;
if (completed.written === 241) { if (err) throw err;
mbtiles.stopWriting(function(err) { verifyWritten();
completed.stopped = true; });
if (err) throw err; }
verifyWritten();
});
}
});
}); });
} }