2

Preface:

in order to have clean and effective code, i want to use external functions in my mapreduce mongo script.

Problem:

given we have following map function (coffeescript syntax):

map: -> 
   key = foo(@field)
   emit(key, value)

Calling an external function 'foo' raise an error

➜ rake mongo:mapreduce
MongoDB shell version: 2.0.5
connecting to: localhost:27017/connect_development
{
    "assertion" : "map invoke failed: JS Error: ReferenceError: foo is not defined nofile_b:2",
    "assertionCode" : 9014,
    "errmsg" : "db assertion failure",
    "ok" : 0
}

The same we will return for reduce context call.

Bad smell decision - self-called anonymous function:

map: -> 
   key = ( (field)->
     # some business logic
   )(@field)

   emit(key, value)

Self-called anonymous function could be very big and not effective to test and may cause leaking memory (not sure on this).

How to resolve this problem?

UPD:

When i said "external function", meant function declared in same file (in same class) with "map/reduce" functions. Of Course, it's invoked on server-side.

4

2 回答 2

2

Map/reduce 函数必须在数据库服务器上运行,在另一个上下文中,所以它们不能触及任何“外部”。

使用内联匿名函数并没有错,它们非常便宜——只要避免深度递归。CoffeeScript 具有用于创建您可能想要使用的闭包的语法:

map: -> 
  key = do =>
    k = @field.doSomething()
    return k
  emit key, value
于 2012-07-05T00:23:12.450 回答
0

Map/Reduce 代码在 MongoDB 服务器上执行。您确实可以选择在服务器端存储可以从 Map/Reduce 调用的函数,但最佳实践建议将函数与您的其余代码一起保存在版本控制中。匿名函数在 JavaScript 中很常用,应该不是问题。

于 2012-07-05T00:29:26.473 回答