Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 153 lines (117 sloc) 3.865 kb
2d4ed9b Kresten Krab Thorup Initial version of riak_column
authored
1 -module(mock_kv_store, [ClientID, ContentTable, MetaTable, MainBucket, MapRedDelay]).
2
d69daea Kresten Krab Thorup add mock_kv_store:put/2
authored
3 -export([get/2, put/1, put/2, put/5, get_bucket/1, set_bucket/2, mapred_bucket_stream/3, get_client_id/0, content_table/0]).
2d4ed9b Kresten Krab Thorup Initial version of riak_column
authored
4
5 -export([init/1, stop/0]).
6
7 -include_lib("eunit/include/eunit.hrl").
8
9 -ifdef(TEST).
10 -export([assertEquals/1,get_contents/0]).
11 -endif.
12
13 init(Contents) ->
14 ets:insert(MetaTable, {{bucket_props, MainBucket}, []}),
15
16 lists:foreach(fun(Obj) ->
17 TabKey = {MainBucket, riak_object:key(Obj)},
18 ets:insert(ContentTable, {TabKey, Obj})
19 end,
20 Contents),
21 ok.
22
23 content_table() ->
24 ContentTable.
25
26 get_client_id() ->
27 ClientID.
28
29 stop() ->
30 ets:delete(ContentTable),
31 ets:delete(MetaTable),
32 ok.
33
34 get_bucket(Bucket) ->
35 ets:lookup_element(MetaTable, {bucket_props, Bucket}, 2).
36
37 set_bucket(Bucket, NewProps) ->
38 OldProps = get_bucket(Bucket),
39 SumProps = lists:ukeymerge(1,
40 lists:ukeysort(1, NewProps),
41 lists:ukeysort(1, OldProps)),
42 ets:insert(MetaTable, {{bucket_props, Bucket}, SumProps}),
43 ok.
44
45 get(Bucket, Key) ->
46 case ets:lookup(ContentTable, {Bucket,Key}) of
47 [] -> {error, notfound};
48 [{_,Obj}] -> {ok, Obj}
49 end.
50
51 put(Obj) ->
d69daea Kresten Krab Thorup add mock_kv_store:put/2
authored
52 THIS:put (Obj, [{w,1},{dw,1},{timeout,1}]).
2d4ed9b Kresten Krab Thorup Initial version of riak_column
authored
53
d69daea Kresten Krab Thorup add mock_kv_store:put/2
authored
54 put(Obj,W,DW,TimeOut,Options) ->
55 THIS:put(Obj,[{w,W},{dw,DW},{timeout,TimeOut}|Options]).
56
57 put(Obj,Options) ->
2d4ed9b Kresten Krab Thorup Initial version of riak_column
authored
58 Bucket = riak_object:bucket(Obj),
59 Key = riak_object:key(Obj),
60
61 % error_logger:info_msg("putting ~p", [Obj]),
62
63 Updated = case is_updated(Obj) of
64 true -> riak_object:increment_vclock(riak_object:apply_updates(Obj), ClientID);
65 false -> Obj
66 end,
67
68 case ets:lookup(ContentTable, {Bucket,Key}) of
69 [] ->
70 Merged = Updated;
71 [{_,OrigObj}] ->
72 Merged = riak_object:reconcile([OrigObj,Updated], true)
73 end,
74
75 % error_logger:info_msg("storing ~p", [{{Bucket,Key}, Merged}]),
76
77 ets:insert(ContentTable, {{Bucket,Key}, Merged}),
78
79 case proplists:get_bool(returnbody, Options) of
80 true ->
81 {ok, Merged};
82 false ->
83 ok
84 end.
85
86
87
88 mapred_bucket_stream(Bucket, Query, ClientPid) ->
89 Ref = make_ref(),
90 spawn_link(fun() -> do_mapred_bucket_stream(Bucket, Query, ClientPid, MapRedDelay, Ref) end),
91 {ok, Ref}.
92
93 do_mapred_bucket_stream(Bucket, Query, ClientPid, MapRedDelay, Ref) ->
94 [{map, F, none, true}] = Query,
95 ets:foldl(fun({{ObjBucket, _}, Obj}, _) ->
96 if ObjBucket =:= Bucket ->
97 timer:sleep(MapRedDelay),
98 MapResult = xapply(F, [Obj, dummyKeyData, dummyAction]),
99 lists:foreach(fun(Res) ->
100 ClientPid ! {flow_results, dummyPhaseID, Ref, Res}
101 end,
102 MapResult);
103 true ->
104 ok
105 end
106 end,
107 dummy,
108 ContentTable),
109 ClientPid ! {flow_results, Ref, done}.
110
111 xapply({modfun, Module, Function}, Args) ->
112 apply(Module, Function, Args);
113 xapply({'fun', Fun}, Args) ->
114 apply(Fun, Args).
115
116 -ifdef(TEST).
117
118 assertEquals(OtherPID) ->
119 HisObjects = OtherPID:get_contents(),
120 MyObjects = get_contents(),
121
122 length(HisObjects) == length(MyObjects).
123
124
125 get_contents() ->
126 mapred_bucket_stream(MainBucket,
127 [{map, {'fun', fun(Obj,_,_) -> [Obj] end}, none, true}],
128 self()),
129 get_flow_contents([]).
130
131 get_flow_contents(Result) ->
132 receive
133 {flow_results, _, _, Obj} ->
134 get_flow_contents([Obj | Result]);
135 {flow_results, _, done} ->
136 Result
137 end.
138
139
140 -endif.
141
142 is_updated(O) ->
143 M = riak_object:get_update_metadata(O),
144 V = riak_object:get_update_value(O),
145 case dict:find(clean, M) of
146 error -> true;
147 {ok,_} ->
148 case V of
149 undefined -> false;
150 _ -> true
151 end
152 end.
Something went wrong with that request. Please try again.