Skip to content

Commit 2934d96

Browse files
committed
Tune DB with pragmas & perform async setup in .setup() method
1 parent 9396193 commit 2934d96

File tree

3 files changed

+54
-22
lines changed

3 files changed

+54
-22
lines changed

filestore.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
"use strict";
2-
var Bluebird = require('bluebird');
3-
var fs = Bluebird.promisifyAll(require('fs'));
2+
var P = require('bluebird');
3+
var fs = P.promisifyAll(require('fs'));
44

55
function FileStore(options) {
66
this.options = options;
77
}
88

9+
FileStore.prototype.setup = function() {
10+
return P.resolve(this);
11+
};
12+
913
FileStore.prototype.checkArticle = function checkArticle (title, oldid) {
1014
var options = this.options;
1115
var dumpDir = options.saveDir + '/' + options.prefix;
@@ -21,7 +25,6 @@ FileStore.prototype.checkArticle = function checkArticle (title, oldid) {
2125
// We already have the article, nothing to do.
2226
// XXX: Also track / check last-modified time for template
2327
// re-expansions without revisions change
24-
console.log('Exists:', title, oldid);
2528
return true;
2629
} else {
2730
return false;
@@ -54,4 +57,6 @@ FileStore.prototype.saveArticle = function saveArticle (body, title, oldid) {
5457
});
5558
};
5659

57-
module.exports = FileStore;
60+
module.exports = function makeFileStore(options) {
61+
return new FileStore(options).setup();
62+
};

htmldumper.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

3-
var Bluebird = require('bluebird');
3+
var P = require('bluebird');
44

5-
var FileStore = require('./filestore');
6-
var SQLiteStore = require('./sqlitestore');
5+
var makeFileStore = require('./filestore');
6+
var makeSQLiteStore = require('./sqlitestore');
77

88
// Enable heap dumps in /tmp on kill -USR2.
99
// See https://github.com/bnoordhuis/node-heapdump/
@@ -26,7 +26,7 @@ function getArticles (options, res) {
2626
var next = res.next || '';
2727
if (next === 'finished') {
2828
// nothing more to do.
29-
return Bluebird.reject('Articles done');
29+
return P.reject('Articles done');
3030
}
3131

3232
var url = options.apiURL + '?action=query&generator=allpages&gapfilterredir=nonredirects'
@@ -70,7 +70,7 @@ function dumpArticle (options, title, oldid) {
7070
if (options.store) {
7171
checkRevision = options.store.checkArticle(title, oldid);
7272
} else {
73-
checkRevision = Bluebird.resolve(false);
73+
checkRevision = P.resolve(false);
7474
}
7575

7676
return checkRevision
@@ -125,12 +125,12 @@ Dumper.prototype.processArticles = function (newArticles) {
125125
Dumper.prototype.getArticle = function () {
126126
var self = this;
127127
if (this.articles.length) {
128-
return Bluebird.resolve(this.articles.shift());
128+
return P.resolve(this.articles.shift());
129129
} else {
130130
if (!this.waiters.length) {
131131
this.articleChunkStream.next().then(this.processArticles.bind(this));
132132
}
133-
return new Bluebird(function(resolve, reject) {
133+
return new P(function(resolve, reject) {
134134
self.waiters.push({resolve: resolve, reject: reject});
135135
});
136136
}
@@ -210,13 +210,18 @@ if (module.parent === null) {
210210

211211
argv.ns = Number(argv.ns);
212212

213+
var storeSetup = P.resolve();
213214
if (argv.saveDir) {
214-
argv.store = new FileStore(argv);
215+
storeSetup = makeFileStore(argv);
215216
} else if (argv.dataBase) {
216-
argv.store = new SQLiteStore(argv);
217+
storeSetup = makeSQLiteStore(argv);
217218
}
218219

219-
return makeDump(argv)
220+
return storeSetup
221+
.then(function(store) {
222+
argv.store = store;
223+
return makeDump(argv);
224+
})
220225
.then(function(res) {
221226
console.log('Dump done.');
222227
})

sqlitestore.js

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ var P = require('bluebird');
33
var fs = P.promisifyAll(require('fs'));
44
var sqlite3 = P.promisifyAll(require('sqlite3'));
55

6+
var pragmas = [
7+
'PRAGMA main.page_size = 4096',
8+
'PRAGMA main.cache_size=10000',
9+
'PRAGMA main.locking_mode=EXCLUSIVE',
10+
'PRAGMA main.synchronous=NORMAL',
11+
'PRAGMA main.journal_mode=WAL',
12+
'PRAGMA main.cache_size=5000'
13+
];
614
var createTableQuery = 'CREATE TABLE IF NOT EXISTS data('
715
+ 'title TEXT, revision INTEGER, body BLOB, namespace INTEGER'
816
+ ', PRIMARY KEY(title ASC, revision DESC)'
@@ -14,16 +22,28 @@ var saveQuery = 'insert into data (title, revision, body, namespace) values (?,?
1422
function SQLiteStore(options) {
1523
this.options = options;
1624
this.db = new sqlite3.Database(options.dataBase);
17-
this.db.exec(createTableQuery);
18-
this.queries = {
19-
check: this.db.prepare(checkQuery),
20-
purgeTitle: this.db.prepare(purgeTitleQuery),
21-
save: this.db.prepare(saveQuery),
22-
};
2325
}
2426

27+
SQLiteStore.prototype.setup = function() {
28+
var self = this;
29+
return this.db.execAsync(createTableQuery)
30+
.then(function() {
31+
return P.all(pragmas.map(function(pragma) {
32+
return self.db.execAsync(pragma);
33+
}));
34+
})
35+
.then(function() {
36+
self.queries = {
37+
check: self.db.prepare(checkQuery),
38+
purgeTitle: self.db.prepare(purgeTitleQuery),
39+
save: self.db.prepare(saveQuery),
40+
};
41+
return self;
42+
});
43+
};
44+
2545
SQLiteStore.prototype.checkArticle = function checkArticle (title, oldid) {
26-
return this.queries.check.getAsync(title, oldid)
46+
return this.queries.check.getAsync(title, oldid);
2747
};
2848

2949
SQLiteStore.prototype.saveArticle = function saveArticle (body, title, oldid) {
@@ -34,4 +54,6 @@ SQLiteStore.prototype.saveArticle = function saveArticle (body, title, oldid) {
3454
});
3555
};
3656

37-
module.exports = SQLiteStore;
57+
module.exports = function makeSQLiteStore(options) {
58+
return new SQLiteStore(options).setup();
59+
};

0 commit comments

Comments
 (0)