本文介绍了Couchbase减少问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是Couchbase文档的编码,我不理解

Here is coding from Couchbase Document and I dont understand it

function(key, values, rereduce) {
  var result = {total: 0, count: 0};
  for(i=0; i < values.length; i++) {
    if(rereduce) {
        result.total = result.total + values[i].total;
        result.count = result.count + values[i].count;
    } else {
        result.total = sum(values);
        result.count = values.length;
    }
  }
  return(result);
}




  1. 减少表示当前函数调用已完成减少与否。对?

  2. reduce函数的第一个参数,key,何时使用?我看到了很多例子,键似乎没有使用

  3. 什么时候rereduce返回true并且数组大小大于1?

  4. 再次, rereduce return何时返回false并且数组大小大于1?

  1. rereduce means the current function call has already done the reduce or not. right?
  2. the first argument of the reduce function, key, when will it be used? I saw a numbers of examples, key seems to be unused
  3. When does rereduce return true and the array size is more than 1?
  4. Again, When does rereduce return is false and the array size is more than 1?


推荐答案


  1. Rereduce意味着以前调用了reduce函数,现在使用第一个reduce调用返回的参数再次调用reduce函数。因此,如果将其分为两个函数,它将看起来像:

  1. Rereduce means that the reduce function is called before and now it is called again with params that were returnd as a result in first reduce call. So if we devide it into two functions it will look like:

function reduce(k,v){
  // ... doing something with map results
  // instead of returning result we must call rereduce function)
  rereduce(null, result)
}
function rereduce(k,v){
  // do something with first reduce result
}

如果您在群集中有2个或更多服务器,或者您的数据库中有很多项目,并且在B * Tree的多个节点上进行了计算,则将出现减少情况的情况。具有2个服务器的示例将更易于理解:
让我们想象一下,您的map函数返回了对: [key1-1,key2-2,key6-6] 第一个服务器,第二个服务器 [key5-5,key7-7] 。您将获得2个reduce函数调用,它们分别是:
reduce([key1,key2,key6],[1,2,6],false) reduce([key5,key7],[5,7],false)。然后,如果我们只是返回值(在reduce中不执行任何操作,仅返回值),则将使用以下参数调用reduce函数: reduce(null,[[1,2,6],[5,7 ]],true)。这里的值将是来自第一次reduce调用的结果数组。

In most cases rereduce will happen when you have 2 or more servers in cluster or you have a lot of items in your database and the calculation is done on multiple "nodes" of the B*Tree. Example with 2 servers will be easier to understand:Let's imagine that your map function returned pairs: [key1-1, key2-2, key6-6] from 1st server and [key5-5,key7-7] from 2nd. You'll get 2 reduce function calls with:reduce([key1,key2,key6],[1,2,6],false) and reduce([key5,key7],[5,7],false). Then if we just return values (do nothing in reduce, just return values), the reduce function will be called with such params: reduce(null, [[1,2,6],[5,7]], true). Here values will be an array of results that came from first reduce calls.

在onreduce上,键将为null。值将是先前的reduce()函数返回的值数组。

On rereduce key will be null. Values will be an array of values as returned by a previous reduce() function.

您可以尝试从和。即您可以修改reduce函数以查看每一步返回的结果:

You can just try to run examples from Views basics and Views with reduce. I.e. you can modify reduce function to see what it returns on each step:

function reduce(k,v,r){
 if (!r){
   // let reduce function return only one value:
   return 1;
 } else {
   // and lets see what values have came in "rereduce"
   return v; 
 }
}

这篇关于Couchbase减少问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 02:52