Skip to content

Instantly share code, notes, and snippets.

@toopay
Created April 1, 2015 18:02
Show Gist options
  • Save toopay/a1f0dd6fcfb4e72a0db5 to your computer and use it in GitHub Desktop.
Save toopay/a1f0dd6fcfb4e72a0db5 to your computer and use it in GitHub Desktop.
Riak injector
%% @doc Prepare map data
prepare_map(MapData) ->
%% The initial map to be expanded
InitialMap = riakc_map:new(),
%% Build tuple elem
Definition = element(2,MapData),
Data = [element(Nth, MapData) || Nth <- lists:seq(3,tuple_size(MapData))],
%% Fill in the data based by definition
update_map_data(Data, Definition, InitialMap).
update_map_data([], [], AccMap) -> AccMap;
update_map_data([CurrentData|DataT],[CurrentDefinition|DefT],InitialMap) ->
{AtomDataKey,DataType} = CurrentDefinition,
DataKey = list_to_binary(atom_to_list(AtomDataKey)),
AccMap = case DataType of
register ->
riakc_map:update({DataKey, DataType},
fun(Val) -> riakc_register:set(CurrentData, Val) end,
InitialMap);
set ->
append_set(CurrentData, DataKey, InitialMap);
counter ->
riakc_map:update({DataKey, DataType},
fun(Val) -> riakc_counter:increment(CurrentData, Val) end,
InitialMap)
end,
update_map_data(DataT,DefT,AccMap).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment