This commit is contained in:
Young Hahn
2012-03-07 04:55:26 -05:00
6 changed files with 65 additions and 61 deletions

View File

@@ -55,9 +55,6 @@ function MBTiles(uri, callback) {
this._batchSize = +uri.query.batch;
Step(function() {
mbtiles._db = new sqlite3.Database(mbtiles.filename, this);
}, function(err) {
if (err) throw err;
mbtiles._setup(this);
}, function(err) {
if (err) throw err;
fs.stat(mbtiles.filename, this);
@@ -174,7 +171,7 @@ MBTiles.prototype.getTile = function(z, x, y, callback) {
} else {
var options = {
'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)
};
return callback(null, row.tile_data, options);
@@ -216,21 +213,22 @@ MBTiles.prototype.getGrid = function(z, x, y, callback) {
if (err) return callback(err);
zlib.inflate(!Buffer.isBuffer(row.grid)
? new Buffer(row.grid, 'binary')
: row.grid
, function(err,buffer) {
if (err) {
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);
return memo;
}, {});
var result = _(JSON.parse(buffer.toString())).extend({ data: data });
callback(null, result);
});
? new Buffer(row.grid, 'binary')
: row.grid,
function(err,buffer) {
if (err) 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);
return memo;
}, {});
var result = _(JSON.parse(buffer.toString())).extend({ data: data });
var options = {
'Content-Type': 'text/javascript',
'Last-Modified': new Date(that._stat.mtime).toUTCString(),
'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 (!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;
if (!this._isWritable) {
this._isWritable = 1;
this._clearCaches();
this._db.run('PRAGMA synchronous=OFF', callback);
} else {
this._isWritable++;
return callback(null);
}
Step(function() {
mbtiles._setup(this);
}, function(err) {
if (err) throw err;
// Sets the synchronous flag to OFF for (much) faster inserts.
// See http://www.sqlite3.org/pragma.html#pragma_synchronous
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() {

View File

@@ -1,6 +1,6 @@
{
"name": "mbtiles",
"version": "0.1.17",
"version": "0.1.18",
"description": "Utilities and tilelive integration for the MBTiles format.",
"url": "http://github.com/mapbox/node-mbtiles",
"author": {

View File

@@ -60,7 +60,7 @@ exports['get/put metadata from empty file'] = function(beforeExit, assert) {
assert.deepEqual({
basename: "empty.mbtiles",
filesize: 16384,
filesize: 0,
id: "empty",
scheme: "tms"
}, data);
@@ -88,7 +88,7 @@ exports['get/put metadata from empty file'] = function(beforeExit, assert) {
assert.deepEqual({
basename: "empty.mbtiles",
filesize: 16384,
filesize: 0,
id: "empty",
scheme: "tms",
version: "1.0.0"

View File

@@ -38,9 +38,12 @@ exports['get tiles'] = function(beforeExit, assert) {
if (coords) {
// Flip Y coordinate because file names are TMS, but .getTile() expects XYZ.
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;
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++;
});
}
@@ -75,9 +78,12 @@ exports['get grids'] = function(beforeExit, assert) {
if (coords) {
// Flip Y coordinate because file names are TMS, but .getTile() expects XYZ.
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;
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++;
});
}

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.
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;
mbtiles.putTile(coords[3] | 0, coords[1] | 0, coords[2] | 0, tile, function(err) {
if (err) throw err;
completed.written++;
if (completed.written === 285) {
mbtiles.stopWriting(function(err) {
completed.stopped = true;
if (err) throw err;
verifyWritten();
});
}
});
completed.written++;
if (completed.written === 285) {
mbtiles.stopWriting(function(err) {
completed.stopped = true;
if (err) throw err;
verifyWritten();
});
}
});
}

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.
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;
mbtiles.putGrid(coords[3] | 0, coords[1] | 0, coords[2] | 0, JSON.parse(grid), function(err) {
if (err) throw err;
completed.written++;
if (completed.written === 241) {
mbtiles.stopWriting(function(err) {
completed.stopped = true;
if (err) throw err;
verifyWritten();
});
}
});
completed.written++;
if (completed.written === 241) {
mbtiles.stopWriting(function(err) {
completed.stopped = true;
if (err) throw err;
verifyWritten();
});
}
});
}