RedactDocumentSource.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 !== DocumentSource.EOF;
  36. },
  37. function(cb) {
  38. self.source.getNext(function(err, input) {
  39. doc = input;
  40. if (input === DocumentSource.EOF)
  41. return cb();
  42. self._variables.setRoot(input);
  43. self._variables.setValue(self._currentId, input);
  44. var result = self.redactObject();
  45. if (result !== DocumentSource.EOF)
  46. return cb(result); //Using the err argument to pass the result document; this lets us break out without having EOF
  47. return cb();
  48. });
  49. },
  50. function(doc) {
  51. if (doc)
  52. return callback(null, doc);
  53. return callback(null, DocumentSource.EOF);
  54. }
  55. );
  56. return doc;
  57. };
  58. proto.redactValue = function redactValue(input) {
  59. // reorder to make JS happy with types
  60. if (input instanceof Array) {
  61. var newArr = [],
  62. arr = input;
  63. for (var i = 0; i < arr.length; i++) {
  64. if ((arr[i] instanceof Object && arr[i].constructor === Object) || arr[i] instanceof Array) {
  65. var toAdd = this.redactValue(arr[i]);
  66. if (toAdd)
  67. newArr.push(arr[i]);
  68. } else {
  69. newArr.push(arr[i]);
  70. }
  71. }
  72. return newArr;
  73. } else if (input instanceof Object && input.constructor === Object) {
  74. this._variables.setValue(this._currentId, input);
  75. var result = this.redactObject();
  76. if (result !== DocumentSource.EOF)
  77. return result;
  78. return null;
  79. } else {
  80. return input;
  81. }
  82. };
  83. /**
  84. * Redacts the current object
  85. **/
  86. proto.redactObject = function redactObject() {
  87. var expressionResult = this._expression.evaluate(this._variables);
  88. if (expressionResult === KEEP_VAL) {
  89. return this._variables.getDocument(this._currentId);
  90. } else if (expressionResult === PRUNE_VAL) {
  91. return DocumentSource.EOF;
  92. } else if (expressionResult === DESCEND_VAL) {
  93. var input = this._variables.getDocument(this._currentId);
  94. var out = {};
  95. var inputKeys = Object.keys(input);
  96. for (var i = 0; i < inputKeys.length; i++) {
  97. var field = inputKeys[i],
  98. value = input[field];
  99. var val = this.redactValue(value);
  100. if (val)
  101. out[field] = val;
  102. }
  103. return out;
  104. } else {
  105. throw new Error("17053 $redact's expression should not return anything aside from the variables $$KEEP, $$DESCEND, and $$PRUNE, but returned " + expressionResult);
  106. }
  107. };
  108. proto.optimize = function optimize() {
  109. this._expression = this._expression.optimize();
  110. };
  111. proto.serialize = function serialize(explain) {
  112. var doc = {};
  113. doc[this.getSourceName()] = this._expression.serialize(explain);
  114. return doc;
  115. };
  116. /**
  117. * Creates a new RedactDocumentSource with the input number as the skip
  118. *
  119. * @param {Number} JsonElement this thing is *called* Json, but it expects a number
  120. **/
  121. klass.createFromJson = function createFromJson(jsonElement, ctx) {
  122. if (!jsonElement)
  123. throw new Error("#createFromJson requires at least one argument");
  124. var idGenerator = new VariablesIdGenerator(),
  125. vps = new VariablesParseState(idGenerator),
  126. currentId = vps.defineVariable("CURRENT"),
  127. descendId = vps.defineVariable("DESCEND"),
  128. pruneId = vps.defineVariable("PRUNE"),
  129. keepId = vps.defineVariable("KEEP");
  130. var expression = new Expression.parseOperand(jsonElement, vps),
  131. source = new RedactDocumentSource(ctx, expression);
  132. source._currentId = currentId;
  133. source._variables = new Variables(idGenerator.getIdCount());
  134. source._variables.setValue(descendId, DESCEND_VAL);
  135. source._variables.setValue(pruneId, PRUNE_VAL);
  136. source._variables.setValue(keepId, KEEP_VAL);
  137. return source;
  138. };