Skip to content

Commit

Permalink
Move setup step to #startWriting method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Young Hahn committed Feb 24, 2012
1 parent 0b85f40 commit cca4271
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
31 changes: 17 additions & 14 deletions lib/mbtiles.js
Expand Up @@ -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);
Expand Down Expand Up @@ -389,18 +386,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() {
Expand Down
4 changes: 2 additions & 2 deletions test/info.test.js
Expand Up @@ -61,7 +61,7 @@ exports['get/put metadata from empty file'] = function(beforeExit) {

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

assert.deepEqual({
basename: "empty.mbtiles",
filesize: 16384,
filesize: 0,
id: "empty",
scheme: "tms",
version: "1.0.0"
Expand Down

0 comments on commit cca4271

Please sign in to comment.