RedactDocumentSource.js 4.4 KB

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