Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E83FD4505 for ; Wed, 8 Jun 2011 18:58:05 +0000 (UTC) Received: (qmail 78238 invoked by uid 500); 8 Jun 2011 18:58:05 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 78196 invoked by uid 500); 8 Jun 2011 18:58:05 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 78189 invoked by uid 99); 8 Jun 2011 18:58:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 08 Jun 2011 18:58:05 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 08 Jun 2011 18:58:04 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 47BC423889D5; Wed, 8 Jun 2011 18:57:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1133510 - in /couchdb/branches/1.1.x: src/couchdb/couch_view.erl test/etap/072-cleanup.t Date: Wed, 08 Jun 2011 18:57:44 -0000 To: commits@couchdb.apache.org From: randall@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110608185744.47BC423889D5@eris.apache.org> Author: randall Date: Wed Jun 8 18:57:43 2011 New Revision: 1133510 URL: http://svn.apache.org/viewvc?rev=1133510&view=rev Log: backport r1133312 from trunk _view_cleanup with no _design docs - COUCHDB-1136 Added: couchdb/branches/1.1.x/test/etap/072-cleanup.t (with props) Modified: couchdb/branches/1.1.x/src/couchdb/couch_view.erl Modified: couchdb/branches/1.1.x/src/couchdb/couch_view.erl URL: http://svn.apache.org/viewvc/couchdb/branches/1.1.x/src/couchdb/couch_view.erl?rev=1133510&r1=1133509&r2=1133510&view=diff ============================================================================== --- couchdb/branches/1.1.x/src/couchdb/couch_view.erl (original) +++ couchdb/branches/1.1.x/src/couchdb/couch_view.erl Wed Jun 8 18:57:43 2011 @@ -92,13 +92,18 @@ cleanup_index_files(Db) -> FileList = list_index_files(Db), - % regex that matches all ddocs - RegExp = "("++ string:join(Sigs, "|") ++")", + DeleteFiles = + if length(Sigs) =:= 0 -> + FileList; + true -> + % regex that matches all ddocs + RegExp = "("++ string:join(Sigs, "|") ++")", + + % filter out the ones in use + [FilePath || FilePath <- FileList, + re:run(FilePath, RegExp, [{capture, none}]) =:= nomatch] + end, - % filter out the ones in use - DeleteFiles = [FilePath - || FilePath <- FileList, - re:run(FilePath, RegExp, [{capture, none}]) =:= nomatch], % delete unused files ?LOG_DEBUG("deleting unused view index files: ~p",[DeleteFiles]), RootDir = couch_config:get("couchdb", "view_index_dir"), Added: couchdb/branches/1.1.x/test/etap/072-cleanup.t URL: http://svn.apache.org/viewvc/couchdb/branches/1.1.x/test/etap/072-cleanup.t?rev=1133510&view=auto ============================================================================== --- couchdb/branches/1.1.x/test/etap/072-cleanup.t (added) +++ couchdb/branches/1.1.x/test/etap/072-cleanup.t Wed Jun 8 18:57:43 2011 @@ -0,0 +1,130 @@ +#!/usr/bin/env escript +%% -*- erlang -*- + +% 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. + +-define(TEST_DB, <<"etap-test-db">>). + +-record(user_ctx, { + name = null, + roles = [], + handler +}). + +-define(ADMIN_USER, #user_ctx{roles=[<<"_admin">>]}). + +main(_) -> + test_util:init_code_path(), + + etap:plan(7), + try test() of + ok -> + etap:end_tests() + catch + Other -> + etap:diag(io_lib:format("Test died abnormally: ~p", [Other])), + timer:sleep(1000), + etap:bail(Other) + end, + ok. + +test() -> + + {ok, _} = couch_server_sup:start_link(test_util:config_files()), + ok = application:start(inets), + couch_server:delete(?TEST_DB, []), + timer:sleep(1000), + + couch_db:create(?TEST_DB, []), + + {ok, AllDbs} = couch_server:all_databases(), + etap:ok(lists:member(?TEST_DB, AllDbs), "Database was created."), + + FooRev = create_design_doc(<<"_design/foo">>, <<"bar">>), + query_view("foo", "bar"), + + BoozRev = create_design_doc(<<"_design/booz">>, <<"baz">>), + query_view("booz", "baz"), + + {ok, Db} = couch_db:open(?TEST_DB, [{user_ctx, ?ADMIN_USER}]), + view_cleanup(), + etap:is(count_index_files(), 2, + "Two index files before any deletions."), + + delete_design_doc(<<"_design/foo">>, FooRev), + view_cleanup(), + etap:is(count_index_files(), 1, + "One index file after first deletion and cleanup."), + + delete_design_doc(<<"_design/booz">>, BoozRev), + view_cleanup(), + etap:is(count_index_files(), 0, + "No index files after second deletion and cleanup."), + + couch_server:delete(?TEST_DB, []), + {ok, AllDbs2} = couch_server:all_databases(), + etap:ok(not lists:member(?TEST_DB, AllDbs2), + "Database was deleted."), + ok. + +create_design_doc(DDName, ViewName) -> + {ok, Db} = couch_db:open(?TEST_DB, [{user_ctx, ?ADMIN_USER}]), + DDoc = couch_doc:from_json_obj({[ + {<<"_id">>, DDName}, + {<<"language">>, <<"javascript">>}, + {<<"views">>, {[ + {ViewName, {[ + {<<"map">>, <<"function(doc) { emit(doc.value, 1); }">>} + ]}} + ]}} + ]}), + {ok, Rev} = couch_db:update_doc(Db, DDoc, []), + couch_db:ensure_full_commit(Db), + couch_db:close(Db), + Rev. + +delete_design_doc(DDName, Rev) -> + {ok, Db} = couch_db:open(?TEST_DB, [{user_ctx, ?ADMIN_USER}]), + DDoc = couch_doc:from_json_obj({[ + {<<"_id">>, DDName}, + {<<"_rev">>, couch_doc:rev_to_str(Rev)}, + {<<"_deleted">>, true} + ]}), + {ok, _} = couch_db:update_doc(Db, DDoc, [Rev]), + couch_db:close(Db). + +db_url() -> + Addr = couch_config:get("httpd", "bind_address", "127.0.0.1"), + Port = integer_to_list(mochiweb_socket_server:get(couch_httpd, port)), + "http://" ++ Addr ++ ":" ++ Port ++ "/" ++ + binary_to_list(?TEST_DB). + +query_view(DDoc, View) -> + {ok, {{_, Code, _}, _Headers, _Body}} = http:request( + get, + {db_url() ++ "/_design/" ++ DDoc ++ "/_view/" ++ View, []}, + [], + [{sync, true}]), + etap:is(Code, 200, "Built view index for " ++ DDoc ++ "."), + ok. + +view_cleanup() -> + {ok, Db} = couch_db:open(?TEST_DB, [{user_ctx, ?ADMIN_USER}]), + couch_view:cleanup_index_files(Db), + couch_db:close(Db). + +count_index_files() -> + % call server to fetch the index files + RootDir = couch_config:get("couchdb", "view_index_dir"), + length(filelib:wildcard(RootDir ++ "/." ++ + binary_to_list(?TEST_DB) ++ "_design"++"/*")). Propchange: couchdb/branches/1.1.x/test/etap/072-cleanup.t ------------------------------------------------------------------------------ svn:executable = *