-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorer.erl
69 lines (61 loc) · 2.02 KB
/
colorer.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
67
68
69
-module(colorer).
-export([load/1]).
-export([colormap/3]).
-export([coloreduce/2]).
-export([get_color/1]).
%% Determine a cat color from its name
get_color(Name) ->
NameTokens = string:tokens(Name, " "),
case lists:member("White", NameTokens) of
true ->
"White";
false ->
case lists:member("Black", NameTokens) of
true ->
"Black";
false ->
"Mixed"
end
end.
%% map phase
colormap(Value, _Keydata, _Arg) ->
ObjKey = riak_object:key(Value),
KeyStr = binary_to_list(ObjKey),
Color = get_color(KeyStr),
error_logger:info_msg("colorer mapped key ~p to ~p", [KeyStr, Color]),
[{Color, 1}].
%% map reduce
coloreduce(List, _Arg) ->
error_logger:info_msg("colorer reduce list ~p", [List]),
ListOfDicts = [dict:from_list([I]) || I <- List],
Merged =
lists:foldl(fun(Item, Acc) -> dict:merge(fun(_, X, Y) -> X + Y end, Item, Acc) end,
dict:new(),
ListOfDicts),
% convert dict to list
dict:to_list(Merged).
%% End of list
insert(_Client, []) ->
ok;
%% Put a cat from head of list to the Riak
insert(Client, [Head | Tail]) ->
% All cats are adorable, so it goes to the Riak value as a JSON
Body = list_to_binary(mochijson2:encode(#{<<"adorable">> => true})),
% Riak key is a cat name
Key = Head,
RawObj = riakc_obj:new(<<"cats">>, Key, Body),
Obj = riakc_obj:update_content_type(RawObj, "application/json"),
case riakc_pb_socket:put(Client, Obj) of
ok ->
io:format("Inserted key ~p~n", [Head]);
{error, Error} ->
io:format("Error inserting key ~p", [Error])
end,
insert(Client, Tail).
%% Load cats from text file
load(Filename) ->
{ok, Client} = riakc_pb_socket:start_link("127.0.0.1", 10017),
{ok, Data} = file:read_file(Filename),
Lines = re:split(Data, "\r?\n", [{return, binary}, trim]),
insert(Client, Lines).
%% vim: et tabstop=2 shiftwidth=2