./ct_report/coverage/mod_pubsub_cache_mnesia.COVER.html

1 -module(mod_pubsub_cache_mnesia).
2
3 -behaviour(mod_pubsub_cache_backend).
4
5 -include("pubsub.hrl").
6 -include("jlib.hrl").
7
8 -export([start/1, stop/0]).
9
10 -export([
11 upsert_last_item/5,
12 get_last_item/2,
13 delete_last_item/2]).
14
15 %% ------------------------ Backend start/stop ------------------------
16
17 -spec start(jid:lserver()) -> ok.
18 start(_) ->
19 3 create_table().
20
21 -spec stop() -> ok.
22 stop() ->
23
:-(
ok.
24
25 -spec upsert_last_item(ServerHost :: binary(),
26 Nidx :: mod_pubsub:nodeIdx(),
27 ItemID :: mod_pubsub:itemId(),
28 Publisher :: jid:jid(),
29 Payload::mod_pubsub:payload()) -> ok | {error, Reason :: term()}.
30 upsert_last_item(_ServerHost, Nidx, ItemId, Publisher, Payload) ->
31 14 CreatedAt = os:system_time(microsecond),
32 14 try mnesia:dirty_write(
33 #pubsub_last_item{
34 nodeid = Nidx,
35 itemid = ItemId,
36 creation = {CreatedAt, {Publisher#jid.luser, Publisher#jid.lserver, <<>>}},
37 payload = Payload}
38 ) of
39 14 ok -> ok
40 catch
41
:-(
exit:{aborted, Reason} -> {error, Reason}
42 end.
43
44 -spec get_last_item(ServerHost :: binary(),
45 Nidx :: mod_pubsub:nodeIdx()) ->
46 {ok, mod_pubsub:pubsubLastItem()} | {error, Reason :: term()}.
47 get_last_item(_ServerHost, Nidx) ->
48 8 try mnesia:dirty_read({pubsub_last_item, Nidx}) of
49 5 [LastItem] -> {ok, LastItem};
50 3 [] -> {error, no_items}
51 catch
52
:-(
exit:{aborted, Reason} -> {error, Reason}
53 end.
54
55 -spec delete_last_item(ServerHost :: binary(),
56 Nidx :: mod_pubsub:nodeIdx()) -> ok | {error, Reason :: term()}.
57 delete_last_item(_ServerHost, Nidx) ->
58 2 try mnesia:dirty_delete({pubsub_last_item, Nidx}) of
59 2 ok -> ok
60 catch
61
:-(
exit:{aborted, Reason} -> {error, Reason}
62 end.
63
64 %% ------------------------ Helpers ----------------------------
65
66 -spec create_table() -> ok.
67 create_table() ->
68 3 mongoose_mnesia:create_table(pubsub_last_item,
69 [{ram_copies, [node()]},
70 {attributes, record_info(fields, pubsub_last_item)}]),
71 3 ok.
Line Hits Source