This commit is contained in:
Young Hahn
2012-03-07 05:02:20 -05:00
8 changed files with 143 additions and 146 deletions
+34 -33
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);
@@ -216,24 +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 });
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);
});
? 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);
});
}
);
};
@@ -397,18 +392,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.exec('PRAGMA locking_mode = EXCLUSIVE; PRAGMA synchronous=OFF; PRAGMA cache_size = 400000;', 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() {
@@ -517,7 +518,7 @@ MBTiles.prototype.stopWriting = function(callback) {
this._commit(function(err) {
if (err) return callback(err);
if (!mbtiles._isWritable) {
mbtiles._db.exec('PRAGMA journal_mode = DELETE; PRAGMA locking_mode = NORMAL; PRAGMA synchronous=NORMAL; PRAGMA cache_size=2000;', callback);
mbtiles._db.run('PRAGMA synchronous=NORMAL', callback);
} else {
return callback(null);
}