diff --git a/README.md b/README.md index 9fda612..ebce55d 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,8 @@ Internally it uses `client.modify` with a mutator function to accomplish this. ### client.post(url, post_body, callback) +### client.index(bucket, index, begin_val, end_val, callback) + --- ## Semi-deprecated / not in use / possibly not working diff --git a/riak.js b/riak.js index e72ce91..46af06a 100644 --- a/riak.js +++ b/riak.js @@ -604,6 +604,45 @@ RiakClient.prototype.post = function (url, post_body, callback) { }); }; +RiakClient.prototype.index = function (bucket, index, begin_val, end_val, callback) { + var self = this; + + if (this.debug_mode) { + this.log("riak index", bucket + ", " + index + ", " + begin_val + ", ", + end_val); + } + + if (typeof callback !== "function") { + throw new Error("Callback passed non-function"); + } + + this.pool.get({ + path: "/buckets/" + encodeURIComponent(bucket) + "/index/" + encodeURIComponent(index) + "/" + encodeURIComponent(begin_val) + "/" + encodeURIComponent(end_val), + headers: { + "X-Riak-ClientId": this.client_id, + "Connection": "keep-alive" + } + }, function (err, res, body) { + if (err) { + return callback(err); + } + var obj = {}; + if (body.length > 0 && res.statusCode === 200) { + try { + obj = JSON.parse(body); + } catch (json_err) { + console.warn("riak index JSON parse: " + body); + return callback(new Error("JSON parse error")); + } + return callback(null, res, obj); + } else { + if (self.debug_mode) { + self.log("riak index", bucket + ", " + index + " returned non-JSON, statusCode: " + res.statusCode + ", body:" + JSON.stringify(body)); + } + return callback(null, res, {error: "non-JSON: " + body}); + } + }); +}; + // TODO - this is no longer used, so it might not work anymore. RiakClient.prototype.solr = function (bucket, query, limit, callback) { var self = this; diff --git a/test.js b/test.js index 191ad50..ef0cd49 100644 --- a/test.js +++ b/test.js @@ -134,4 +134,24 @@ specify("append", function (assert) { }); }); +specify("index", function (assert) { + var bucket = "bucket_1", + key = "key_6", + message = "some string full of juicy data", + options = {"http_headers": {"x-riak-index-id_int": "1"}}, + index = "id_int", + begin_val = "1", + end_val = ""; + client.put(bucket, key, message, options, function (err, res, obj) { + assert.ok(!err); + assert.equal(res.statusCode, 204); + + client.index(bucket, index, begin_val, end_val, function(err, res, obj) { + assert.ok(!err); + assert.equal(res.statusCode, 200); + assert.deepEqual(obj, {"keys": ["key_6"]}); + }); + }); +}); + specify.run(filters);