API Docs for: 2.2.2
Show:

File: lib/commands/kv/fetchpropsbase.js

  1. 'use strict';
  2.  
  3. var CommandBase = require('../commandbase');
  4. var inherits = require('util').inherits;
  5.  
  6. /**
  7. * Base class for FetchBucketProps and FetchBucketTypeProps classes.
  8. * @module KV
  9. */
  10.  
  11. /**
  12. * @class FetchPropsBase
  13. * @constructor
  14. * @param {String} pbRequestName name of the Riak protocol buffer this command will send
  15. * @param {String} pbResponseName name of the Riak protocol buffer this command will receive
  16. * @param {Function} callback The callback to be executed when the operation completes.
  17. * @param {String} callback.err An error message. Will ne null if no error.
  18. * @param {Object} callback.response The response from Riak. This is an oject with all the bucket properties.
  19. * @param {Object} callback.data additional error data. Will be null if no error.
  20. * @extends CommandBase
  21. */
  22. function FetchPropsBase(pbRequestName, pbResponseName, callback) {
  23. CommandBase.call(this, pbRequestName, pbResponseName, callback);
  24. }
  25.  
  26. inherits(FetchPropsBase, CommandBase);
  27.  
  28. FetchPropsBase.prototype.onSuccess = function(rpbGetBucketResp) {
  29.  
  30. var rpbBucketProps = rpbGetBucketResp.getProps();
  31.  
  32. var bucketProps = {
  33.  
  34. nVal : rpbBucketProps.getNVal(),
  35. allowMult : rpbBucketProps.getAllowMult(),
  36. lastWriteWins: rpbBucketProps.getLastWriteWins(),
  37. hasPrecommit : rpbBucketProps.getHasPrecommit(),
  38. hasPostcommit: rpbBucketProps.getHasPostcommit(),
  39. oldVClock : rpbBucketProps.getOldVclock(),
  40. youngVClock: rpbBucketProps.getYoungVclock(),
  41. bigVClock : rpbBucketProps.getBigVclock(),
  42. smallVClock: rpbBucketProps.getSmallVclock(),
  43. pr: rpbBucketProps.getPr(),
  44. r: rpbBucketProps.getR(),
  45. w: rpbBucketProps.getW(),
  46. pw: rpbBucketProps.getPw(),
  47. dw: rpbBucketProps.getDw(),
  48. rw: rpbBucketProps.getRw(),
  49. basicQuorum: rpbBucketProps.getBasicQuorum(),
  50. notFoundOk: rpbBucketProps.getNotfoundOk(),
  51. search: rpbBucketProps.getSearch(),
  52. consistent: rpbBucketProps.getConsistent(),
  53. repl: rpbBucketProps.getRepl()
  54. };
  55.  
  56. var backend;
  57. if ((backend = rpbBucketProps.getBackend()) !== null) {
  58. bucketProps.backend = backend.toString('utf8');
  59. }
  60.  
  61. var searchIndex;
  62. if ((searchIndex = rpbBucketProps.getSearchIndex()) !== null) {
  63. bucketProps.searchIndex = searchIndex.toString('utf8');
  64. }
  65.  
  66. var dataType;
  67. if ((dataType = rpbBucketProps.getDatatype()) !== null) {
  68. bucketProps.dataType = dataType.toString('utf8');
  69. }
  70.  
  71. if (rpbBucketProps.getHasPrecommit()) {
  72. bucketProps.precommit = this._parseHooks(rpbBucketProps.getPrecommit());
  73. } else {
  74. bucketProps.precommit = [];
  75. }
  76.  
  77. if (rpbBucketProps.getHasPostcommit()) {
  78. bucketProps.postcommit = this._parseHooks(rpbBucketProps.getPostcommit());
  79. } else {
  80. bucketProps.postcommit = [];
  81. }
  82.  
  83. var chashKeyFun;
  84. if ((chashKeyFun = rpbBucketProps.getChashKeyfun()) !== null) {
  85. bucketProps.chashKeyfun = { mod: chashKeyFun.module.toString('utf8'),
  86. fun: chashKeyFun.function.toString('utf8') };
  87. }
  88.  
  89. var linkfun;
  90. if ((linkfun = rpbBucketProps.getLinkfun()) !== null) {
  91. bucketProps.linkFun = { mod: linkfun.module.toString('utf8'),
  92. fun: linkfun.function.toString('utf8') };
  93. }
  94.  
  95. this._callback(null, bucketProps);
  96. return true;
  97.  
  98. };
  99.  
  100. FetchPropsBase.prototype._parseHooks = function(rpbCommitHooks) {
  101. var hooks = new Array(rpbCommitHooks.length);
  102. for (var i = 0; i < rpbCommitHooks.length; i++) {
  103. if (rpbCommitHooks[i].name) {
  104. hooks[i] = { name: rpbCommitHooks[i].name.toString('utf8') };
  105. } else {
  106. hooks[i] = { mod: rpbCommitHooks[i].modfun.module.toString('utf8'),
  107. fun: rpbCommitHooks[i].modfun.function.toString('utf8') };
  108. }
  109. }
  110. return hooks;
  111. };
  112.  
  113. function Builder() {}
  114.  
  115. /**
  116. * Set the bucket type.
  117. * If not supplied, 'default' is used.
  118. * @method withBucketType
  119. * @param {String} bucketType the bucket type in riak
  120. * @chainable
  121. */
  122. Builder.prototype.withBucketType = function(bucketType) {
  123. this.bucketType = bucketType;
  124. return this;
  125. };
  126.  
  127. /**
  128. * Set the callback to be executed when the operation completes.
  129. * @method withCallback
  130. * @param {Function} callback The callback to be executed when the operation completes.
  131. * @param {String} callback.err An error message. Will ne null if no error.
  132. * @param {Object} callback.response The response from Riak. This is an oject with all the bucket properties.
  133. * @chainable
  134. */
  135. Builder.prototype.withCallback = function(callback) {
  136. this.callback = callback;
  137. return this;
  138. };
  139.  
  140. module.exports = FetchPropsBase;
  141. module.exports.Builder = Builder;
  142.  
  143.