Permalink
Browse files

Massively cleaned up inner join. Renamed Tuple#equals to Tuple#isEqual.

  • Loading branch information...
1 parent e4caad2 commit 56b2e60e0daaaf4ccf74e736b80df6d492275cf9 @nathansobo committed Jan 4, 2011
@@ -22,9 +22,13 @@ _.constructor("Monarch.Model.CompositeTuple", {
return this.leftTuple.record(table) || this.rightTuple.record(table);
},
- equals: function(other) {
+ isEqual: function(other) {
if (!other.constructor === this.constructor) return false;
- return this.leftTuple.equals(other.leftTuple) && this.rightTuple.equals(other.rightTuple);
+ return this.leftTuple.isEqual(other.leftTuple) && this.rightTuple.isEqual(other.rightTuple);
+ },
+
+ wireRepresentation: function() {
+ return [this.leftTuple.wireRepresentation(), this.rightTuple.wireRepresentation()]
}
});
@@ -301,7 +301,7 @@
this.subscriptions.destroy();
},
- equals: function(other) {
+ isEqual: function(other) {
return this === other;
},
@@ -48,84 +48,95 @@ _.constructor("Monarch.Model.Relations.InnerJoin", Monarch.Model.Relations.Relat
cartesianProduct: function() {
var product = [];
var self = this;
- _.each(self.leftOperand.tuples(), function(leftTuple) {
- _.each(self.rightOperand.tuples(), function(rightTuple) {
+ this.leftOperand.each(function(leftTuple) {
+ this.rightOperand.each(function(rightTuple) {
product.push(new Monarch.Model.CompositeTuple(leftTuple, rightTuple));
- });
- })
+ }, this);
+ }, this)
return product;
},
- subscribeToOperands: function() {
- var self = this;
- this.operandsSubscriptionBundle.add(this.leftOperand.onInsert(function(leftTuple) {
- _.each(self.rightOperand.tuples(), function(rightTuple) {
- var compositeTuple = new Monarch.Model.CompositeTuple(leftTuple, rightTuple);
- if (self.predicate.evaluate(compositeTuple)) self.tupleInsertedRemotely(compositeTuple);
- });
- }));
-
- this.operandsSubscriptionBundle.add(this.rightOperand.onInsert(function(rightTuple) {
- _.each(self.leftOperand.tuples(), function(leftTuple) {
- var compositeTuple = new Monarch.Model.CompositeTuple(leftTuple, rightTuple);
- if (self.predicate.evaluate(compositeTuple)) self.tupleInsertedRemotely(compositeTuple);
- });
- }));
-
- this.operandsSubscriptionBundle.add(this.leftOperand.onRemove(function(leftTuple) {
- _.each(self.tuples(), function(compositeTuple) {
- if (compositeTuple.leftTuple == leftTuple) self.tupleRemovedRemotely(compositeTuple);
- });
- }));
-
- this.operandsSubscriptionBundle.add(this.rightOperand.onRemove(function(rightTuple) {
- _.each(self.tuples(), function(compositeTuple) {
- if (compositeTuple.rightTuple == rightTuple) self.tupleRemovedRemotely(compositeTuple);
- });
- }));
-
- this.operandsSubscriptionBundle.add(this.leftOperand.onUpdate(function(leftTuple, changeset) {
- _.each(self.rightOperand.tuples(), function(rightTuple) {
- var newCompositeTuple = new Monarch.Model.CompositeTuple(leftTuple, rightTuple);
- var extantCompositeTuple = self.storedTuples.find(self.buildSortKey(newCompositeTuple, changeset));
-
- if (self.predicate.evaluate(newCompositeTuple)) {
- if (extantCompositeTuple) {
- self.tupleUpdatedRemotely(extantCompositeTuple, changeset);
- } else {
- self.tupleInsertedRemotely(newCompositeTuple);
- }
- } else {
- if (extantCompositeTuple) {
- self.tupleRemovedRemotely(extantCompositeTuple, changeset);
- }
- }
- });
- }));
-
- this.operandsSubscriptionBundle.add(this.rightOperand.onUpdate(function(rightTuple, changeset) {
- _.each(self.leftOperand.tuples(), function(leftTuple) {
- var newCompositeTuple = new Monarch.Model.CompositeTuple(leftTuple, rightTuple);
- var extantCompositeTuple = self.storedTuples.find(self.buildSortKey(newCompositeTuple, changeset));
-
- if (self.predicate.evaluate(newCompositeTuple)) {
- if (extantCompositeTuple) {
- self.tupleUpdatedRemotely(extantCompositeTuple, changeset);
- } else {
- self.tupleInsertedRemotely(newCompositeTuple);
- }
- } else {
- if (extantCompositeTuple) self.tupleRemovedRemotely(extantCompositeTuple, changeset);
- }
- })
- }));
- },
-
- findCompositeTupleThatMatches: function(compositeTuple1) {
- return _.detect(this.tuples(), function(compositeTuple2) {
- return compositeTuple1.equals(compositeTuple2);
- })
- }
+ onLeftOperandInsert: function(leftTuple) {
+ this.rightOperand.each(function(rightTuple) {
+ this.evaluateCompositeAfterOperandInsert(new Monarch.Model.CompositeTuple(leftTuple, rightTuple));
+ }, this);
+ },
+
+ onRightOperandInsert: function(rightTuple) {
+ this.leftOperand.each(function(leftTuple) {
+ this.evaluateCompositeAfterOperandInsert(new Monarch.Model.CompositeTuple(leftTuple, rightTuple));
+ }, this);
+ },
+
+ evaluateCompositeAfterOperandInsert: function(compositeTuple) {
+ if (this.predicate.evaluate(compositeTuple)) {
+ var sortKey = this.buildSortKey(compositeTuple);
+ this.tupleInsertedRemotely(compositeTuple, sortKey, sortKey);
+ }
+ },
+
+ onLeftOperandUpdate: function(leftTuple, changeset) {
+ this.rightOperand.each(function(rightTuple) {
+ this.evaluateCompositeAfterOperandUpdate(new Monarch.Model.CompositeTuple(leftTuple, rightTuple), changeset);
+ }, this);
+ },
+
+ onRightOperandUpdate: function(rightTuple, changeset) {
+ this.leftOperand.each(function(leftTuple) {
+ this.evaluateCompositeAfterOperandUpdate(new Monarch.Model.CompositeTuple(leftTuple, rightTuple), changeset);
+ }, this);
+ },
+
+ evaluateCompositeAfterOperandUpdate: function(compositeTuple, changeset) {
+ var newKey = this.buildSortKey(compositeTuple)
+ var oldKey = this.buildSortKey(compositeTuple, changeset);
+ var extantCompositeTuple = this.findByKey(oldKey);
+
+ if (this.predicate.evaluate(compositeTuple)) {
+ if (extantCompositeTuple) {
+ this.tupleUpdatedRemotely(extantCompositeTuple, changeset, newKey, oldKey);
+ } else {
+ this.tupleInsertedRemotely(compositeTuple, newKey, oldKey);
+ }
+ } else {
+ if (extantCompositeTuple) this.tupleRemovedRemotely(extantCompositeTuple, newKey, oldKey);
+ }
+ },
+
+ onLeftOperandRemove: function(leftTuple, newKey, oldKey) {
+ this.each(function(compositeTuple) {
+ if (compositeTuple.leftTuple.isEqual(leftTuple)) {
+ this.removeCompositeAfterOperandRemove(compositeTuple, oldKey)
+ }
+ }, this);
+ },
+
+ onRightOperandRemove: function(rightTuple, newKey, oldKey) {
+ this.each(function(compositeTuple) {
+ if (compositeTuple.rightTuple.isEqual(rightTuple)) {
+ this.removeCompositeAfterOperandRemove(compositeTuple, oldKey);
+ }
+ }, this);
+ },
+
+ removeCompositeAfterOperandRemove: function(compositeTuple, oldKey) {
+ var newCompositeKey = this.buildSortKey(compositeTuple);
+ var oldCompositeKey = _.extend({}, newCompositeKey, oldKey);
+ this.tupleRemovedRemotely(compositeTuple, newCompositeKey, oldCompositeKey);
+ },
+
+
+ onLeftOperandDirty: function() {},
+ onRightOperandDirty: function() {},
+
+ onLeftOperandClean: function() {},
+ onRightOperandClean: function() {},
+
+ onLeftOperandInvalid: function() {},
+ onRightOperandInvalid: function() {},
+
+ onLeftOperandValid: function() {},
+ onRightOperandValid: function() {}
});
})(Monarch);
@@ -349,21 +349,30 @@ _.constructor("Monarch.Model.Relations.Relation", {
},
subscribeToOperands: function() {
- if (this.numOperands === 1) {
- this.operandsSubscriptionBundle.add(this.operand.onInsert(this.hitch('onOperandInsert')));
- this.operandsSubscriptionBundle.add(this.operand.onUpdate(this.hitch('onOperandUpdate')));
- this.operandsSubscriptionBundle.add(this.operand.onRemove(this.hitch('onOperandRemove')));
- this.operandsSubscriptionBundle.add(this.operand.onDirty(this.hitch('onOperandDirty')));
- this.operandsSubscriptionBundle.add(this.operand.onClean(this.hitch('onOperandClean')));
- this.operandsSubscriptionBundle.add(this.operand.onInvalid(this.hitch('onOperandInvalid')));
- this.operandsSubscriptionBundle.add(this.operand.onValid(this.hitch('onOperandValid')));
- } else {
- throw new Error("Not implemented")
+ switch(this.numOperands) {
+ case 1:
+ this.subscribeToOperand(this.operand);
+ break;
+ case 2:
+ this.subscribeToOperand(this.leftOperand, 'left');
+ this.subscribeToOperand(this.rightOperand, 'right');
+ break;
+ default:
+ throw new Error("Don't understand that number of operands");
}
},
- onOperandInsert: function() {},
- onOperandUpdate: function() {},
+ // meta-programming to subscribe to all event types for a given operand. should expand to code like:
+ // this.operandsSubscriptionBundle.add(operand.onInsert(this.hitch('onLeftOperandInsert')));
+ subscribeToOperand: function(operand, leftOrRight) {
+ var events = ['insert', 'update', 'remove', 'dirty', 'clean', 'invalid', 'valid'];
+
+ _.each(events, function(event) {
+ var onEvent = 'on' + _.capitalize(event);
+ var onOperandEvent = _.camelize('on_' + (leftOrRight || "") + "_operand_" + event, true);
+ this.operandsSubscriptionBundle.add(operand[onEvent](this.hitch(onOperandEvent)));
+ }, this);
+ },
onOperandRemove: function(tuple, index, newKey, oldKey) {
if (this.findByKey(oldKey)) this.tupleRemovedRemotely(tuple, newKey, oldKey);
@@ -39,9 +39,6 @@ _.constructor("Monarch.Model.Relations.Selection", Monarch.Model.Relations.Relat
},
evaluateInRepository: function(repository) {
-
- if (!this.operand.evaluateInRepository(repository)) debugger;
-
return new Monarch.Model.Relations.Selection(this.operand.evaluateInRepository(repository), this.predicate);
},
@@ -39,6 +39,11 @@ _.constructor("Monarch.Model.Tuple", {
digestInput.push(key, this.field(key).valueWireRepresentation());
}, this);
return b64_md5(digestInput.join(""));
+ },
+
+ isEqual: function(other) {
+ if (!other.constructor === this.constructor) return false;
+ return this.hashCode === other.hashCode();
}
});
Oops, something went wrong.

0 comments on commit 56b2e60

Please sign in to comment.