Faster min and max of various array components with CouchDb map / reduce?

I have a CouchDB database with a view whose values ​​are paired numbers of the form [x, y]. For documents with the same key, I need to (simultaneously) calculate the minimum x and maximum y. The database in which I work contains about 50,000 documents. Building a look takes several hours, which seems somewhat excessive. (The keys themselves are three length arrays.) I show the map and reduce the functions below, but the main question is: how can I speed up this process?

Note that inline functions will not work, because the values ​​must be numbers, not a length or two arrays. It is possible that I could make two different representations (one for min (x) and one for max (y)), but it is not clear to me how to combine them to get both results at the same time.

My current display function looks mostly like

function(doc) {
  emit ([doc.a, doc.b, doc.c], [doc.x, doc.y])
}

and my reduction function looks like

function(keys, values) {
  var x = null;
  var y = null;
  for (i = 0; i < values.length; i++) {
    if (values[i][0] == null) break;
    if (values[i][1] == null) break;
    if (x == null) x = values[i][0];
    if (y == null) y = values[i][1];
    if (values[i][0] < x) x = values[i][0];
    if (values[i][1] > y) y = values[i][1];
  }
  emit([x, y]);
}
+3
source share
3 answers

Two more notes. Using Math.max () and Math.min () should be a little faster.

function(keys, values) {
  var x = -Infinity,
      y = Infinity;
  for (var i = 0, v; v = values[i]; i++) {
    x = Math.max(x, v[0]);
    y = Math.min(y, v[1]);
  }
  return [x, y];
}

And if CouchDB treats the values ​​as strings, this is because you store them as strings in the document.

Hope this helps.

+2
source

. , "emit", "return".

. "" , CouchDB . parseInt .

, .

+1

Please check out http://www.geeksforgeeks.org/archives/4583 . This can be extended to your application.

0
source

All Articles