问题描述
我需要一些帮助,将以下CouchDB视图从javascript转换为erlang。我需要它们在erlang,因为在javascript视图使用所有可用的堆栈内存和崩溃couchjs(请参阅这个bugreport )。
I need some help with translating the following CouchDB views from javascript to erlang. I need them in erlang, because in javascript the view uses all of the available stack memory and crashes couchjs (see this bugreport https://issues.apache.org/jira/browse/COUCHDB-893).
我在javascript中的当前地图功能是:
The current map functions I have in javascript are:
sync / transaction_keys
function(doc) {
if(doc.doc_type == "Device") {
for(key in doc.transactions)
emit(key, null);
}
}
和同步/转录
function(doc) {
if(doc.doc_type == "Device") {
for(key in doc.transactions) {
t = doc.transactions[key];
t.device = doc.device;
emit(key, t);
}
}
}
一个示例文档将是:
{
"_id": "fcef7b5c-cbe6-31af-8363-2b446a7e4cf2",
"_rev": "3-c90abd075404a75744fd3e5e4f04ebad",
"device": "fcef7b5c-cbe6-31af-8363-2b446a7e4cf2",
"doc_type": "Device",
"transactions": {
"79fe8630-c0c0-30c6-9913-79b2f93e3e6e": {
"timestamp": 1309489169533,
"version": 10008,
"some_more_data" : "more_data"
}
"e4678930-c465-76a6-8821-75a3e888765a": {
"timestamp": 1309489169533,
"version": 10008,
"some_more_data" : "more_data"
}
}
}
基本上sync / transaction_keys发出事务字典和同步/事务的所有键都会发出事务字典中的所有条目。
Basically sync/transaction_keys emits all keys of the transaction dictionary and sync/transaction does emit all entries in the transaction dictionary.
不幸的是,我从来没有使用过Erlang d很快就重写这个代码,所以任何帮助都是非常受欢迎的。
Unfortunately I never used Erlang before and I need to rewrite that code pretty soon, so any help is very welcomed.
提前感谢。
推荐答案
我刚刚做了你的第二个(越复杂的一个)。第一个可以从那里轻松推断:
I just did your second one (the more complicated one). The first can easily be extrapolated from there:
fun({Doc}) ->
%% Helper function to get a toplevel value from this doc.
F = fun(B) -> proplists:get_value(B, Doc) end,
%% switch on doc type
case F(<<"doc_type">>) of
<<"Device">> ->
%% Grab the transactions from this document
{Txns} = F(<<"transactions">>),
lists:foreach(fun({K,V}) ->
%% Emit the key and the value as
%% the transaction + the device
%% id
{T} = proplists:get_value(K, Txns),
Emit(K, {[{<<"device">>, F(<<"device">>)} | T]})
end,
Txns);
_ -> false %% Not a device -- ignoring this document
end
end.
这篇关于将CouchDB的javascript视图转换为erlang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!