Reputation: 177
I have a replication filter in JavaScript which is running too slowly, and it was suggested that I try re-writing it in erlang.
I've set up the environment, and am able to successfully execute simple erlang views in the futon "temp_view" dialog, but I'm not having any luck translating my JavaScript function to erlang.
Can anyone suggest how the following might best be re-written in erlang? Any help would be greatly appreciated.
function(doc, req) {
if (doc.date && doc.user_id && (doc.user_id == req.query.userid) && (doc._id.indexOf(\"_design\") != 0)){
var doc_month = "" + doc.date[1];
if(doc_month.length == 1) {
doc_month = "0" + doc_month;
}
var doc_day = "" + doc.date[2];
if(doc_day.length == 1) {
doc_day = "0" + doc_day;
}
var req_month = "" + req.query.month;
if(req_month.length == 1) {
req_month = "0" + req_month;
}
var req_day = "" + req.query.day;
if(req_day.length == 1) {
req_day = "0" + req_day;
}
var doc_datestring = doc.date[0] + "-" + doc_month + "-" + doc_day;
var req_datestring = req.query.year + "-" + req_month + "-" + req_day;
return (doc_datestring >= req_datestring);
} else {
return false;
}
}
Upvotes: 1
Views: 256
Reputation: 7275
I just finished writing something that I believe is similar to your javascript function. I have a year and a month field in my documents - both represented as integers.
This is what I ended up with:
{
"_id": "_design/fast_segmenting",
"language": "erlang",
"filters": {
"by_year_month": "... see below for erlang function ..."
}
}
Kind of ugly, so here is the Erlang filter function in a better format:
fun({Doc}, {Req}) ->
{Query} = proplists:get_value((<<\"query\">>, Req),
Month = list_to_integer(binary_to_list(proplists:get_value((<<\"month\">>, Query))),
Year = list_to_integer(binary_to_list(proplists:get_value((<<\"year\">>, Query))),
case {proplists:get_value((<<\"pubMonth\">>, Doc), proplists:get_value((<<\"pubYear\">>, Doc)} of
{Month, Year} -> true;
_ -> false
end
end.
The most fun was discovering that the values in the Query object are binaries in Erlang, which is why we need to convert them to integers.
Upvotes: 1