Skip to content

Commit 7c44f7d

Browse files
iequidoolink2xt
authored andcommitted
feat: Add timestamp to msgs_index7 to employ it in calc_sort_timestamp()
Tested on some random chat, the SQL query took 1.411202ms (vs 6.692714ms before) in median. Still looks a bit slow, but already better.
1 parent 4f623a1 commit 7c44f7d

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

src/chat.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,20 +1240,19 @@ impl ChatId {
12401240
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
12411241
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
12421242
// fresh ones, they rather mean that older incoming messages are actually seen as well.
1243+
let states = match incoming {
1244+
true => "13, 16, 18, 20, 24, 26", // `> MessageState::InFresh`
1245+
false => "18, 20, 24, 26", // `> MessageState::InSeen`
1246+
};
12431247
context
12441248
.sql
12451249
.query_row_optional(
1246-
"SELECT MAX(timestamp)
1247-
FROM msgs
1248-
WHERE chat_id=? AND hidden=0 AND state>?
1249-
HAVING COUNT(*) > 0",
1250-
(
1251-
self,
1252-
match incoming {
1253-
true => MessageState::InFresh,
1254-
false => MessageState::InSeen,
1255-
},
1250+
&format!(
1251+
"SELECT MAX(timestamp) FROM msgs
1252+
WHERE state IN ({states}) AND hidden=0 AND chat_id=?
1253+
HAVING COUNT(*) > 0"
12561254
),
1255+
(self,),
12571256
|row| {
12581257
let ts: i64 = row.get(0)?;
12591258
Ok(ts)

src/message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ pub enum MessageState {
14461446
OutDelivered = 26,
14471447

14481448
/// Outgoing message read by the recipient (two checkmarks; this
1449-
/// requires goodwill on the receiver's side). Not used in the db for new messages.
1449+
/// requires goodwill on the receiver's side). API-only, not used in the db.
14501450
OutMdnRcvd = 28,
14511451
}
14521452

src/sql/migrations.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,18 @@ CREATE INDEX gossip_timestamp_index ON gossip_timestamp (chat_id, fingerprint);
12711271
.await?;
12721272
}
12731273

1274+
inc_and_check(&mut migration_version, 135)?;
1275+
if dbversion < migration_version {
1276+
// Tweak the index for `chat::calc_sort_timestamp()`.
1277+
sql.execute_migration(
1278+
"UPDATE msgs SET state=26 WHERE state=28;
1279+
DROP INDEX IF EXISTS msgs_index7;
1280+
CREATE INDEX msgs_index7 ON msgs (state, hidden, chat_id, timestamp);",
1281+
migration_version,
1282+
)
1283+
.await?;
1284+
}
1285+
12741286
let new_version = sql
12751287
.get_raw_config_int(VERSION_CFG)
12761288
.await?

src/tests/verified_chats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ async fn test_old_message_5() -> Result<()> {
294294
.await?
295295
.unwrap();
296296

297-
assert!(msg_sent.sort_timestamp == msg_incoming.sort_timestamp);
297+
assert_eq!(msg_sent.sort_timestamp, msg_incoming.sort_timestamp);
298298
alice
299299
.golden_test_chat(msg_sent.chat_id, "test_old_message_5")
300300
.await;

0 commit comments

Comments
 (0)