couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dam...@apache.org
Subject svn commit: r663786 - in /incubator/couchdb/trunk: share/www/script/couch_tests.js src/couchdb/couch_btree.erl src/couchdb/couch_httpd.erl
Date Thu, 05 Jun 2008 23:49:48 GMT
Author: damien
Date: Thu Jun  5 16:49:48 2008
New Revision: 663786

URL: http://svn.apache.org/viewvc?rev=663786&view=rev
Log:
Added reduce/combine example. Fixed  broken node chunking with very large keys/reduction values

Modified:
    incubator/couchdb/trunk/share/www/script/couch_tests.js
    incubator/couchdb/trunk/src/couchdb/couch_btree.erl
    incubator/couchdb/trunk/src/couchdb/couch_httpd.erl

Modified: incubator/couchdb/trunk/share/www/script/couch_tests.js
URL: http://svn.apache.org/viewvc/incubator/couchdb/trunk/share/www/script/couch_tests.js?rev=663786&r1=663785&r2=663786&view=diff
==============================================================================
--- incubator/couchdb/trunk/share/www/script/couch_tests.js [utf-8] (original)
+++ incubator/couchdb/trunk/share/www/script/couch_tests.js [utf-8] Thu Jun  5 16:49:48 2008
@@ -294,6 +294,9 @@
       result = db.query(map, reduce, {startkey: i, endkey: numDocs - i});
       T(result.rows[0].value == summate(numDocs-i) - summate(i-1));
     }
+    
+    db.deleteDb();
+    db.createDb();
 
     for(var i=1; i <= 5; i++) {
       
@@ -340,6 +343,68 @@
       T(equals(results.rows[5], {key:["d","b"],value:10*i}));
       T(equals(results.rows[6], {key:["d","c"],value:10*i}));
     }
+    
+    // now test out more complex reductions that need to use the combine option.
+    
+    db.deleteDb();
+    db.createDb();
+
+      
+    var map = function (doc) {emit(null, doc.val)};
+    var reduceCombine = function (keys, values, combine) {
+        // this computes the standard deviation
+        
+        var stdDeviation=0;
+        var count=0;
+        var total=0;
+        var sqrTotal=0;
+          
+        if (combine) {
+          for(var i in values) {
+            count = count + values[i].count;
+            total = total + values[i].total;
+            sqrTotal = sqrTotal + (values[i].sqrTotal * values[i].sqrTotal);
+          }
+          var variance =  (sqrTotal - ((total * total)/count)) / count;
+          stdDeviation = Math.sqrt(variance);
+          
+          return {"stdDeviation":stdDeviation,"count":count,
+              "total":total,"sqrTotal":sqrTotal};
+        }
+        else {
+          for(var i in values) {
+            total = total + values[i]
+            sqrTotal = sqrTotal + (values[i] * values[i])
+          }
+          count = values.length;
+          var variance =  (sqrTotal - ((total * total)/count)) / count;
+          stdDeviation = Math.sqrt(variance);
+        }
+          
+        return {"stdDeviation":stdDeviation,"count":count,
+            "total":total,"sqrTotal":sqrTotal};
+      };
+      
+      for(var j=0; j < 10; j++) {
+        // these docs are in the order of the keys collation, for clarity
+        var docs = [];
+        docs.push({val:10});
+        docs.push({val:20});
+        docs.push({val:30});
+        docs.push({val:40});
+        docs.push({val:50});
+        docs.push({val:60});
+        docs.push({val:70});
+        docs.push({val:80});
+        docs.push({val:90});
+        docs.push({val:100});
+        T(db.bulkSave(docs).ok);
+      }
+      
+      var results = db.query(map, reduceCombine);
+      //account for floating point error
+      T(results.rows[0].value.stdDeviation == 28.722813232690143);
+      
   },
 
   multiple_rows: function(debug) {

Modified: incubator/couchdb/trunk/src/couchdb/couch_btree.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/trunk/src/couchdb/couch_btree.erl?rev=663786&r1=663785&r2=663786&view=diff
==============================================================================
--- incubator/couchdb/trunk/src/couchdb/couch_btree.erl (original)
+++ incubator/couchdb/trunk/src/couchdb/couch_btree.erl Thu Jun  5 16:49:48 2008
@@ -255,7 +255,7 @@
     lists:reverse([lists:reverse(OutList) | OutputChunks]);
 chunkify(Bt, [InElement | RestInList], ChunkThreshold, OutList, OutListSize, OutputChunks)
->
     case size(term_to_binary(InElement)) of
-    Size when (Size + OutListSize) > ChunkThreshold ->
+    Size when (Size + OutListSize) > ChunkThreshold andalso OutList /= [] ->
         chunkify(Bt, RestInList, ChunkThreshold, [], 0, [lists:reverse([InElement | OutList])
| OutputChunks]);
     Size ->
         chunkify(Bt, RestInList, ChunkThreshold, [InElement | OutList], OutListSize + Size,
OutputChunks)
@@ -398,6 +398,9 @@
     end.
 
 
+reduce_stream_node(_Bt, _Dir, nil, _KeyStart, _KeyEnd, GroupedKey, GroupedKVsAcc, 
+        GroupedRedsAcc, _KeyGroupFun, _Fun, Acc) ->
+    {ok, Acc, GroupedRedsAcc, GroupedKVsAcc, GroupedKey}; 
 reduce_stream_node(Bt, Dir, {P, _R}, KeyStart, KeyEnd, GroupedKey, GroupedKVsAcc, 
         GroupedRedsAcc, KeyGroupFun, Fun, Acc) ->
     case get_node(Bt, P) of

Modified: incubator/couchdb/trunk/src/couchdb/couch_httpd.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/trunk/src/couchdb/couch_httpd.erl?rev=663786&r1=663785&r2=663786&view=diff
==============================================================================
--- incubator/couchdb/trunk/src/couchdb/couch_httpd.erl (original)
+++ incubator/couchdb/trunk/src/couchdb/couch_httpd.erl Thu Jun  5 16:49:48 2008
@@ -469,7 +469,9 @@
             Resp:write_chunk(AccSeparator ++ Json),
             {ok, {",",0,AccCount-1}};
         (Key, Red, {AccSeparator,0,AccCount})
-                when is_tuple(Key) and is_integer(GroupLevel) ->
+                when is_integer(GroupLevel) 
+                andalso is_tuple(Key) 
+                andalso element(1, Key) /= obj  ->
             Json = lists:flatten(cjson:encode(
                 {obj, [{key, list_to_tuple(lists:sublist(tuple_to_list(Key), GroupLevel))},
                         {value, Red}]})),
@@ -557,8 +559,7 @@
                     Json = lists:flatten(cjson:encode({obj, [{ok, JsonDoc}]})),
                     Resp:write_chunk(AccSeparator ++ Json);
                 {{not_found, missing}, RevId} ->
-                    Json = {obj, [{"missing", RevId}]},
-                    Json = lists:flatten(cjson:encode(Json)),
+                    Json = lists:flatten(cjson:encode({obj, [{"missing", RevId}]})),
                     Resp:write_chunk(AccSeparator ++ Json)
                 end,
                 "," % AccSeparator now has a comma



Mime
View raw message