summaryrefslogtreecommitdiff
path: root/src/couch_mrview/src/couch_mrview_index.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/couch_mrview/src/couch_mrview_index.erl')
-rw-r--r--src/couch_mrview/src/couch_mrview_index.erl174
1 files changed, 90 insertions, 84 deletions
diff --git a/src/couch_mrview/src/couch_mrview_index.erl b/src/couch_mrview/src/couch_mrview_index.erl
index 68f1d2322..a024d35c8 100644
--- a/src/couch_mrview/src/couch_mrview_index.erl
+++ b/src/couch_mrview/src/couch_mrview_index.erl
@@ -12,7 +12,6 @@
-module(couch_mrview_index).
-
-export([get/2]).
-export([init/2, open/2, close/1, reset/1, delete/1, shutdown/1]).
-export([start_update/4, purge/4, process_doc/3, finish_update/1, commit/1]).
@@ -24,7 +23,6 @@
-include_lib("couch/include/couch_db.hrl").
-include_lib("couch_mrview/include/couch_mrview.hrl").
-
get(db_name, #mrst{db_name = DbName}) ->
DbName;
get(idx_name, #mrst{idx_name = IdxName}) ->
@@ -39,9 +37,18 @@ get(update_options, #mrst{design_opts = Opts}) ->
IncDesign = couch_util:get_value(<<"include_design">>, Opts, false),
LocalSeq = couch_util:get_value(<<"local_seq">>, Opts, false),
Partitioned = couch_util:get_value(<<"partitioned">>, Opts, false),
- if IncDesign -> [include_design]; true -> [] end
- ++ if LocalSeq -> [local_seq]; true -> [] end
- ++ if Partitioned -> [partitioned]; true -> [] end;
+ if
+ IncDesign -> [include_design];
+ true -> []
+ end ++
+ if
+ LocalSeq -> [local_seq];
+ true -> []
+ end ++
+ if
+ Partitioned -> [partitioned];
+ true -> []
+ end;
get(fd, #mrst{fd = Fd}) ->
Fd;
get(language, #mrst{language = Language}) ->
@@ -69,11 +76,12 @@ get(info, State) ->
{ok, [
{signature, list_to_binary(couch_index_util:hexsig(Sig))},
{language, Lang},
- {sizes, {[
- {file, FileSize},
- {active, ActiveSize},
- {external, ExternalSize}
- ]}},
+ {sizes,
+ {[
+ {file, FileSize},
+ {active, ActiveSize},
+ {external, ExternalSize}
+ ]}},
{update_seq, UpdateSeq},
{purge_seq, PurgeSeq},
{update_options, UpdateOptions}
@@ -81,16 +89,14 @@ get(info, State) ->
get(Other, _) ->
throw({unknown_index_property, Other}).
-
init(Db, DDoc) ->
{ok, State} = couch_mrview_util:ddoc_to_mrst(couch_db:name(Db), DDoc),
{ok, set_partitioned(Db, State)}.
-
open(Db, State0) ->
#mrst{
- db_name=DbName,
- sig=Sig
+ db_name = DbName,
+ sig = Sig
} = State = set_partitioned(Db, State0),
IndexFName = couch_mrview_util:index_file(DbName, Sig),
@@ -128,14 +134,18 @@ open(Db, State0) ->
ensure_local_purge_doc(Db, NewSt),
{ok, NewSt};
{ok, {WrongSig, _}} ->
- couch_log:error("~s has the wrong signature: expected: ~p but got ~p",
- [IndexFName, Sig, WrongSig]),
+ couch_log:error(
+ "~s has the wrong signature: expected: ~p but got ~p",
+ [IndexFName, Sig, WrongSig]
+ ),
NewSt = couch_mrview_util:reset_index(Db, Fd, State),
ensure_local_purge_doc(Db, NewSt),
{ok, NewSt};
{ok, Else} ->
- couch_log:error("~s has a bad header: got ~p",
- [IndexFName, Else]),
+ couch_log:error(
+ "~s has a bad header: got ~p",
+ [IndexFName, Else]
+ ),
NewSt = couch_mrview_util:reset_index(Db, Fd, State),
ensure_local_purge_doc(Db, NewSt),
{ok, NewSt};
@@ -145,17 +155,17 @@ open(Db, State0) ->
{ok, NewSt}
end;
{error, Reason} = Error ->
- couch_log:error("Failed to open view file '~s': ~s",
- [IndexFName, file:format_error(Reason)]),
+ couch_log:error(
+ "Failed to open view file '~s': ~s",
+ [IndexFName, file:format_error(Reason)]
+ ),
Error
end.
-
close(State) ->
erlang:demonitor(State#mrst.fd_monitor, [flush]),
couch_file:close(State#mrst.fd).
-
% This called after ddoc_updated event occurrs, and
% before we shutdown couch_index process.
% We unlink couch_index from corresponding couch_file and demonitor it.
@@ -167,19 +177,16 @@ shutdown(State) ->
erlang:demonitor(State#mrst.fd_monitor, [flush]),
unlink(State#mrst.fd).
-
-delete(#mrst{db_name=DbName, sig=Sig}=State) ->
+delete(#mrst{db_name = DbName, sig = Sig} = State) ->
couch_file:close(State#mrst.fd),
catch couch_mrview_util:delete_files(DbName, Sig).
-
reset(State) ->
couch_util:with_db(State#mrst.db_name, fun(Db) ->
NewState = couch_mrview_util:reset_index(Db, State#mrst.fd, State),
{ok, NewState}
end).
-
start_update(PartialDest, State, NumChanges, NumChangesDone) ->
couch_mrview_updater:start_update(
PartialDest,
@@ -188,94 +195,93 @@ start_update(PartialDest, State, NumChanges, NumChangesDone) ->
NumChangesDone
).
-
purge(Db, PurgeSeq, PurgedIdRevs, State) ->
couch_mrview_updater:purge(Db, PurgeSeq, PurgedIdRevs, State).
-
process_doc(Doc, Seq, State) ->
couch_mrview_updater:process_doc(Doc, Seq, State).
-
finish_update(State) ->
couch_mrview_updater:finish_update(State).
-
commit(State) ->
Header = {State#mrst.sig, couch_mrview_util:make_header(State)},
couch_file:write_header(State#mrst.fd, Header).
-
compact(Db, State, Opts) ->
couch_mrview_compactor:compact(Db, State, Opts).
-
swap_compacted(OldState, NewState) ->
couch_mrview_compactor:swap_compacted(OldState, NewState).
-
remove_compacted(State) ->
couch_mrview_compactor:remove_compacted(State).
-
index_file_exists(State) ->
#mrst{
- db_name=DbName,
- sig=Sig
+ db_name = DbName,
+ sig = Sig
} = State,
IndexFName = couch_mrview_util:index_file(DbName, Sig),
filelib:is_file(IndexFName).
-
verify_index_exists(DbName, Props) ->
try
Type = couch_util:get_value(<<"type">>, Props),
- if Type =/= <<"mrview">> -> false; true ->
- DDocId = couch_util:get_value(<<"ddoc_id">>, Props),
- couch_util:with_db(DbName, fun(Db) ->
- case couch_db:get_design_doc(Db, DDocId) of
- {ok, #doc{} = DDoc} ->
- {ok, IdxState} = couch_mrview_util:ddoc_to_mrst(
- DbName, DDoc),
- IdxSig = IdxState#mrst.sig,
- SigInLocal = couch_util:get_value(
- <<"signature">>, Props),
- couch_index_util:hexsig(IdxSig) == SigInLocal;
- {not_found, _} ->
- false
- end
- end)
+ if
+ Type =/= <<"mrview">> ->
+ false;
+ true ->
+ DDocId = couch_util:get_value(<<"ddoc_id">>, Props),
+ couch_util:with_db(DbName, fun(Db) ->
+ case couch_db:get_design_doc(Db, DDocId) of
+ {ok, #doc{} = DDoc} ->
+ {ok, IdxState} = couch_mrview_util:ddoc_to_mrst(
+ DbName, DDoc
+ ),
+ IdxSig = IdxState#mrst.sig,
+ SigInLocal = couch_util:get_value(
+ <<"signature">>, Props
+ ),
+ couch_index_util:hexsig(IdxSig) == SigInLocal;
+ {not_found, _} ->
+ false
+ end
+ end)
end
- catch _:_ ->
- false
+ catch
+ _:_ ->
+ false
end.
-
set_partitioned(Db, State) ->
#mrst{
design_opts = DesignOpts
} = State,
DbPartitioned = couch_db:is_partitioned(Db),
ViewPartitioned = couch_util:get_value(
- <<"partitioned">>, DesignOpts, DbPartitioned),
+ <<"partitioned">>, DesignOpts, DbPartitioned
+ ),
IsPartitioned = DbPartitioned andalso ViewPartitioned,
State#mrst{partitioned = IsPartitioned}.
-
ensure_local_purge_docs(DbName, DDocs) ->
couch_util:with_db(DbName, fun(Db) ->
- lists:foreach(fun(DDoc) ->
- try couch_mrview_util:ddoc_to_mrst(DbName, DDoc) of
- {ok, MRSt} ->
- ensure_local_purge_doc(Db, MRSt)
- catch _:_ ->
- ok
- end
- end, DDocs)
+ lists:foreach(
+ fun(DDoc) ->
+ try couch_mrview_util:ddoc_to_mrst(DbName, DDoc) of
+ {ok, MRSt} ->
+ ensure_local_purge_doc(Db, MRSt)
+ catch
+ _:_ ->
+ ok
+ end
+ end,
+ DDocs
+ )
end).
-
-ensure_local_purge_doc(Db, #mrst{}=State) ->
+ensure_local_purge_doc(Db, #mrst{} = State) ->
Sig = couch_index_util:hexsig(get(signature, State)),
DocId = couch_mrview_util:get_local_purge_doc_id(Sig),
case couch_db:open_doc(Db, DocId, []) of
@@ -285,33 +291,33 @@ ensure_local_purge_doc(Db, #mrst{}=State) ->
ok
end.
-
create_local_purge_doc(Db, State) ->
PurgeSeq = couch_db:get_purge_seq(Db),
update_local_purge_doc(Db, State, PurgeSeq).
-
update_local_purge_doc(Db, State) ->
update_local_purge_doc(Db, State, get(purge_seq, State)).
-
update_local_purge_doc(Db, State, PSeq) ->
Sig = couch_index_util:hexsig(State#mrst.sig),
DocId = couch_mrview_util:get_local_purge_doc_id(Sig),
{Mega, Secs, _} = os:timestamp(),
NowSecs = Mega * 1000000 + Secs,
- BaseDoc = couch_doc:from_json_obj({[
- {<<"_id">>, DocId},
- {<<"type">>, <<"mrview">>},
- {<<"purge_seq">>, PSeq},
- {<<"updated_on">>, NowSecs},
- {<<"ddoc_id">>, get(idx_name, State)},
- {<<"signature">>, Sig}
- ]}),
- Doc = case couch_db:open_doc(Db, DocId, []) of
- {ok, #doc{revs = Revs}} ->
- BaseDoc#doc{revs = Revs};
- {not_found, _} ->
- BaseDoc
- end,
+ BaseDoc = couch_doc:from_json_obj(
+ {[
+ {<<"_id">>, DocId},
+ {<<"type">>, <<"mrview">>},
+ {<<"purge_seq">>, PSeq},
+ {<<"updated_on">>, NowSecs},
+ {<<"ddoc_id">>, get(idx_name, State)},
+ {<<"signature">>, Sig}
+ ]}
+ ),
+ Doc =
+ case couch_db:open_doc(Db, DocId, []) of
+ {ok, #doc{revs = Revs}} ->
+ BaseDoc#doc{revs = Revs};
+ {not_found, _} ->
+ BaseDoc
+ end,
couch_db:update_doc(Db, Doc, []).