chaindb: async loading.

This commit is contained in:
Christopher Jeffrey 2016-01-21 04:10:21 -08:00
parent c5bc356bc9
commit 34a44fe391
2 changed files with 33 additions and 7 deletions

View File

@ -97,15 +97,27 @@ Chain.prototype._init = function _init() {
utils.nextTick(function() { utils.nextTick(function() {
var count = self.db.count(); var count = self.db.count();
var i, entry; var entry;
var i = 1;
for (i = 1; i < count; i++) function done() {
self._saveEntry(self.db.get(i)); self.loading = false;
self.emit('load');
self.loading = false; utils.debug('Chain successfully loaded.');
self.emit('load'); }
utils.debug('Chain successfully loaded.'); (function next() {
if (i >= count)
return done();
self.db.getAsync(i, function(err, entry) {
if (err)
throw err;
self._saveEntry(entry);
i += 1;
next();
});
})();
}); });
}; };
@ -996,7 +1008,7 @@ ChainDB.prototype._read = function _read(size, offset) {
} }
}; };
ChainDB.prototype._readAsync = function _read(size, offset) { ChainDB.prototype._readAsync = function _read(size, offset, callback) {
var self = this; var self = this;
var data = new Buffer(size); var data = new Buffer(size);
var index = 0; var index = 0;

View File

@ -414,6 +414,9 @@ Pool.prototype._addLoader = function _addLoader() {
}; };
Pool.prototype.startSync = function startSync() { Pool.prototype.startSync = function startSync() {
if (this.loading)
return this.once('load', this.startSync.bind(this));
this.syncing = true; this.syncing = true;
this._startInterval(); this._startInterval();
@ -436,6 +439,9 @@ Pool.prototype.stopSync = function stopSync() {
this.syncing = false; this.syncing = false;
if (this.loading)
return;
this._stopInterval(); this._stopInterval();
this._stopTimer(); this._stopTimer();
}; };
@ -1052,6 +1058,9 @@ Pool.prototype.addWallet = function addWallet(w, defaultTs) {
var self = this; var self = this;
var e; var e;
if (this.loading)
return this.once('load', this.addWallet.bind(this, w, defaultTs));
if (this.wallets.indexOf(w) !== -1) if (this.wallets.indexOf(w) !== -1)
return false; return false;
@ -1089,6 +1098,7 @@ Pool.prototype.addWallet = function addWallet(w, defaultTs) {
Pool.prototype.removeWallet = function removeWallet(w) { Pool.prototype.removeWallet = function removeWallet(w) {
var i = this.wallets.indexOf(w); var i = this.wallets.indexOf(w);
assert(!this.loading);
if (i == -1) if (i == -1)
return; return;
this.wallets.splice(i, 1); this.wallets.splice(i, 1);
@ -1127,6 +1137,8 @@ Pool.prototype.searchWallet = function(w) {
var self = this; var self = this;
var ts; var ts;
assert(!this.loading);
if (this.options.fullNode) if (this.options.fullNode)
return; return;
@ -1167,6 +1179,8 @@ Pool.prototype.search = function search(id, range, e) {
var self = this; var self = this;
var hashes, pending, listener, timeout, done, total, cb; var hashes, pending, listener, timeout, done, total, cb;
assert(!this.loading);
if (this.options.fullNode) if (this.options.fullNode)
return; return;