RedactDocumentSource.js 4.6 KB

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