mirror of
https://github.com/processone/ejabberd.git
synced 2024-11-20 16:15:59 +01:00
PubSub: Use integer type for timestamps (SQL)
Store PubSub item creation/modification timestamps as integers instead of "$megasec:$sec:$microsec" strings. This can improve the performance of certain SQL queries significantly. Thanks to Ammonit Measurement GmbH for sponsoring this work.
This commit is contained in:
parent
ebf03a3745
commit
79ab6bb47c
@ -262,8 +262,8 @@ CREATE TABLE pubsub_item (
|
||||
nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE,
|
||||
itemid text NOT NULL,
|
||||
publisher text NOT NULL,
|
||||
creation varchar(32) NOT NULL,
|
||||
modification varchar(32) NOT NULL,
|
||||
creation BIGINT UNSIGNED NOT NULL,
|
||||
modification BIGINT UNSIGNED NOT NULL,
|
||||
payload text NOT NULL DEFAULT ''
|
||||
);
|
||||
CREATE INDEX i_pubsub_item_itemid ON pubsub_item (itemid);
|
||||
|
@ -238,8 +238,8 @@ CREATE TABLE pubsub_item (
|
||||
nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE,
|
||||
itemid text NOT NULL,
|
||||
publisher text NOT NULL,
|
||||
creation varchar(32) NOT NULL,
|
||||
modification varchar(32) NOT NULL,
|
||||
creation BIGINT UNSIGNED NOT NULL,
|
||||
modification BIGINT UNSIGNED NOT NULL,
|
||||
payload text NOT NULL DEFAULT ''
|
||||
);
|
||||
CREATE INDEX i_pubsub_item_itemid ON pubsub_item (itemid);
|
||||
|
@ -210,8 +210,8 @@ CREATE TABLE [dbo].[pubsub_item] (
|
||||
[nodeid] [bigint] NULL,
|
||||
[itemid] [varchar] (255) NOT NULL,
|
||||
[publisher] [varchar] (250) NOT NULL,
|
||||
[creation] [varchar] (32) NOT NULL,
|
||||
[modification] [varchar] (32) NOT NULL,
|
||||
[creation] [bigint] NOT NULL,
|
||||
[modification] [bigint] NOT NULL,
|
||||
[payload] [text] NOT NULL DEFAULT ''
|
||||
) TEXTIMAGE_ON [PRIMARY];
|
||||
|
||||
|
@ -277,8 +277,8 @@ CREATE TABLE pubsub_item (
|
||||
nodeid bigint,
|
||||
itemid text NOT NULL,
|
||||
publisher text NOT NULL,
|
||||
creation varchar(32) NOT NULL,
|
||||
modification varchar(32) NOT NULL,
|
||||
creation BIGINT UNSIGNED NOT NULL,
|
||||
modification BIGINT UNSIGNED NOT NULL,
|
||||
payload mediumtext NOT NULL
|
||||
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
CREATE INDEX i_pubsub_item_itemid ON pubsub_item(itemid(36));
|
||||
|
@ -253,8 +253,8 @@ CREATE TABLE pubsub_item (
|
||||
nodeid bigint,
|
||||
itemid text NOT NULL,
|
||||
publisher text NOT NULL,
|
||||
creation varchar(32) NOT NULL,
|
||||
modification varchar(32) NOT NULL,
|
||||
creation BIGINT UNSIGNED NOT NULL,
|
||||
modification BIGINT UNSIGNED NOT NULL,
|
||||
payload mediumtext NOT NULL
|
||||
) ENGINE=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
CREATE INDEX i_pubsub_item_itemid ON pubsub_item(itemid(36));
|
||||
|
@ -427,8 +427,8 @@ CREATE TABLE pubsub_item (
|
||||
nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE,
|
||||
itemid text NOT NULL,
|
||||
publisher text NOT NULL,
|
||||
creation varchar(32) NOT NULL,
|
||||
modification varchar(32) NOT NULL,
|
||||
creation BIGINT NOT NULL,
|
||||
modification BIGINT NOT NULL,
|
||||
payload text NOT NULL DEFAULT ''
|
||||
);
|
||||
CREATE INDEX i_pubsub_item_itemid ON pubsub_item USING btree (itemid);
|
||||
|
@ -256,8 +256,8 @@ CREATE TABLE pubsub_item (
|
||||
nodeid bigint REFERENCES pubsub_node(nodeid) ON DELETE CASCADE,
|
||||
itemid text NOT NULL,
|
||||
publisher text NOT NULL,
|
||||
creation varchar(32) NOT NULL,
|
||||
modification varchar(32) NOT NULL,
|
||||
creation BIGINT NOT NULL,
|
||||
modification BIGINT NOT NULL,
|
||||
payload text NOT NULL DEFAULT ''
|
||||
);
|
||||
CREATE INDEX i_pubsub_item_itemid ON pubsub_item USING btree (itemid);
|
||||
|
@ -820,16 +820,16 @@ set_item(Item) ->
|
||||
P = encode_jid(JID),
|
||||
Payload = Item#pubsub_item.payload,
|
||||
XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>),
|
||||
SM = encode_now(M),
|
||||
SC = encode_now(C),
|
||||
SM = misc:now_to_usec(M),
|
||||
SC = misc:now_to_usec(C),
|
||||
?SQL_UPSERT_T(
|
||||
"pubsub_item",
|
||||
["!nodeid=%(Nidx)d",
|
||||
"!itemid=%(ItemId)s",
|
||||
"publisher=%(P)s",
|
||||
"modification=%(SM)s",
|
||||
"modification=%(SM)d",
|
||||
"payload=%(XML)s",
|
||||
"-creation=%(SC)s"
|
||||
"-creation=%(SC)d"
|
||||
]),
|
||||
ok.
|
||||
|
||||
@ -1063,20 +1063,27 @@ rsm_page(Count, Index, Offset, Items) ->
|
||||
first = #rsm_first{index = Offset, data = First},
|
||||
last = Last}.
|
||||
|
||||
%% Convert <<"2021-08-22T19:25:52.817368Z">> to <<"1629660352817368">>.
|
||||
-spec encode_stamp(binary()) -> binary().
|
||||
encode_stamp(Stamp) ->
|
||||
try xmpp_util:decode_timestamp(Stamp) of
|
||||
Now ->
|
||||
encode_now(Now)
|
||||
catch _:{bad_timestamp, _} ->
|
||||
Stamp % We should return a proper error to the client instead.
|
||||
<<"0">> % We should return a proper error to the client instead.
|
||||
end.
|
||||
|
||||
%% Convert <<"1629660352817368">> to <<"2021-08-22T19:25:52.817368Z">>.
|
||||
-spec decode_stamp(binary()) -> binary().
|
||||
decode_stamp(Stamp) ->
|
||||
xmpp_util:encode_timestamp(decode_now(Stamp)).
|
||||
|
||||
encode_now({T1, T2, T3}) ->
|
||||
<<(misc:i2l(T1, 6))/binary, ":",
|
||||
(misc:i2l(T2, 6))/binary, ":",
|
||||
(misc:i2l(T3, 6))/binary>>.
|
||||
decode_now(NowStr) ->
|
||||
[MS, S, US] = binary:split(NowStr, <<":">>, [global]),
|
||||
{binary_to_integer(MS), binary_to_integer(S), binary_to_integer(US)}.
|
||||
%% Convert {1629, 660352, 817368} to <<"1629660352817368">>.
|
||||
-spec encode_now(erlang:timestamp()) -> binary().
|
||||
encode_now(Now) ->
|
||||
integer_to_binary(misc:now_to_usec(Now)).
|
||||
|
||||
%% Convert <<"1629660352817368">> to {1629, 660352, 817368}.
|
||||
-spec decode_now(binary()) -> erlang:timestamp().
|
||||
decode_now(Str) ->
|
||||
misc:usec_to_now(binary_to_integer(Str)).
|
||||
|
@ -184,15 +184,10 @@ export(_Server) ->
|
||||
payload = Payload}) ->
|
||||
P = jid:encode(JID),
|
||||
XML = str:join([fxml:element_to_binary(X) || X<-Payload], <<>>),
|
||||
SM = encode_now(M),
|
||||
SC = encode_now(C),
|
||||
SM = misc:now_to_usec(M),
|
||||
SC = misc:now_to_usec(C),
|
||||
[?SQL("insert into pubsub_item(itemid,nodeid,creation,modification,publisher,payload)"
|
||||
" values (%(ItemId)s, %(Nidx)d, %(SC)s, %(SM)s, %(P)s, %(XML)s);")];
|
||||
" values (%(ItemId)s, %(Nidx)d, %(SC)d, %(SM)d, %(P)s, %(XML)s);")];
|
||||
(_Host, _R) ->
|
||||
[]
|
||||
end}].
|
||||
|
||||
encode_now({T1, T2, T3}) ->
|
||||
<<(misc:i2l(T1, 6))/binary, ":",
|
||||
(misc:i2l(T2, 6))/binary, ":",
|
||||
(misc:i2l(T3, 6))/binary>>.
|
||||
|
Loading…
Reference in New Issue
Block a user