Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5bb356bae7
Fetching contributors…

Cannot retrieve contributors at this time

file 94 lines (78 sloc) 3.253 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at

%% http://www.apache.org/licenses/LICENSE-2.0
%%[{ totals, 4239, 44.484, 22.362}].
%%[{ totals, 4951, 36.381, 26.965}].


%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.

%% @doc riak_redis_backend is a Riak storage backend using erldis.


%% Blog post about the initial release :
%% http://cestari.wordpress.com/2009/12/22/a-redis-backend-for-riak/
-module(riak_redis_backend).
-author('Eric Cestari <eric@ohmforce.com').
-export([start/1,stop/1,get/2,put/3,list/1,list_bucket/2,delete/2]).

% @type state() = term().
-record(state, {pid, partition}).

% @spec start(Partition :: integer()) ->
% {ok, state()} | {{error, Reason :: term()}, state()}
start(Partition)->
  {ok, Pid} = erldis_sync_client:connect(),
  P=list_to_binary(integer_to_list(Partition)),
  {ok, #state{pid=Pid, partition = P}}.

% @spec stop(state()) -> ok | {error, Reason :: term()}
stop(_State)->
  ok.

% get(state(), Key :: binary()) ->
% {ok, Val :: binary()} | {error, Reason :: term()}
get(#state{partition=P, pid=Pid}, BK)->
  case erldis:get(Pid, k2l(P,BK)) of
    nil -> {error, notfound};
    Val ->
    case catch binary_to_term(Val) of
      {'EXIT', _}->
        throw({badterm, BK, Val});
      V ->
        {ok, V}
    end
  end.

% put(state(), Key :: binary(), Val :: binary()) ->
% ok | {error, Reason :: term()}
put(#state{partition=P,pid=Pid}, {Bucket, Key}=BK, Value)->
  erldis:sadd(Pid, <<"buckets:",P/binary>>,Bucket),
  case erldis:set(Pid, k2l(P,BK), term_to_binary(Value)) of
    ok ->
      erldis:sadd(Pid, <<P/binary,Bucket/binary>>, Key),
      erldis:sadd(Pid, <<"world:",P/binary>>, term_to_binary(BK)),
      ok;
    _ -> {error, unable_to_put}
  end.

% delete(state(), Key :: binary()) ->
% ok | {error, Reason :: term()}
delete(#state {partition=P, pid=Pid }, {Bucket, Key}=BK) ->
  erldis:srem(Pid, <<"buckets:",P/binary>>,Bucket),
  case erldis:del(Pid, k2l(P,BK)) of
    true ->
      erldis:srem(Pid, <<P/binary,Bucket/binary>>, Key),
      erldis:srem(Pid, <<"world:",P/binary>>, term_to_binary(BK)),
      ok;
    _ ->
      {error, unable_to_delete}
  end.
  
% list(state()) -> [Key :: binary()]
list(#state {partition=P, pid=Pid }) ->
  lists:map(fun binary_to_term/1,
      erldis:smembers(Pid, <<"world:",P/binary>>)).

list_bucket(#state {partition=P, pid=Pid }, '_')->
  erldis:smembers(Pid, <<"buckets:",P/binary>>);
    
list_bucket(#state {partition=P, pid=Pid }, {filter, Bucket, Fun})->
  lists:filter(Fun, erldis:smembers(Pid, <<P/binary,Bucket/binary>>));
list_bucket(#state {partition=P, pid=Pid }, Bucket) ->
  erldis:smembers(Pid, <<P/binary,Bucket/binary>>).

k2l(P,{B, V})->
  <<P/binary,B/binary,V/binary>>.
Something went wrong with that request. Please try again.