Skip to content

Commit 59dbec9

Browse files
authored
Mas i1847 conditionalput (#86) (#87)
Add support for `if_not_modified` option, which should work as with riak-erlang-client (and use the X-Riak-If-Not-Modified) header in Riak 3.0.13. Add support for using `if_none_match`, and `if_match`. However, if possible, `if_not_modified` should always in preference to `if_match` as `if_match` requires re-calculation of the etag, using knowledge that should be internal to Riak.
1 parent 556a355 commit 59dbec9

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

include/raw_http.hrl

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
-define(HEAD_CLIENT, "X-Riak-ClientId").
3939
-define(HEAD_USERMETA_PREFIX, "x-riak-meta-").
4040
-define(HEAD_INDEX_PREFIX, "X-Riak-Index-").
41+
-define(HEAD_IF_NOT_MODIFIED, "X-Riak-If-Not-Modified").
4142

4243
%%======================================================================
4344
%% JSON keys/values

src/rhc.erl

+29-2
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,9 @@ put(Rhc, Object, Options) ->
866866
true -> put
867867
end,
868868
{Headers0, Body} = rhc_obj:serialize_riakc_obj(Rhc, Object),
869-
Headers = [{?HEAD_CLIENT, client_id(Rhc, Options)}
870-
|Headers0],
869+
PutHeaders = conditional_put_headers(Options, Object),
870+
Headers =
871+
[{?HEAD_CLIENT, client_id(Rhc, Options)} |Headers0] ++ PutHeaders,
871872
case request(Method, Url, ["200", "204", "300"], Headers, Body, Rhc) of
872873
{ok, Status, ReplyHeaders, ReplyBody} ->
873874
if Status =:= "204" ->
@@ -1893,6 +1894,32 @@ get_ssl_options(Options) ->
18931894
[]
18941895
end.
18951896

1897+
conditional_put_headers(Options, Object) ->
1898+
NoneMatch =
1899+
case lists:member(if_none_match, Options) of
1900+
true ->
1901+
[{"If-None-Match", "*"}];
1902+
false ->
1903+
[]
1904+
end,
1905+
Match =
1906+
case lists:member(if_match, Options) of
1907+
true ->
1908+
VTag = riakc_obj:get_vtag(Object),
1909+
[{"If-Match", VTag}];
1910+
false ->
1911+
[]
1912+
end,
1913+
NotModified =
1914+
case lists:member(if_not_modified, Options) of
1915+
true ->
1916+
VC = base64:encode(riakc_obj:vclock(Object)),
1917+
[{?HEAD_IF_NOT_MODIFIED, VC}];
1918+
false ->
1919+
[]
1920+
end,
1921+
NoneMatch ++ Match ++ NotModified.
1922+
18961923
extract_bucket_type({<<"default">>, B}) ->
18971924
{undefined, B};
18981925
extract_bucket_type({T,B}) ->

0 commit comments

Comments
 (0)