Anonymous avatar Anonymous committed 7f1c564

keys *must* be binaries - guard in riak_object + change tests and docs

Comments (0)

Files changed (6)

src/jiak_default.erl

 expires_in_seconds(_Key, ReqData, Context) ->
     {proplists:get_value(expiry_seconds, BucketProps, 600), ReqData, Context}.
 
-%% @spec check_write({container|item, riak_object:binary_key()},
+%% @spec check_write({container|item, riak_object:key()},
 %%                   jiak_object(), webmachine:wrq(), jiak_context()) ->
 %%         {{ok, jiak_object()}|{error, term()},
 %%          webmachine:wrq(), jiak_context()}

src/jiak_example.erl

 expires_in_seconds(_Key, ReqData, Context) ->
     {600, ReqData, Context}.
 
-%% @spec check_write({container|item, riak_object:binary_key()},
+%% @spec check_write({container|item, riak_object:key()},
 %%                   jiak_object(), webmachine:wrq(), jiak_context()) ->
 %%         {{ok, jiak_object()}|{error, term()},
 %%          webmachine:wrq(), jiak_context()}

src/jiak_object.erl

 -export([diff/2, undefined/0]).
 -export([test_roundtrip_vclock/0]).
 
-%% @type jiak_key()=riak_object:binary_key().
 
-%% @spec new(riak_object:bucket(), jiak_key()) -> jiak_object()
+%% @spec new(riak_object:bucket(), riak_object:key()) -> jiak_object()
 %% @doc produces an empty jiak object
 %% @equiv new(B, K, {struct, []}, [])
 new(B, K) -> new(B, K, {struct, []}, []).
 
-%% @spec new(riak_object:bucket(), jiak_key(), mochijson2()) -> jiak_object()
+%% @spec new(riak_object:bucket(),riak_object:key(),mochijson2())->jiak_object()
 %% @doc produces a new jiak object with no links
 %% @equiv new(B, K, O, [])
 new(B, K, O) -> new(B, K, O, []).
 
-%% @spec new(riak_object:bucket(), jiak_key(), mochijson2(), 
+%% @spec new(riak_object:bucket(), riak_object:key(), mochijson2(), 
 %%           [link()]) -> jiak_object()
 %% @doc produces empty jiak object, a mochijson2 structure that
 %%      would encode to the JSON structure:
     Links = links(JiakObject),
     set_links(JiakObject, [ L || L <- Links, L /= RemLink]).
 
-%% @spec remove_link(jiak_object, riak_object:bucket(), jiak_key(), term()) 
+%% @spec remove_link(jiak_object, riak_object:bucket(), riak_object:key(), term()) 
 %%         -> jiak_object()
 %% @equiv remove_link(JiakObject, [Bucket, Key, Tag])
 remove_link(JiakObject, Bucket, Key, Tag) ->

src/jiak_resource.erl

 
 -include_lib("webmachine/include/webmachine.hrl").
 
-%% @type key() = container|riak_object:binary_key()
+%% @type key() = container|schema|riak_object:key()
 
 %% @spec init(proplist()) -> {ok, context()}
 %% @doc Initialize this webmachine resource.  This function will

src/riak_bucketkeys.erl

 handle_call(_,_,State) -> {reply,no_call_support,State}.
 
 handle_cast({OpType,Bucket,Key},State=#state{ops=Ops}) ->
-    BucketFrag = lists:flatten(
-                   io_lib:format("~s-~b", 
-                                 [Bucket, 
-                                  erlang:phash2(Key) rem ?NFRAGS])),
+    BucketFrag = list_to_binary(
+                   lists:flatten(
+                     io_lib:format("~s-~b", 
+                                   [Bucket, 
+                                    erlang:phash2(Key) rem ?NFRAGS]))),
     NewState = ensure_ring(State),
     OpList = case dict:find(BucketFrag, Ops) of
         error ->   [{OpType,Key}];
 
 %% @private 
 all_frags(Bucket) when is_atom(Bucket) ->
-    [atom_to_list(Bucket) ++ "-" ++ X || X <- [integer_to_list(I) || 
-                                               I <- lists:seq(0, ?NFRAGS-1)]].
+    [list_to_binary(F) || F <- [atom_to_list(Bucket) ++ "-" ++ X || X <- [integer_to_list(I) || I <- lists:seq(0, ?NFRAGS-1)]]].
 %% @private
 ensure_ring(State=#state{ring=undefined}) ->
     riak_ring_manager:subscribe(self()),

src/riak_object.erl

 %% @type riak_object().  Opaque container for Riak objects.
 -record(r_object, {
           bucket :: atom(),
-          key :: string()|binary(),
+          key :: binary(),
           contents :: [#r_content{}],
           vclock :: [vclock:vclock()],
           updatemetadata=dict:store(clean, true, dict:new()) :: dict(),
           updatevalue :: term()
          }).
 
-%% @type binary_key()=binary().
-%% @type string_key()=string().
-%% @type key()=binary_key()|string_key().
+-define(MAX_KEY_SIZE, 65536).
+
+%% @type key()=binary().
 %% @type bucket()=atom().
 %% @type value()=term().
 
 
 object_test() ->
     B = buckets_are_atoms,
-    K = "keys are strings",
+    K = <<"keys are binaries">>,
     V = <<"values are anything">>,
     O = riak_object:new(B,K,V),
     B = riak_object:bucket(O),
     O3 = riak_object:syntactic_merge(O,O3,node_does_not_matter_here),
     {O,O3}.    
 
+largekey_test() ->
+    TooLargeKey = <<0:(65537*8)>>,
+    try
+        riak_object:new(a, TooLargeKey, <<>>)
+    catch throw:{error, key_too_large} ->
+            ok
+    end.
+            
+
+
 %% @spec new(Bucket::bucket(), Key::key(), Value::value()) -> riak_object()
 %% @doc Constructor for new riak objects.
-new(B, K, V) when is_atom(B), (is_list(K) orelse is_binary(K)) ->
-    Contents = [#r_content{metadata=dict:new(), value=V}],
-    #r_object{bucket=B, key=K, contents=Contents, vclock=vclock:fresh()}.
+new(B, K, V) when is_atom(B), is_binary(K) ->
+    case size(K) > ?MAX_KEY_SIZE of
+        true ->
+            throw({error,key_too_large});
+        false ->
+            Contents = [#r_content{metadata=dict:new(), value=V}],
+            #r_object{bucket=B,key=K,contents=Contents,vclock=vclock:fresh()}
+    end.
 
 %% @spec equal(riak_object(), riak_object()) -> true | false
 %% @doc Deep (expensive) comparison of Riak objects.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.