Permalink
Browse files

replace bucket and key for a path Array - still experimenting

  • Loading branch information...
Francisco Treacy
Francisco Treacy committed Nov 22, 2011
1 parent e1f9a7f commit 9da4969a817084565b3f0727919d7f998a3eb9c8
Showing with 57 additions and 37 deletions.
  1. +17 −14 lib/http-client.js
  2. +27 −7 lib/http-meta.js
  3. +3 −4 lib/meta.js
  4. +9 −11 test/http-meta-test.js
  5. +1 −1 test/meta-test.js
View
@@ -17,18 +17,18 @@ util.inherits(HttpClient, Client);
// basic
HttpClient.prototype.get = function(bucket, key, options, callback) {
- var meta = new Meta(this._defaults, options, { bucket: bucket, key: key, method: 'get', callback: callback });
+ var meta = new Meta(this._defaults, options, { path: [bucket, key], method: 'get', callback: callback });
this._execute(meta);
}
HttpClient.prototype.head = function(bucket, key, options, callback) {
- var meta = new Meta(this._defaults, options, { bucket: bucket, key: key, method: 'head', callback: callback });
+ var meta = new Meta(this._defaults, options, { path: [bucket, key], method: 'head', callback: callback });
this._execute(meta);
}
HttpClient.prototype.exists = function(bucket, key, options, callback) {
- var meta = new Meta(this._defaults, options, { bucket: bucket, key: key, callback: callback });
+ var meta = new Meta(this._defaults, options, { path: [bucket, key], callback: callback });
var callback = meta.callback;
this.head(bucket, key, meta, function(err, data, meta) {
@@ -45,13 +45,13 @@ HttpClient.prototype.exists = function(bucket, key, options, callback) {
HttpClient.prototype.save = function(bucket, key, data, options, callback) {
var method = key ? 'put' : 'post',
- meta = new Meta(this._defaults, { method: method }, options, { bucket: bucket, key: key, callback: callback });
+ meta = new Meta(this._defaults, { method: method }, options, { path: [bucket, key], callback: callback });
meta.data = data;
this._execute(meta);
}
HttpClient.prototype.remove = function(bucket, key, options, callback) {
- var meta = new Meta(this._defaults, options, { bucket: bucket, key: key, method: 'delete', callback: callback });
+ var meta = new Meta(this._defaults, options, { path: [bucket, key], method: 'delete', callback: callback });
this._execute(meta);
}
@@ -89,7 +89,7 @@ HttpClient.prototype.buckets = function(options, callback) {
}
HttpClient.prototype.getBucket = function(bucket, options, callback) {
- var meta = new Meta(this._defaults, options, { bucket: bucket, callback: callback }),
+ var meta = new Meta(this._defaults, options, { path: [bucket], callback: callback }),
callback = meta.callback;
meta.callback = function(err, data, _meta) { callback(err, data.props, _meta) }
this._execute(meta);
@@ -103,7 +103,7 @@ HttpClient.prototype.saveBucket = function(bucket, props, options, callback) {
// keys
HttpClient.prototype.keys = function(bucket, options, callback) {
- var meta = new Meta(this._defaults, options, { bucket: bucket, keys: 'stream', props: false, callback: callback }),
+ var meta = new Meta(this._defaults, options, { path: [bucket], keys: 'stream', props: false, callback: callback }),
callback = meta.callback;
meta.callback = function(err, data, _meta) {
@@ -153,7 +153,7 @@ HttpClient.prototype.removeFile = function(key, options, callback) {
// 2i
HttpClient.prototype.query = function(bucket, query, options, callback) {
- var meta = new Meta(this._defaults, options, { resource: 'buckets', encodeUri: false, callback: callback });
+ var meta = new Meta(this._defaults, options, { resource: 'buckets', callback: callback });
var field = Object.keys(query)[0],
value = query[field],
@@ -165,14 +165,17 @@ HttpClient.prototype.query = function(bucket, query, options, callback) {
}
var type = typeof value == 'number' ? 'int' : 'bin',
- key = "index/" + field + "_" + type + "/" + encodeURIComponent(value);
+ path = [bucket, 'index', field + "_" + type, value, end];
- if (end) key += "/" + encodeURIComponent(end);
+ meta.path = path;
- this.get(bucket, key, meta, function(err, data, _meta) {
+ var callback = meta.callback;
+ meta.callback = function(err, data, _meta) {
data = data ? data.keys : undefined;
- meta.callback(err, data, _meta);
- });
+ callback(err, data, _meta);
+ };
+
+ this._execute(meta);
}
@@ -308,7 +311,7 @@ HttpClient.prototype._execute = function(meta) {
// deal with errors
if (meta.statusCode >= 400) {
var err = new Error();
- err.message = buffer && buffer.trim();
+ err.message = buffer && buffer.toString().trim();
err.statusCode = meta.statusCode;
buffer = err;
}
View
@@ -28,8 +28,7 @@ HttpMeta.prototype.loadResponse = function(response) {
var match = this._headers.location.match(/^\/([^\/]+)(?:\/([^\/]+))?\/([^\/]+)$/);
if (match) {
this.resource = match[1];
- this.bucket = match[2];
- this.key = match[3];
+ this.path = match.slice(2);
}
}
@@ -105,13 +104,34 @@ Object.defineProperty(HttpMeta.prototype, 'path', {
HttpMeta.queryProperties.forEach(function(p) {
if (this[p] !== undefined) qp[p] = this[p];
}.bind(this));
+
+ var qs = querystring.stringify(qp);
+
+ this._path = this._path || [];
+
+ var path = this._path
+ .map(function(component) {
+ if (!component) return;
+ return _encodeUri(component, this.encodeUri);
+ }.bind(this));
+
+ // make bucket and key available
+ this.bucket = path[0];
+ this.key = path[1];
+
+ path = path
+ .filter(function(e) { return !!e })
+ .join('/');
+
+ path = [this.resource, path].join('/');
+ if (qs) path = path + '?' + qs;
- var queryString = querystring.stringify(qp),
- bq = this.bucket ? "/" + _encodeUri(this.bucket, this.encodeUri) : '',
- kq = this.key ? "/" + _encodeUri(this.key, this.encodeUri) : '';
- qs = queryString ? "?" + queryString : '';
+ return '/' + path;
- return "/" + this.resource + bq + kq + qs;
+ },
+
+ set: function(path) {
+ this._path = path;
},
enumerable: true
View
@@ -142,11 +142,10 @@ Meta.defaults = {
Meta.keywords = [
'host',
'port',
- 'bucket',
'buckets',
'logger',
- 'key',
'keys',
+ 'path',
'contentType',
'contentLength',
'vclock',
@@ -175,9 +174,9 @@ Meta.keywords = [
'accept',
'data',
'stream',
- 'headers', // http, we want to keep these so that users can override headers (TODO check)
+ 'headers',
'callback',
- 'method' // TODO temp
+ 'method'
]
module.exports = Meta;
View
@@ -4,8 +4,8 @@ var Meta = require('../lib/http-meta'),
test('Sets its attributes');
-var meta = new Meta({ bucket: 'bucket', key: 'key', contentType: 'png', data: 'd32n92390XMIW0' });
-
+var meta = new Meta({ path: ['bucket','key'], contentType: 'png', data: 'd32n92390XMIW0' });
+meta.path;
assert.equal(meta.bucket, 'bucket');
assert.equal(meta.key, 'key');
assert.equal(meta.vclock, null);
@@ -14,11 +14,11 @@ assert.equal(meta.data, 'd32n92390XMIW0');
test('Gives back its HTTP path');
-meta = new Meta({ bucket: 'bucket', key: 'key' });
+meta = new Meta({ path: ['bucket', 'key'] });
assert.equal("/riak/bucket/key", meta.path);
meta.resource = 'luwak';
-meta.bucket = '';
+meta.path = ['key'];
assert.equal("/luwak/key", meta.path);
test('Parses headers when loaded with a Riak HTTP response');
@@ -40,7 +40,7 @@ var riakResponse = {
statusCode: 200
}
-meta = new Meta({ bucket: 'bucket', key: 'key' });
+meta = new Meta({ path: ['bucket', 'key'] });
meta.loadResponse(riakResponse);
// assert.deepEqual(meta.usermeta, { acl: 'users:r,administrators:f' }); -- usermeta is not supported
@@ -54,7 +54,7 @@ assert.equal(meta.path, '/riak/bucket/key');
test('Custom headers are correctly included and override');
meta = new Meta({
- links: [{ bucket: 'test', key: 'doc%2$@', tag: 'next' }],
+ links: [{ path: ['bucket', 'doc%2$@'], tag: 'next' }],
headers: { Authorization: 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', 'X-Riak-Meta-fire': 'yes' }
});
@@ -77,7 +77,7 @@ riakResponse = {
meta = new Meta();
meta.loadResponse(riakResponse);
-assert.equal(meta.bucket, 'test');
+meta.path;
assert.equal(meta.key, 'bzPygTesROPtGGVUKfyvp2RR49');
assert.equal(meta.statusCode, 201);
@@ -96,8 +96,7 @@ assert.equal(headers['X-Riak-ClientId'], undefined);
test('It returns its full path including query properties');
meta = new Meta({
- bucket: 'bucket',
- key: 'key',
+ path: ['bucket', 'key'],
r: 1,
w: 2,
dw: 2,
@@ -114,8 +113,7 @@ assert.equal("/riak/bucket/key?r=1&w=2&dw=2&rw=2&keys=true&props=false&vtag=aswe
test('Returns an URI-encoded path if used with encodeUri option');
meta = new Meta({
- bucket: 'spåce bucket',
- key: 'çøµπléx–key',
+ path: ['spåce bucket', 'çøµπléx–key'],
encodeUri: true
});
View
@@ -4,7 +4,7 @@ var Meta = require('../lib/meta'),
test('Sets its attributes');
-var meta = new Meta({ bucket: 'bucket', key: 'key', contentType: 'png', data: 'd32n92390XMIW0', host: '192.168.1.2', myown: 'field' });
+var meta = new Meta({ path: ['bucket', 'key'], contentType: 'png', data: 'd32n92390XMIW0', host: '192.168.1.2', myown: 'field' });
assert.equal(meta.bucket, 'bucket');
assert.equal(meta.key, 'key');

0 comments on commit 9da4969

Please sign in to comment.