Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Serializiation of operation to mapreduce conditions

  • Loading branch information...
commit e17319918ca5b2b1048c9e2217688887c25e75e9 1 parent a5b8827
mde authored
97 lib/adapters/riak/index.js
View
@@ -1,4 +1,6 @@
var utils = require('utilities')
+ , operation = require('../../query/operation')
+ , datatypes = require('../../datatypes')
, request = utils.request
, BaseAdapter = require('../base_adapter').BaseAdapter
, _baseConfig
@@ -34,6 +36,75 @@ _bucketizeModelName = function (name) {
utils.mixin(Adapter.prototype, new (function () {
+ var _operationSymbols = {
+ 'and': '&&'
+ , 'or': '||'
+ };
+
+ this._serializeConditions = function (conditions) {
+ var cond = this._serializeOperation(conditions);
+ return cond;
+ };
+
+ this._serializeOperation = function (op) {
+ var self = this
+ , ops = [];
+ if (op.isEmpty()) {
+ return '(true)';
+ }
+ else {
+ op.forEach(function (o) {
+ if (o instanceof operation.OperationBase) {
+ ops.push(self._serializeOperation(o));
+ }
+ else {
+ ops.push(self._serializeComparison(o));
+ }
+ });
+ if (op.type == 'not') {
+ return '(!(' + self._serializeOperation(op.operand()) + '))';
+ }
+ else {
+ return '(' + ops.join(' ' + _operationSymbols[op.type.toLowerCase()] +
+ ' ') + ')';
+ }
+ }
+ };
+
+ this._serializeComparison = function (comp) {
+ return [this._serializeComparisonFieldName(comp),
+ this._serializeComparisonComparator(comp),
+ this._serializeComparisonValue(comp)].join(' ');
+ };
+
+ this._serializeComparisonFieldName = function (comp) {
+ // Use bracket-notation in case field-name has hyphens in it
+ var name = 'data[\'' + comp.field + '\']';
+ if (comp.opts.lowercase) {
+ name += '.toLowerCase()';
+ }
+ return name;
+ };
+
+ this._serializeComparisonComparator = function (comp) {
+ var comparator = comp.jsComparatorString;
+ return comparator;
+ };
+
+ this._serializeComparisonValue = function (comp) {
+ var val = comp.value;
+ if (val === null) {
+ val = 'null';
+ }
+ else {
+ val = datatypes[comp.datatype].serialize(val, {
+ useQuotes: true
+ , escape: true
+ });
+ }
+ return val;
+ };
+
this.init = function () {
};
@@ -42,8 +113,7 @@ utils.mixin(Adapter.prototype, new (function () {
, config = this.config;
request({
method: opts.method || 'GET'
- , url: config.protocol + '://' + config.host + ':' + config.port +
- '/riak/' + opts.url
+ , url: config.protocol + '://' + config.host + ':' + config.port + opts.url
, data: opts.data || null
, dataType: 'json'
, headers: {
@@ -55,12 +125,13 @@ utils.mixin(Adapter.prototype, new (function () {
this.load = function (query, callback) {
var bucket = _bucketizeModelName(query.model.modelName)
, id = query.byId
- , requestOpts;
+ , requestOpts
+ , conditions;
// Single instance-lookup by id
if (id) {
requestOpts = {
- url: bucket + '/' + id
+ url: '/riak/' + bucket + '/' + id
, method: 'GET'
};
this.request(requestOpts, function (err, data) {
@@ -90,7 +161,17 @@ utils.mixin(Adapter.prototype, new (function () {
}
// Teh mapreducy
else {
- callback(new Error('Complex queries not supported'), null);
+ 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 []; } }"}}]}'
+ };
+ this.request(requestOpts, function (err, data) {
+ utils.log(arguments);
+ });
+ //callback(new Error('Complex queries not supported'), null);
}
};
@@ -110,7 +191,7 @@ utils.mixin(Adapter.prototype, new (function () {
item = JSON.stringify(item);
requestOpts = {
- url: bucket + '/' + id
+ url: '/riak/' + bucket + '/' + id
, method: 'PUT'
, data: item
};
@@ -141,7 +222,7 @@ utils.mixin(Adapter.prototype, new (function () {
// Single instance-lookup by id
if (id) {
requestOpts = {
- url: bucket + '/' + id
+ url: '/riak/' + bucket + '/' + id
, method: 'DELETE'
};
this.request(requestOpts, function (err, data) {
@@ -166,7 +247,7 @@ utils.mixin(Adapter.prototype, new (function () {
, item = data
, bucket = _bucketizeModelName(item.type)
, id = utils.string.uuid()
- , url = bucket + '/' + id
+ , url = '/riak/' + bucket + '/' + id
, requestOpts;
item.id = id;
5 lib/adapters/sql/base.js
View
@@ -59,8 +59,7 @@ utils.mixin(Adapter.prototype, new (function () {
this._serializeOperation = function (op) {
var self = this
- , ops = []
- , str = '';
+ , ops = [];
if (op.isEmpty()) {
return '';
}
@@ -98,7 +97,7 @@ utils.mixin(Adapter.prototype, new (function () {
this._serializeComparisonComparator = function (comp) {
var val = comp.value
- , comparator = comp.comparatorString;
+ , comparator = comp.sqlComparatorString;
if (val === null) {
comparator = comp.type == 'EqualTo' ?
'IS' : 'IS NOT';
34 lib/query/comparison.js
View
@@ -32,35 +32,41 @@ ComparisonBase = function () {
comparisonTypes = {
EqualToComparison: function () {
- this.comparatorString = '=';
+ this.jsComparatorString = '==';
+ this.sqlComparatorString = '=';
}
, NotEqualToComparison: function () {
- this.comparatorString = '!=';
- }
-
-, InclusionComparison: function () {
- this.comparatorString = 'IN';
- }
-
-, LikeComparison: function () {
- this.comparatorString = 'LIKE';
+ this.jsComparatorString = '!=';
+ this.sqlComparatorString = '!=';
}
, GreaterThanComparison: function () {
- this.comparatorString = '>';
+ this.jsComparatorString = '>';
+ this.sqlComparatorString = '>';
}
, LessThanComparison: function () {
- this.comparatorString = '<';
+ this.jsComparatorString = '<';
+ this.sqlComparatorString = '<';
}
, GreaterThanOrEqualComparison: function () {
- this.comparatorString = '>=';
+ this.jsComparatorString = '>=';
+ this.sqlComparatorString = '>=';
}
, LessThanOrEqualComparison: function () {
- this.comparatorString = '<=';
+ this.jsComparatorString = '<=';
+ this.sqlComparatorString = '<=';
+ }
+
+, InclusionComparison: function () {
+ this.sqlComparatorString = 'IN';
+ }
+
+, LikeComparison: function () {
+ this.sqlComparatorString = 'LIKE';
}
};
14 test/adapters/riak/index.js
View
@@ -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) {
@@ -73,7 +73,18 @@ tests = {
});
});
}
+*/
+, 'test all': function (next) {
+ Zooby.all({foo: 'FOO'}, {}, function (err, data) {
+ if (err) {
+ throw err;
+ }
+ //assert.equal(data.id, currentId);
+ next();
+ });
+ }
+/*
, 'test remove': function (next) {
Zooby.remove(currentId, {}, function (err, data) {
if (err) {
@@ -88,6 +99,7 @@ tests = {
});
});
}
+*/
};
Please sign in to comment.
Something went wrong with that request. Please try again.