From 4361a832c18be3f9d96e7943f6e1bbce92a94cad Mon Sep 17 00:00:00 2001 From: Filipe David Borba Manana Date: Fri, 20 May 2011 10:56:30 +0000 Subject: Replication manager: don't update doc if new state == current state This is to avoid unncessary updates. This is a backport of revision 1125317 (trunk). git-svn-id: https://svn.apache.org/repos/asf/couchdb/branches/1.1.x@1125320 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_rep.erl | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'src/couchdb/couch_rep.erl') diff --git a/src/couchdb/couch_rep.erl b/src/couchdb/couch_rep.erl index 5c9fbce6..e4b4264e 100644 --- a/src/couchdb/couch_rep.erl +++ b/src/couchdb/couch_rep.erl @@ -883,13 +883,18 @@ update_rep_doc({Props} = _RepDoc, KVs) -> update_rep_doc(RepDb, #doc{body = {RepDocBody}} = RepDoc, KVs) -> NewRepDocBody = lists:foldl( - fun({<<"_replication_state">> = K, _V} = KV, Body) -> - Body1 = lists:keystore(K, 1, Body, KV), - {Mega, Secs, _} = erlang:now(), - UnixTime = Mega * 1000000 + Secs, - lists:keystore( - <<"_replication_state_time">>, 1, - Body1, {<<"_replication_state_time">>, UnixTime}); + fun({<<"_replication_state">> = K, State} = KV, Body) -> + case couch_util:get_value(K, Body) of + State -> + Body; + _ -> + Body1 = lists:keystore(K, 1, Body, KV), + {Mega, Secs, _} = erlang:now(), + UnixTime = Mega * 1000000 + Secs, + lists:keystore( + <<"_replication_state_time">>, 1, + Body1, {<<"_replication_state_time">>, UnixTime}) + end; ({K, _V} = KV, Body) -> lists:keystore(K, 1, Body, KV) end, -- cgit v1.2.3 From 86c8ffc732052ba5eb942330ad431d1a42297034 Mon Sep 17 00:00:00 2001 From: Filipe David Borba Manana Date: Sat, 21 May 2011 12:29:38 +0000 Subject: Merged revision 1125680 from trunk Use RFC3339 timestamps in replication documents As recently proposed by Max Odgen, RFC3339 timestamps are now used instead of Unix timestamps. git-svn-id: https://svn.apache.org/repos/asf/couchdb/branches/1.1.x@1125682 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_rep.erl | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src/couchdb/couch_rep.erl') diff --git a/src/couchdb/couch_rep.erl b/src/couchdb/couch_rep.erl index e4b4264e..49a82e5d 100644 --- a/src/couchdb/couch_rep.erl +++ b/src/couchdb/couch_rep.erl @@ -889,11 +889,9 @@ update_rep_doc(RepDb, #doc{body = {RepDocBody}} = RepDoc, KVs) -> Body; _ -> Body1 = lists:keystore(K, 1, Body, KV), - {Mega, Secs, _} = erlang:now(), - UnixTime = Mega * 1000000 + Secs, lists:keystore( <<"_replication_state_time">>, 1, - Body1, {<<"_replication_state_time">>, UnixTime}) + Body1, {<<"_replication_state_time">>, timestamp()}) end; ({K, _V} = KV, Body) -> lists:keystore(K, 1, Body, KV) @@ -909,6 +907,26 @@ update_rep_doc(RepDb, #doc{body = {RepDocBody}} = RepDoc, KVs) -> [] ). +% RFC3339 timestamps. +% Note: doesn't include the time seconds fraction (RFC3339 says it's optional). +timestamp() -> + {{Year, Month, Day}, {Hour, Min, Sec}} = calendar:now_to_local_time(now()), + UTime = erlang:universaltime(), + LocalTime = calendar:universal_time_to_local_time(UTime), + DiffSecs = calendar:datetime_to_gregorian_seconds(LocalTime) - + calendar:datetime_to_gregorian_seconds(UTime), + zone(DiffSecs div 3600, (DiffSecs rem 3600) div 60), + iolist_to_binary( + io_lib:format("~4..0w-~2..0w-~2..0wT~2..0w:~2..0w:~2..0w~s", + [Year, Month, Day, Hour, Min, Sec, + zone(DiffSecs div 3600, (DiffSecs rem 3600) div 60)])). + +zone(Hr, Min) when Hr >= 0, Min >= 0 -> + io_lib:format("+~2..0w:~2..0w", [Hr, Min]); +zone(Hr, Min) -> + io_lib:format("-~2..0w:~2..0w", [abs(Hr), abs(Min)]). + + maybe_set_triggered({RepProps} = RepDoc, RepId) -> case couch_util:get_value(<<"_replication_state">>, RepProps) of <<"triggered">> -> -- cgit v1.2.3 From 172a751ce84a46d2f121a1c57f6d5554447c7bee Mon Sep 17 00:00:00 2001 From: Filipe David Borba Manana Date: Wed, 25 May 2011 19:01:03 +0000 Subject: Backported revision 1127632 from trunk Force non admins to supply a user_ctx in replication documents This is to prevent users deleting replication documents added by other users and to make it clear who triggers which replications. git-svn-id: https://svn.apache.org/repos/asf/couchdb/branches/1.1.x@1127634 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_rep.erl | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/couchdb/couch_rep.erl') diff --git a/src/couchdb/couch_rep.erl b/src/couchdb/couch_rep.erl index 49a82e5d..fd323f7f 100644 --- a/src/couchdb/couch_rep.erl +++ b/src/couchdb/couch_rep.erl @@ -899,13 +899,14 @@ update_rep_doc(RepDb, #doc{body = {RepDocBody}} = RepDoc, KVs) -> RepDocBody, KVs ), - % might not succeed - when the replication doc is deleted right - % before this update (not an error) - couch_db:update_doc( - RepDb, - RepDoc#doc{body = {NewRepDocBody}}, - [] - ). + case NewRepDocBody of + RepDocBody -> + ok; + _ -> + % might not succeed - when the replication doc is deleted right + % before this update (not an error) + couch_db:update_doc(RepDb, RepDoc#doc{body = {NewRepDocBody}}, []) + end. % RFC3339 timestamps. % Note: doesn't include the time seconds fraction (RFC3339 says it's optional). -- cgit v1.2.3