Comparing version 0.8.0 to 0.8.5
@@ -6,2 +6,7 @@ var Bucket = require('../bucket'); | ||
var Input = function RiakMapReduceInput(value, keyData) { | ||
this.value = value; | ||
this.keyData = keyData; | ||
} | ||
var processInput = function processInput(spec) { | ||
@@ -19,6 +24,3 @@ switch (spec.constructor.name) { | ||
return [{ | ||
object: bucket.getObject(spec[1]), | ||
auxData: spec.length === 3 ? spec[3] : null | ||
}]; | ||
return new Input(bucket.getObject(spec[1]), spec.length === 3 ? spec[3] : null); | ||
@@ -34,6 +36,3 @@ case 'String': | ||
bucket.allObjects().forEach(function(object) { | ||
result.push({ | ||
object: object, | ||
auxData: null | ||
}); | ||
result.push(new Input(object, null)); | ||
}); | ||
@@ -58,6 +57,3 @@ | ||
if (filterKey(object.key, spec.key_filters)) { | ||
result.push({ | ||
object: object, | ||
auxData: null | ||
}); | ||
result.push(new Input(object, null)); | ||
} | ||
@@ -76,2 +72,4 @@ }); | ||
processInput.RiakInput = Input; | ||
module.exports = processInput; |
var Link = require('../link'); | ||
var Input = require('./input'); | ||
@@ -17,3 +18,3 @@ var LinkPhase = function LinkPhase(spec) { | ||
parent: null, | ||
obj: object.object | ||
obj: object.constructor.name === 'RiakMapReduceInput' ? object.value : object | ||
}; | ||
@@ -26,6 +27,3 @@ }); | ||
section.forEach(function(object) { | ||
results.push({ | ||
object: object.obj, | ||
auxData: null | ||
}); | ||
results.push(new Input.RiakInput(object.obj, null)); | ||
}); | ||
@@ -32,0 +30,0 @@ }); |
@@ -9,3 +9,10 @@ var restify = require('restify'); | ||
this.script = vm.createScript('var f = ' + spec.source + '; var result = f(object.object, object.auxData, arg)'); | ||
if (spec.source) { | ||
this.script = vm.createScript('var f = ' + spec.source + '; var result = f(value, keyData, arg)'); | ||
} else if (spec.name) { | ||
this.script = vm.createScript('var f = ' + spec.name + '; var result = f(value, keyData, arg)'); | ||
} else { | ||
throw new restify.BadRequestError('The map phase ' + JSON.stringify(spec) + ' has no script name or source.'); | ||
} | ||
this.arg = spec.arg; | ||
@@ -49,18 +56,27 @@ }; | ||
objects.forEach(function(object) { | ||
var keyData, value; | ||
if (object.constructor.name === 'RiakMapReduceInput') { | ||
keyData = object.keyData; | ||
value = { | ||
bucket: object.value.bucket.bucketName, | ||
key: object.value.key, | ||
vclock: Math.random(100000), | ||
values: [ | ||
{ | ||
metadata: objectMetadata(object.value), | ||
data: object.value.contents | ||
} | ||
] | ||
}; | ||
} else { | ||
value = object; | ||
} | ||
var sandbox = { | ||
arg: this.arg, | ||
object: { | ||
object: { | ||
bucket: object.object.bucket.bucketName, | ||
key: object.object.key, | ||
vclock: Math.random(100000), | ||
values: [ | ||
{ | ||
metadata: objectMetadata(object.object), | ||
data: object.object.contents | ||
} | ||
] | ||
}, | ||
auxData: object.auxData | ||
} | ||
value: value, | ||
keyData: keyData, | ||
console: console, | ||
Riak: require('./riak_builtin_functions') | ||
}; | ||
@@ -70,6 +86,3 @@ | ||
results.push({ | ||
object: sandbox.result, | ||
auxData: null | ||
}); | ||
results = results.concat(sandbox.result); | ||
}, this); | ||
@@ -76,0 +89,0 @@ |
var vm = require('vm'); | ||
var restify = require('restify'); | ||
@@ -8,3 +9,10 @@ var ReducePhase = function ReducePhase(spec) { | ||
this.script = vm.createScript('var f = ' + spec.source + '; var result = f(objects, arg)'); | ||
if (spec.source) { | ||
this.script = vm.createScript('var f = ' + spec.source + '; var result = f(objects, arg)'); | ||
} else if (spec.name) { | ||
this.script = vm.createScript('var f = ' + spec.name + '; var result = f(objects, arg)'); | ||
} else { | ||
throw new restify.BadRequestError('The reduce phase ' + JSON.stringify(spec) + ' has no script name or source.'); | ||
} | ||
this.arg = spec.arg; | ||
@@ -17,4 +25,9 @@ }; | ||
objects: objects.map(function(object) { | ||
return object.object; | ||
if (object.constructor.name === 'RiakMapReduceInput') { | ||
return object.value; | ||
} else { | ||
return object; | ||
} | ||
}), | ||
Riak: require('./riak_builtin_functions') | ||
}; | ||
@@ -21,0 +34,0 @@ |
@@ -26,2 +26,6 @@ var crypto = require('crypto'); | ||
if (contentType === 'application/json') { | ||
contents = JSON.stringify(contents); | ||
} | ||
if (!obj) { | ||
@@ -28,0 +32,0 @@ obj = new RiakObject(bucket, key, meta, contentType, contents, links, indices); |
{ | ||
"name": "mock-riak", | ||
"version": "0.8.0", | ||
"version": "0.8.5", | ||
"author": "Marco Tabini <marcot@tabini.ca>", | ||
@@ -5,0 +5,0 @@ "description": "A mock library that simulates a running Riak cluster (without actually having one)", |
@@ -33,3 +33,5 @@ # Riak-mock: A pretend Riak server for your testing pleasure | ||
- There are likely to be many subtle differences between the way Riak and Mock-Riak work. The long-term goal is to track those down and fix them, but _caveat emptor._ | ||
- Riak-Mock doesn't support siblings. | ||
- Only JavaScript map/reduce jobs are supported. | ||
- Map/Reduce phases in the form `"language":"javascript","bucket":"myjs","key":"mymap"` are not supported. | ||
- Overall, this project is very young; if your Riak usage goes beyond the basics, you're likely to encounter problems (in which case, bug reports—and, especially, patches—are welcome). | ||
@@ -36,0 +38,0 @@ |
@@ -40,2 +40,4 @@ var Riak = require('../index'); | ||
function checkMeta(doc, meta, callback) { | ||
doc = JSON.parse(doc); | ||
expect(doc).to.be.an('object'); | ||
@@ -84,2 +86,4 @@ expect(doc).to.deep.equal(payload); | ||
doc = JSON.parse(doc); | ||
expect(doc).to.be.an('object'); | ||
@@ -128,2 +132,4 @@ expect(doc).to.deep.equal(payloads[1]); | ||
result = result.map(JSON.parse); | ||
expect(result[0]).to.deep.equal(payloads[0]); | ||
@@ -130,0 +136,0 @@ expect(result[1]).to.deep.equal(payloads[1]); |
@@ -41,2 +41,4 @@ var Riak = require('../index'); | ||
function checkMeta(doc, meta, callback) { | ||
doc = JSON.parse(doc); | ||
expect(doc).to.be.an('object'); | ||
@@ -43,0 +45,0 @@ expect(doc).to.deep.equal(payload); |
@@ -60,3 +60,4 @@ var Riak = require('../index'); | ||
.map(function(v, args, arg) { | ||
var key = v.values[0].data.gender + arg.b; | ||
var data = JSON.parse(v.values[0].data); | ||
var key = data.gender + arg.b; | ||
var result = {}; | ||
@@ -101,2 +102,52 @@ | ||
}); | ||
it('should support built-in named functions', function(done) { | ||
var payloads = [ | ||
{ | ||
data : { | ||
name : 'Marco' , | ||
gender : 'male' | ||
} , | ||
key : 'marco' , | ||
links : [ | ||
{ | ||
bucket : 'users' , | ||
key : 'daniel' , | ||
tag : 'friend' | ||
}, | ||
{ | ||
bucket : 'users', | ||
key: 'andrea', | ||
tag : 'child' | ||
}] | ||
}, | ||
{ data : { name : 'Daniel' , email : 'daniel@example.org' , gender : 'male' } , links : [ { bucket : 'users' , key : 'andrea' , tag : 'sibling' } ] , key : 'daniel' }, | ||
{ data : { name : 'Andrea' , email : 'andrea@example.org' , gender : 'female' } , key : 'andrea' } ]; | ||
var bucket = 'users'; | ||
async.each( | ||
payloads, | ||
function iterator(element, callback) { | ||
client.save(bucket, element.key, element.data, element.links ? { links : element.links } : null, callback); | ||
}, | ||
function(err) { | ||
client.mapreduce | ||
.add([[bucket, 'marco']]) | ||
.map('Riak.mapValuesJson') | ||
.run(function(err, result) { | ||
expect(err).to.be.null; | ||
expect(result).to.be.an('array'); | ||
expect(result).to.have.length(1); | ||
expect(result).to.deep.equal([ { name: 'Marco', gender: 'male' } ]); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
@@ -103,0 +154,0 @@ after(function (done) { |
@@ -36,2 +36,4 @@ var Riak = require('../index'); | ||
client.save('people', null, payload, { returnbody : true }, function(err, doc) { | ||
doc = JSON.parse(doc); | ||
expect(err).to.be.null; | ||
@@ -49,2 +51,4 @@ expect(doc).to.deep.equal(payload); | ||
client.save('people', key, payload, { returnbody : true }, function(err, doc) { | ||
doc = JSON.parse(doc); | ||
expect(err).to.be.null; | ||
@@ -63,2 +67,4 @@ expect(doc).to.deep.equal(payload); | ||
client.save(bucket, key, payload, { returnbody : true }, function(err, doc) { | ||
doc = JSON.parse(doc); | ||
expect(err).to.be.null; | ||
@@ -68,2 +74,4 @@ expect(doc).to.deep.equal(payload); | ||
client.get(bucket, key, function(err, document, meta) { | ||
document = JSON.parse(document); | ||
expect(err).to.be.null; | ||
@@ -83,2 +91,4 @@ expect(document).to.deep.equal(payload); | ||
client.save(bucket, key, payload, { returnbody : true }, function(err, doc) { | ||
doc = JSON.parse(doc); | ||
expect(err).to.be.null; | ||
@@ -124,2 +134,4 @@ expect(doc).to.deep.equal(payload); | ||
function checkMeta(doc, meta, callback) { | ||
doc = JSON.parse(doc); | ||
expect(doc).to.be.an('object'); | ||
@@ -126,0 +138,0 @@ expect(doc).to.deep.equal(payload); |
78812
30
1614
42