Skip to content

Commit 295215c

Browse files
committed
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 32a850c commit 295215c

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
@@ -1455,20 +1455,19 @@ impl ChatId {
14551455
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
14561456
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
14571457
// fresh ones, they rather mean that older incoming messages are actually seen as well.
1458+
let states = match incoming {
1459+
true => "13, 16, 18, 20, 24, 26", // `> MessageState::InFresh`
1460+
false => "18, 20, 24, 26", // `> MessageState::InSeen`
1461+
};
14581462
context
14591463
.sql
14601464
.query_row_optional(
1461-
"SELECT MAX(timestamp)
1462-
FROM msgs
1463-
WHERE chat_id=? AND hidden=0 AND state>?
1464-
HAVING COUNT(*) > 0",
1465-
(
1466-
self,
1467-
match incoming {
1468-
true => MessageState::InFresh,
1469-
false => MessageState::InSeen,
1470-
},
1465+
&format!(
1466+
"SELECT MAX(timestamp) FROM msgs
1467+
WHERE state IN ({states}) AND hidden=0 AND chat_id=?
1468+
HAVING COUNT(*) > 0"
14711469
),
1470+
(self,),
14721471
|row| {
14731472
let ts: i64 = row.get(0)?;
14741473
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
@@ -1261,6 +1261,18 @@ CREATE INDEX gossip_timestamp_index ON gossip_timestamp (chat_id, fingerprint);
12611261
.await?;
12621262
}
12631263

1264+
inc_and_check(&mut migration_version, 134)?;
1265+
if dbversion < migration_version {
1266+
// Tweak the index for `chat::calc_sort_timestamp()`.
1267+
sql.execute_migration(
1268+
"UPDATE msgs SET state=26 WHERE state=28;
1269+
DROP INDEX IF EXISTS msgs_index7;
1270+
CREATE INDEX msgs_index7 ON msgs (state, hidden, chat_id, timestamp);",
1271+
migration_version,
1272+
)
1273+
.await?;
1274+
}
1275+
12641276
let new_version = sql
12651277
.get_raw_config_int(VERSION_CFG)
12661278
.await?

src/tests/verified_chats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ async fn test_old_message_5() -> Result<()> {
360360
.await?
361361
.unwrap();
362362

363-
assert!(msg_sent.sort_timestamp == msg_incoming.sort_timestamp);
363+
assert_eq!(msg_sent.sort_timestamp, msg_incoming.sort_timestamp);
364364
alice
365365
.golden_test_chat(msg_sent.chat_id, "test_old_message_5")
366366
.await;

0 commit comments

Comments
 (0)