25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-22 17:28:25 +01:00
xmpp.chapril.org-ejabberd/src/ejabberd_riak.erl

568 lines
18 KiB
Erlang
Raw Normal View History

2012-11-13 13:56:27 +01:00
%%%-------------------------------------------------------------------
2017-01-03 15:58:52 +01:00
%%% File : ejabberd_riak.erl
%%% Author : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : Interface for Riak database
2012-01-25 11:02:16 +01:00
%%% Created : 29 Dec 2011 by Alexey Shchepin <alexey@process-one.net>
2017-01-03 15:58:52 +01:00
%%%
2012-01-25 11:02:16 +01:00
%%%
2019-01-08 22:53:27 +01:00
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
2015-10-07 00:06:58 +02:00
%%%
2012-01-25 11:02:16 +01:00
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%%% General Public License for more details.
%%%
2015-02-25 15:19:33 +01:00
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2012-01-25 11:02:16 +01:00
%%%
2012-11-13 13:56:27 +01:00
%%%-------------------------------------------------------------------
2012-01-25 11:02:16 +01:00
-module(ejabberd_riak).
2012-11-13 13:56:27 +01:00
-behaviour(gen_server).
%% API
2016-03-18 17:41:14 +01:00
-export([start_link/5, get_proc/1, make_bucket/1, put/2, put/3,
get/2, get/3, get_by_index/4, delete/1, delete/2,
count_by_index/3, get_by_index_range/5,
get_keys/1, get_keys_by_index/3, is_connected/0,
2012-11-13 13:56:27 +01:00
count/1, delete_by_index/3]).
%% For debugging
-export([get_tables/0]).
%% map/reduce exports
-export([map_key/3]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
2012-01-25 11:02:16 +01:00
-include("logger.hrl").
2012-01-25 11:02:16 +01:00
2012-11-13 13:56:27 +01:00
-record(state, {pid = self() :: pid()}).
-type index() :: {binary(), any()}.
-type index_info() :: [{i, any()} | {'2i', [index()]}].
%% The `record_schema()' is just a tuple:
%% {record_info(fields, some_record), #some_record{}}
-type record_schema() :: {[atom()], tuple()}.
2012-11-13 13:56:27 +01:00
%% The `index_info()' is used in put/delete functions:
%% `i' defines a primary index, `` '2i' '' defines secondary indexes.
%% There must be only one primary index. If `i' is not specified,
%% the first element of the record is assumed as a primary index,
%% i.e. `i' = element(2, Record).
2017-02-18 07:36:27 +01:00
-export_type([index_info/0]).
2012-11-13 13:56:27 +01:00
%%%===================================================================
2012-01-25 11:02:16 +01:00
%%% API
2012-11-13 13:56:27 +01:00
%%%===================================================================
%% @private
2016-03-18 17:41:14 +01:00
start_link(Num, Server, Port, _StartInterval, Options) ->
gen_server:start_link({local, get_proc(Num)}, ?MODULE, [Server, Port, Options], []).
2014-07-09 14:40:18 +02:00
%% @private
is_connected() ->
2016-03-22 17:32:30 +01:00
lists:all(
fun({_Id, Pid, _Type, _Modules}) when is_pid(Pid) ->
2016-03-22 17:58:59 +01:00
case catch riakc_pb_socket:is_connected(get_riak_pid(Pid)) of
2016-03-22 17:32:30 +01:00
true -> true;
_ -> false
2016-03-22 17:58:59 +01:00
end;
(_) ->
false
2016-03-22 17:32:30 +01:00
end, supervisor:which_children(ejabberd_riak_sup)).
2014-07-09 14:40:18 +02:00
%% @private
get_proc(I) ->
misc:binary_to_atom(
2014-07-09 14:40:18 +02:00
iolist_to_binary(
[atom_to_list(?MODULE), $_, integer_to_list(I)])).
2012-11-13 13:56:27 +01:00
-spec make_bucket(atom()) -> binary().
%% @doc Makes a bucket from a table name
%% @private
make_bucket(Table) ->
erlang:atom_to_binary(Table, utf8).
-spec put(tuple(), record_schema()) -> ok | {error, any()}.
2012-11-13 13:56:27 +01:00
%% @equiv put(Record, [])
put(Record, RecFields) ->
?MODULE:put(Record, RecFields, []).
2012-11-13 13:56:27 +01:00
-spec put(tuple(), record_schema(), index_info()) -> ok | {error, any()}.
2012-11-13 13:56:27 +01:00
%% @doc Stores a record `Rec' with indexes described in ``IndexInfo''
put(Rec, RecSchema, IndexInfo) ->
2012-11-13 13:56:27 +01:00
Key = encode_key(proplists:get_value(i, IndexInfo, element(2, Rec))),
SecIdxs = [encode_index_key(K, V) ||
{K, V} <- proplists:get_value('2i', IndexInfo, [])],
Table = element(1, Rec),
Value = encode_record(Rec, RecSchema),
2012-11-13 13:56:27 +01:00
case put_raw(Table, Key, Value, SecIdxs) of
ok ->
ok;
2014-07-09 16:02:13 +02:00
{error, _} = Error ->
2012-11-13 13:56:27 +01:00
log_error(Error, put, [{record, Rec},
{index_info, IndexInfo}]),
Error
end.
put_raw(Table, Key, Value, Indexes) ->
Bucket = make_bucket(Table),
Obj = riakc_obj:new(Bucket, Key, Value, "application/x-erlang-term"),
Obj1 = if Indexes /= [] ->
MetaData = dict:store(<<"index">>, Indexes, dict:new()),
riakc_obj:update_metadata(Obj, MetaData);
true ->
Obj
end,
2014-07-09 14:40:18 +02:00
catch riakc_pb_socket:put(get_random_pid(), Obj1).
2012-11-13 13:56:27 +01:00
get_object_raw(Table, Key) ->
Bucket = make_bucket(Table),
2014-07-09 14:40:18 +02:00
catch riakc_pb_socket:get(get_random_pid(), Bucket, Key).
2012-01-25 11:02:16 +01:00
-spec get(atom(), record_schema()) -> {ok, [any()]} | {error, any()}.
2012-11-13 13:56:27 +01:00
%% @doc Returns all objects from table `Table'
get(Table, RecSchema) ->
2012-11-13 13:56:27 +01:00
Bucket = make_bucket(Table),
2014-07-09 14:40:18 +02:00
case catch riakc_pb_socket:mapred(
get_random_pid(),
Bucket,
[{map, {modfun, riak_kv_mapreduce, map_object_value},
none, true}]) of
2012-11-13 13:56:27 +01:00
{ok, [{_, Objs}]} ->
{ok, lists:flatmap(
fun(Obj) ->
case catch decode_record(Obj, RecSchema) of
2012-11-13 13:56:27 +01:00
{'EXIT', _} ->
Error = {error, make_invalid_object(Obj)},
log_error(Error, get,
[{table, Table}]),
[];
Term ->
[Term]
end
end, Objs)};
2014-07-09 16:02:13 +02:00
{ok, []} ->
{ok, []};
2012-11-13 13:56:27 +01:00
{error, notfound} ->
{ok, []};
2014-07-09 14:40:18 +02:00
{error, _} = Error ->
2012-11-13 13:56:27 +01:00
Error
end.
-spec get(atom(), record_schema(), any()) -> {ok, any()} | {error, any()}.
2012-11-13 13:56:27 +01:00
%% @doc Reads record by `Key' from table `Table'
get(Table, RecSchema, Key) ->
2012-11-13 13:56:27 +01:00
case get_raw(Table, encode_key(Key)) of
{ok, Val} ->
case catch decode_record(Val, RecSchema) of
2012-11-13 13:56:27 +01:00
{'EXIT', _} ->
Error = {error, make_invalid_object(Val)},
log_error(Error, get, [{table, Table}, {key, Key}]),
{error, notfound};
Term ->
{ok, Term}
end;
2014-07-09 16:02:13 +02:00
{error, _} = Error ->
2012-11-13 13:56:27 +01:00
log_error(Error, get, [{table, Table},
{key, Key}]),
Error
end.
-spec get_by_index(atom(), record_schema(), binary(), any()) ->
{ok, [any()]} | {error, any()}.
2019-06-14 11:33:26 +02:00
%% @doc Reads records by `Index' and value `Key' from `Table'
get_by_index(Table, RecSchema, Index, Key) ->
2012-11-13 13:56:27 +01:00
{NewIndex, NewKey} = encode_index_key(Index, Key),
case get_by_index_raw(Table, NewIndex, NewKey) of
{ok, Vals} ->
{ok, lists:flatmap(
fun(Val) ->
case catch decode_record(Val, RecSchema) of
2012-11-13 13:56:27 +01:00
{'EXIT', _} ->
Error = {error, make_invalid_object(Val)},
log_error(Error, get_by_index,
[{table, Table},
{index, Index},
{key, Key}]),
[];
Term ->
[Term]
end
end, Vals)};
{error, notfound} ->
{ok, []};
2014-07-09 16:02:13 +02:00
{error, _} = Error ->
2012-11-13 13:56:27 +01:00
log_error(Error, get_by_index,
[{table, Table},
{index, Index},
{key, Key}]),
Error
end.
-spec get_by_index_range(atom(), record_schema(), binary(), any(), any()) ->
2012-11-13 13:56:27 +01:00
{ok, [any()]} | {error, any()}.
%% @doc Reads records by `Index' in the range `FromKey'..`ToKey' from `Table'
get_by_index_range(Table, RecSchema, Index, FromKey, ToKey) ->
2012-11-13 13:56:27 +01:00
{NewIndex, NewFromKey} = encode_index_key(Index, FromKey),
{NewIndex, NewToKey} = encode_index_key(Index, ToKey),
case get_by_index_range_raw(Table, NewIndex, NewFromKey, NewToKey) of
{ok, Vals} ->
{ok, lists:flatmap(
fun(Val) ->
case catch decode_record(Val, RecSchema) of
2012-11-13 13:56:27 +01:00
{'EXIT', _} ->
Error = {error, make_invalid_object(Val)},
log_error(Error, get_by_index_range,
[{table, Table},
{index, Index},
{start_key, FromKey},
{end_key, ToKey}]),
[];
Term ->
[Term]
end
end, Vals)};
{error, notfound} ->
{ok, []};
2014-07-09 16:02:13 +02:00
{error, _} = Error ->
2012-11-13 13:56:27 +01:00
log_error(Error, get_by_index_range,
[{table, Table}, {index, Index},
{start_key, FromKey}, {end_key, ToKey}]),
Error
end.
get_raw(Table, Key) ->
case get_object_raw(Table, Key) of
2012-01-25 11:02:16 +01:00
{ok, Obj} ->
{ok, riakc_obj:get_value(Obj)};
2014-07-09 16:02:13 +02:00
{error, _} = Error ->
2012-01-25 11:02:16 +01:00
Error
end.
2012-11-13 13:56:27 +01:00
-spec get_keys(atom()) -> {ok, [any()]} | {error, any()}.
%% @doc Returns a list of index values
get_keys(Table) ->
Bucket = make_bucket(Table),
2014-07-09 14:40:18 +02:00
case catch riakc_pb_socket:mapred(
get_random_pid(),
Bucket,
[{map, {modfun, ?MODULE, map_key}, none, true}]) of
2012-11-13 13:56:27 +01:00
{ok, [{_, Keys}]} ->
{ok, Keys};
2014-07-09 16:02:13 +02:00
{ok, []} ->
{ok, []};
2014-07-09 14:40:18 +02:00
{error, _} = Error ->
2012-11-13 13:56:27 +01:00
log_error(Error, get_keys, [{table, Table}]),
Error
end.
-spec get_keys_by_index(atom(), binary(),
any()) -> {ok, [any()]} | {error, any()}.
%% @doc Returns a list of primary keys of objects indexed by `Key'.
get_keys_by_index(Table, Index, Key) ->
{NewIndex, NewKey} = encode_index_key(Index, Key),
Bucket = make_bucket(Table),
2014-07-09 14:40:18 +02:00
case catch riakc_pb_socket:mapred(
get_random_pid(),
{index, Bucket, NewIndex, NewKey},
[{map, {modfun, ?MODULE, map_key}, none, true}]) of
2012-11-13 13:56:27 +01:00
{ok, [{_, Keys}]} ->
{ok, Keys};
2014-07-09 16:02:13 +02:00
{ok, []} ->
{ok, []};
2014-07-09 14:40:18 +02:00
{error, _} = Error ->
2012-11-13 13:56:27 +01:00
log_error(Error, get_keys_by_index, [{table, Table},
{index, Index},
{key, Key}]),
Error
end.
%% @hidden
get_tables() ->
2014-07-09 14:40:18 +02:00
catch riakc_pb_socket:list_buckets(get_random_pid()).
2012-11-13 13:56:27 +01:00
get_by_index_raw(Table, Index, Key) ->
Bucket = make_bucket(Table),
2012-01-25 11:02:16 +01:00
case riakc_pb_socket:mapred(
2014-07-09 14:40:18 +02:00
get_random_pid(),
2012-01-25 11:02:16 +01:00
{index, Bucket, Index, Key},
2012-11-13 13:56:27 +01:00
[{map, {modfun, riak_kv_mapreduce, map_object_value},
none, true}]) of
2012-01-25 11:02:16 +01:00
{ok, [{_, Objs}]} ->
{ok, Objs};
2014-07-09 16:02:13 +02:00
{ok, []} ->
{ok, []};
2014-07-09 14:40:18 +02:00
{error, _} = Error ->
2012-01-25 11:02:16 +01:00
Error
end.
2012-11-13 13:56:27 +01:00
get_by_index_range_raw(Table, Index, FromKey, ToKey) ->
Bucket = make_bucket(Table),
2014-07-09 14:40:18 +02:00
case catch riakc_pb_socket:mapred(
get_random_pid(),
{index, Bucket, Index, FromKey, ToKey},
[{map, {modfun, riak_kv_mapreduce, map_object_value},
none, true}]) of
2012-01-25 11:02:16 +01:00
{ok, [{_, Objs}]} ->
{ok, Objs};
2014-07-09 16:02:13 +02:00
{ok, []} ->
{ok, []};
2014-07-09 14:40:18 +02:00
{error, _} = Error ->
2012-01-25 11:02:16 +01:00
Error
end.
2012-11-13 13:56:27 +01:00
-spec count(atom()) -> {ok, non_neg_integer()} | {error, any()}.
%% @doc Returns the number of objects in the `Table'
count(Table) ->
Bucket = make_bucket(Table),
2014-07-09 14:40:18 +02:00
case catch riakc_pb_socket:mapred(
get_random_pid(),
Bucket,
[{reduce, {modfun, riak_kv_mapreduce, reduce_count_inputs},
none, true}]) of
2012-11-13 13:56:27 +01:00
{ok, [{_, [Cnt]}]} ->
{ok, Cnt};
2014-07-09 14:40:18 +02:00
{error, _} = Error ->
2012-11-13 13:56:27 +01:00
log_error(Error, count, [{table, Table}]),
2012-01-25 11:02:16 +01:00
Error
end.
2012-11-13 13:56:27 +01:00
-spec count_by_index(atom(), binary(), any()) ->
{ok, non_neg_integer()} | {error, any()}.
%% @doc Returns the number of objects in the `Table' by index
count_by_index(Tab, Index, Key) ->
{NewIndex, NewKey} = encode_index_key(Index, Key),
case count_by_index_raw(Tab, NewIndex, NewKey) of
{ok, Cnt} ->
{ok, Cnt};
{error, notfound} ->
{ok, 0};
2014-07-09 16:02:13 +02:00
{error, _} = Error ->
2012-11-13 13:56:27 +01:00
log_error(Error, count_by_index,
[{table, Tab},
{index, Index},
{key, Key}]),
Error
end.
count_by_index_raw(Table, Index, Key) ->
Bucket = make_bucket(Table),
2014-07-09 14:40:18 +02:00
case catch riakc_pb_socket:mapred(
get_random_pid(),
{index, Bucket, Index, Key},
[{reduce, {modfun, riak_kv_mapreduce, reduce_count_inputs},
none, true}]) of
2012-01-25 11:02:16 +01:00
{ok, [{_, [Cnt]}]} ->
{ok, Cnt};
2014-07-09 14:40:18 +02:00
{error, _} = Error ->
2012-01-25 11:02:16 +01:00
Error
end.
2012-11-13 13:56:27 +01:00
-spec delete(tuple() | atom()) -> ok | {error, any()}.
%% @doc Same as delete(T, []) when T is record.
%% Or deletes all elements from table if T is atom.
delete(Rec) when is_tuple(Rec) ->
delete(Rec, []);
delete(Table) when is_atom(Table) ->
try
{ok, Keys} = ?MODULE:get_keys(Table),
lists:foreach(
fun(K) ->
ok = delete(Table, K)
end, Keys)
catch _:{badmatch, Err} ->
Err
end.
-spec delete(tuple() | atom(), index_info() | any()) -> ok | {error, any()}.
%% @doc Delete an object
delete(Rec, Opts) when is_tuple(Rec) ->
Table = element(1, Rec),
Key = proplists:get_value(i, Opts, element(2, Rec)),
delete(Table, Key);
delete(Table, Key) when is_atom(Table) ->
case delete_raw(Table, encode_key(Key)) of
ok ->
ok;
Err ->
log_error(Err, delete, [{table, Table}, {key, Key}]),
Err
end.
delete_raw(Table, Key) ->
Bucket = make_bucket(Table),
2014-07-09 14:40:18 +02:00
catch riakc_pb_socket:delete(get_random_pid(), Bucket, Key).
2012-01-25 11:02:16 +01:00
2012-11-13 13:56:27 +01:00
-spec delete_by_index(atom(), binary(), any()) -> ok | {error, any()}.
%% @doc Deletes objects by index
delete_by_index(Table, Index, Key) ->
try
{ok, Keys} = get_keys_by_index(Table, Index, Key),
lists:foreach(
fun(K) ->
ok = delete(Table, K)
end, Keys)
catch _:{badmatch, Err} ->
Err
end.
%%%===================================================================
%%% map/reduce functions
%%%===================================================================
%% @private
map_key(Obj, _, _) ->
[case riak_object:key(Obj) of
<<"b_", B/binary>> ->
B;
<<"i_", B/binary>> ->
(binary_to_integer(B));
2012-11-13 13:56:27 +01:00
B ->
erlang:binary_to_term(B)
end].
%%%===================================================================
%%% gen_server API
%%%===================================================================
%% @private
2016-03-18 17:41:14 +01:00
init([Server, Port, Options]) ->
case riakc_pb_socket:start(Server, Port, Options) of
2012-11-13 13:56:27 +01:00
{ok, Pid} ->
erlang:monitor(process, Pid),
{ok, #state{pid = Pid}};
Err ->
{stop, Err}
end.
%% @private
2014-07-09 14:40:18 +02:00
handle_call(get_pid, _From, #state{pid = Pid} = State) ->
{reply, {ok, Pid}, State};
2012-11-13 13:56:27 +01:00
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
%% @private
handle_cast(_Msg, State) ->
{noreply, State}.
%% @private
handle_info({'DOWN', _MonitorRef, _Type, _Object, _Info}, State) ->
{stop, normal, State};
handle_info(_Info, State) ->
2019-06-24 19:32:34 +02:00
?ERROR_MSG("Unexpected info: ~p", [_Info]),
2012-11-13 13:56:27 +01:00
{noreply, State}.
%% @private
2014-07-09 14:40:18 +02:00
terminate(_Reason, _State) ->
2012-11-13 13:56:27 +01:00
ok.
%% @private
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
encode_index_key(Idx, Key) when is_integer(Key) ->
{<<Idx/binary, "_int">>, Key};
encode_index_key(Idx, Key) ->
{<<Idx/binary, "_bin">>, encode_key(Key)}.
encode_key(Bin) when is_binary(Bin) ->
<<"b_", Bin/binary>>;
encode_key(Int) when is_integer(Int) ->
<<"i_", ((integer_to_binary(Int)))/binary>>;
2012-11-13 13:56:27 +01:00
encode_key(Term) ->
erlang:term_to_binary(Term).
log_error({error, notfound}, _, _) ->
ok;
log_error({error, Why} = Err, Function, Opts) ->
Txt = lists:map(
fun({table, Table}) ->
io_lib:fwrite("** Table: ~p~n", [Table]);
({key, Key}) ->
io_lib:fwrite("** Key: ~p~n", [Key]);
({index, Index}) ->
io_lib:fwrite("** Index = ~p~n", [Index]);
({start_key, Key}) ->
io_lib:fwrite("** Start Key: ~p~n", [Key]);
({end_key, Key}) ->
io_lib:fwrite("** End Key: ~p~n", [Key]);
({record, Rec}) ->
io_lib:fwrite("** Record = ~p~n", [Rec]);
({index_info, IdxInfo}) ->
io_lib:fwrite("** Index info = ~p~n", [IdxInfo]);
(_) ->
""
end, Opts),
ErrTxt = if is_binary(Why) ->
io_lib:fwrite("** Error: ~s", [Why]);
true ->
io_lib:fwrite("** Error: ~p", [Err])
end,
2019-06-24 19:32:34 +02:00
?ERROR_MSG("Database error:~n** Function: ~p~n~s~s",
2012-11-13 13:56:27 +01:00
[Function, Txt, ErrTxt]);
log_error(_, _, _) ->
ok.
make_invalid_object(Val) ->
(str:format("Invalid object: ~p", [Val])).
2014-07-09 14:40:18 +02:00
get_random_pid() ->
2019-06-14 11:33:26 +02:00
case ejabberd_riak_sup:start() of
ok ->
PoolPid = ejabberd_riak_sup:get_random_pid(),
get_riak_pid(PoolPid);
{error, _} = Err ->
Err
end.
2016-03-22 17:58:59 +01:00
get_riak_pid(PoolPid) ->
2014-07-09 14:40:18 +02:00
case catch gen_server:call(PoolPid, get_pid) of
{ok, Pid} ->
Pid;
{'EXIT', {timeout, _}} ->
throw({error, timeout});
{'EXIT', Err} ->
throw({error, Err})
end.
encode_record(Rec, {Fields, DefRec}) ->
term_to_binary(encode_record(Rec, Fields, DefRec, 2)).
encode_record(Rec, [FieldName|Fields], DefRec, Pos) ->
Value = element(Pos, Rec),
DefValue = element(Pos, DefRec),
if Value == DefValue ->
encode_record(Rec, Fields, DefRec, Pos+1);
true ->
[{FieldName, Value}|encode_record(Rec, Fields, DefRec, Pos+1)]
end;
encode_record(_, [], _, _) ->
[].
decode_record(Bin, {Fields, DefRec}) ->
decode_record(binary_to_term(Bin), Fields, DefRec, 2).
decode_record(KeyVals, [FieldName|Fields], Rec, Pos) ->
case lists:keyfind(FieldName, 1, KeyVals) of
{_, Value} ->
NewRec = setelement(Pos, Rec, Value),
decode_record(KeyVals, Fields, NewRec, Pos+1);
false ->
decode_record(KeyVals, Fields, Rec, Pos+1)
end;
decode_record(_, [], Rec, _) ->
Rec.