Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_pager.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2014 Joyent, Inc. All rights reserved.
  2. var EventEmitter = require('events').EventEmitter;
  3. var util = require('util');
  4. var assert = require('assert-plus');
  5. var dn = require('../dn');
  6. var messages = require('../messages/index');
  7. var Protocol = require('../protocol');
  8. var PagedControl = require('../controls/paged_results_control.js');
  9. ///--- API
  10. /**
  11. * Handler object for paged search operations.
  12. *
  13. * Provided to consumers in place of the normal search EventEmitter it adds the
  14. * following new events:
  15. * 1. page - Emitted whenever the end of a result page is encountered.
  16. * If this is the last page, 'end' will also be emitted.
  17. * The event passes two arguments:
  18. * 1. The result object (similar to 'end')
  19. * 2. A callback function optionally used to continue the search
  20. * operation if the pagePause option was specified during
  21. * initialization.
  22. * 2. pageError - Emitted if the server does not support paged search results
  23. * If there are no listeners for this event, the 'error' event
  24. * will be emitted (and 'end' will not be). By listening to
  25. * 'pageError', a successful search that lacks paging will be
  26. * able to emit 'end'.
  27. * 3. search - Emitted as an internal event to trigger another client search.
  28. */
  29. function SearchPager(opts) {
  30. assert.object(opts);
  31. assert.func(opts.callback);
  32. assert.number(opts.pageSize);
  33. EventEmitter.call(this, {});
  34. this.callback = opts.callback;
  35. this.controls = opts.controls;
  36. this.pageSize = opts.pageSize;
  37. this.pagePause = opts.pagePause;
  38. this.controls.forEach(function (control) {
  39. if (control.type === PagedControl.OID) {
  40. // The point of using SearchPager is not having to do this.
  41. // Toss an error if the pagedResultsControl is present
  42. throw new Error('redundant pagedResultControl');
  43. }
  44. });
  45. this.finished = false;
  46. this.started = false;
  47. var emitter = new EventEmitter();
  48. emitter.on('searchEntry', this.emit.bind(this, 'searchEntry'));
  49. emitter.on('end', this._onEnd.bind(this));
  50. emitter.on('error', this._onError.bind(this));
  51. this.childEmitter = emitter;
  52. }
  53. util.inherits(SearchPager, EventEmitter);
  54. module.exports = SearchPager;
  55. /**
  56. * Start the paged search.
  57. */
  58. SearchPager.prototype.begin = function begin() {
  59. // Starting first page
  60. this._nextPage(null);
  61. };
  62. SearchPager.prototype._onEnd = function _onEnd(res) {
  63. var self = this;
  64. var cookie = null;
  65. res.controls.forEach(function (control) {
  66. if (control.type === PagedControl.OID) {
  67. cookie = control.value.cookie;
  68. }
  69. });
  70. // Pass a noop callback by default for page events
  71. var nullCb = function () { };
  72. if (cookie === null) {
  73. // paged search not supported
  74. this.finished = true;
  75. this.emit('page', res, nullCb);
  76. var err = new Error('missing paged control');
  77. err.name = 'PagedError';
  78. if (this.listeners('pageError').length > 0) {
  79. this.emit('pageError', err);
  80. // If the consumer as subscribed to pageError, SearchPager is absolved
  81. // from deliverying the fault via the 'error' event. Emitting an 'end'
  82. // event after 'error' breaks the contract that the standard client
  83. // provides, so it's only a possibility if 'pageError' is used instead.
  84. this.emit('end', res);
  85. } else {
  86. this.emit('error', err);
  87. // No end event possible per explaination above.
  88. }
  89. return;
  90. }
  91. if (cookie.length === 0) {
  92. // end of paged results
  93. this.finished = true;
  94. this.emit('page', nullCb);
  95. this.emit('end', res);
  96. } else {
  97. if (this.pagePause) {
  98. // Wait to fetch next page until callback is invoked
  99. // Halt page fetching if called with error
  100. this.emit('page', res, function (err) {
  101. if (!err) {
  102. self._nextPage(cookie);
  103. } else {
  104. // the paged search has been canceled so emit an end
  105. self.emit('end', res);
  106. }
  107. });
  108. } else {
  109. this.emit('page', res, nullCb);
  110. this._nextPage(cookie);
  111. }
  112. }
  113. };
  114. SearchPager.prototype._onError = function _onError(err) {
  115. this.finished = true;
  116. this.emit('error', err);
  117. };
  118. /**
  119. * Initiate a search for the next page using the returned cookie value.
  120. */
  121. SearchPager.prototype._nextPage = function _nextPage(cookie) {
  122. var controls = this.controls.slice(0);
  123. controls.push(new PagedControl({
  124. value: {
  125. size: this.pageSize,
  126. cookie: cookie
  127. }
  128. }));
  129. this.emit('search', controls, this.childEmitter,
  130. this._sendCallback.bind(this));
  131. };
  132. /**
  133. * Callback provided to the client API for successful transmission.
  134. */
  135. SearchPager.prototype._sendCallback = function _sendCallback(err, res) {
  136. if (err) {
  137. this.finished = true;
  138. if (!this.started) {
  139. // EmitSend error during the first page, bail via callback
  140. this.callback(err, null);
  141. } else {
  142. this.emit('error', err);
  143. }
  144. } else {
  145. // search successfully send
  146. if (!this.started) {
  147. this.started = true;
  148. // send self as emitter as the client would
  149. this.callback(null, this);
  150. }
  151. }
  152. };