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

map reduce fixes and improvements

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