GroupDocumentSource.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.createMax,
  35. "$min": Accumulators.MinMax.createMin,
  36. "$push": Accumulators.Push,
  37. "$sum": Accumulators.Sum
  38. };
  39. klass.createFromJson = function createFromJson(groupObj) {
  40. if(!(groupObj instanceof Object && groupObj.constructor.name === "Object"))
  41. throw new Error("a group's fields must be specified in an object");
  42. var idSet = false,
  43. group = new GroupDocumentSource();
  44. for(var groupFieldName in groupObj){
  45. if(groupObj.hasOwnProperty(groupFieldName)){
  46. var groupField = groupObj[groupFieldName];
  47. if(groupFieldName === "_id"){
  48. if(idSet) {
  49. throw new Error("15948 a group's _id may only be specified once");
  50. }
  51. if(groupField instanceof Object && groupField.constructor.name === "Object"){
  52. var objCtx = new Expression.ObjectCtx({isDocumentOk:true});
  53. group.idExpression = Expression.parseObject(groupField, objCtx);
  54. idSet = true;
  55. }else if( typeof groupField === "string"){
  56. if(groupField[0] !== "$") {
  57. group.idExpression = new ConstantExpression(groupField);
  58. }
  59. else {
  60. var pathString = Expression.removeFieldPrefix(groupField);
  61. group.idExpression = new FieldPathExpression(pathString);
  62. }
  63. idSet = true;
  64. }else{
  65. var typeStr = group._getTypeStr(groupField);
  66. switch(typeStr){
  67. case "number":
  68. case "string":
  69. case "boolean":
  70. case "Object":
  71. case "Array":
  72. group.idExpression = new ConstantExpression(groupField);
  73. idSet = true;
  74. break;
  75. default:
  76. throw new Error("a group's _id may not include fields of type " + typeStr + "");
  77. }
  78. }
  79. }else{
  80. if(groupFieldName.indexOf(".") !== -1)
  81. throw new Error("16414 the group aggregate field name '" + groupFieldName + "' cannot contain '.'");
  82. if(groupFieldName[0] === "$")
  83. throw new Error("15950 the group aggregate field name '" + groupFieldName + "' cannot be an operator name");
  84. if(group._getTypeStr(groupFieldName) === "Object")
  85. throw new Error("15951 the group aggregate field '" + groupFieldName + "' must be defined as an expression inside an object");
  86. var subFieldCount = 0;
  87. for(var subFieldName in groupField){
  88. if(groupField.hasOwnProperty(subFieldName)){
  89. var subField = groupField[subFieldName],
  90. op = klass.GroupOps[subFieldName];
  91. if(!op)
  92. throw new Error("15952 unknown group operator '" + subFieldName + "'");
  93. var groupExpression,
  94. subFieldTypeStr = group._getTypeStr(subField);
  95. if(subFieldTypeStr === "Object"){
  96. var subFieldObjCtx = new Expression.ObjectCtx({isDocumentOk:true});
  97. groupExpression = Expression.parseObject(subField, subFieldObjCtx);
  98. }else if(subFieldTypeStr === "Array"){
  99. throw new Error("15953 aggregating group operators are unary (" + subFieldName + ")");
  100. }else{
  101. groupExpression = Expression.parseOperand(subField);
  102. }
  103. group.addAccumulator(groupFieldName,op, groupExpression);
  104. ++subFieldCount;
  105. }
  106. }
  107. if(subFieldCount != 1)
  108. throw new Error("15954 the computed aggregate '" + groupFieldName + "' must specify exactly one operator");
  109. }
  110. }
  111. }
  112. if(!idSet) {
  113. throw new Error("15955 a group specification must include an _id");
  114. }
  115. return group;
  116. };
  117. proto._getTypeStr = function _getTypeStr(obj){
  118. var typeofStr=typeof obj,
  119. typeStr=(typeofStr == "object" ? obj.constructor.name : typeofStr);
  120. return typeStr;
  121. };
  122. klass.groupName = "$group";
  123. proto.getSourceName = function getSourceName(){
  124. return klass.groupName;
  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(idHash in this.groups){
  164. group = this.groups[idHash];
  165. }else{
  166. this.groups[idHash] = group = [];
  167. this.groupsKeys[this.currentGroupsKeysIndex] = idHash;
  168. ++this.currentGroupsKeysIndex;
  169. for(var ai =0; ai < this.accumulatorFactories.length; ++ai){
  170. var accumulator = new this.accumulatorFactories[ai]();
  171. accumulator.addOperand(this.expressions[ai]);
  172. group.push(accumulator);
  173. }
  174. }
  175. // tickle all the accumulators for the group we found
  176. for(var gi=0; gi < group.length; ++gi)
  177. group[gi].evaluate(currentDocument);
  178. }
  179. this.currentGroupsKeysIndex = 0; // Start the group
  180. if(this.groupsKeys.length > 0)
  181. this.currentDocument = this.makeDocument(this.currentGroupsKeysIndex);
  182. this.populated = true;
  183. };
  184. proto.makeDocument = function makeDocument(groupKeyIndex){
  185. var groupKey = this.groupsKeys[groupKeyIndex],
  186. group = this.groups[groupKey],
  187. doc = {};
  188. doc[Document.ID_PROPERTY_NAME] = JSON.parse(groupKey);
  189. for(var i = 0; i < this.fieldNames.length; ++i){
  190. var fieldName = this.fieldNames[i],
  191. item = group[i];
  192. if((item !== "null") && (typeof item !== "undefined")){
  193. doc[fieldName] = item.getValue();
  194. }
  195. }
  196. return doc;
  197. };
  198. /**
  199. * Reset the document source so that it is ready for a new stream of data.
  200. * Note that this is a deviation from the mongo implementation.
  201. *
  202. * @method reset
  203. **/
  204. proto.reset = function reset(){
  205. this.populated = false;
  206. this.groups = [];
  207. this.groupsKeys = [];
  208. this.currentDocument = null;
  209. this.currentGroupsKeysIndex = 0;
  210. };
  211. return klass;
  212. })();