API Docs for: 0.9.0
Show:

File: lib/connectionAdapter/riakConnectionAdapter.js

  1. // Copyright 2014, Yahoo! Inc.
  2. // Copyrights licensed under the Mit License. See the accompanying LICENSE file for terms.
  3.  
  4. var Promise = require('promise');
  5. var ConnectionAdapter = require('./connectionAdapter');
  6.  
  7. /**
  8. * Riak accessor class
  9. *
  10. * @class RiakConnectionAdapter
  11. * @extends ConnectionAdapter
  12. * @param {object} [options]
  13. * @param {object} [options.schema='http']
  14. * @param {object} [options.host='localhost']
  15. * @param {object} [options.port=8098]
  16. * @constructor
  17. *
  18. * @property {string} _schema
  19. * @property {string} _host
  20. * @property {int} _port
  21. */
  22. var RiakConnectionAdapter = ConnectionAdapter.extend(
  23.  
  24. {
  25. /**
  26. * Initializes the source-adapter
  27. *
  28. * @method initialize
  29. */
  30. initialize: function () {
  31. this.__super();
  32.  
  33. this._schema = this._options.schema || 'http';
  34. this._host = this._options.host || 'localhost';
  35. this._port = this._options.port || 8098;
  36. },
  37.  
  38.  
  39. /**
  40. * Gets the complete url for a path specified
  41. *
  42. * @method _getUrlWithPath
  43. * @param {string} path
  44. * @return {string}
  45. * @private
  46. */
  47. _getUrlWithPath: function (path) {
  48. return this._schema + '://' + this._host + ':' + this._port + path;
  49. },
  50.  
  51. /**
  52. * Gets all bucket names
  53. *
  54. * @method getBuckets
  55. * @param {function} [filterFn]
  56. * @return {Promise} With {string[]} List of buckets
  57. */
  58. getBuckets: function (filterFn) {
  59. return this.getPromise().then(function () {
  60. return this._request({
  61. method: 'GET',
  62. url: this._getUrlWithPath('/buckets?buckets=true')
  63. }).then(function (result) {
  64. return JSON.parse(result.body)['buckets'];
  65. }).then(function (result) {
  66. if (filterFn) {
  67. return result.filter(filterFn);
  68. } else {
  69. return result;
  70. }
  71. });
  72. }.bind(this));
  73. },
  74.  
  75. /**
  76. * Does the instance have a specific bucket?
  77. *
  78. * @method hasBucket
  79. * @param {string} bucket
  80. * @param {function} [filterFn]
  81. * @return {Promise} With {boolean} Bucket exists?
  82. */
  83. hasBucket: function (bucket, filterFn) {
  84. return this.getBuckets(filterFn).then(function (buckets) {
  85. return (buckets.indexOf(bucket) !== -1);
  86. });
  87. },
  88.  
  89. /**
  90. * Gets all bucket keys
  91. *
  92. * @method getBucketKeys
  93. * @param {string} bucket
  94. * @param {function} [filterFn]
  95. * @return {Promise} With {string[]} Bucket keys
  96. */
  97. getBucketKeys: function (bucket, filterFn) {
  98. return this.getPromise().then(function () {
  99. return this._request({
  100. method: 'GET',
  101. url: this._getUrlWithPath('/buckets/' + encodeURIComponent(bucket) + '/keys?keys=true')
  102. }).then(function (result) {
  103. return JSON.parse(result.body)['keys'];
  104. }).then(function (result) {
  105. if (filterFn) {
  106. return result.filter(filterFn);
  107. } else {
  108. return result;
  109. }
  110. });
  111. }.bind(this));
  112. },
  113.  
  114. /**
  115. * Does the instance have a specific key in a bucket?
  116. *
  117. * @method hasBucketKey
  118. * @param {string} bucket
  119. * @param {string} key
  120. * @param {function} [filterFn]
  121. * @return {Promise} With {boolean} Bucket key exists?
  122. */
  123. hasBucketKey: function (bucket, key, filterFn) {
  124. return this.getBucketKeys(bucket, filterFn).then(function (keys) {
  125. return (keys.indexOf(key) !== -1);
  126. });
  127. },
  128.  
  129. /**
  130. * Gets an object
  131. *
  132. * @method getObject
  133. * @param {string} bucket
  134. * @param {string} key
  135. * @return {Promise} With {Buffer}
  136. */
  137. getObject: function (bucket, key) {
  138. return this.getPromise().then(function () {
  139. return this._request({
  140. method: 'GET',
  141. url: this._getUrlWithPath('/buckets/' + encodeURIComponent(bucket) + '/keys/' + encodeURIComponent(key))
  142. }).then(function (response) {
  143. return response.body;
  144. });
  145. }.bind(this));
  146. },
  147.  
  148. /**
  149. * Gets an object as JSON
  150. *
  151. * @method getObjectAsJSON
  152. * @param {string} bucket
  153. * @param {string} key
  154. * @return {Promise} With {*}
  155. */
  156. getObjectAsJSON: function (bucket, key) {
  157. return this.getObject(bucket, key).then(function (result) {
  158. return JSON.parse(result);
  159. });
  160. },
  161.  
  162.  
  163. /**
  164. * Sets an object
  165. *
  166. * @method setObject
  167. * @param {string} bucket
  168. * @param {string} key
  169. * @param {Buffer|string} data
  170. * @param {string} [mimeType='application/octet-stream']
  171. * @return {Promise}
  172. */
  173. setObject: function (bucket, key, data, mimeType) {
  174. mimeType = mimeType || 'application/octet-stream';
  175. return this.getPromise().then(function () {
  176. return this._request({
  177. method: 'PUT',
  178. url: this._getUrlWithPath('/buckets/' + encodeURIComponent(bucket) + '/keys/' + encodeURIComponent(key)),
  179. headers: {
  180. "Content-Type": mimeType
  181. },
  182. body: data
  183. });
  184. }.bind(this));
  185. },
  186.  
  187. /**
  188. * Sets an object from JSON
  189. *
  190. * @method setObjectFromJSON
  191. * @param {string} bucket
  192. * @param {string} key
  193. * @param {*} data
  194. * @return {Promise}
  195. */
  196. setObjectFromJSON: function (bucket, key, data) {
  197. return this.setObject(bucket, key, JSON.stringify(data), 'application/json');
  198. },
  199.  
  200.  
  201. /**
  202. * Removes an object
  203. *
  204. * @method removeObject
  205. * @param {string} bucket
  206. * @param {string} key
  207. * @return {Promise}
  208. */
  209. removeObject: function (bucket, key) {
  210. return this.getPromise().then(function () {
  211. return this._request({
  212. method: 'DELETE',
  213. url: this._getUrlWithPath('/buckets/' + encodeURIComponent(bucket) + '/keys/' + encodeURIComponent(key))
  214. });
  215. }.bind(this));
  216. },
  217.  
  218. /**
  219. * Removes all object of a bucket
  220. *
  221. * @method removeAllObjects
  222. * @param {string} bucket
  223. * @return {Promise}
  224. */
  225. removeAllObjects: function (bucket) {
  226. return this.getBucketKeys(bucket).then(function (keys) {
  227. var i, len,
  228. promise = Promise.resolve();
  229.  
  230. for (i = 0, len = keys.length; i < len; i++) {
  231. (function (i) {
  232. promise = promise.then(function () {
  233. return this.removeObject(bucket, keys[i]);
  234. }.bind(this));
  235. }.bind(this)(i));
  236. }
  237.  
  238. return promise;
  239. }.bind(this));
  240. }
  241. },
  242.  
  243. {
  244. /**
  245. * Type of class
  246. *
  247. * @property TYPE
  248. * @type string
  249. */
  250. TYPE: 'RiakConnectionAdapter'
  251. });
  252.  
  253. module.exports = RiakConnectionAdapter;
  254.