Code coverage report for kobold-core/lib/connectionAdapter/riakConnectionAdapter.js

Statements: 100% (43 / 43)      Branches: 100% (12 / 12)      Functions: 100% (28 / 28)      Lines: 100% (43 / 43)      Ignored: none     

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