Skip to content

Commit

Permalink
add basic support for r/w/dw parameters to fetch and store
Browse files Browse the repository at this point in the history
  • Loading branch information
orlandov committed Feb 10, 2010
1 parent 93189a8 commit 975b23c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ SYNOPSIS
port: 8098,
});

// store nyarlathotep in deities as plain text
db.store('deities', 'nyarlathotep', "The crawling chaos")
// store nyarlathotep in deities as plain text with an 'r' value of 1
db.store('deities', 'nyarlathotep', "The crawling chaos", { r: 1 })
.addCallback(function (resp, statusCode) {
// ...
});
Expand Down Expand Up @@ -58,6 +58,12 @@ If the HTTP response code isn't in the 2XX range an errback will be fired
instead of a promise. It will have the same arguments as described in the
success case above.

TESTS
-----

By default the node-riak tests run on localhost:8098 on the `node-riak-test-pots`

NODE_LIB=lib node tests/test_riak.js

SEE ALSO
--------
Expand Down
32 changes: 25 additions & 7 deletions lib/riak.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var assert = require('assert'),
http = require('http'),
path = require('path'),
sys = require('sys'),
url = require('url');
var assert = require('assert'),
http = require('http'),
path = require('path'),
querystring = require('querystring'),
sys = require('sys'),
url = require('url');

var Client = exports.Client = function (params) {
this.params = params;
Expand All @@ -23,7 +24,7 @@ Client.prototype.store = function(bucket, key, data, params) {

var headers = (params && params.headers) || {};

switch (params && params.type) {
switch (params && params.type) {
case "json":
headers['content-type'] = "application/json";
data = JSON.stringify(data);
Expand All @@ -37,15 +38,32 @@ Client.prototype.store = function(bucket, key, data, params) {
case "text":
headers['content-type'] = "text/plain";
break;

default:
headers['content-type'] = params.type;
}

var document_path = this.documentPath(bucket, key);

if (params) {
var query = {};
['r', 'w', 'dw'].forEach(function (f) {
if (params[f]) query[f] = params[f];
});
if (Object.keys(query).length > 0)
document_path += '?' + querystring.stringify(query);
}

return this.makeRequest('PUT', this.documentPath(bucket, key),
return this.makeRequest('PUT', document_path,
headers, data);
}

Client.prototype.fetch = function(bucket, key, params) {
var document_path = this.documentPath(bucket, key);

if (params && 'r' in params) {
document_path += '?' + 'r=' + params.r;
}
return this.makeRequest('GET', this.documentPath(bucket, key));
}

Expand Down
7 changes: 4 additions & 3 deletions t/test_riak.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ var db = new Riak.Client({
port: 8098,
});

var plan = 6;
var plan = 7;
var tc = 0;

var str = "Ya'll are brutalizing me" + Math.random();
var mahbucket = 'node-riak-test-posts';

db.store(mahbucket, 'post0', str).addCallback(function (resp) {
// store `post0` into mahbucket with r: 2, w: 1
db.store(mahbucket, 'post0', str, { r: 2, w: 1 }).addCallback(function (resp) {
tc++;
equal(typeof resp.headers, 'object');

db.fetch(mahbucket, 'post0').addCallback(function (resp) {
db.fetch(mahbucket, 'post0', {}, { r: 2 }).addCallback(function (resp) {
ok("x-riak-vclock" in resp.headers);
tc++;
equal(str, resp.data);
Expand Down

0 comments on commit 975b23c

Please sign in to comment.