summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Kocoloski <adam@cloudant.com>2010-08-18 21:46:42 -0400
committerAdam Kocoloski <adam@cloudant.com>2010-08-27 16:34:31 -0400
commit0f257eb59352fd14f85c98a172388e2afab8fd3a (patch)
tree88ab80f217f9723d94f25ce42c4ccdb2ed2ab289
parente4470d5017f098af9a9d7b3c0d92362d8eb90cce (diff)
implement cleanup_index_files()
-rw-r--r--apps/fabric/src/fabric.erl26
1 files changed, 25 insertions, 1 deletions
diff --git a/apps/fabric/src/fabric.erl b/apps/fabric/src/fabric.erl
index b3bf3b15..d620b985 100644
--- a/apps/fabric/src/fabric.erl
+++ b/apps/fabric/src/fabric.erl
@@ -31,7 +31,8 @@
get_view_group_info/2]).
% miscellany
--export([design_docs/1, reset_validation_funs/1]).
+-export([design_docs/1, reset_validation_funs/1, cleanup_index_files/0,
+ cleanup_index_files/1]).
-include("fabric.hrl").
@@ -167,6 +168,29 @@ reset_validation_funs(DbName) ->
[rexi:cast(Node, {fabric_rpc, reset_validation_funs, [Name]}) ||
#shard{node=Node, name=Name} <- mem3:shards(DbName)].
+cleanup_index_files() ->
+ {ok, DbNames} = fabric:all_dbs(),
+ [cleanup_index_files(Name) || Name <- DbNames].
+
+cleanup_index_files(DbName) ->
+ {ok, DesignDocs} = fabric:design_docs(DbName),
+
+ ActiveSigs = lists:map(fun(#doc{id = GroupId}) ->
+ {ok, Info} = fabric:get_view_group_info(DbName, GroupId),
+ binary_to_list(couch_util:get_value(signature, Info))
+ end, [couch_doc:from_json_obj(DD) || DD <- DesignDocs]),
+
+ FileList = filelib:wildcard([couch_config:get("couchdb", "view_index_dir"),
+ "/.shards/*/", couch_util:to_list(DbName), "_design/*"]),
+
+ DeleteFiles = if ActiveSigs =:= [] -> FileList; true ->
+ {ok, RegExp} = re:compile([$(, string:join(ActiveSigs, "|"), $)]),
+ lists:filter(fun(FilePath) ->
+ re:run(FilePath, RegExp, [{capture, none}]) == nomatch
+ end, FileList)
+ end,
+ [file:delete(File) || File <- DeleteFiles].
+
%% some simple type validation and transcoding
dbname(DbName) when is_list(DbName) ->