Permalink
Browse files

Replaced memoized tuples array with a skip list. Have lots of specs p…

…assing, but the ones related to ordering are still failing. Need to use a custom comparator.
  • Loading branch information...
1 parent 89d0058 commit 58c62208c14eb0f93b79d18ee92cc82a9be5871d @nathansobo committed Jan 1, 2011
@@ -10,8 +10,7 @@ _.constructor("Monarch.Model.Relations.InnerJoin", Monarch.Model.Relations.Relat
},
tuples: function() {
- if (this._tuples) return this._tuples;
-
+ if (this._tuples) return this._tuples.values();
return _.filter(this.cartesianProduct(), function(compositeTuple) {
return this.predicate.evaluate(compositeTuple);
}, this);
@@ -60,13 +60,11 @@ _.constructor("Monarch.Model.Relations.Ordering", Monarch.Model.Relations.Relati
// private
tupleInsertedRemotely: function(tuple) {
- var position = _.comparatorSortedIndex(this._tuples, tuple, this.comparator);
- this._tuples.splice(position, 0, tuple);
+ var position = this._tuples.insert(tuple)
this.onInsertNode.publish(tuple, position);
},
tupleUpdatedRemotely: function($super, tuple, changedFields) {
- var currentPosition = _.indexOf(this._tuples, tuple);
var positionMayChange = _.any(changedFields, function(changedField) {
return this.sortingOnColumn(changedField.column);
}, this);
@@ -75,15 +73,13 @@ _.constructor("Monarch.Model.Relations.Ordering", Monarch.Model.Relations.Relati
return;
}
- this._tuples.splice(currentPosition, 1);
- var newPosition = _.comparatorSortedIndex(this._tuples, tuple, this.comparator);
- this._tuples.splice(newPosition, 0, tuple);
- $super(tuple, changedFields, newPosition, currentPosition);
+ var oldPosition = this._tuples.remove(tuple);
+ var newPosition = this._tuples.insert(tuple);
+ $super(tuple, changedFields, newPosition, oldPosition);
},
tupleRemovedRemotely: function(record) {
- var position = _.indexOf(this._tuples, record);
- this._tuples.splice(position, 1);
+ var position = this._tuples.remove(record);
this.onRemoveNode.publish(record, position);
},
@@ -16,7 +16,7 @@ _.constructor("Monarch.Model.Relations.Projection", Monarch.Model.Relations.Rela
},
tuples: function() {
- if (this._tuples) return this._tuples;
+ if (this._tuples) return this._tuples.values();
this.tuplesByOperandRecordId = {};
return _.map(this.operand.tuples(), function(operandTuple) {
@@ -234,13 +234,17 @@ _.constructor("Monarch.Model.Relations.Relation", {
},
memoizeTuples: function() {
- this._tuples = this.tuples();
+ var tuples = this.buildSkipList();
+ tuples.insertAll(this.tuples());
+ this._tuples = tuples;
+ },
+
+ buildSkipList: function() {
+ return new Monarch.SkipList(this.comparator);
},
tupleInsertedRemotely: function(record) {
- if (!this.contains(record)) {
- this._tuples.push(record)
- }
+ this._tuples.insert(record)
this.onInsertNode.publish(record);
},
@@ -257,17 +261,16 @@ _.constructor("Monarch.Model.Relations.Relation", {
},
tupleRemovedRemotely: function(record) {
- var position = _.indexOf(this._tuples, record);
- this._tuples.splice(position, 1);
+ this._tuples.remove(record);
this.onRemoveNode.publish(record);
},
contains: function(record) {
- var tuples = this.tuples();
- for(var i = 0; i < tuples.length; i++) {
- if (tuples[i] == record) return true;
+ if (this._tuples) {
+ return this._tuples.find(record) !== undefined;
+ } else {
+ return _.indexOf(this.tuples(), record) !== -1;
}
- return false;
},
subscribeToOperandsIfNeeded: function() {
@@ -308,7 +311,7 @@ _.constructor("Monarch.Model.Relations.Relation", {
var predicates = [];
_.each(hash, function(value, key) {
var column = this.column(key);
- if (!column) { debugger; throw new Error("No column named " + key + " found."); }
+ if (!column) { throw new Error("No column named " + key + " found."); }
predicates.push(column.eq(value))
}, this);
@@ -9,7 +9,9 @@ _.constructor("Monarch.Model.Relations.Selection", Monarch.Model.Relations.Relat
},
tuples: function() {
- if (this._tuples) return this._tuples;
+ if (this._tuples) {
+ return this._tuples.values();
+ }
return _.filter(this.operand.tuples(), function(tuple) {
return this.predicate.evaluate(tuple);
}, this);
@@ -8,14 +8,20 @@ _.constructor("Monarch.Model.Relations.Table", Monarch.Model.Relations.Relation,
this.recordConstructor = recordConstructor;
this.columnsByName = {};
this.syntheticColumnsByName = {};
- this._tuples = [];
+ this._tuples = this.buildSkipList();
this.tuplesById = {};
this.initializeEventsSystem();
this.onPauseEventsNode = new Monarch.SubscriptionNode();
this.onResumeEventsNode = new Monarch.SubscriptionNode();
},
+ comparator: function(a, b) {
+ if (a.id() < b.id()) return -1;
+ if (a.id() > b.id()) return 1;
+ return 0;
+ },
+
defineColumn: function(name, type) {
return this.columnsByName[name] = new Monarch.Model.Column(this, name, type);
},
@@ -44,14 +50,13 @@ _.constructor("Monarch.Model.Relations.Table", Monarch.Model.Relations.Relation,
this.tupleRemovedRemotely(record);
},
- tupleInsertedRemotely: function(record) {
- this._tuples.push(record);
+ tupleInsertedRemotely: function($super, record) {
this.tuplesById[record.id()] = record;
- this.onInsertNode.publish(record);
+ $super(record);
},
tuples: function() {
- return this._tuples.concat();
+ return this._tuples.values();
},
find: function(predicateOrId) {
@@ -88,7 +93,6 @@ _.constructor("Monarch.Model.Relations.Table", Monarch.Model.Relations.Relation,
return future;
},
-
column: function(name) {
return this.columnsByName[name] || this.syntheticColumnsByName[name];
},
@@ -154,7 +158,7 @@ _.constructor("Monarch.Model.Relations.Table", Monarch.Model.Relations.Relation,
},
clear: function() {
- this._tuples = [];
+ this._tuples = this.buildSkipList();
this.tuplesById = {}
this.onInsertNode = new Monarch.SubscriptionNode();
this.onRemoveNode = new Monarch.SubscriptionNode();
@@ -14,7 +14,7 @@ _.constructor("Monarch.Model.Relations.TableProjection", Monarch.Model.Relations
hasOperands: true,
tuples: function() {
- if (this._tuples) return this._tuples;
+ if (this._tuples) return this._tuples.values();
var tuples = [];
_.each(this.operand.tuples(), function(compositeTuple) {
@@ -8,7 +8,7 @@ _.constructor("Monarch.Model.Relations.Union", Monarch.Model.Relations.Relation,
},
tuples: function() {
- if (this._tuples) return this._tuples;
+ if (this._tuples) return this._tuples.values();
var tuplesByHashCode = {};
View
@@ -56,6 +56,12 @@ _.constructor("Monarch.SkipList", {
}
},
+ insertAll: function(array) {
+ _.each(array, function(elt) {
+ this.insert(elt);
+ }, this);
+ },
+
remove: function(key) {
var next = this.buildNextArray();
var nextDistance = this.buildNextDistanceArray();
@@ -1,7 +1,7 @@
//= require "../../../monarch_spec_helper"
Screw.Unit(function(c) { with(c) {
- describe("Relations.Selection", function() {
+ describe("Relations.InnerJoin", function() {
useLocalFixtures();
var user, blog1, blog2, post1, post2, post3;
@@ -79,56 +79,6 @@ Screw.Unit(function(c) { with(c) {
});
});
- describe("#onInsert", function() {
- it("returns a Monarch.Subscription with #onInsertNode as its #node", function() {
- var subscription = join.onInsert(function() {});
- expect(subscription.node).to(eq, join.onInsertNode);
- });
- });
-
- describe("#onUpdate", function() {
- it("returns a Monarch.Subscription with #onUpdateNode as its #node", function() {
- var subscription = join.onUpdate(function() {});
- expect(subscription.node).to(eq, join.onUpdateNode);
- });
- });
-
- describe("#onRemove", function() {
- it("returns a Monarch.Subscription with #onRemoveNode as its #node", function() {
- var subscription = join.onRemove(function() {});
- expect(subscription.node).to(eq, join.onRemoveNode);
- });
- });
-
- describe("#hasSubscribers", function() {
- context("if a callback has been registered with #onInsert", function() {
- it("returns true", function() {
- join.onInsert(function() {});
- expect(join.hasSubscribers()).to(beTrue);
- });
- });
-
- context("if a callback has been registered with #onRemove", function() {
- it("returns true", function() {
- join.onRemove(function() {});
- expect(join.hasSubscribers()).to(beTrue);
- });
- });
-
- context("if a callback has been registered with #onUpdate", function() {
- it("returns true", function() {
- join.onUpdate(function() {});
- expect(join.hasSubscribers()).to(beTrue);
- });
- });
-
- context("if no callbacks have been registered", function() {
- it("returns false", function() {
- expect(join.hasSubscribers()).to(beFalse);
- });
- });
- });
-
describe("event handling", function() {
var insertHandler, removeHandler, updateHandler;
before(function() {
@@ -176,12 +126,16 @@ Screw.Unit(function(c) { with(c) {
context("when a tuple is inserted into the right operand", function() {
context("when the insertion causes #carteseanProduct to contain a new CompositeTuple that matches the predicate", function() {
it("triggers #onInsert handlers with the new CompositeTuple", function() {
- blog1.blogPosts().create().afterEvents(function(blogPost) {
- expect(insertHandler).to(haveBeenCalled, once);
- var compositeTuple = insertHandler.mostRecentArgs[0];
- expect(compositeTuple.leftTuple).to(eq, blog1);
- expect(compositeTuple.rightTuple).to(eq, blogPost);
- });
+
+ console.debug(leftOperand.tuples());
+
+ var blogPost = blog1.blogPosts().createFromRemote()
+
+
+ expect(insertHandler).to(haveBeenCalled, once);
+ var compositeTuple = insertHandler.mostRecentArgs[0];
+ expect(compositeTuple.leftTuple).to(eq, blog1);
+ expect(compositeTuple.rightTuple).to(eq, blogPost);
});
});

0 comments on commit 58c6220

Please sign in to comment.