couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cml...@apache.org
Subject svn commit: r644734 - in /incubator/couchdb/branches/mochiweb: ./ share/server/ share/www/script/ src/couchdb/
Date Fri, 04 Apr 2008 15:02:57 GMT
Author: cmlenz
Date: Fri Apr  4 08:02:51 2008
New Revision: 644734

URL: http://svn.apache.org/viewvc?rev=644734&view=rev
Log:
mochiweb branch: Merged revisions 642965-644443,644445-644674 via svnmerge from /incubator/couchdb/trunk.

Modified:
    incubator/couchdb/branches/mochiweb/   (props changed)
    incubator/couchdb/branches/mochiweb/AUTHORS
    incubator/couchdb/branches/mochiweb/BUGS
    incubator/couchdb/branches/mochiweb/NEWS
    incubator/couchdb/branches/mochiweb/NOTICE
    incubator/couchdb/branches/mochiweb/README
    incubator/couchdb/branches/mochiweb/THANKS
    incubator/couchdb/branches/mochiweb/share/server/main.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/browse.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/couch.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/couch_tests.js   (contents, props
changed)
    incubator/couchdb/branches/mochiweb/share/www/script/jquery.cookies.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/jquery.dialog.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/jquery.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/jquery.resizer.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/jquery.suggest.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/json2.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/pprint.js   (props changed)
    incubator/couchdb/branches/mochiweb/share/www/script/shell.js   (props changed)
    incubator/couchdb/branches/mochiweb/src/couchdb/couch_btree.erl
    incubator/couchdb/branches/mochiweb/src/couchdb/couch_db.erl
    incubator/couchdb/branches/mochiweb/src/couchdb/couch_file.erl
    incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl
    incubator/couchdb/branches/mochiweb/src/couchdb/couch_stream.erl
    incubator/couchdb/branches/mochiweb/src/couchdb/couch_util.erl
    incubator/couchdb/branches/mochiweb/src/couchdb/couch_view.erl

Propchange: incubator/couchdb/branches/mochiweb/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-/incubator/couchdb/trunk:1-642955
+/incubator/couchdb/trunk:1-642955,642965-644443,644445-644674

Modified: incubator/couchdb/branches/mochiweb/AUTHORS
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/AUTHORS?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/AUTHORS (original)
+++ incubator/couchdb/branches/mochiweb/AUTHORS Fri Apr  4 08:02:51 2008
@@ -1,4 +1,5 @@
-# Authors #
+Apache CouchDB AUTHORS
+======================
 
 Apache CouchDB was originally developed by Damien Katz <damien@apache.org> and
 he remains the project lead.
@@ -6,10 +7,8 @@
 A number of people have contributed directly to Apache CouchDB by writing
 documentation or developing software. A list of these people is included below.
 
- * William Beh <willbeh@gmail.com>
  * Jan Lehnardt <jan@apache.org>
  * Christopher Lenz <cmlenz@apache.org>
- * Dirk Schalge <dirk@epd-me.net>
  * Noah Slater <nslater@apache.org>
 
 For a list of other credits see the `THANKS` file.

Modified: incubator/couchdb/branches/mochiweb/BUGS
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/BUGS?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/BUGS (original)
+++ incubator/couchdb/branches/mochiweb/BUGS Fri Apr  4 08:02:51 2008
@@ -1,4 +1,5 @@
-# Bugs #
+Apache CouchDB BUGS
+===================
 
 Please see the [documentation][1] on how to report bugs with Apache CouchDB.
 

Modified: incubator/couchdb/branches/mochiweb/NEWS
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/NEWS?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/NEWS (original)
+++ incubator/couchdb/branches/mochiweb/NEWS Fri Apr  4 08:02:51 2008
@@ -1,6 +1,8 @@
-# News #
+Apache CouchDB NEWS
+===================
 
-## Version 0.7.3a1 ##
+Version 0.7.3a1
+---------------
 
 This version is unreleased and only available from the source repository.
 
@@ -13,16 +15,19 @@
  * System logrotate configuration provided.
  * Improved handling of ICU shared libraries.
 
-## Version 0.7.2 ##
+Version 0.7.2
+-------------
 
  * Small changes to build process and `couchdb` command.
  * Database server official port is now 5984 TCP/UDP instead of 8888.
 
-## Version 0.7.1 ##
+Version 0.7.1
+-------------
 
  * Small compatibility issue with Firefox 3 fixed.
 
-## Version 0.7.0 ##
+Version 0.7.0
+-------------
 
  * Infrastructure rewritten to use the GNU build system for portability.
  * The built-in database browsing tool has been rewritten to provide a much
@@ -30,19 +35,22 @@
  * XML and Fabric have been replaced with JSON and JavaScript for data
    transport and View definitions.
 
-## Version 0.6.0 ##
+Version 0.6.0
+-------------
 
  * A replication facility is now available.
  * CouchPeek can now create, delete and view documents.
  * Building from source is easier and less error prone.
 
-## Version 0.5.0 ##
+Version 0.5.0
+-------------
 
  * A built-in CouchPeek utility.
  * A full install kit buildable from a single command.
  * A new GNU/Linux version is available. An OS X version is coming soon.
 
-## Version 0.4.0 ##
+Version 0.4.0
+-------------
 
  * Non-existant variables are now nil lists.
  * Couch error codes and messages are no longer sent in the HTTP fields,
@@ -57,7 +65,8 @@
  * Support for Perl/Ruby like regular expressions.
  * Added `total_rows` and `result_start` attributes to tables.
 
-## Version 0.3.0 ##
+Version 0.3.0
+-------------
 
  * CouchDB now fully supports Unicode and locale specific collation via the ICU
    library, both in the Fabric engine and computed tables.

Modified: incubator/couchdb/branches/mochiweb/NOTICE
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/NOTICE?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/NOTICE (original)
+++ incubator/couchdb/branches/mochiweb/NOTICE Fri Apr  4 08:02:51 2008
@@ -1,19 +1,20 @@
-# Notice #
+Apache CouchDB NOTICE
+=====================
 
 Developed at [The Apache Software Foundation][1].
 
 Core software:
 
- * Copyright (C) 2008 Damien Katz <damien@apache.org>.
- * Copyright (C) 2008 Jan Lehnardt <jan@apache.org>.
- * Copyright (C) 2008 Christopher Lenz <cmlenz@apache.org>.
- * Copyright (C) 2008 Noah Slater <nslater@apache.org>.
+ * Copyright 2008 Damien Katz <damien@apache.org>
+ * Copyright 2008 Jan Lehnardt <jan@apache.org>
+ * Copyright 2008 Christopher Lenz <cmlenz@apache.org>
+ * Copyright 2008 Noah Slater <nslater@apache.org>
 
 Supporting software:
 
- * m4/ac_check_icu.m4 (Copyright (C) 2005 Akos Maroy <darkeye@tyrell.hu>.)
- * share/www/script/jquery.js (Copyright (C) 2008 John Resig.)
- * share/www/script/json2.js (This file is in the public domain.)
- * src/mochiweb (Copyright (C) 2007 Mochi Media, Inc.)
+ * m4/ac_check_icu.m4 - Copyright 2005 Akos Maroy <darkeye@tyrell.hu>
+ * share/www/script/jquery.js - Copyright 2008 John Resig
+ * share/www/script/json2.js - Public domain
+ * src/mochiweb - Copyright 2007 Mochi Media, Inc.
 
 [1]: http://www.apache.org/

Modified: incubator/couchdb/branches/mochiweb/README
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/README?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/README (original)
+++ incubator/couchdb/branches/mochiweb/README Fri Apr  4 08:02:51 2008
@@ -1,6 +1,8 @@
-# Readme #
+Apache CouchDB README
+=====================
 
-## Building From Subversion ##
+Building From Subversion
+------------------------
 
 You can skip this section if you are installing from a release tarball.
 
@@ -40,7 +42,8 @@
 
 You can use the `-C` option to generate a dummy `ChangeLog` file.
 
-## Installation And First Run ##
+Installation And First Run
+--------------------------
 
 You will need the following installed:
 
@@ -233,7 +236,8 @@
 
 Windows documentation is incomplete. Please submit suggestions.
 
-## Development ##
+Development
+-----------
 
 ### Reconfiguring the Build System ###
 

Modified: incubator/couchdb/branches/mochiweb/THANKS
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/THANKS?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/THANKS (original)
+++ incubator/couchdb/branches/mochiweb/THANKS Fri Apr  4 08:02:51 2008
@@ -1,14 +1,17 @@
-# Thanks #
+Apache CouchDB THANKS
+=====================
 
 Apache CouchDB was originally developed by Damien Katz <damien_katz@yahoo.com>
 and a number of other contributors. Many people further contributed to Apache
 CouchDB by reporting problems, suggesting various improvements or submitting
 changes. A list of these people is included below.
 
+ * William Beh <willbeh@gmail.com>
  * Benoit Chesneau <bchesneau@gmail.com>
  * Till Klampaeckel <till@klampaeckel.de>
  * Roger Leigh <rleigh@debian.org>
  * Sam Ruby <rubys@intertwingly.net>
+ * Dirk Schalge <dirk@epd-me.net>
  * Carlos Valiente <superdupont@gmail.com>
 
 For a list of authors see the `AUTHORS` file.

Propchange: incubator/couchdb/branches/mochiweb/share/server/main.js
------------------------------------------------------------------------------
    svn:mime-type = text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/browse.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/couch.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Modified: incubator/couchdb/branches/mochiweb/share/www/script/couch_tests.js
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/share/www/script/couch_tests.js?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
Binary files - no diff available.

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/couch_tests.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/jquery.cookies.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/jquery.dialog.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/jquery.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/jquery.resizer.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/jquery.suggest.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/json2.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/pprint.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Propchange: incubator/couchdb/branches/mochiweb/share/www/script/shell.js
------------------------------------------------------------------------------
--- svn:mime-type (original)
+++ svn:mime-type Fri Apr  4 08:02:51 2008
@@ -1 +1 @@
-application/javascript
+text/javascript;charset=utf-8

Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_btree.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/src/couchdb/couch_btree.erl?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/src/couchdb/couch_btree.erl (original)
+++ incubator/couchdb/branches/mochiweb/src/couchdb/couch_btree.erl Fri Apr  4 08:02:51 2008
@@ -12,7 +12,7 @@
 
 -module(couch_btree).
 
--export([open/2, open/3, query_modify/4, add_remove/3, foldl/3, foldl/4]).
+-export([open/2, open/3, query_modify/4, add/2, add_remove/3, foldl/3, foldl/4]).
 -export([foldr/3, foldr/4, fold/4, fold/5, row_count/1]).
 -export([lookup/2, get_state/1, test/1, test/0]).
 
@@ -85,9 +85,12 @@
     {_ContinueFlag, Acc2} = stream_node(Bt, 0, Bt#btree.root, Key, Dir, convert_fun_arity(Fun),
Acc),
     {ok, Acc2}.
 
+add(Bt, InsertKeyValues) ->
+    add_remove(Bt, InsertKeyValues, []).
+
 add_remove(Bt, InsertKeyValues, RemoveKeys) ->
-    {Result, [], Bt2} = query_modify(Bt, [], InsertKeyValues, RemoveKeys),
-    {Result, Bt2}.
+    {ok, [], Bt2} = query_modify(Bt, [], InsertKeyValues, RemoveKeys),
+    {ok, Bt2}.
 
 query_modify(Bt, LookupKeys, InsertValues, RemoveKeys) ->
     #btree{root=Root} = Bt,

Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_db.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/src/couchdb/couch_db.erl?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/src/couchdb/couch_db.erl (original)
+++ incubator/couchdb/branches/mochiweb/src/couchdb/couch_db.erl Fri Apr  4 08:02:51 2008
@@ -13,20 +13,21 @@
 -module(couch_db).
 -behaviour(gen_server).
 
--export([open/2,create/2,create/3,get_doc_info/2]).
+-export([open/2,create/2,create/3,get_doc_info/2,start_compact/1]).
 -export([save_docs/2, save_docs/3, get_db_info/1, update_doc/3, update_docs/2, update_docs/3]).
--export([delete_doc/3,open_doc/2,open_doc/3,close/1,enum_docs_since/4,enum_docs_since/5]).
+-export([delete_doc/3,open_doc/2,open_doc/3,enum_docs_since/4,enum_docs_since/5]).
 -export([enum_docs/4,enum_docs/5, open_doc_revs/4, get_missing_revs/2]).
--export([start_update_loop/1]).
+-export([start_update_loop/2]).
 -export([init/1,terminate/2,handle_call/3,handle_cast/2,code_change/3,handle_info/2]).
+-export([start_copy_compact_int/2]).
 
 -include("couch_db.hrl").
 
 -record(db_header,
     {write_version = 0,
-     last_update_seq = 0,
+     update_seq = 0,
      summary_stream_state = nil,
-     docinfo_by_Id_btree_state = nil,
+     fulldocinfo_by_id_btree_state = nil,
      docinfo_by_seq_btree_state = nil,
      local_docs_btree_state = nil,
      doc_count=0,
@@ -34,32 +35,64 @@
     }).
 
 -record(db,
-    {main_pid,
-    update_pid,
+    {main_pid=nil,
+    update_pid=nil,
+    compactor_pid=nil,
     fd,
     header = #db_header{},
     summary_stream,
-    docinfo_by_Id_btree,
+    fulldocinfo_by_id_btree,
     docinfo_by_seq_btree,
     local_docs_btree,
-    last_update_seq,
+    update_seq,
     doc_count,
     doc_del_count,
-    name
+    name,
+    filepath
     }).
 
+% small value used in revision trees to indicate the revision isn't stored
+-define(REV_MISSING, []).
+
 start_link(DbName, Filepath, Options) ->
+    catch start_link0(DbName, Filepath, Options).
+        
+start_link0(DbName, Filepath, Options) ->
+     % first delete the old file previous compaction
+    Fd = 
     case couch_file:open(Filepath, Options) of
-    {ok, Fd} ->
-         Result = gen_server:start_link(couch_db, {DbName, Fd, Options}, []),
-         unlink(Fd),
-         Result;
+    {ok, Fd0} ->
+        Fd0;
     {error, enoent} ->
-        % couldn't find file
-        {error, not_found};
+        % couldn't find file. is there a compact version? This can happen if
+        % crashed during the file switch.
+        case couch_file:open(Filepath ++ ".compact") of
+        {ok, Fd0} ->
+            couch_log:info("Found ~s~s compaction file, using as primary storage.", [Filepath,
".compact"]),
+            ok = file:rename(Filepath ++ ".compact", Filepath),
+            Fd0;
+        {error, enoent} ->
+            throw({error, notfound})
+        end;
     Else ->
-        Else
-    end.
+        throw(Else)
+    end,
+    
+    StartResult = gen_server:start_link(couch_db, {DbName, Filepath, Fd, Options}, []),
+    unlink(Fd),
+    case StartResult of
+    {ok, _} ->
+        % We successfully opened the db, delete old storage files if around
+        case file:delete(Filepath ++ ".old") of
+        ok ->
+            couch_log:info("Deleted old storage file ~s~s", [Filepath, ".old"]);
+        {error, enoent} ->
+            ok  % normal result
+        end;
+    _ ->
+        ok
+    end,
+    StartResult.
 
 %%% Interface functions %%%
 
@@ -72,6 +105,9 @@
 open(DbName, Filepath) ->
     start_link(DbName, Filepath, []).
 
+start_compact(MainPid) ->
+    gen_server:cast(MainPid, start_compact).
+
 delete_doc(MainPid, Id, Revisions) ->
     DeletedDocs = [#doc{id=Id, revs=[Rev], deleted=true} || Rev <- Revisions],
     {ok, [Result]} = update_docs(MainPid, DeletedDocs, [new_edits]),
@@ -128,15 +164,23 @@
 get_full_doc_infos(MainPid, Ids) when is_pid(MainPid) ->
     get_full_doc_infos(get_db(MainPid), Ids);
 get_full_doc_infos(#db{}=Db, Ids) ->
-    couch_btree:lookup(Db#db.docinfo_by_Id_btree, Ids).
+    couch_btree:lookup(Db#db.fulldocinfo_by_id_btree, Ids).
 
 get_db_info(MainPid) when is_pid(MainPid) ->
     get_db_info(get_db(MainPid));
-get_db_info(#db{doc_count=Count, doc_del_count=DelCount, last_update_seq=SeqNum}) ->
+get_db_info(Db) ->
+    #db{fd=Fd,
+        compactor_pid=Compactor,
+        doc_count=Count,
+        doc_del_count=DelCount,
+        update_seq=SeqNum} = Db,
+    {ok, Size} = couch_file:bytes(Fd),
     InfoList = [
         {doc_count, Count},
         {doc_del_count, DelCount},
-        {last_update_seq, SeqNum}
+        {update_seq, SeqNum},
+        {compacting, Compactor/=nil},
+        {size, Size}
         ],
     {ok, InfoList}.
 
@@ -315,35 +359,18 @@
 
 enum_docs(MainPid, StartId, Direction, InFun, InAcc) ->
     Db = get_db(MainPid),
-    couch_btree:fold(Db#db.docinfo_by_Id_btree, StartId, Direction, InFun, InAcc).
+    couch_btree:fold(Db#db.fulldocinfo_by_id_btree, StartId, Direction, InFun, InAcc).
 
 enum_docs(MainPid, StartId, InFun, Ctx) ->
     enum_docs(MainPid, StartId, fwd, InFun, Ctx).
 
-close(MainPid) ->
-    Ref = erlang:monitor(process, MainPid),
-    unlink(MainPid),
-    exit(MainPid, normal),
-    receive
-    {'DOWN', Ref, process, MainPid, _Reason} ->
-        ok
-    end.
-
-
 % server functions
 
-init({DbName, Fd, Options}) ->
-    link(Fd),
-    case lists:member(create, Options) of
-    true ->
-        % create a new header and writes it to the file
-        Header =  #db_header{},
-        ok = couch_file:write_header(Fd, <<$g, $m, $k, 0>>, Header),
-        ok = couch_file:sync(Fd),
-        init_main(DbName, Fd, Header);
-    false ->
-        {ok, Header} = couch_file:read_header(Fd, <<$g, $m, $k, 0>>),
-        init_main(DbName, Fd, Header)
+init(InitArgs) ->
+    spawn_link(couch_db, start_update_loop, [self(), InitArgs]),
+    receive
+    {initialized, Db} ->
+        {ok, Db}
     end.
 
 btree_by_seq_split(DocInfo) ->
@@ -374,10 +401,10 @@
     #full_doc_info{id=Id, update_seq=Seq, rev_tree=Tree}.
     
 
-init_main(DbName, Fd, Header) ->
+init_db(DbName, Filepath, Fd, Header) ->
     {ok, SummaryStream} = couch_stream:open(Header#db_header.summary_stream_state, Fd),
     ok = couch_stream:set_min_buffer(SummaryStream, 10000),
-    {ok, IdBtree} = couch_btree:open(Header#db_header.docinfo_by_Id_btree_state, Fd,
+    {ok, IdBtree} = couch_btree:open(Header#db_header.fulldocinfo_by_id_btree_state, Fd,
         [{split, fun(V) -> btree_by_name_split(V) end},
         {join, fun(K,V) -> btree_by_name_join(K,V) end}] ),
     {ok, SeqBtree} = couch_btree:open(Header#db_header.docinfo_by_seq_btree_state, Fd,
@@ -385,27 +412,27 @@
             {join, fun(K,V) -> btree_by_seq_join(K,V) end}] ),
     {ok, LocalDocsBtree} = couch_btree:open(Header#db_header.local_docs_btree_state, Fd),
 
-    Db = #db{
-        main_pid=self(),
+    #db{
+        update_pid=self(),
         fd=Fd,
         header=Header,
         summary_stream = SummaryStream,
-        docinfo_by_Id_btree = IdBtree,
+        fulldocinfo_by_id_btree = IdBtree,
         docinfo_by_seq_btree = SeqBtree,
         local_docs_btree = LocalDocsBtree,
-        last_update_seq = Header#db_header.last_update_seq,
+        update_seq = Header#db_header.update_seq,
         doc_count = Header#db_header.doc_count,
         doc_del_count = Header#db_header.doc_del_count,
-        name = DbName
-        },
-
-    UpdatePid = spawn_link(couch_db, start_update_loop, [Db]),
-
-    {ok, Db#db{update_pid=UpdatePid}}.
+        name = DbName,
+        filepath=Filepath
+        }.
 
+close_db(#db{fd=Fd,summary_stream=Ss}) ->
+    couch_file:close(Fd),
+    couch_stream:close(Ss).
+    
 terminate(_Reason, Db) ->
-    Db#db.update_pid ! close,
-    couch_file:close(Db#db.fd).
+    exit(Db#db.update_pid, kill).
     
 handle_call({update_docs, DocActions, Options}, From, #db{update_pid=Updater}=Db) ->
     Updater ! {From, update_docs, DocActions, Options},
@@ -416,22 +443,48 @@
     {reply, ok, NewDb}.
 
 
-handle_cast(foo, Main) ->
-    {noreply, Main}.
+handle_cast(start_compact, #db{update_pid=Updater}=Db) ->
+    Updater ! compact,
+    {noreply, Db}.
+
+code_change(_OldVsn, State, _Extra) ->
+    {ok, State}.
+
+handle_info(Msg, Db) ->
+    couch_log:error("Bad message received for db ~s: ~p", [Db#db.name, Msg]),
+    exit({error, Msg}).
+
 
 %%% Internal function %%%
 
-start_update_loop(Db) ->
-    update_loop(Db#db{update_pid=self()}).
+start_update_loop(MainPid, {DbName, Filepath, Fd, Options}) ->
+    link(Fd),
+    
+    case lists:member(create, Options) of
+    true ->
+        % create a new header and writes it to the file
+        Header =  #db_header{},
+        ok = couch_file:write_header(Fd, <<$g, $m, $k, 0>>, Header),
+        % delete any old compaction files that might be hanging around
+        file:delete(Filepath ++ ".compact"),
+        file:delete(Filepath ++ ".old");
+    false ->
+        {ok, Header} = couch_file:read_header(Fd, <<$g, $m, $k, 0>>)
+    end,
+    
+    Db = init_db(DbName, Filepath, Fd, Header),
+    Db2 = Db#db{main_pid=MainPid},
+    MainPid ! {initialized, Db2},
+    update_loop(Db2).
     
-update_loop(Db) ->
+update_loop(#db{name=Name,filepath=Filepath, main_pid=MainPid}=Db) ->
     receive
     {OrigFrom, update_docs, DocActions, Options} ->
         case (catch update_docs_int(Db, DocActions, Options)) of
         {ok, Db2} ->
-            ok = gen_server:call(Db2#db.main_pid, {db_updated, Db2}),
+            ok = gen_server:call(MainPid, {db_updated, Db2}),
             gen_server:reply(OrigFrom, ok),
-            couch_db_update_notifier:notify({updated, Db2#db.name}),
+            couch_db_update_notifier:notify({updated, Name}),
             update_loop(Db2);
         conflict ->
             gen_server:reply(OrigFrom, conflict),
@@ -439,9 +492,48 @@
         Error ->
             exit(Error) % we crashed
         end;
-    close ->
-        % terminate loop
-        exit(normal)
+    compact ->
+        case Db#db.compactor_pid of
+        nil ->
+            couch_log:info("Starting compaction for db \"~s\"", [Name]),
+            Pid = spawn_link(couch_db, start_copy_compact_int, [Db, true]),
+            Db2 = Db#db{compactor_pid=Pid},
+            ok = gen_server:call(MainPid, {db_updated, Db2}),
+            update_loop(Db2);
+        _ ->
+            update_loop(Db) % already started
+        end;
+    {compact_done, CompactFilepath} ->
+        {ok, NewFd} = couch_file:open(CompactFilepath),
+        {ok, NewHeader} = couch_file:read_header(NewFd, <<$g, $m, $k, 0>>),
+        #db{update_seq=NewSeq}= NewDb =
+                init_db(Name, CompactFilepath, NewFd, NewHeader),
+        case Db#db.update_seq == NewSeq of
+        true ->
+            couch_log:debug("CouchDB swapping files ~s and ~s.", [Filepath, CompactFilepath]),
+            ok = file:rename(Filepath, Filepath ++ ".old"),
+            ok = file:rename(CompactFilepath, Filepath),
+            
+            NewDb2 = NewDb#db{
+                main_pid = Db#db.main_pid,
+                doc_count = Db#db.doc_count,
+                doc_del_count = Db#db.doc_del_count,
+                filepath = Filepath},
+            close_db(Db),
+            ok = gen_server:call(MainPid, {db_updated, NewDb2}),
+            couch_log:info("Compaction for db ~p completed.", [Name]),
+            update_loop(NewDb2#db{compactor_pid=nil});
+        false ->
+            couch_log:info("Compaction file still behind main file "
+                "(update seq=~p. compact update seq=~p). Retrying.",
+                [Db#db.update_seq, NewSeq]),
+            Pid = spawn_link(couch_db, start_copy_compact_int, [Db, false]),
+            Db2 = Db#db{compactor_pid=Pid},
+            update_loop(Db2)
+        end;
+    Else ->
+        couch_log:error("Unknown message received in db ~s:~p", [Db#db.name, Else]),
+        exit({error, Else})
     end.
 
 get_db(MainPid) ->
@@ -466,7 +558,7 @@
         FoundResults =
         lists:map(fun({Rev, Value, FoundRevPath}) ->
             case Value of
-            0 ->
+            ?REV_MISSING ->
                 % we have the rev in our list but know nothing about it
                 {{not_found, missing}, Rev};
             {IsDeleted, SummaryPtr} ->
@@ -538,7 +630,7 @@
 doc_to_tree(Doc, [RevId]) ->
     [{RevId, Doc, []}];
 doc_to_tree(Doc, [RevId | Rest]) ->
-    [{RevId, [], doc_to_tree(Doc, Rest)}].
+    [{RevId, ?REV_MISSING, doc_to_tree(Doc, Rest)}].
 
 make_doc(Db, Id, Deleted, SummaryPointer, RevisionPath) ->
     {BodyData, BinValues} =
@@ -613,9 +705,9 @@
 
 update_docs_int(Db, DocsList, Options) ->
     #db{
-        docinfo_by_Id_btree = DocInfoByIdBTree,
+        fulldocinfo_by_id_btree = DocInfoByIdBTree,
         docinfo_by_seq_btree = DocInfoBySeqBTree,
-        last_update_seq = LastSeq,
+        update_seq = LastSeq,
         doc_count = FullDocCount,
         doc_del_count = FullDelCount
         } = Db,
@@ -678,9 +770,9 @@
     {ok, DocInfoByIdBTree2} = couch_btree:add_remove(DocInfoByIdBTree, InfoById, []),
 
     Db3 = Db2#db{
-        docinfo_by_Id_btree = DocInfoByIdBTree2,
+        fulldocinfo_by_id_btree = DocInfoByIdBTree2,
         docinfo_by_seq_btree = DocInfoBySeqBTree2,
-        last_update_seq = NewSeq,
+        update_seq = NewSeq,
         doc_count = FullDocCount + NewDocsCount - OldCount,
         doc_del_count = FullDelCount + NewDelCount - OldDelCount
         },
@@ -689,7 +781,7 @@
     true ->
         {ok, Db3};
     false ->
-        commit_outstanding(Db3)
+        {ok, commit_data(Db3)}
     end.
 
 update_local_docs(#db{local_docs_btree=Btree}=Db, Docs) ->
@@ -697,20 +789,20 @@
     OldDocLookups = couch_btree:lookup(Btree, Ids),
     BtreeEntries = lists:zipwith(
         fun(#doc{id=Id,deleted=Delete,revs=Revs,body=Body}, OldDocLookup) ->
-            BasedOnRev =
+            NewRev =
             case Revs of
                 [] -> 0;
-                [RevStr|_] -> list_to_integer(RevStr) - 1
+                [RevStr|_] -> list_to_integer(RevStr)
             end,
             OldRev =
             case OldDocLookup of
                 {ok, {_, {OldRev0, _}}} -> OldRev0;
                 not_found -> 0
             end,
-            case OldRev == BasedOnRev of
+            case OldRev + 1 == NewRev of
             true ->
                 case Delete of
-                    false -> {update, {Id, {OldRev+1, Body}}};
+                    false -> {update, {Id, {NewRev, Body}}};
                     true  -> {remove, Id}
                 end;
             false ->
@@ -729,29 +821,109 @@
 
 
 
-commit_outstanding(#db{fd=Fd, header=Header} = Db) ->
-    ok = couch_file:sync(Fd), % commit outstanding data
+commit_data(#db{fd=Fd, header=Header} = Db) ->
     Header2 = Header#db_header{
-        last_update_seq = Db#db.last_update_seq,
+        update_seq = Db#db.update_seq,
         summary_stream_state = couch_stream:get_state(Db#db.summary_stream),
         docinfo_by_seq_btree_state = couch_btree:get_state(Db#db.docinfo_by_seq_btree),
-        docinfo_by_Id_btree_state = couch_btree:get_state(Db#db.docinfo_by_Id_btree),
+        fulldocinfo_by_id_btree_state = couch_btree:get_state(Db#db.fulldocinfo_by_id_btree),
         local_docs_btree_state = couch_btree:get_state(Db#db.local_docs_btree),
         doc_count = Db#db.doc_count,
         doc_del_count = Db#db.doc_del_count
         },
-    ok = couch_file:write_header(Fd, <<$g, $m, $k, 0>>, Header2),
-    ok = couch_file:sync(Fd), % commit header to disk
-    Db2 = Db#db{
-        header = Header2
-        },
-    {ok, Db2}.
+    if Header == Header2 ->
+        Db; % unchanged. nothing to do
+    true ->
+        ok = couch_file:write_header(Fd, <<$g, $m, $k, 0>>, Header2),
+        Db#db{header = Header2}
+    end.
 
+copy_raw_doc(SrcFd, SrcSp, DestFd, DestStream) ->
+    {ok, {BodyData, BinInfos}} = couch_stream:read_term(SrcFd, SrcSp),
+    % copy the bin values
+    NewBinInfos = lists:map(fun({Name, {Type, BinSp, Len}}) ->
+        {ok, NewBinSp} = couch_stream:copy_to_new_stream(SrcFd, BinSp, Len, DestFd),
+        {Name, {Type, NewBinSp, Len}}
+        end, BinInfos),
+    % now write the document summary
+    {ok, Sp} = couch_stream:write_term(DestStream, {BodyData, NewBinInfos}),
+    Sp.
+
+copy_rev_tree(_SrcFd, _DestFd, _DestStream, []) ->
+    [];
+copy_rev_tree(SrcFd, DestFd, DestStream, [{RevId, {IsDel, Sp}, []} | RestTree]) ->
+    % This is a leaf node, copy it over
+    NewSp = copy_raw_doc(SrcFd, Sp, DestFd, DestStream),
+    [{RevId, {IsDel, NewSp}, []} | copy_rev_tree(SrcFd, DestFd, DestStream, RestTree)];
+copy_rev_tree(SrcFd, DestFd, DestStream, [{RevId, _, SubTree} | RestTree]) ->
+    % inner node, only copy info/data from leaf nodes
+    [{RevId, ?REV_MISSING, copy_rev_tree(SrcFd, DestFd, DestStream, SubTree)} | copy_rev_tree(SrcFd,
DestFd, DestStream, RestTree)].
+    
+copy_docs(#db{fd=SrcFd}=Db, #db{fd=DestFd,summary_stream=DestStream}=NewDb, InfoBySeq) ->
+    Ids = [Id || #doc_info{id=Id} <- InfoBySeq],
+    LookupResults = couch_btree:lookup(Db#db.fulldocinfo_by_id_btree, Ids),
+    NewFullDocInfos = lists:map(
+        fun({ok, #full_doc_info{rev_tree=RevTree}=Info}) ->
+            Info#full_doc_info{rev_tree=copy_rev_tree(SrcFd, DestFd, DestStream, RevTree)}
+        end, LookupResults),
+    NewDocInfos = [couch_doc:to_doc_info(FullDocInfo) || FullDocInfo <- NewFullDocInfos],
+    {ok, DocInfoBTree} =
+        couch_btree:add_remove(NewDb#db.docinfo_by_seq_btree, NewDocInfos, []),
+    {ok, FullDocInfoBTree} =
+        couch_btree:add_remove(NewDb#db.fulldocinfo_by_id_btree, NewFullDocInfos, []),
+    NewDb#db{fulldocinfo_by_id_btree=FullDocInfoBTree, docinfo_by_seq_btree=DocInfoBTree}.
+
+
+          
+copy_compact_docs(Db, NewDb) ->
+    EnumBySeqFun =
+    fun(#doc_info{update_seq=Seq}=DocInfo, _Offset, {AccNewDb, AccUncopied}) ->
+        case couch_util:should_flush() of
+        true ->
+            NewDb2 = copy_docs(Db, AccNewDb, lists:reverse(AccUncopied, DocInfo)),
+            {ok, {commit_data(NewDb2#db{update_seq=Seq}), []}};
+        false ->    
+            {ok, {AccNewDb, [DocInfo | AccUncopied]}}
+        end
+    end,
+    {ok, {NewDb2, Uncopied}} =
+        couch_btree:foldl(Db#db.docinfo_by_seq_btree, NewDb#db.update_seq + 1, EnumBySeqFun,
{NewDb, []}),
 
-code_change(_OldVsn, State, _Extra) ->
-    {ok, State}.
+    case Uncopied of
+    [#doc_info{update_seq=LastSeq} | _] ->
+        commit_data( copy_docs(Db, NewDb2#db{update_seq=LastSeq},
+            lists:reverse(Uncopied)));
+    [] ->
+        NewDb2
+    end.
 
-handle_info(_Info, State) ->
-    {noreply, State}.
+start_copy_compact_int(#db{name=Name,filepath=Filepath}=Db, CopyLocal) ->
+    CompactFile = Filepath ++ ".compact",
+    couch_log:debug("Compaction process spawned for db \"~s\"", [Name]),
+    case couch_file:open(CompactFile) of
+    {ok, Fd} ->
+        couch_log:debug("Found existing compaction file for db \"~s\"", [Name]),
+        {ok, Header} = couch_file:read_header(Fd, <<$g, $m, $k, 0>>);
+    {error, enoent} -> %
+        {ok, Fd} = couch_file:open(CompactFile, [create]),
+        Header =  #db_header{},
+        ok = couch_file:write_header(Fd, <<$g, $m, $k, 0>>, Header)
+    end,
+    NewDb = init_db(Name, CompactFile, Fd, Header),
+    NewDb2 = copy_compact_docs(Db, NewDb),
+    NewDb3 =
+    case CopyLocal of
+    true ->
+        % suck up all the local docs into memory and write them to the new db
+        {ok, LocalDocs} = couch_btree:foldl(Db#db.local_docs_btree,
+                fun(Value, _Offset, Acc) -> {ok, [Value | Acc]} end, []),
+        {ok, NewLocalBtree} = couch_btree:add(NewDb2#db.local_docs_btree, LocalDocs),
+        commit_data(NewDb2#db{local_docs_btree=NewLocalBtree});
+    _ ->
+        NewDb2
+    end,
+    close_db(NewDb3),
+    Db#db.update_pid ! {compact_done, CompactFile}.
     
     
+    
\ No newline at end of file

Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_file.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/src/couchdb/couch_file.erl?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/src/couchdb/couch_file.erl (original)
+++ incubator/couchdb/branches/mochiweb/src/couchdb/couch_file.erl Fri Apr  4 08:02:51 2008
@@ -142,8 +142,7 @@
 
 
 write_header(Fd, Prefix, Data) ->
-    % The leading bytes in every db file, the sig and the file version:
-    %the actual header data
+    ok = sync(Fd),
     TermBin = term_to_binary(Data),
     % the size of all the bytes written to the header, including the md5 signature (16 bytes)
     FilledSize = size(Prefix) + size(TermBin) + 16,
@@ -159,7 +158,8 @@
         WriteBin = <<Prefix/binary, TermBin/binary, PadZeros/binary, Sig/binary>>,
         ?HEADER_SIZE = size(WriteBin), % sanity check
         DblWriteBin = [WriteBin, WriteBin],
-        ok = pwrite(Fd, 0, DblWriteBin)
+        ok = pwrite(Fd, 0, DblWriteBin),
+        ok = sync(Fd)
     end.
 
 

Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl (original)
+++ incubator/couchdb/branches/mochiweb/src/couchdb/couch_httpd.erl Fri Apr  4 08:02:51 2008
@@ -222,7 +222,7 @@
 handle_db_request(_Req, _Method, {_DbName, _Db, ["_bulk_docs"]}) ->
     throw({method_not_allowed, "POST"});
 
-handle_db_request(Req, Method, {DbName, Db, ["_fulltext"]}) ->
+handle_db_request(Req, 'GET', {DbName, _Db, ["_fulltext"]}) ->
     case Req:parse_qs() of 
         [{"q", Query}] when (length(Query) > 0) ->
             {ok, Response} = couch_ft_query:execute(DbName, Query),
@@ -230,6 +230,9 @@
         _Error ->
             throw({no_fulltext_query, "Empty Query String"})
     end;
+
+handle_db_request(_Req, _Method, {_DbName, _Db, ["_full_text"]}) ->
+    throw({method_not_allowed, "GET,HEAD"});
 
 % View request handlers
 

Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_stream.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/src/couchdb/couch_stream.erl?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/src/couchdb/couch_stream.erl (original)
+++ incubator/couchdb/branches/mochiweb/src/couchdb/couch_stream.erl Fri Apr  4 08:02:51 2008
@@ -15,7 +15,7 @@
 
 -export([test/1]).
 -export([open/1, open/2, close/1, read/3, read_term/2, write/2, write_term/2, get_state/1,
foldl/5]).
--export([copy/4]).
+-export([copy/4, copy_to_new_stream/4]).
 -export([ensure_buffer/2, set_min_buffer/2]).
 -export([init/1, terminate/2, handle_call/3]).
 -export([handle_cast/2,code_change/3,handle_info/2]).
@@ -78,12 +78,18 @@
     Bin = list_to_binary(lists:reverse(RevBin)),
     {ok, Bin, Sp2}.
 
-copy(#stream{pid = _Pid, fd = Fd}, Sp, Num, DestStream) ->
-    copy(Fd, Sp, Num, DestStream);
-copy(Fd, Sp, Num, DestStream) ->
-    {ok, NewSp, _Sp2} = stream_data(Fd, Sp, Num, ?HUGE_CHUNK,
+copy_to_new_stream(Src, Sp, Len, DestFd) ->
+    Dest = open(DestFd),
+    {ok, NewSp} = copy(Src, Sp, Len, Dest),
+    close(Dest),
+    {ok, NewSp}.
+
+copy(#stream{pid = _Pid, fd = Fd}, Sp, Len, DestStream) ->
+    copy(Fd, Sp, Len, DestStream);
+copy(Fd, Sp, Len, DestStream) ->
+    {ok, NewSp, _Sp2} = stream_data(Fd, Sp, Len, ?HUGE_CHUNK,
         fun(Bin, AccPointer) ->
-            {ok, NewPointer} = write(Bin, DestStream),
+            {ok, NewPointer} = write(DestStream, Bin),
             if AccPointer == null -> NewPointer; true -> AccPointer end
         end,
         null),

Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_util.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/src/couchdb/couch_util.erl?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/src/couchdb/couch_util.erl (original)
+++ incubator/couchdb/branches/mochiweb/src/couchdb/couch_util.erl Fri Apr  4 08:02:51 2008
@@ -14,7 +14,7 @@
 -behaviour(gen_server).
 
 -export([start_link/0,start_link/1]).
--export([parse_ini/1]).
+-export([parse_ini/1,should_flush/0, should_flush/1]).
 -export([new_uuid/0, rand32/0, implode/2, collate/2, collate/3]).
 -export([abs_pathname/1,abs_pathname/2, trim/1, ascii_lower/1, test/0]).
 -export([encodeBase64/1, decodeBase64/1]).
@@ -22,6 +22,8 @@
 -export([init/1, terminate/2, handle_call/3]).
 -export([handle_cast/2,code_change/3,handle_info/2]).
 
+% arbitrarily chosen amount of memory to use before flushing to disk
+-define(FLUSH_MAX_MEM, 10000000).
 
 start_link() ->
     start_link("").
@@ -246,6 +248,22 @@
         [2] -> 0
     end.
 
+should_flush() ->
+    should_flush(?FLUSH_MAX_MEM).
+    
+should_flush(MemThreshHold) ->
+    case process_info(self(), memory) of
+    {memory, Mem} when Mem > 2*MemThreshHold ->
+        garbage_collect(),
+        case process_info(self(), memory) of
+        {memory, Mem} when Mem > MemThreshHold ->
+            true;
+        _ ->
+            false
+        end;
+    _ ->
+        false
+    end.
 
 
 

Modified: incubator/couchdb/branches/mochiweb/src/couchdb/couch_view.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/branches/mochiweb/src/couchdb/couch_view.erl?rev=644734&r1=644733&r2=644734&view=diff
==============================================================================
--- incubator/couchdb/branches/mochiweb/src/couchdb/couch_view.erl (original)
+++ incubator/couchdb/branches/mochiweb/src/couchdb/couch_view.erl Fri Apr  4 08:02:51 2008
@@ -20,9 +20,6 @@
 
 -include("couch_db.hrl").
 
-% arbitrarily chosen amount of memory to use before flushing to disk
--define(FLUSH_MAX_MEM, 10000000).
-
 -record(group,
     {db,
     fd,
@@ -68,12 +65,11 @@
 	    receive
     	{Pid, Response} ->
     	    erlang:demonitor(Mref),
-    	    receive 
-    		{'DOWN', Mref, _, _, _} -> 
-    		    Response
-    	    after 0 -> 
-    		    Response
-    	    end;
+    	    receive
+        		{'DOWN', Mref, _, _, _} -> ok
+        	    after 0 -> ok
+    	    end,
+    	    Response;
     	{'DOWN', Mref, _, _, Reason} ->
     	    throw(Reason)
         end
@@ -183,9 +179,12 @@
     file:delete(Root ++ "/." ++ DbName ++ "_temp"),
     {noreply, Server}.
 
+handle_info({'EXIT', _FromPid, normal}, Server) ->
+    {noreply, Server};
 handle_info({'EXIT', FromPid, Reason}, #server{root_dir=RootDir}=Server) ->
     case ets:lookup(couch_views_by_updater, FromPid) of
     [] -> % non-updater linked process must have died, we propagate the error
+        couch_log:error("Exit on non-updater process: ~p", [Reason]),
         exit(Reason);
     [{_, {DbName, "_temp_" ++ _ = GroupId}}] ->
         delete_from_ets(FromPid, DbName, GroupId),
@@ -201,7 +200,10 @@
     [{_, {DbName, GroupId}}] ->
         delete_from_ets(FromPid, DbName, GroupId)
     end,
-    {noreply, Server}.
+    {noreply, Server};
+handle_info(Msg, _Server) ->
+    couch_log:error("Bad message received for view module: ~p", [Msg]),
+    exit({error, Msg}).
     
 add_to_ets(Pid, DbName, GroupId) ->
     true = ets:insert(couch_views_by_updater, {Pid, {DbName, GroupId}}),
@@ -216,11 +218,6 @@
 code_change(_OldVsn, State, _Extra) ->
     {ok, State}.
 
-start_update_loop(RootDir, DbName, GroupId) ->
-    % wait for a notify request before doing anything. This way, we can just
-    % exit and any exits will be noticed by the callers.
-    start_update_loop(RootDir, DbName, GroupId, get_notify_pids(1000)).
-
 
 start_temp_update_loop(DbName, Fd, Lang, Query) ->
     NotifyPids = get_notify_pids(1000),
@@ -243,7 +240,12 @@
     {ok, Group2} = update_group(Group),
     [Pid ! {self(), {ok, Group2}} || Pid <- NotifyPids],
     garbage_collect(),
-    temp_update_loop(Group2, get_notify_pids(100000)).
+    temp_update_loop(Group2, get_notify_pids(10000)).
+
+start_update_loop(RootDir, DbName, GroupId) ->
+    % wait for a notify request before doing anything. This way, we can just
+    % exit and any exits will be noticed by the callers.
+    start_update_loop(RootDir, DbName, GroupId, get_notify_pids(1000)).
     
 start_update_loop(RootDir, DbName, GroupId, NotifyPids) ->
     {Db, DefLang, Defs} =
@@ -284,13 +286,16 @@
     update_loop(Group2).
     
 update_loop(Group) ->
-    update_loop(Group, get_notify_pids()).
+    update_loop(Group, get_notify_pids(100000)).
 
 % wait for the first request to come in.
 get_notify_pids(Wait) ->
     receive
     {Pid, get_updated} ->
-        [Pid | get_notify_pids()]
+        [Pid | get_notify_pids()];
+    Else ->
+        couch_log:error("Unexpected message in view updater: ~p", [Else]),
+        exit({error, Else})
     after Wait ->
         exit(wait_timeout)
 	end.
@@ -526,15 +531,15 @@
             {ok, Doc} = couch_db:open_doc(Db, DocInfo, [conflicts, deleted_conflicts]),
             {[Doc | Docs], DocIdViewIdKeys}
         end,
-        case process_info(self(), memory) of
-        {memory, Mem} when Mem > ?FLUSH_MAX_MEM ->
+        case couch_util:should_flush() of
+        true ->
             {Group1, Results} = view_compute(Group, Docs2),
             {ViewKVs3, DocIdViewIdKeys3} = view_insert_query_results(Docs2, Results, ViewKVs,
DocIdViewIdKeys2),
             {ok, Group2} = write_changes(Group1, ViewKVs3, DocIdViewIdKeys3, Seq),
             garbage_collect(),
             ViewEmptyKeyValues = [{View, []} || View <- Group2#group.views],
             {ok, {[], Group2, ViewEmptyKeyValues, [], Seq}};
-        _Else ->
+        false ->
             {ok, {Docs2, Group, ViewKVs, DocIdViewIdKeys2, Seq}}
         end
     end.



Mime
View raw message