GroupDocumentSource.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. var DocumentSource = require("./DocumentSource"),
  2. Accumulators = require("../accumulators/"),
  3. Document = require("../Document"),
  4. Expression = require("../expressions/Expression"),
  5. ConstantExpression = require("../expressions/ConstantExpression"),
  6. FieldPathExpression = require("../expressions/FieldPathExpression"),
  7. GroupDocumentSource = module.exports = (function(){
  8. // CONSTRUCTOR
  9. /**
  10. * A class for grouping documents together
  11. *
  12. * @class GroupDocumentSource
  13. * @namespace munge.pipeline.documentsource
  14. * @module munge
  15. * @constructor
  16. * @param {ExpressionContext}
  17. **/
  18. var klass = module.exports = GroupDocumentSource = function GroupDocumentSource(){
  19. this.populated = false;
  20. this.idExpression = null;
  21. this.groups = {}; // GroupsType Value -> Accumulators[]
  22. this.groupsKeys = []; // This is to faciliate easier look up of groups
  23. this.fieldNames = [];
  24. this.accumulatorFactories = [];
  25. this.expressions = [];
  26. this.currentDocument = null;
  27. this.currentGroupsKeysIndex = 0;
  28. }, base = DocumentSource, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  29. klass.GroupOps = {
  30. "$addToSet": Accumulators.AddToSet,
  31. "$avg": Accumulators.Avg,
  32. "$first": Accumulators.First,
  33. "$last": Accumulators.Last,
  34. "$max": Accumulators.MinMax.bind(null, 1),
  35. "$min": Accumulators.MinMax.bind(null, -1),
  36. "$push": Accumulators.Push,
  37. "$sum": Accumulators.Sum
  38. };
  39. klass.createFromJson = function createFromJson(groupElement) {
  40. if(!(groupElement instanceof Object && groupElement.constructor.name === "Object") || Object.keys(groupElement).length < 1)
  41. throw new Error("a group's fields must be specified in an object");
  42. var idSet = false,
  43. group = new GroupDocumentSource(),
  44. groupObj = groupElement[group.getSourceName()];
  45. for(var groupFieldName in groupObj){
  46. if(groupObj.hasOwnProperty(groupFieldName)){
  47. var groupField = groupObj[groupFieldName];
  48. if(groupFieldName === "_id"){
  49. if(idSet) {
  50. throw new Error("15948 a group's _id may only be specified once");
  51. }
  52. if(groupField instanceof Object && groupField.constructor.name === "Object"){
  53. var objCtx = new Expression.ObjectCtx({isDocumentOk:true});
  54. group.idExpression = Expression.parseObject(groupField, objCtx);
  55. idSet = true;
  56. }else if( typeof groupField === "string"){
  57. if(groupField[0] !== "$") {
  58. group.idExpression = new ConstantExpression(groupField);
  59. }
  60. else {
  61. var pathString = Expression.removeFieldPrefix(groupField);
  62. group.idExpression = new FieldPathExpression(pathString);
  63. }
  64. idSet = true;
  65. }else{
  66. var typeStr = group._getTypeStr(groupField);
  67. switch(typeStr){
  68. case "number":
  69. case "string":
  70. case "boolean":
  71. case "Object":
  72. case "Array":
  73. group.idExpression = new ConstantExpression(groupField);
  74. idSet = true;
  75. break;
  76. default:
  77. throw new Error("a group's _id may not include fields of type " + typeStr + "");
  78. }
  79. }
  80. }else{
  81. if(groupFieldName.indexOf(".") !== -1)
  82. throw new Error("16414 the group aggregate field name '" + groupFieldName + "' cannot contain '.'");
  83. if(groupFieldName[0] === "$")
  84. throw new Error("15950 the group aggregate field name '" + groupFieldName + "' cannot be an operator name");
  85. if(group._getTypeStr(groupFieldName) === "Object")
  86. throw new Error("15951 the group aggregate field '" + groupFieldName + "' must be defined as an expression inside an object");
  87. var subFieldCount = 0;
  88. for(var subFieldName in groupField){
  89. if(groupField.hasOwnProperty(subFieldName)){
  90. var subField = groupField[subFieldName],
  91. op = klass.GroupOps[subFieldName];
  92. if(!op)
  93. throw new Error("15952 unknown group operator '" + subFieldName + "'");
  94. var groupExpression,
  95. subFieldTypeStr = group._getTypeStr(subField);
  96. if(subFieldTypeStr === "Object"){
  97. var subFieldObjCtx = new Expression.ObjectCtx({isDocumentOk:true});
  98. groupExpression = Expression.parseObject(subField, subFieldObjCtx);
  99. }else if(subFieldTypeStr === "Array"){
  100. throw new Error("15953 aggregating group operators are unary (" + subFieldName + ")");
  101. }else{
  102. groupExpression = Expression.parseOperand(subField);
  103. }
  104. group.addAccumulator(groupFieldName,op, groupExpression);
  105. ++subFieldCount;
  106. }
  107. }
  108. if(subFieldCount != 1)
  109. throw new Error("15954 the computed aggregate '" + groupFieldName + "' must specify exactly one operator");
  110. }
  111. }
  112. }
  113. if(!idSet) {
  114. throw new Error("15955 a group specification must include an _id");
  115. }
  116. return group;
  117. };
  118. proto._getTypeStr = function _getTypeStr(obj){
  119. var typeofStr=typeof obj,
  120. typeStr=(typeofStr == "object" ? obj.constructor.name : typeofStr);
  121. return typeStr;
  122. };
  123. proto.getSourceName = function getSourceName(){
  124. return "$group";
  125. };
  126. proto.advance = function advance(){
  127. base.prototype.advance.call(this); // Check for interupts ????
  128. if(!this.populated)
  129. this.populate();
  130. //verify(this.currentGroupsKeysIndex < this.groupsKeys.length);
  131. ++this.currentGroupsKeysIndex;
  132. if(this.currentGroupsKeysIndex === this.groupsKeys.length){
  133. this.currentDocument = null;
  134. return false;
  135. }
  136. this.currentDocument = this.makeDocument(this.currentGroupsKeysIndex);
  137. return true;
  138. };
  139. proto.eof = function eof(){
  140. if(!this.populated)
  141. this.populate();
  142. return this.currentGroupsKeysIndex === this.groupsKeys.length;
  143. };
  144. proto.getCurrent = function getCurrent(){
  145. if(!this.populated)
  146. this.populate();
  147. return this.currentDocument;
  148. };
  149. proto.addAccumulator = function addAccumulator(fieldName, accumulatorFactory, expression){
  150. this.fieldNames.push(fieldName);
  151. this.accumulatorFactories.push(accumulatorFactory);
  152. this.expressions.push(expression);
  153. };
  154. proto.populate = function populate(){
  155. for(var hasNext = !this.pSource.eof(); hasNext; hasNext = this.pSource.advance()){
  156. var group,
  157. currentDocument = this.pSource.getCurrent(),
  158. _id = this.idExpression.evaluate(currentDocument);
  159. if(undefined === _id) {
  160. _id = null;
  161. }
  162. var idHash = JSON.stringify(_id); //! @todo USE A REAL HASH. I didn't have time to take collision into account.
  163. if(_id in this.groups){
  164. group = this.groups[idHash];
  165. }else{
  166. this.groups[idHash] = group = [];
  167. this.groupsKeys[this.currentGroupsKeysIndex] = idHash;
  168. for(var ai =0; ai < this.accumulatorFactories.length; ++ai){
  169. var accumulator = new this.accumulatorFactories[ai]();
  170. accumulator.addOperand(this.expressions[ai]);
  171. group.push(accumulator);
  172. }
  173. }
  174. // tickle all the accumulators for the group we found
  175. for(var gi=0; gi < group.length; ++gi)
  176. group[gi].evaluate(currentDocument);
  177. this.currentGroupsKeysIndex = 0; // Start the group
  178. if(this.currentGroupsKeysIndex < this.groupsKeys.length)
  179. this.currentDocument = this.makeDocument(this.currentGroupsKeysIndex);
  180. this.populated = true;
  181. }
  182. };
  183. proto.makeDocument = function makeDocument(groupKeyIndex){
  184. var groupKey = this.groupsKeys[groupKeyIndex],
  185. group = this.groups[groupKey],
  186. doc = {};
  187. doc[Document.ID_PROPERTY_NAME] = JSON.parse(groupKey);
  188. for(var i = 0; i < this.fieldNames.length; ++i){
  189. var fieldName = this.fieldNames[i],
  190. idx = this.groupsKeys[i];
  191. if((idx !== "null") && (typeof idx !== "undefined")){
  192. var item = group[idx];
  193. doc[fieldName] = item.value;
  194. }
  195. }
  196. return doc;
  197. };
  198. return klass;
  199. })();