Permalink
Browse files

Pull mapreduce transformations into separate lib

  • Loading branch information...
mde
mde committed Sep 15, 2012
1 parent 4a4492c commit 366b9c8b878d173a53b5cf58983205be5e0d4dd2
Showing with 117 additions and 107 deletions.
  1. +4 −107 lib/adapters/riak/index.js
  2. +113 −0 lib/adapters/transformers/mr.js
View
@@ -1,15 +1,12 @@
var model = require('../../index')
, utils = require('utilities')
- , operation = require('../../query/operation')
- , comparison = require('../../query/comparison')
- , datatypes = require('../../datatypes')
+ , mr = require('../transformers/mr')
, request = utils.request
, BaseAdapter = require('../base_adapter').BaseAdapter
, _baseConfig
, _reduceFunction
, _mapReduceQuery
- , _bucketizeModelName
- , _serializeForDataType;
+ , _bucketizeModelName;
_baseConfig = {
protocol: 'http'
@@ -103,110 +100,8 @@ _mapReduceQuery = function (bucket, conditions, sort) {
' { return [data]; } else { return []; } }"}}' + reduce + ']}';
};
-_serializeForDataType = function (datatype, val) {
- var ret;
- switch (true) {
- case val === null:
- ret = 'null';
- break;
- case val === '':
- ret = '\'\'';
- break;
- case datatype == 'date' || datatype == 'datetime':
- ret = JSON.stringify(val).replace(/"/g, "'");
- break;
- default:
- ret = datatypes[datatype].serialize(val, {
- useQuotes: true
- , escape: true
- });
- }
- return ret;
-};
-
utils.mixin(Adapter.prototype, new (function () {
- var _operationSymbols = {
- 'and': '&&'
- , 'or': '||'
- };
-
- this.transformSortOrder = function (sort) {
- return sort ? JSON.stringify(sort).replace(/"/g, "'") : '';
- };
-
- this.transformConditions = function (conditions) {
- var cond = this.transformOperation(conditions);
- return cond;
- };
-
- this.transformOperation = function (op) {
- var self = this
- , ops = [];
- if (op.isEmpty()) {
- return '(true)';
- }
- else {
- op.forEach(function (o) {
- if (o instanceof operation.OperationBase) {
- ops.push(self.transformOperation(o));
- }
- else {
- ops.push(self.transformComparison(o));
- }
- });
- if (op.type == 'not') {
- return '(!(' + self.transformOperation(op.operand()) + '))';
- }
- else {
- return '(' + ops.join(' ' + _operationSymbols[op.type.toLowerCase()] +
- ' ') + ')';
- }
- }
- };
-
- this.transformComparison = function (comp) {
- var ret = ''
- , name = this.transformComparisonFieldName(comp)
- , arr = [];
- switch (true) {
- case comp instanceof comparison.LikeComparison:
- ret = name + '.indexOf(' +
- this.transformComparisonValue(comp) + ') === 0';
- break;
- case comp instanceof comparison.InclusionComparison:
- comp.value.forEach(function (item) {
- arr.push(name + ' == ' +
- _serializeForDataType(comp.datatype, item));
- });
- ret = arr.join(' || ');
- break;
- default:
- ret = [name, this.transformComparisonComparator(comp),
- this.transformComparisonValue(comp)].join(' ');
-
- }
- return ret;
- };
-
- this.transformComparisonFieldName = function (comp) {
- // Use bracket-notation, in case field-name has special chars
- // or is a reserved word
- var name = 'data[\'' + comp.field + '\']';
- if (comp.opts.nocase) {
- name += '.toLowerCase()';
- }
- return name;
- };
-
- this.transformComparisonComparator = function (comp) {
- var comparator = comp.jsComparatorString;
- return comparator;
- };
-
- this.transformComparisonValue = function (comp) {
- return _serializeForDataType(comp.datatype, comp.value);
- };
this.init = function () {};
@@ -504,5 +399,7 @@ utils.mixin(Adapter.prototype, new (function () {
})());
+utils.mixin(Adapter.prototype, mr);
+
module.exports.Adapter = Adapter;
@@ -0,0 +1,113 @@
+var operation = require('../../query/operation')
+ , comparison = require('../../query/comparison')
+ , datatypes = require('../../datatypes')
+ , _serializeForDataType;
+
+_serializeForDataType = function (datatype, val) {
+ var ret;
+ switch (true) {
+ case val === null:
+ ret = 'null';
+ break;
+ case val === '':
+ ret = '\'\'';
+ break;
+ case datatype == 'date' || datatype == 'datetime':
+ ret = JSON.stringify(val).replace(/"/g, "'");
+ break;
+ default:
+ ret = datatypes[datatype].serialize(val, {
+ useQuotes: true
+ , escape: true
+ });
+ }
+ return ret;
+};
+
+var mr = new (function () {
+
+ var _operationSymbols = {
+ 'and': '&&'
+ , 'or': '||'
+ };
+
+ this.transformSortOrder = function (sort) {
+ return sort ? JSON.stringify(sort).replace(/"/g, "'") : '';
+ };
+
+ this.transformConditions = function (conditions) {
+ var cond = this.transformOperation(conditions);
+ return cond;
+ };
+
+ this.transformOperation = function (op) {
+ var self = this
+ , ops = [];
+ if (op.isEmpty()) {
+ return '(true)';
+ }
+ else {
+ op.forEach(function (o) {
+ if (o instanceof operation.OperationBase) {
+ ops.push(self.transformOperation(o));
+ }
+ else {
+ ops.push(self.transformComparison(o));
+ }
+ });
+ if (op.type == 'not') {
+ return '(!(' + self.transformOperation(op.operand()) + '))';
+ }
+ else {
+ return '(' + ops.join(' ' + _operationSymbols[op.type.toLowerCase()] +
+ ' ') + ')';
+ }
+ }
+ };
+
+ this.transformComparison = function (comp) {
+ var ret = ''
+ , name = this.transformComparisonFieldName(comp)
+ , arr = [];
+ switch (true) {
+ case comp instanceof comparison.LikeComparison:
+ ret = name + '.indexOf(' +
+ this.transformComparisonValue(comp) + ') === 0';
+ break;
+ case comp instanceof comparison.InclusionComparison:
+ comp.value.forEach(function (item) {
+ arr.push(name + ' == ' +
+ _serializeForDataType(comp.datatype, item));
+ });
+ ret = arr.join(' || ');
+ break;
+ default:
+ ret = [name, this.transformComparisonComparator(comp),
+ this.transformComparisonValue(comp)].join(' ');
+
+ }
+ return ret;
+ };
+
+ this.transformComparisonFieldName = function (comp) {
+ // Use bracket-notation, in case field-name has special chars
+ // or is a reserved word
+ var name = 'data[\'' + comp.field + '\']';
+ if (comp.opts.nocase) {
+ name += '.toLowerCase()';
+ }
+ return name;
+ };
+
+ this.transformComparisonComparator = function (comp) {
+ var comparator = comp.jsComparatorString;
+ return comparator;
+ };
+
+ this.transformComparisonValue = function (comp) {
+ return _serializeForDataType(comp.datatype, comp.value);
+ };
+
+})();
+
+module.exports = mr;

0 comments on commit 366b9c8

Please sign in to comment.