javascript - 将 CouchDB javascript View 转换为 erlang

标签 javascript erlang couchdb

我需要一些帮助将以下 CouchDB View 从 javascript 转换为 erlang。我在 erlang 中需要它们,因为在 javascript 中, View 使用了所有可用的堆栈内存并使 couchjs 崩溃(请参阅此错误报告 https://issues.apache.org/jira/browse/COUCHDB-893)。

我在 javascript 中的当前 map 函数是:

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 会发出交易字典中的所有键,而 sync/transaction 会发出交易字典中的所有条目。

不幸的是,我以前从未使用过 Erlang,我需要尽快重写该代码,因此非常欢迎任何帮助。

提前致谢。

最佳答案

我刚做了你的第二个(更复杂的那个)。第一个可以很容易地从那里推断出来:

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.

关于javascript - 将 CouchDB javascript View 转换为 erlang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6800688/

相关文章:

用于存储文件 + 复制的 MongoDB/CouchDB?

java - 如何使用破折号作为分隔符在 JSTL 中获取语言环境?

javascript - 为什么 for..in 循环不遍历对象的原型(prototype)

mysql - 哪个 NoSQL 数据库最适合存储聊天消息?

ssl - 使用 Erlang 无需验证即可获取客户端证书

binary - Erlang 二进制分割

CouchDB 的 C# 库?

ubuntu - Couchdb 无法在 Ubuntu hardy 8.04 上启动

javascript - Angular 更优雅的隐藏/显示模式吗?

javascript - 显式赋值后 JavaScript 中的变量未定义?