tagging 1.0.0

git-svn-id: https://svn.apache.org/repos/asf/couchdb/tags/1.0.0@962579 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES b/CHANGES
index 5bc5b68..f1a282f 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,21 +6,21 @@
 
 Security:
 
-* Added authentication caching, to avoid repeated opening and closing of the
-  users database for each request requiring authentication.
+ * Added authentication caching, to avoid repeated opening and closing of the
+   users database for each request requiring authentication.
 
 Storage System:
 
-* Small optimization for reordering result lists.
-* More efficient header commits.
-* Use O_APPEND to save lseeks.
-* Faster implementation of pread_iolist(). Further improves performance on
-  concurrent reads.
+ * Small optimization for reordering result lists.
+ * More efficient header commits.
+ * Use O_APPEND to save lseeks.
+ * Faster implementation of pread_iolist(). Further improves performance on
+   concurrent reads.
 
 View Server:
 
-* Faster default view collation.
-* Added option to include update_seq in view responses.
+ * Faster default view collation.
+ * Added option to include update_seq in view responses.
 
 Version 0.11.1
 --------------
diff --git a/NEWS b/NEWS
index 0e99802..1cdeb9f 100644
--- a/NEWS
+++ b/NEWS
@@ -10,13 +10,13 @@
 Version 1.0.0
 -------------
 
-* More efficient header commits.
-* Use O_APPEND to save lseeks.
-* Faster implementation of pread_iolist(). Further improves performance on
-  concurrent reads.
-* Added authentication caching
-* Faster default view collation.
-* Added option to include update_seq in view responses.
+ * More efficient header commits.
+ * Use O_APPEND to save lseeks.
+ * Faster implementation of pread_iolist(). Further improves performance on
+   concurrent reads.
+ * Added authentication caching
+ * Faster default view collation.
+ * Added option to include update_seq in view responses.
 
 Version 0.11.1
 --------------
diff --git a/share/Makefile.am b/share/Makefile.am
index fc052de..c85f683 100644
--- a/share/Makefile.am
+++ b/share/Makefile.am
@@ -162,6 +162,7 @@
     www/script/test/view_collation.js \
     www/script/test/view_collation_raw.js \
     www/script/test/view_conflicts.js \
+    www/script/test/view_compaction.js \
     www/script/test/view_errors.js \
     www/script/test/view_include_docs.js \
     www/script/test/view_multi_key_all_docs.js \
diff --git a/share/www/script/couch_tests.js b/share/www/script/couch_tests.js
index dc283bb..c5257ea 100644
--- a/share/www/script/couch_tests.js
+++ b/share/www/script/couch_tests.js
@@ -85,6 +85,7 @@
 loadTest("view_collation.js");
 loadTest("view_collation_raw.js");
 loadTest("view_conflicts.js");
+loadTest("view_compaction.js");
 loadTest("view_errors.js");
 loadTest("view_include_docs.js");
 loadTest("view_multi_key_all_docs.js");
diff --git a/share/www/script/jquery.couch.js b/share/www/script/jquery.couch.js
index f9647ec..879c244 100644
--- a/share/www/script/jquery.couch.js
+++ b/share/www/script/jquery.couch.js
@@ -595,6 +595,7 @@
 
   function ajax(obj, options, errorMessage, ajaxOptions) {
     options = $.extend({successStatus: 200}, options);
+    ajaxOptions = $.extend({contentType: "application/json"}, ajaxOptions);
     errorMessage = errorMessage || "Unknown error";
     $.ajax($.extend($.extend({
       type: "GET", dataType: "json", cache : !$.browser.msie,
diff --git a/share/www/script/test/view_compaction.js b/share/www/script/test/view_compaction.js
new file mode 100644
index 0000000..a11fb7b
--- /dev/null
+++ b/share/www/script/test/view_compaction.js
@@ -0,0 +1,104 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+couchTests.view_compaction = function(debug) {
+
+  if (debug) debugger;
+
+  var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit": "true"});
+
+  db.deleteDb();
+  db.createDb();
+
+  var ddoc = {
+    _id: "_design/foo",
+    language: "javascript",
+    views: {
+      view1: {
+        map: "function(doc) { emit(doc._id, doc.value) }"
+      },
+      view2: {
+        map: "function(doc) { emit(doc._id, doc.value); }",
+        reduce: "function(keys, values, rereduce) { return sum(values); }"
+      }
+    }
+  };
+  T(db.save(ddoc).ok);
+
+  var docs = makeDocs(0, 1000);
+  db.bulkSave(docs);
+
+  var resp = db.view('foo/view1', {});
+  T(resp.rows.length === 1000);
+
+  resp = db.view('foo/view2', {});
+  T(resp.rows.length === 1);
+
+  resp = db.designInfo("_design/foo");
+  T(resp.view_index.update_seq === 1001);
+
+
+  // update docs
+  for (var i = 0; i < docs.length; i++) {
+    docs[i].integer = docs[i].integer + 1;
+  }
+  db.bulkSave(docs);
+
+
+  resp = db.view('foo/view1', {});
+  T(resp.rows.length === 1000);
+
+  resp = db.view('foo/view2', {});
+  T(resp.rows.length === 1);
+
+  resp = db.designInfo("_design/foo");
+  T(resp.view_index.update_seq === 2001);
+
+
+  // update docs again...
+  for (var i = 0; i < docs.length; i++) {
+    docs[i].integer = docs[i].integer + 2;
+  }
+  db.bulkSave(docs);
+
+
+  resp = db.view('foo/view1', {});
+  T(resp.rows.length === 1000);
+
+  resp = db.view('foo/view2', {});
+  T(resp.rows.length === 1);
+
+  resp = db.designInfo("_design/foo");
+  T(resp.view_index.update_seq === 3001);
+
+  var disk_size_before_compact = resp.view_index.disk_size;
+
+  // compact view group
+  var xhr = CouchDB.request("POST", "/" + db.name + "/_compact" + "/foo");
+  T(JSON.parse(xhr.responseText).ok === true);
+
+  resp = db.designInfo("_design/foo");
+  while (resp.view_index.compact_running === true) {
+    resp = db.designInfo("_design/foo");
+  }
+
+
+  resp = db.view('foo/view1', {});
+  T(resp.rows.length === 1000);
+
+  resp = db.view('foo/view2', {});
+  T(resp.rows.length === 1);
+
+  resp = db.designInfo("_design/foo");
+  T(resp.view_index.update_seq === 3001);
+  T(resp.view_index.disk_size < disk_size_before_compact);
+};
\ No newline at end of file
diff --git a/src/couchdb/couch_db.erl b/src/couchdb/couch_db.erl
index cf25dc6..80f0d7b 100644
--- a/src/couchdb/couch_db.erl
+++ b/src/couchdb/couch_db.erl
@@ -103,7 +103,7 @@
     erlang:monitor(process, MainPid).
 
 start_compact(#db{update_pid=Pid}) ->
-    gen_server:cast(Pid, start_compact).
+    gen_server:call(Pid, start_compact).
 
 delete_doc(Db, Id, Revisions) ->
     DeletedDocs = [#doc{id=Id, revs=[Rev], deleted=true} || Rev <- Revisions],
diff --git a/src/couchdb/couch_db_updater.erl b/src/couchdb/couch_db_updater.erl
index 3027501..f087970 100644
--- a/src/couchdb/couch_db_updater.erl
+++ b/src/couchdb/couch_db_updater.erl
@@ -143,21 +143,22 @@
 
     ok = gen_server:call(Db2#db.main_pid, {db_updated, Db2}),
     couch_db_update_notifier:notify({updated, Db#db.name}),
-    {reply, {ok, (Db2#db.header)#db_header.purge_seq, IdRevsPurged}, Db2}.
-
-
-handle_cast(start_compact, Db) ->
+    {reply, {ok, (Db2#db.header)#db_header.purge_seq, IdRevsPurged}, Db2};
+handle_call(start_compact, _From, Db) ->
     case Db#db.compactor_pid of
     nil ->
         ?LOG_INFO("Starting compaction for db \"~s\"", [Db#db.name]),
         Pid = spawn_link(fun() -> start_copy_compact(Db) end),
         Db2 = Db#db{compactor_pid=Pid},
         ok = gen_server:call(Db#db.main_pid, {db_updated, Db2}),
-        {noreply, Db2};
+        {reply, ok, Db2};
     _ ->
         % compact currently running, this is a no-op
-        {noreply, Db}
-    end;
+        {reply, ok, Db}
+    end.
+
+
+
 handle_cast({compact_done, CompactFilepath}, #db{filepath=Filepath}=Db) ->
     {ok, NewFd} = couch_file:open(CompactFilepath),
     {ok, NewHeader} = couch_file:read_header(NewFd),
diff --git a/src/couchdb/couch_httpd_db.erl b/src/couchdb/couch_httpd_db.erl
index f151478..783ed9f 100644
--- a/src/couchdb/couch_httpd_db.erl
+++ b/src/couchdb/couch_httpd_db.erl
@@ -111,12 +111,15 @@
 handle_changes_req(#httpd{path_parts=[_,<<"_changes">>]}=Req, _Db) ->
     send_method_not_allowed(Req, "GET,HEAD").
 
-handle_compact_req(#httpd{method='POST',path_parts=[DbName,_,Id|_]}=Req, _Db) ->
+handle_compact_req(#httpd{method='POST',path_parts=[DbName,_,Id|_]}=Req, Db) ->
+    ok = couch_db:check_is_admin(Db),
     couch_httpd:validate_ctype(Req, "application/json"),
     ok = couch_view_compactor:start_compact(DbName, Id),
     send_json(Req, 202, {[{ok, true}]});
 
 handle_compact_req(#httpd{method='POST'}=Req, Db) ->
+    ok = couch_db:check_is_admin(Db),
+    couch_httpd:validate_ctype(Req, "application/json"),
     ok = couch_db:start_compact(Db),
     send_json(Req, 202, {[{ok, true}]});
 
@@ -125,6 +128,8 @@
 
 handle_view_cleanup_req(#httpd{method='POST'}=Req, Db) ->
     % delete unreferenced index files
+    ok = couch_db:check_is_admin(Db),
+    couch_httpd:validate_ctype(Req, "application/json"),
     ok = couch_view:cleanup_index_files(Db),
     send_json(Req, 202, {[{ok, true}]});
 
diff --git a/src/couchdb/couch_view_group.erl b/src/couchdb/couch_view_group.erl
index 8d3a3d8..f01befd 100644
--- a/src/couchdb/couch_view_group.erl
+++ b/src/couchdb/couch_view_group.erl
@@ -186,7 +186,7 @@
     ?LOG_INFO("View index compaction complete for ~s ~s", [DbName, GroupId]),
     FileName = index_file_name(RootDir, DbName, GroupSig),
     CompactName = index_file_name(compact, RootDir, DbName, GroupSig),
-    couch_file:delete(FileName),
+    ok = couch_file:delete(RootDir, FileName),
     ok = file:rename(CompactName, FileName),
 
     %% if an updater is running, kill it and start a new one
@@ -545,7 +545,7 @@
     init_group(Db, Fd, reset_group(Group), nil).
 
 delete_index_file(RootDir, DbName, GroupSig) ->
-    couch_file:delete(index_file_name(RootDir, DbName, GroupSig)).
+    couch_file:delete(RootDir, index_file_name(RootDir, DbName, GroupSig)).
 
 init_group(Db, Fd, #group{views=Views}=Group, nil) ->
     init_group(Db, Fd, Group,