use RBTree for in-memory storage

This commit is contained in:
Kevin Jahns
2015-07-08 21:25:36 +02:00
parent fe4564542b
commit a1026bc365
9 changed files with 48 additions and 43 deletions

View File

@@ -29,14 +29,13 @@ Y.Memory = (function(){ //eslint-disable-line no-unused-vars
this.os = store.os;
}
*setOperation (op) {
if (op.struct === "Insert" && op.right === undefined) {
throw new Error("here!");
}
this.os[JSON.stringify(op.id)] = op;
// TODO: you can remove this step! probs..
var n = this.os.findNode(op.id);
n.val = op;
return op;
}
*getOperation (id) {
var op = this.os[JSON.stringify(id)];
var op = this.os.find(id);
if (op == null) {
throw new Error("Op does not exist..");
} else {
@@ -44,7 +43,7 @@ Y.Memory = (function(){ //eslint-disable-line no-unused-vars
}
}
*removeOperation (id) {
delete this.os[JSON.stringify(id)];
this.os.delete(id);
}
*setState (state : State) : State {
this.ss[state.user] = state.clock;
@@ -61,7 +60,6 @@ Y.Memory = (function(){ //eslint-disable-line no-unused-vars
}
*getStateVector () : StateVector {
var stateVector = [];
for (var user in this.ss) {
var clock = this.ss[user];
stateVector.push({
@@ -75,6 +73,7 @@ Y.Memory = (function(){ //eslint-disable-line no-unused-vars
return this.ss;
}
*getOperations (startSS : StateSet) {
// TODO: use bounds here!
if (startSS == null){
startSS = {};
}
@@ -89,15 +88,15 @@ Y.Memory = (function(){ //eslint-disable-line no-unused-vars
var startPos = startSS[user] || 0;
var endPos = endState.clock;
for (var clock = startPos; clock <= endPos; clock++) {
var op = this.os[JSON.stringify([user, clock])];
if (op != null) {
op = Struct[op.struct].encode(op);
ops.push(yield* this.makeOperationReady.call(this, startSS, op));
}
}
this.os.iterate([user, startPos], [user, endPos], function(op){//eslint-disable-line
ops.push(Struct[op.struct].encode(op));
});
}
return ops;
var res = [];
for (var op of ops) {
res.push(yield* this.makeOperationReady.call(this, startSS, op));
}
return res;
}
*makeOperationReady (ss, op) {
// instead of ss, you could use currSS (a ss that increments when you add an operation)
@@ -119,7 +118,7 @@ Y.Memory = (function(){ //eslint-disable-line no-unused-vars
class OperationStore extends AbstractOperationStore { //eslint-disable-line no-undef
constructor (y) {
super(y);
this.os = {};
this.os = new RBTree();
this.ss = {};
}
requestTransaction (makeGen : Function) {

View File

@@ -1,6 +1,4 @@
class N {
// A created node is always red!
constructor (val) {

View File

@@ -1,7 +1,7 @@
/* @flow */
/*eslint-env browser,jasmine,console */
var numberOfTests = 10009;
var numberOfRBTreeTests = 1000;
function itRedNodesDoNotHaveBlackChildren (tree) {
it("Red nodes do not have black children", function(){
@@ -107,16 +107,15 @@ describe("RedBlack Tree", function(){
itRootNodeIsBlack(tree, []);
itBlackHeightOfSubTreesAreEqual(tree, []);
itRedNodesDoNotHaveBlackChildren(tree, []);
});
describe(`After adding&deleting (0.8/0.2) ${numberOfTests} times`, function () {
describe(`After adding&deleting (0.8/0.2) ${numberOfRBTreeTests} times`, function () {
var elements = [];
var tree = new RBTree();
for(var i = 0; i < numberOfTests; i++) {
for(var i = 0; i < numberOfRBTreeTests; i++) {
var r = Math.random();
if (r < 0.8) {
var obj = Math.floor(Math.random() * numberOfTests * 10000);
var obj = Math.floor(Math.random() * numberOfRBTreeTests * 10000);
elements.push(obj);
tree.add({id: obj});
} else if (elements.length > 0) {