summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_view_group.erl
diff options
context:
space:
mode:
authorDamien F. Katz <damien@apache.org>2009-09-14 22:33:05 +0000
committerDamien F. Katz <damien@apache.org>2009-09-14 22:33:05 +0000
commit8d7a1c6c21fc253a5772350b159d6c2a273f197a (patch)
tree196e0a5ed4b7c7b9afb0998de3d79a8712f4e6ba /src/couchdb/couch_view_group.erl
parent159ce7f416c430137b2b71f0ef6ffbb6a5c8f2ec (diff)
View refactoring and addition of raw collationoption. Significant performance improvements in view indexer.
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@814893 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb/couch_view_group.erl')
-rw-r--r--src/couchdb/couch_view_group.erl51
1 files changed, 28 insertions, 23 deletions
diff --git a/src/couchdb/couch_view_group.erl b/src/couchdb/couch_view_group.erl
index 8cae3e72..2a757c55 100644
--- a/src/couchdb/couch_view_group.erl
+++ b/src/couchdb/couch_view_group.erl
@@ -438,20 +438,21 @@ open_temp_group(DbName, Language, DesignOptions, MapSrc, RedSrc) ->
id_num=0,
btree=nil,
def=MapSrc,
- reduce_funs= if RedSrc==[] -> []; true -> [{<<"_temp">>, RedSrc}] end},
+ reduce_funs= if RedSrc==[] -> []; true -> [{<<"_temp">>, RedSrc}] end,
+ options=DesignOptions},
- {ok, Db, #group{
- name = <<"_temp">>,
- db=Db,
- views=[View],
- def_lang=Language,
- design_options=DesignOptions,
- sig = erlang:md5(term_to_binary({[View], Language, DesignOptions}))
- }};
+ {ok, Db, set_view_sig(#group{name = <<"_temp">>, db=Db, views=[View],
+ def_lang=Language, design_options=DesignOptions})};
Error ->
Error
end.
+set_view_sig(#group{
+ views=Views,
+ def_lang=Language,
+ design_options=DesignOptions}=G) ->
+ G#group{sig=erlang:md5(term_to_binary({Views, Language, DesignOptions}))}.
+
open_db_group(DbName, GroupId) ->
case couch_db:open(DbName, []) of
{ok, Db} ->
@@ -484,20 +485,17 @@ design_doc_to_view_group(#doc{id=Id,body={Fields}}) ->
Language = proplists:get_value(<<"language">>, Fields, <<"javascript">>),
{DesignOptions} = proplists:get_value(<<"options">>, Fields, {[]}),
{RawViews} = proplists:get_value(<<"views">>, Fields, {[]}),
- % sort the views by name to avoid spurious signature changes
- SortedRawViews = lists:sort(fun({Name1, _}, {Name2, _}) ->
- Name1 >= Name2
- end, RawViews),
% add the views to a dictionary object, with the map source as the key
DictBySrc =
lists:foldl(
fun({Name, {MRFuns}}, DictBySrcAcc) ->
MapSrc = proplists:get_value(<<"map">>, MRFuns),
RedSrc = proplists:get_value(<<"reduce">>, MRFuns, null),
+ {ViewOptions} = proplists:get_value(<<"options">>, MRFuns, {[]}),
View =
- case dict:find(MapSrc, DictBySrcAcc) of
+ case dict:find({MapSrc, ViewOptions}, DictBySrcAcc) of
{ok, View0} -> View0;
- error -> #view{def=MapSrc} % create new view object
+ error -> #view{def=MapSrc, options=ViewOptions} % create new view object
end,
View2 =
if RedSrc == null ->
@@ -505,16 +503,15 @@ design_doc_to_view_group(#doc{id=Id,body={Fields}}) ->
true ->
View#view{reduce_funs=[{Name,RedSrc}|View#view.reduce_funs]}
end,
- dict:store(MapSrc, View2, DictBySrcAcc)
- end, dict:new(), SortedRawViews),
+ dict:store({MapSrc, ViewOptions}, View2, DictBySrcAcc)
+ end, dict:new(), RawViews),
% number the views
{Views, _N} = lists:mapfoldl(
fun({_Src, View}, N) ->
{View#view{id_num=N},N+1}
- end, 0, dict:to_list(DictBySrc)),
+ end, 0, lists:sort(dict:to_list(DictBySrc))),
- Group = #group{name=Id, views=Views, def_lang=Language, design_options=DesignOptions},
- Group#group{sig=erlang:md5(term_to_binary({Views, Language, DesignOptions}))}.
+ set_view_sig(#group{name=Id, views=Views, def_lang=Language, design_options=DesignOptions}).
reset_group(#group{views=Views}=Group) ->
Views2 = [View#view{btree=nil} || View <- Views],
@@ -534,12 +531,13 @@ init_group(Db, Fd, #group{views=Views}=Group, nil) ->
init_group(Db, Fd, Group,
#index_header{seq=0, purge_seq=couch_db:get_purge_seq(Db),
id_btree_state=nil, view_states=[nil || _ <- Views]});
-init_group(Db, Fd, #group{def_lang=Lang,views=Views}=Group, IndexHeader) ->
+init_group(Db, Fd, #group{def_lang=Lang,views=Views}=
+ Group, IndexHeader) ->
#index_header{seq=Seq, purge_seq=PurgeSeq,
id_btree_state=IdBtreeState, view_states=ViewStates} = IndexHeader,
{ok, IdBtree} = couch_btree:open(IdBtreeState, Fd),
Views2 = lists:zipwith(
- fun(BtreeState, #view{reduce_funs=RedFuns}=View) ->
+ fun(BtreeState, #view{reduce_funs=RedFuns,options=Options}=View) ->
FunSrcs = [FunSrc || {_Name, FunSrc} <- RedFuns],
ReduceFun =
fun(reduce, KVs) ->
@@ -555,8 +553,15 @@ init_group(Db, Fd, #group{def_lang=Lang,views=Views}=Group, IndexHeader) ->
UserReds),
{Count, Reduced}
end,
+
+ case proplists:get_value(<<"collation">>, Options, <<"default">>) of
+ <<"default">> ->
+ Less = fun couch_view:less_json_ids/2;
+ <<"raw">> ->
+ Less = fun(A,B) -> A < B end
+ end,
{ok, Btree} = couch_btree:open(BtreeState, Fd,
- [{less, fun couch_view:less_json_keys/2},
+ [{less, Less},
{reduce, ReduceFun}]),
View#view{btree=Btree}
end,