This repository has been archived by the owner on Mar 19, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wanderer
committed
Mar 28, 2015
1 parent
d5b6a1e
commit f3d9e56
Showing
6 changed files
with
227 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,69 @@ | ||
const Tree = require('functional-red-black-tree'); | ||
const Set = require('es6-set'); | ||
const Account = require('../account.js'); | ||
|
||
var Cache = module.exports = function() { | ||
var Cache = module.exports = function(trie) { | ||
this._cache = Tree(); | ||
this._checkpoints = []; | ||
this._trie = trie; | ||
} | ||
|
||
Cache.prototype.populateCache = function(accounts, cb) { | ||
//shim till async supports iterators | ||
var accountArr = []; | ||
accounts.forEach(function(val) { | ||
if (val) accountArr.push(val); | ||
}) | ||
Cache.prototype.put = function(key, val) { | ||
key = key.toString('hex'); | ||
|
||
async.eachSeries(accountArr, function(acnt, done) { | ||
console.log(acnt); | ||
self.trie.get(new Buffer(acnt, 'hex'), function(err, val) { | ||
self.cache = self.cache.insert(acnt, val); | ||
done(); | ||
var it = this._cache.find(key); | ||
if(it.node){ | ||
this._cache = it.update(val); | ||
}else{ | ||
this._cache = this._cache.insert(key, val); | ||
} | ||
} | ||
|
||
Cache.prototype.get = function(key){ | ||
key = key.toString('hex'); | ||
|
||
var it = this._cache.find(key) | ||
if(it.node){ | ||
return it.value; | ||
}else{ | ||
return new Account(); | ||
} | ||
} | ||
|
||
Cache.prototype.getOrLoad = function(key, cb){ | ||
var self = this; | ||
|
||
if(!key){ | ||
return cb(); | ||
} | ||
|
||
key = key.toString('hex'); | ||
|
||
var it = this._cache.find(key) | ||
if(it.node){ | ||
cb(null, it.value); | ||
}else{ | ||
this._trie.get(new Buffer(key, 'hex'), function(err, raw){ | ||
var account = new Account(raw); | ||
self._cache = self._cache.insert(key, account); | ||
cb(err, account); | ||
}); | ||
}, cb); | ||
} | ||
} | ||
|
||
Cache.prototype.checkpoint = function(){ | ||
this._checkpoints.push(this._cache) | ||
} | ||
|
||
Cache.prototype.revert = function(){ | ||
this._cache = this._checkpoints.pop(this._cache) | ||
} | ||
|
||
Cache.prototype.commit = function(){ | ||
this._checkpoints.pop(); | ||
} | ||
|
||
Cache.prototype.del = function(key){ | ||
key = key.toString('hex'); | ||
this._cache = this._cache.remove(key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.