Permalink
Browse files

Finished up basic map-reduce generation, tests

  • Loading branch information...
mde
mde committed Aug 25, 2012
1 parent e173199 commit 40be334852b432a6bd2728762d00c2fcf0f819fb
Showing with 76 additions and 28 deletions.
  1. +56 −17 lib/adapters/riak/index.js
  2. +3 −2 lib/query/query.js
  3. +17 −9 test/adapters/riak/index.js
View
@@ -4,6 +4,7 @@ var utils = require('utilities')
, request = utils.request
, BaseAdapter = require('../base_adapter').BaseAdapter
, _baseConfig
+ , _mapReduceQuery
, _bucketizeModelName;
_baseConfig = {
@@ -34,6 +35,10 @@ _bucketizeModelName = function (name) {
return bucketName;
};
+_mapReduceQuery = function (bucket, conditions) {
+ return '{"inputs": "' + bucket + '", "query": [{"map": {"language": "javascript","source": "function (value, keyData, arg) { var data = Riak.mapValuesJson(value)[0]; if ' + conditions + ' { return [data]; } else { return []; } }"}}]}';
+};
+
utils.mixin(Adapter.prototype, new (function () {
var _operationSymbols = {
@@ -78,7 +83,8 @@ utils.mixin(Adapter.prototype, new (function () {
};
this._serializeComparisonFieldName = function (comp) {
- // Use bracket-notation in case field-name has hyphens in it
+ // Use bracket-notation, in case field-name has special chars
+ // or is a reserved word
var name = 'data[\'' + comp.field + '\']';
if (comp.opts.lowercase) {
name += '.toLowerCase()';
@@ -93,20 +99,26 @@ utils.mixin(Adapter.prototype, new (function () {
this._serializeComparisonValue = function (comp) {
var val = comp.value;
- if (val === null) {
- val = 'null';
- }
- else {
- val = datatypes[comp.datatype].serialize(val, {
- useQuotes: true
- , escape: true
- });
+ switch (true) {
+ case val === null:
+ val = 'null';
+ break;
+ case val === '':
+ val = '\'\'';
+ break;
+ case comp.datatype == 'date' || comp.datatype == 'datetime':
+ val = JSON.stringify(val).replace(/"/g, "'");
+ break;
+ default:
+ val = datatypes[comp.datatype].serialize(val, {
+ useQuotes: true
+ , escape: true
+ });
}
return val;
};
- this.init = function () {
- };
+ this.init = function () {};
this.request = function (options, callback) {
var opts = options || {}
@@ -162,16 +174,33 @@ utils.mixin(Adapter.prototype, new (function () {
// Teh mapreducy
else {
conditions = this._serializeConditions(query.conditions);
- //utils.log(conditions);
requestOpts = {
url: '/mapred'
, method: 'POST'
- , data: '{"inputs": "' + bucket + '", "query": [{"map": {"language": "javascript","source": "function (value, keyData, arg) { var data = Riak.mapValuesJson(value)[0]; if ' + conditions + ' { return [data]; } else { return []; } }"}}]}'
+ , data: _mapReduceQuery(bucket, conditions)
};
this.request(requestOpts, function (err, data) {
- utils.log(arguments);
+ var rows
+ , res = [];
+ if (err) {
+ callback(err, null);
+ }
+ else {
+ rows = data;
+ rows.forEach(function (row) {
+ var inst = query.model.create(row);
+ inst.id = row.id;
+ inst._saved = true;
+ res.push(inst);
+ });
+ // If explicitly limited to one, just return the single instance
+ // This is also used by the `first` method
+ if (query.opts.limit == 1) {
+ res = res[0];
+ }
+ callback(null, res);
+ }
});
- //callback(new Error('Complex queries not supported'), null);
}
};
@@ -188,6 +217,7 @@ utils.mixin(Adapter.prototype, new (function () {
}
item = item.toData();
+ item.id = id;
item = JSON.stringify(item);
requestOpts = {
@@ -250,16 +280,25 @@ utils.mixin(Adapter.prototype, new (function () {
, url = '/riak/' + bucket + '/' + id
, requestOpts;
- item.id = id;
item = item.toData();
+ item.id = id;
item = JSON.stringify(item);
requestOpts = {
url: url
, method: 'POST'
, data: item
};
- this.request(requestOpts, callback);
+ this.request(requestOpts, function (err, res) {
+ if (err) {
+ callback(err, null);
+ }
+ else {
+ data.id = id;
+ data._saved = true;
+ callback(null, data);
+ }
+ });
};
// May need to set bucket props here?
View
@@ -217,12 +217,13 @@ Query.prototype = new (function () {
return ret;
};
- this.initialize = function (model, conditions, opts) {
+ this.initialize = function (model, conditionParams, opts) {
this.byId = null;
this.model = model;
this.isValidField = _createIsValidField.apply(this);
this.opts = _parseOpts.apply(this, [opts || {}]);
- this.conditions = _createOperation.apply(this, [conditions]);
+ this.conditions = _createOperation.apply(this, [conditionParams]);
+ this.rawConditions = conditionParams;
};
})();
@@ -28,7 +28,7 @@ tests = {
, 'test create adapter': function () {
assert.ok(adapter instanceof Adapter);
}
-/*
+
, 'test save new, string UUID id': function (next) {
var z = Zooby.create({foo: 'FOO'});
z.save(function (err, data) {
@@ -45,7 +45,7 @@ tests = {
if (err) {
throw err;
}
- assert.equal(data.id, currentId);
+ assert.equal(currentId, data.id);
next();
});
}
@@ -67,24 +67,33 @@ tests = {
if (err) {
throw err;
}
- assert.equal(data.foo, 'BAR');
+ assert.equal('BAR', data.foo);
next();
});
});
});
}
-*/
-, 'test all': function (next) {
- Zooby.all({foo: 'FOO'}, {}, function (err, data) {
+
+, 'test all, by id': function (next) {
+ Zooby.all({id: currentId}, {}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ assert.equal(currentId, data[0].id);
+ next();
+ });
+ }
+
+, 'test all, by map-reduce': function (next) {
+ Zooby.all({foo: 'BAR'}, {}, function (err, data) {
if (err) {
throw err;
}
- //assert.equal(data.id, currentId);
+ assert.equal(data[0].id, currentId);
next();
});
}
-/*
, 'test remove': function (next) {
Zooby.remove(currentId, {}, function (err, data) {
if (err) {
@@ -99,7 +108,6 @@ tests = {
});
});
}
-*/
};

0 comments on commit 40be334

Please sign in to comment.