|
| 1 | +%%%---------------------------------------------------------------------- |
| 2 | +%%% File : ejabberd_db_serialize.erl |
| 3 | +%%% Author : Pawel Chmielowski <[email protected]> |
| 4 | +%%% Purpose : DB data de/serialization |
| 5 | +%%% Created : 20 Nov 2025 by Pawel Chmielowski <[email protected]> |
| 6 | +%%% |
| 7 | +%%% |
| 8 | +%%% ejabberd, Copyright (C) 2002-2025 ProcessOne |
| 9 | +%%% |
| 10 | +%%% This program is free software; you can redistribute it and/or |
| 11 | +%%% modify it under the terms of the GNU General Public License as |
| 12 | +%%% published by the Free Software Foundation; either version 2 of the |
| 13 | +%%% License, or (at your option) any later version. |
| 14 | +%%% |
| 15 | +%%% This program is distributed in the hope that it will be useful, |
| 16 | +%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | +%%% General Public License for more details. |
| 19 | +%%% |
| 20 | +%%% You should have received a copy of the GNU General Public License along |
| 21 | +%%% with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | +%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | +%%% |
| 24 | +%%%---------------------------------------------------------------------- |
| 25 | +-module(ejabberd_db_serialize). |
| 26 | + |
| 27 | +%% API |
| 28 | +-export([serialize_module/3, deserialize_module/3]). |
| 29 | + |
| 30 | +-callback serialize(binary(), non_neg_integer(), undefined | term()) -> |
| 31 | + {ok, [term()], term()} | {error, iolist()}. |
| 32 | +-callback deserialize_start(binary()) -> ok | {error, iolist()}. |
| 33 | +-callback deserialize(binary(), [term()]) -> ok | {error, iolist()}. |
| 34 | + |
| 35 | +-define(BATCH_SIZE, 1000). |
| 36 | + |
| 37 | +serialize_module(Host, ejabberd_auth, Dir) -> |
| 38 | + DbMod2 = |
| 39 | + lists:foldl( |
| 40 | + fun(Method, undefined) -> |
| 41 | + DbMod = ejabberd:module_name([<<"auth">>, Method]), |
| 42 | + case erlang:function_exported(DbMod, serialize, 3) of |
| 43 | + true -> DbMod; |
| 44 | + _ -> undefined |
| 45 | + end; |
| 46 | + (_, Mod) -> Mod |
| 47 | + end, undefined, ejabberd_option:auth_method(Host)), |
| 48 | + case DbMod2 of |
| 49 | + undefined -> |
| 50 | + {error, iolist_to_binary(io_lib:format("No auth module offer serialization", |
| 51 | + []))}; |
| 52 | + _ -> |
| 53 | + serialize_db_module(Host, ejabberd_auth, DbMod2, Dir) |
| 54 | + end; |
| 55 | +serialize_module(Host, Mod, Dir) -> |
| 56 | + case gen_mod:is_loaded(Host, Mod) of |
| 57 | + true -> |
| 58 | + serialize_db_module(Host, Mod, gen_mod:db_mod(Host, Mod), Dir); |
| 59 | + _ -> |
| 60 | + {error, iolist_to_binary(io_lib:format("Module ~s is not loaded on host ~s", |
| 61 | + [Mod, Host]))} |
| 62 | + end. |
| 63 | + |
| 64 | +serialize_db_module(Host, Mod, DbMod, Dir) -> |
| 65 | + case erlang:function_exported(DbMod, serialize, 3) of |
| 66 | + true -> |
| 67 | + FN = <<(atom_to_binary(Mod, latin1))/binary, ".dbser">>, |
| 68 | + Path = filename:join([Dir, FN]), |
| 69 | + write_data(Path, fun(Next) -> DbMod:serialize(Host, ?BATCH_SIZE, Next) end); |
| 70 | + _ -> |
| 71 | + {error, iolist_to_binary(io_lib:format("Module ~s doesn't offer serialization", |
| 72 | + [Mod]))} |
| 73 | + end. |
| 74 | + |
| 75 | +deserialize_module(Host, ejabberd_auth, Dir) -> |
| 76 | + DbMod2 = |
| 77 | + lists:foldl( |
| 78 | + fun(Method, undefined) -> |
| 79 | + DbMod = ejabberd:module_name([<<"auth">>, Method]), |
| 80 | + case erlang:function_exported(DbMod, deserialize, 2) andalso |
| 81 | + erlang:function_exported(DbMod, deserialize_start, 1) of |
| 82 | + true -> DbMod; |
| 83 | + _ -> undefined |
| 84 | + end; |
| 85 | + (_, Mod) -> Mod |
| 86 | + end, undefined, ejabberd_option:auth_method(Host)), |
| 87 | + case DbMod2 of |
| 88 | + undefined -> |
| 89 | + {error, iolist_to_binary(io_lib:format("No auth module offer serialization", |
| 90 | + []))}; |
| 91 | + _ -> |
| 92 | + deserialize_db_module(Host, ejabberd_auth, DbMod2, Dir) |
| 93 | + end; |
| 94 | +deserialize_module(Host, Mod, Dir) -> |
| 95 | + case gen_mod:is_loaded(Host, Mod) of |
| 96 | + true -> |
| 97 | + deserialize_db_module(Host, Mod, gen_mod:db_mod(Host, Mod), Dir); |
| 98 | + _ -> |
| 99 | + {error, iolist_to_binary(io_lib:format("Module ~s is not loaded on host ~s", |
| 100 | + [Mod, Host]))} |
| 101 | + end. |
| 102 | + |
| 103 | +deserialize_db_module(Host, Mod, DbMod, Dir) -> |
| 104 | + case erlang:function_exported(DbMod, deserialize, 2) andalso |
| 105 | + erlang:function_exported(DbMod, deserialize_start, 1) |
| 106 | + of |
| 107 | + true -> |
| 108 | + FN = <<(atom_to_binary(Mod, latin1))/binary, ".dbser">>, |
| 109 | + Path = filename:join([Dir, FN]), |
| 110 | + DbMod:deserialize_start(Host), |
| 111 | + read_data(Path, fun(Chunk) -> DbMod:deserialize(Host, Chunk) end); |
| 112 | + _ -> |
| 113 | + {error, iolist_to_binary(io_lib:format("Module ~s doesn't offer serialization", |
| 114 | + [Mod]))} |
| 115 | + end. |
| 116 | + |
| 117 | +write_loop(Path, Io, Producer, Key) -> |
| 118 | + case Producer(Key) of |
| 119 | + {ok, [], _} -> |
| 120 | + ok; |
| 121 | + {ok, Data, NextKey} -> |
| 122 | + Ser = term_to_binary(Data), |
| 123 | + SerLen = byte_size(Ser), |
| 124 | + maybe |
| 125 | + ok ?= file:write(Io, <<"dbdb", SerLen:32/unsigned-little-integer>>), |
| 126 | + ok ?= file:write(Io, Ser), |
| 127 | + write_loop(Path, Io, Producer, NextKey) |
| 128 | + else |
| 129 | + {error, Msg} -> |
| 130 | + {error, io_lib:format("Error when writing to file ~s: ~p", |
| 131 | + [Path, Msg])} |
| 132 | + end; |
| 133 | + Error -> Error |
| 134 | + end. |
| 135 | + |
| 136 | +write_data(Path, Producer) -> |
| 137 | + case file:open(Path, [write, raw, binary]) of |
| 138 | + {ok, IO} -> |
| 139 | + case write_loop(Path, IO, Producer, undefined) of |
| 140 | + ok -> |
| 141 | + file:close(IO), |
| 142 | + ok; |
| 143 | + {error, Msg} -> |
| 144 | + file:close(IO), |
| 145 | + file:delete(Path), |
| 146 | + {error, iolist_to_binary(Msg)} |
| 147 | + end; |
| 148 | + {error, Msg} -> |
| 149 | + {error, iolist_to_binary(io_lib:format("Unable to open file ~s for write: ~p", |
| 150 | + [Path, Msg]))} |
| 151 | + end. |
| 152 | + |
| 153 | +read_loop(Path, Io, Consumer) -> |
| 154 | + case file:read(Io, 8) of |
| 155 | + {ok, <<"dbdb", Len:32/unsigned-little-integer>>} -> |
| 156 | + case file:read(Io, Len) of |
| 157 | + {ok, Data} when byte_size(Data) == Len -> |
| 158 | + try |
| 159 | + Decoded = erlang:binary_to_term(iolist_to_binary(Data)), |
| 160 | + case Consumer(Decoded) of |
| 161 | + ok -> |
| 162 | + read_loop(Path, Io, Consumer); |
| 163 | + Err -> Err |
| 164 | + end |
| 165 | + catch |
| 166 | + _:_ -> |
| 167 | + {error, io_lib:format("Unable to decode data from file ~s", [Path])} |
| 168 | + end; |
| 169 | + {ok, _} -> |
| 170 | + {error, io_lib:format("File ~s is too short", [Path])}; |
| 171 | + eof -> |
| 172 | + {error, io_lib:format("File ~s is too short", [Path])}; |
| 173 | + {error, Msg} -> |
| 174 | + {error, io_lib:format("Error when reading file ~s: ~p", [Path, Msg])} |
| 175 | + end; |
| 176 | + {ok, _} -> |
| 177 | + {error, io_lib:format("File ~s has wrong header", [Path])}; |
| 178 | + eof -> |
| 179 | + ok; |
| 180 | + {error, Msg} -> |
| 181 | + {error, io_lib:format("Error when reading file ~s: ~p", [Path, Msg])} |
| 182 | + end. |
| 183 | + |
| 184 | +read_data(Path, Consumer) -> |
| 185 | + case file:open(Path, [raw, read, binary]) of |
| 186 | + {ok, IO} -> |
| 187 | + case read_loop(Path, IO, Consumer) of |
| 188 | + ok -> |
| 189 | + file:close(IO), |
| 190 | + ok; |
| 191 | + {error, Msg} -> |
| 192 | + file:close(IO), |
| 193 | + {error, iolist_to_binary(Msg)} |
| 194 | + end; |
| 195 | + {error, Msg} -> |
| 196 | + {error, iolist_to_binary(io_lib:format("Unable to open file ~s for write: ~p", |
| 197 | + [Path, Msg]))} |
| 198 | + end. |
0 commit comments