RedactDocumentSource.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. var async = require("async"),
  3. Expression = require("../expressions/Expression"),
  4. Variables = require("../expressions/Variables"),
  5. VariablesIdGenerator = require("../expressions/VariablesIdGenerator"),
  6. VariablesParseState = require("../expressions/VariablesParseState");
  7. /**
  8. * A document source skipper
  9. * @class RedactDocumentSource
  10. * @namespace mungedb-aggregate.pipeline.documentSources
  11. * @module mungedb-aggregate
  12. * @constructor
  13. * @param [ctx] {ExpressionContext}
  14. **/
  15. var RedactDocumentSource = module.exports = function RedactDocumentSource(ctx, expression){
  16. if (arguments.length > 2) throw new Error("up to two args expected");
  17. base.call(this, ctx);
  18. this._expression = expression;
  19. this._variables = new Variables();
  20. this._currentId = null;
  21. }, klass = RedactDocumentSource, base = require("./DocumentSource"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}}); //jshint ignore:line
  22. klass.redactName = "$redact";
  23. proto.getSourceName = function getSourceName(){
  24. return klass.redactName;
  25. };
  26. var DESCEND_VAL = "descend",
  27. PRUNE_VAL = "prune",
  28. KEEP_VAL = "keep";
  29. proto.getNext = function getNext(callback) {
  30. var self = this,
  31. doc;
  32. async.whilst(
  33. function() {
  34. return doc !== null;
  35. },
  36. function(cb) {
  37. self.source.getNext(function(err, input) {
  38. doc = input;
  39. if (input === null)
  40. return cb();
  41. var result;
  42. try {
  43. self._variables.setRoot(input);
  44. self._variables.setValue(self._currentId, input);
  45. result = self.redactObject();
  46. } catch (ex) {
  47. return cb(ex);
  48. }
  49. if (result !== null)
  50. return cb(result); //Using the err argument to pass the result document; this lets us break out without having EOF
  51. return cb();
  52. });
  53. },
  54. function(doc) {
  55. if (doc){
  56. if (doc instanceof Error) return callback(doc);
  57. else return callback(null, doc);
  58. }
  59. return callback(null, null);
  60. }
  61. );
  62. return doc;
  63. };
  64. proto.redactValue = function redactValue(input) {
  65. // reorder to make JS happy with types
  66. if (input instanceof Array) {
  67. var newArr = [],
  68. arr = input;
  69. for (var i = 0; i < arr.length; i++) {
  70. if ((arr[i] instanceof Object && arr[i].constructor === Object) || arr[i] instanceof Array) {
  71. var toAdd = this.redactValue(arr[i]);
  72. if (toAdd)
  73. newArr.push(arr[i]);
  74. } else {
  75. newArr.push(arr[i]);
  76. }
  77. }
  78. return newArr;
  79. } else if (input instanceof Object && input.constructor === Object) {
  80. this._variables.setValue(this._currentId, input);
  81. var result = this.redactObject();
  82. if (result !== null)
  83. return result;
  84. return null;
  85. } else {
  86. return input;
  87. }
  88. };
  89. /**
  90. * Redacts the current object
  91. **/
  92. proto.redactObject = function redactObject() {
  93. var expressionResult = this._expression.evaluate(this._variables);
  94. if (expressionResult === KEEP_VAL) {
  95. return this._variables.getDocument(this._currentId);
  96. } else if (expressionResult === PRUNE_VAL) {
  97. return null;
  98. } else if (expressionResult === DESCEND_VAL) {
  99. var input = this._variables.getDocument(this._currentId);
  100. var out = {};
  101. var inputKeys = Object.keys(input);
  102. for (var i = 0; i < inputKeys.length; i++) {
  103. var field = inputKeys[i],
  104. value = input[field];
  105. var val = this.redactValue(value);
  106. if (val)
  107. out[field] = val;
  108. }
  109. return out;
  110. } else {
  111. throw new Error("$redact's expression should not return anything " +
  112. "aside from the variables $$KEEP, $$DESCEND, and " +
  113. "$$PRUNE, but returned " + expressionResult + "; uasserted code 17053");
  114. }
  115. };
  116. proto.optimize = function optimize() {
  117. this._expression = this._expression.optimize();
  118. };
  119. proto.serialize = function serialize(explain) {
  120. var doc = {};
  121. doc[this.getSourceName()] = this._expression.serialize(explain);
  122. return doc;
  123. };
  124. /**
  125. * Creates a new RedactDocumentSource with the input number as the skip
  126. *
  127. * @param {Number} JsonElement this thing is *called* Json, but it expects a number
  128. **/
  129. klass.createFromJson = function createFromJson(jsonElement, ctx) {
  130. if (!jsonElement)
  131. throw new Error("#createFromJson requires at least one argument");
  132. var idGenerator = new VariablesIdGenerator(),
  133. vps = new VariablesParseState(idGenerator),
  134. currentId = vps.defineVariable("CURRENT"),
  135. descendId = vps.defineVariable("DESCEND"),
  136. pruneId = vps.defineVariable("PRUNE"),
  137. keepId = vps.defineVariable("KEEP");
  138. var expression = new Expression.parseOperand(jsonElement, vps),
  139. source = new RedactDocumentSource(ctx, expression);
  140. source._currentId = currentId;
  141. source._variables = new Variables(idGenerator.getIdCount());
  142. source._variables.setValue(descendId, DESCEND_VAL);
  143. source._variables.setValue(pruneId, PRUNE_VAL);
  144. source._variables.setValue(keepId, KEEP_VAL);
  145. return source;
  146. };