merkle-btree
Version:
Content-addressed b-tree
97 lines (70 loc) • 3.33 kB
JavaScript
;
exports.__esModule = true;
var _TreeNode = require('./TreeNode');
var _TreeNode2 = _interopRequireDefault(_TreeNode);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var MerkleBTree = function () {
function MerkleBTree(storage) {
var maxChildren = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
var rootNode = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new _TreeNode2.default();
_classCallCheck(this, MerkleBTree);
this.rootNode = rootNode;
this.storage = storage;
this.maxChildren = Math.max(maxChildren, 2);
}
MerkleBTree.prototype.get = function get(key) {
return this.rootNode.get(key, this.storage);
};
MerkleBTree.prototype.searchText = function searchText(query, limit, cursor) {
var reverse = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
return this.rootNode.searchText(query, limit, cursor, reverse, this.storage);
};
MerkleBTree.prototype.searchRange = function searchRange(lowerBound, upperBound, limit) {
var includeLowerBound = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
var includeUpperBound = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true;
var reverse = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
return this.rootNode.searchRange(lowerBound, upperBound, false, limit, includeLowerBound, includeUpperBound, reverse, this.storage);
};
MerkleBTree.prototype.put = function put(key, value) {
var _this = this;
return this.rootNode.put(key, value, this.storage, this.maxChildren).then(function (newRoot) {
_this.rootNode = newRoot;
return _this.rootNode.hash;
});
};
MerkleBTree.prototype.delete = function _delete(key) {
var _this2 = this;
return this.rootNode.delete(key, this.storage, this.maxChildren).then(function (newRoot) {
_this2.rootNode = newRoot;
return _this2.rootNode.hash;
});
};
MerkleBTree.prototype.print = function print() {
return this.rootNode.print(this.storage);
};
/* Return number of keys stored in tree */
MerkleBTree.prototype.size = function size() {
return this.rootNode.size(this.storage);
};
MerkleBTree.prototype.toSortedList = function toSortedList() {
return this.rootNode.toSortedList(this.storage);
};
MerkleBTree.prototype.save = function save() {
return this.rootNode.save(this.storage);
};
MerkleBTree.getByHash = function getByHash(hash, storage, maxChildren) {
return storage.get(hash).then(function (data) {
var rootNode = _TreeNode2.default.deserialize(data);
return new MerkleBTree(storage, maxChildren, rootNode);
});
};
MerkleBTree.fromSortedList = function fromSortedList(list, maxChildren, storage) {
return _TreeNode2.default.fromSortedList(list, maxChildren, storage).then(function (rootNode) {
return new MerkleBTree(storage, maxChildren, rootNode);
});
};
return MerkleBTree;
}();
exports.default = MerkleBTree;
module.exports = exports['default'];