/riak_redis_backend.erl

https://github.com/albsen/riak_redis_backend · Erlang · 113 lines · 49 code · 19 blank · 45 comment · 0 complexity · cafb5488e7205d33dcc2c2adf4f1c18b MD5 · raw file

  1. %% This file is provided to you under the Apache License,
  2. %% Version 2.0 (the "License"); you may not use this file
  3. %% except in compliance with the License. You may obtain
  4. %% a copy of the License at
  5. %% http://www.apache.org/licenses/LICENSE-2.0
  6. %% Unless required by applicable law or agreed to in writing,
  7. %% software distributed under the License is distributed on an
  8. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  9. %% KIND, either express or implied. See the License for the
  10. %% specific language governing permissions and limitations
  11. %% under the License.
  12. %% @doc riak_redis_backend is a Riak storage backend using redis_drv.
  13. -module(riak_redis_backend).
  14. -author('Eric Cestari <eric@ohmforce.com').
  15. -export([start/1,start/2,stop/1,get/2,put/3,list/1,list_bucket/2,delete/2]).
  16. -define(RSEND(V), redis_send(fun()-> V end)).
  17. % @type state() = term().
  18. -record(state, {pid, partition}).
  19. % @spec start(Partition :: integer()) ->
  20. % {ok, state()} | {{error, Reason :: term()}, state()}
  21. start(Partition)->
  22. {ok, Pid} = redis_drv:start_link(),
  23. P=list_to_binary(atom_to_list(node()) ++ integer_to_list(Partition)),
  24. {ok, #state{pid=Pid, partition = P}}.
  25. start(Partition, _Config)->
  26. {ok, Pid} = redis_drv:start_link(),
  27. P=list_to_binary(atom_to_list(node()) ++ integer_to_list(Partition)),
  28. {ok, #state{pid=Pid, partition = P}}.
  29. % @spec stop(state()) -> ok | {error, Reason :: term()}
  30. stop(_State)->
  31. ok.
  32. % get(state(), Key :: binary()) ->
  33. % {ok, Val :: binary()} | {error, Reason :: term()}
  34. get(#state{partition=P, pid=Pid}, BK)->
  35. case redis_drv:get(Pid, k2l(P,BK)) of
  36. nil -> {error, notfound};
  37. Val ->
  38. case catch binary_to_term(Val) of
  39. {'EXIT', _}->
  40. throw({badterm, BK, Val});
  41. V ->
  42. {ok, V}
  43. end
  44. end.
  45. % put(state(), Key :: binary(), Val :: binary()) ->
  46. % ok | {error, Reason :: term()}
  47. put(#state{partition=P, pid=Pid}, {Bucket, Key}=BK, Value)->
  48. %Fun = fun(_C)->
  49. % erldis:set_pipelining(Pid,true),
  50. redis_drv:sadd(Pid, <<"buckets:",P/binary>>,Bucket),
  51. redis_drv:set(Pid, k2l(P,BK), term_to_binary(Value)),
  52. redis_drv:sadd(Pid, <<P/binary,Bucket/binary>>, Key),
  53. redis_drv:sadd(Pid, <<"world:",P/binary>>, term_to_binary(BK)),
  54. % erldis:get_all_results(Pid),
  55. % erldis:set_pipelining(Pid,false),
  56. ok.
  57. %end,
  58. %case erldis:exec(Pid, Fun) of
  59. % [_,_, _, _] ->
  60. % ok;
  61. % _ ->
  62. % {error, unable_to_put}
  63. %end.
  64. % delete(state(), Key :: binary()) ->
  65. % ok | {error, Reason :: term()}
  66. delete(#state {partition=P, pid=Pid}, {Bucket, Key}=BK) ->
  67. %Fun = fun(_C)->
  68. % redis_drv:set_pipelining(Pid,true),
  69. redis_drv:srem(Pid, <<"buckets:",P/binary>>,Bucket),
  70. redis_drv:del(Pid, k2l(P,BK)),
  71. redis_drv:srem(Pid, <<P/binary,Bucket/binary>>, Key),
  72. redis_drv:srem(Pid, <<"world:",P/binary>>, term_to_binary(BK)),
  73. % redis_drv:get_all_results(Pid),
  74. % redis_drv:set_pipelining(Pid,false),
  75. ok.
  76. %end,
  77. %case erldis:exec(Pid, Fun) of
  78. % [_,_, _, _] ->
  79. % ok;
  80. % _ ->
  81. % {error, unable_to_delete}
  82. %end.
  83. % list(state()) -> [Key :: binary()]
  84. list(#state {partition=P, pid=Pid}) ->
  85. lists:map(fun binary_to_term/1,
  86. redis_drv:smembers(Pid, <<"world:",P/binary>>)).
  87. list_bucket(#state{partition=P, pid=Pid}, '_')->
  88. redis_drv:smembers(Pid, <<"buckets:",P/binary>>);
  89. list_bucket(#state{partition=P, pid=Pid}, {filter, Bucket, Fun})->
  90. lists:filter(Fun, redis_drv:smembers(Pid, <<P/binary,Bucket/binary>>));
  91. list_bucket(#state{partition=P, pid=Pid}, Bucket) ->
  92. redis_drv:smembers(Pid, <<P/binary,Bucket/binary>>).
  93. k2l(P,{B, V})->
  94. <<P/binary,B/binary,V/binary>>.