提交 6861dca8 编写于 作者: S Sercan

map reduce fixes and improvements

上级 74efa910
......@@ -23,7 +23,11 @@ Template.renderAfterQueryExecution = function (err, result) {
} else {
errorMessage = result.error.message;
}
toastr.error("Couldn't execute query: " + errorMessage);
if (errorMessage) {
toastr.error("Couldn't execute query: " + errorMessage);
} else {
toastr.error("Couldn't execute query, unknown reason ");
}
} else {
Template.browseCollection.setResult(result.result);
}
......
......@@ -3,8 +3,7 @@
<label class="col-lg-1 control-label">Map</label>
<div class="col-lg-11">
<pre class="form-control" style="height: 150px" id='aceMap'></pre>
<span class="help-block m-b-none">You can use <strong>string</strong> to query <strong>ISODate</strong> and <strong>ObjectID</strong>, date format is <strong>YYYY-MM-DD
HH:mm:ss</strong><br/>Please provide a valid function which starts with <strong>function</strong> keyword</span>
<span class="help-block m-b-none">Please provide a valid function which starts with <strong>function</strong> keyword</span>
</div>
</div>
......@@ -12,8 +11,7 @@
<label class="col-lg-1 control-label">Reduce</label>
<div class="col-lg-11">
<pre class="form-control" style="height: 150px" id='aceReduce'></pre>
<span class="help-block m-b-none">You can use <strong>string</strong> to query <strong>ISODate</strong> and <strong>ObjectID</strong>, date format is <strong>YYYY-MM-DD
HH:mm:ss</strong><br/>Please provide a valid function which starts with <strong>function</strong> keyword</span>
<span class="help-block m-b-none">Please provide a valid function which starts with <strong>function</strong> keyword</span>
</div>
</div>
......
......@@ -41,16 +41,14 @@ Template.mapReduce.executeQuery = function () {
var map = ace.edit("aceMap").getSession().getValue();
var reduce = ace.edit("aceReduce").getSession().getValue();
map = map.parseFunction();
reduce = reduce.parseFunction();
if (map == null) {
if (map.parseFunction() == null) {
toastr.error("Syntax error on map, not a valid function ");
Ladda.stopAll();
return;
}
if (reduce == null) {
if (reduce.parseFunction() == null) {
toastr.error("Syntax error on reduce, not a valid function ");
Ladda.stopAll();
return;
......
......@@ -42,8 +42,7 @@
<label class="col-lg-1 control-label">finalize</label>
<div class="col-lg-11">
<pre class="form-control" style="height: 150px" id='aceFinalize'></pre>
<span class="help-block m-b-none">You can use <strong>string</strong> to query <strong>ISODate</strong> and <strong>ObjectID</strong>, date format is <strong>YYYY-MM-DD
HH:mm:ss</strong><br/>Please provide a valid function which starts with <strong>function</strong> keyword</span>
<span class="help-block m-b-none">Please provide a valid function which starts with <strong>function</strong> keyword</span>
</div>
</div>
</template>
......
......@@ -44,9 +44,7 @@ Template.mapReduceOptions.getOptions = function () {
if ($.inArray("FINALIZE", Session.get(Template.strSessionSelectedOptions)) != -1) {
var finalize = ace.edit("aceFinalize").getSession().getValue();
finalize = finalize.parseFunction();
if (finalize == null) {
if (finalize.parseFunction() == null) {
result["ERROR"] = "Syntax Error on finalize, not a valid function";
return;
}
......
......@@ -23,12 +23,7 @@ Meteor.methods({
},
'mapReduce': function (connection, selectedCollection, map, reduce, options) {
var methodArray = [
{
"mapReduce": [map, reduce, options]
}
];
return proceedQueryExecution(connection, selectedCollection, methodArray);
return proceedMapReduceExecution(connection, selectedCollection, map, reduce, options);
},
'isCapped': function (connection, selectedCollection) {
......@@ -211,6 +206,54 @@ Meteor.methods({
}
});
// TODO mapReduce BSON support for ObjectID and Date in map,reduce,finalize function strings
var proceedMapReduceExecution = function (connection, selectedCollection, map, reduce, options) {
var connectionUrl = getConnectionUrl(connection);
var mongodbApi = Meteor.npmRequire('mongodb').MongoClient;
convertJSONtoBSON(options);
console.log('Connection: ' + connectionUrl + '/' + selectedCollection + ', Map: ' + map + ', Reduce: ' + reduce + ',Options: ' + JSON.stringify(options));
var result = Async.runSync(function (done) {
mongodbApi.connect(connectionUrl, function (mainError, db) {
if(mainError){
done(mainError, null);
if (db) {
db.close();
}
return;
}
try {
var collection = db.collection(selectedCollection);
collection.mapReduce(map, reduce, options, function (err, resultCollection) {
if (err) {
done(err, null);
if (db) {
db.close();
}
return;
}
resultCollection.find().toArray(function (err, result) {
done(err, result);
if (db) {
db.close();
}
});
});
}
catch (ex) {
done(new Meteor.Error(ex.message), null);
if (db) {
db.close();
}
}
});
});
convertBSONtoJSON(result);
return result;
};
var proceedQueryExecution = function (connection, selectedCollection, methodArray) {
var connectionUrl = getConnectionUrl(connection);
var mongodbApi = Meteor.npmRequire('mongodb').MongoClient;
......@@ -218,7 +261,14 @@ var proceedQueryExecution = function (connection, selectedCollection, methodArra
console.log('Connection: ' + connectionUrl + '/' + selectedCollection + ', MethodArray: ' + JSON.stringify(methodArray));
var result = Async.runSync(function (done) {
mongodbApi.connect(connectionUrl, function (err, db) {
mongodbApi.connect(connectionUrl, function (mainError, db) {
if(mainError){
done(mainError, null);
if (db) {
db.close();
}
return;
}
try {
var execution = db.collection(selectedCollection);
for (var i = 0; i < methodArray.length; i++) {
......@@ -230,7 +280,9 @@ var proceedQueryExecution = function (connection, selectedCollection, methodArra
if (last && key == Object.keys(entry)[Object.keys(entry).length - 1]) {
entry[key].push(function (err, docs) {
done(err, docs);
db.close();
if (db) {
db.close();
}
});
execution[key].apply(execution, entry[key]);
}
......@@ -241,9 +293,10 @@ var proceedQueryExecution = function (connection, selectedCollection, methodArra
}
}
catch (ex) {
console.error(ex);
done(ex, null);
db.close();
done(new Meteor.Error(ex.message), null);
if (db) {
db.close();
}
}
});
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册