Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PubSub: Use integer type for timestamps (SQL) #3667

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sql/lite.new.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions sql/lite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions sql/mssql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
4 changes: 2 additions & 2 deletions sql/mysql.new.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions sql/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions sql/pg.new.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions sql/pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 19 additions & 12 deletions src/node_flat_sql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)).
11 changes: 3 additions & 8 deletions src/pubsub_db_sql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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>>.