/
car_riak.erl
66 lines (52 loc) · 2.17 KB
/
car_riak.erl
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
-module(car_riak).
-compile(export_all).
-define(CB, <<"car">>). % I CAN HAZ RIAK BUKKET?
%%%----------------------------------------------------------------------
%%% Data Doers
%%%----------------------------------------------------------------------
for_each_pointer_index(KeyProcessingFun, OldHash) when
is_function(KeyProcessingFun) andalso is_binary(OldHash) ->
case get_index(<<"points-to_bin">>, OldHash) of
[] -> no_index;
Results -> % we can paralleize the crap out of this:
[KeyProcessingFun(FoundNode) || FoundNode <- Results]
end.
%%%----------------------------------------------------------------------
%%% Object Wrappers
%%%----------------------------------------------------------------------
obj(Ref, Value) ->
riakc_obj:new(?CB, Ref, Value).
obj(Ref, Value, Idxs) ->
riakc_obj:update_metadata(obj(Ref, Value), Idxs).
md(RiakObj) ->
dict:to_list(riakc_obj:get_metadata(RiakObj)).
%%%----------------------------------------------------------------------
%%% Raw Riak Ops Against Pools
%%%----------------------------------------------------------------------
-compile({inline, [get/1, get/2, get_value/1, get_value/2,
put/1, get_index/2, get_index/3]}).
put(RiakObj) ->
riak_pool:put(car, RiakObj).
get(Bucket, Ref) ->
riak_pool:get(car, Bucket, Ref).
get_index(Bucket, Idx, Value) ->
case riak_pool:get_index(car, Bucket, Idx, Value) of
{ok, []} -> []; % shorthand so we don't run an empty comprehension
{ok, FoundOnes} -> [FoundKey || [_Bucket, FoundKey] <- FoundOnes];
_ -> [] % technically, we should never get here.
% non-existing idxs return {ok, []}, not notfound.
end.
%%%----------------------------------------------------------------------
%%% Wrapped Raw Riak Ops Against Pools
%%%----------------------------------------------------------------------
get(Ref) ->
get(?CB, Ref).
get_value(Ref) ->
get_value(?CB, Ref).
get_value(Bucket, Ref) ->
case get(Bucket, Ref) of
{ok, Got} -> riakc_obj:get_value(Got);
{error, notfound} -> throw({notfound, Bucket, Ref})
end.
get_index(Idx, Value) ->
get_index(?CB, Idx, Value).