couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fdman...@apache.org
Subject svn commit: r978933 - in /couchdb/branches/new_replicator/src/couchdb: couch_api_wrap.erl couch_changes.erl couch_replicate.erl
Date Sat, 24 Jul 2010 19:47:37 GMT
Author: fdmanana
Date: Sat Jul 24 19:47:36 2010
New Revision: 978933

URL: http://svn.apache.org/viewvc?rev=978933&view=rev
Log:
Refactoring to use always the internal _changes API for the filtered replication (undoing
a previous refactoring of couch_changes.erl).
This allows to reuse code after the continuous replication is implemented.

Modified:
    couchdb/branches/new_replicator/src/couchdb/couch_api_wrap.erl
    couchdb/branches/new_replicator/src/couchdb/couch_changes.erl
    couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl

Modified: couchdb/branches/new_replicator/src/couchdb/couch_api_wrap.erl
URL: http://svn.apache.org/viewvc/couchdb/branches/new_replicator/src/couchdb/couch_api_wrap.erl?rev=978933&r1=978932&r2=978933&view=diff
==============================================================================
--- couchdb/branches/new_replicator/src/couchdb/couch_api_wrap.erl (original)
+++ couchdb/branches/new_replicator/src/couchdb/couch_api_wrap.erl Sat Jul 24 19:47:36 2010
@@ -44,7 +44,7 @@
     open_doc/4,
     open_doc_revs/6,
     update_doc/4,
-    changes_since/6
+    changes_since/5
     ]).
 
 db_open(Db, Options) ->
@@ -312,7 +312,7 @@ update_doc(#httpdb{} = HttpDb, Doc, Opti
 update_doc(Db, Doc, Options, Type) ->
     couch_db:update_doc(Db, Doc, Options, Type).
 
-changes_since(#httpdb{} = HttpDb, Style, StartSeq, UserFun, Acc, Options) ->
+changes_since(#httpdb{} = HttpDb, Style, StartSeq, UserFun, Options) ->
     #httpdb{url=Url, headers=Headers, oauth=OAuth} = HttpDb,
     Url2 = Url ++ "_changes",
     QArgs = changes_q_args(
@@ -332,25 +332,34 @@ changes_since(#httpdb{} = HttpDb, Style,
         end
     end,
     EventFun = fun(Ev) ->
-        changes_ev1(Ev, UserFun, Acc)
+        changes_ev1(Ev, fun(DocInfo, _Acc) -> UserFun(DocInfo) end, [])
     end,
     try
         json_stream_parse:events(DataFun, EventFun)
     after
         catch ibrowse:stop_worker_process(Worker)
     end;
-changes_since(Db, Style, StartSeq, UserFun, Acc, Options) ->
-    FilterName = ?b2l(couch_util:get_value(filter, Options, <<>>)),
+changes_since(Db, Style, StartSeq, UserFun, Options) ->
+    Args = #changes_args{
+        style = Style,
+        since = StartSeq,
+        filter = ?b2l(couch_util:get_value(filter, Options, <<>>)),
+        feed = case couch_util:get_value(continuous, Options, false) of
+            true ->
+                "continuous";
+            false ->
+                "normal"
+        end,
+        timeout = infinity
+    },
     QueryParams = couch_util:get_value(query_params, Options, {[]}),
-    JsonReq = changes_json_req(Db, FilterName, QueryParams),
-    DocFilterFun = couch_changes:doc_info_filter_fun(FilterName, Style,
-        {json_req, JsonReq}, Db),
-    ChangesFun = fun(DocInfo, Acc2) ->
-        DocInfoList = DocFilterFun(DocInfo),
-        Acc3 = lists:foldl(UserFun, Acc2, DocInfoList),
-        {ok, Acc3}
-    end,
-    couch_db:changes_since(Db, Style, StartSeq, ChangesFun, Acc).
+    Req = changes_json_req(Db, Args#changes_args.filter, QueryParams),
+    ChangesFeedFun = couch_changes:handle_changes(Args, {json_req, Req}, Db),
+    ChangesFeedFun(fun({change, Change, _}, _) ->
+            UserFun(json_to_doc_info(Change));
+        (_, _) ->
+            ok
+    end).
 
 
 % internal functions

Modified: couchdb/branches/new_replicator/src/couchdb/couch_changes.erl
URL: http://svn.apache.org/viewvc/couchdb/branches/new_replicator/src/couchdb/couch_changes.erl?rev=978933&r1=978932&r2=978933&view=diff
==============================================================================
--- couchdb/branches/new_replicator/src/couchdb/couch_changes.erl (original)
+++ couchdb/branches/new_replicator/src/couchdb/couch_changes.erl Sat Jul 24 19:47:36 2010
@@ -14,7 +14,6 @@
 -include("couch_db.hrl").
 
 -export([handle_changes/3]).
--export([doc_info_filter_fun/4]).
 
 %% @type Req -> #httpd{} | {json_req, JsonObj()}
 handle_changes(#changes_args{style=Style}=Args1, Req, Db) ->
@@ -75,73 +74,47 @@ handle_changes(#changes_args{style=Style
 
 %% @type Req -> #httpd{} | {json_req, JsonObj()}
 make_filter_fun(FilterName, Style, Req, Db) ->
-    DocInfoFilter = doc_info_filter_fun(FilterName, Style, Req, Db),
-    fun(DocInfo) ->
-         DocInfoList = DocInfoFilter(DocInfo),
-         [{[{<<"rev">>, couch_doc:rev_to_str(R)}]} ||
-            #doc_info{revs=[#rev_info{rev=R} | _]} <- DocInfoList]
-    end.
-
-doc_info_filter_fun(FilterName, Style, Req, Db) ->
-    case [?l2b(couch_httpd:unquote(Part))
-        || Part <- string:tokens(FilterName, "/")] of
+    case [list_to_binary(couch_httpd:unquote(Part))
+            || Part <- string:tokens(FilterName, "/")] of
     [] ->
-        fun(#doc_info{revs=Revs} = DocInfo) ->
+        fun(#doc_info{revs=[#rev_info{rev=Rev}|_]=Revs}) ->
             case Style of
             main_only ->
-                [DocInfo];
+                [{[{<<"rev">>, couch_doc:rev_to_str(Rev)}]}];
             all_docs ->
-                [DocInfo#doc_info{revs=[RevInfo]} || RevInfo <- Revs]
+                [{[{<<"rev">>, couch_doc:rev_to_str(R)}]}
+                        || #rev_info{rev=R} <- Revs]
             end
         end;
-    [DDocName, FName] ->
-        DesignId = <<"_design/", DDocName/binary>>,
+    [DName, FName] ->
+        DesignId = <<"_design/", DName/binary>>,
         DDoc = couch_httpd_db:couch_doc_open(Db, DesignId, nil, []),
-        {ok, DDoc} = couch_db:open_doc(Db, <<"_design/", DDocName/binary>>),
         % validate that the ddoc has the filter fun
         #doc{body={Props}} = DDoc,
         couch_util:get_nested_json_value({Props}, [<<"filters">>, FName]),
         fun(DocInfo) ->
-            apply_filter_docinfo(DocInfo, Style, Req, Db, FName, DDoc)
+            DocInfos =
+            case Style of
+            main_only ->
+                [DocInfo];
+            all_docs ->
+                [DocInfo#doc_info{revs=[Rev]}|| Rev <- DocInfo#doc_info.revs]
+            end,
+            Docs = [Doc || {ok, Doc} <- [
+                    couch_db:open_doc(Db, DocInfo2, [deleted, conflicts])
+                        || DocInfo2 <- DocInfos]],
+            {ok, Passes} = couch_query_servers:filter_docs(
+                Req, Db, DDoc, FName, Docs
+            ),
+            [{[{<<"rev">>, couch_doc:rev_to_str({RevPos,RevId})}]}
+                || {Pass, #doc{revs={RevPos,[RevId|_]}}}
+                <- lists:zip(Passes, Docs), Pass == true]
         end;
     _Else ->
         throw({bad_request,
             "filter parameter must be of the form `designname/filtername`"})
     end.
 
-apply_filter_docinfo(DocInfo, Style, Req, Db, FilterName, DDoc) ->
-    DocInfos = case Style of
-    main_only ->
-        [DocInfo];
-    all_docs ->
-        [DocInfo#doc_info{revs=[Rev]} || Rev <- DocInfo#doc_info.revs]
-    end,
-    {_, PosDocInfoDict, Docs} = lists:foldl(
-        fun(DI, {P, Dict, Acc}) ->
-            case couch_db:open_doc(Db, DI, [deleted, conflicts]) of
-            {ok, Doc} ->
-                {P + 1, dict:store(P, DI, Dict), [Doc | Acc]};
-            _ ->
-                {P, Dict, Acc}
-            end
-        end,
-        {1, dict:new(), []},
-        DocInfos
-    ),
-    {ok, Passes} = couch_query_servers:filter_docs(
-        Req, Db, DDoc, FilterName, lists:reverse(Docs)
-    ),
-    {_, Filtered} = lists:foldl(
-        fun(true, {P, Acc}) ->
-            {P + 1, [dict:fetch(P, PosDocInfoDict) | Acc]};
-        (_, {P, Acc}) ->
-            {P + 1, Acc}
-        end,
-        {1, []},
-        Passes
-    ),
-    lists:reverse(Filtered).
-
 get_changes_timeout(Args, Callback) ->
     #changes_args{
         heartbeat = Heartbeat,

Modified: couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl
URL: http://svn.apache.org/viewvc/couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl?rev=978933&r1=978932&r2=978933&view=diff
==============================================================================
--- couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl (original)
+++ couchdb/branches/new_replicator/src/couchdb/couch_replicate.erl Sat Jul 24 19:47:36 2010
@@ -156,11 +156,11 @@ spawn_changes_reader(Cp, StartSeq, Sourc
     spawn_link(
         fun()->
             couch_api_wrap:changes_since(Source, all_docs, StartSeq,
-                fun(#doc_info{high_seq=Seq, revs=Revs} = DocInfo, _) ->
+                fun(#doc_info{high_seq=Seq, revs=Revs} = DocInfo) ->
                     Cp ! {seq_start, {Seq, length(Revs)}},
                     Cp ! {add_stat, {#stats.missing_checked, length(Revs)}},
                     ok = couch_work_queue:queue(ChangesQueue, DocInfo)
-                end, ok, Options),
+                end, Options),
             couch_work_queue:close(ChangesQueue)
         end).
 



Mime
View raw message