Skip to content

Commit 247ec1e

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 1eb4432 commit 247ec1e

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
@@ -1220,20 +1220,19 @@ impl ChatId {
12201220
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
12211221
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
12221222
// fresh ones, they rather mean that older incoming messages are actually seen as well.
1223+
let states = match incoming {
1224+
true => "13, 16, 18, 20, 24, 26", // `> MessageState::InFresh`
1225+
false => "18, 20, 24, 26", // `> MessageState::InSeen`
1226+
};
12231227
context
12241228
.sql
12251229
.query_row_optional(
1226-
"SELECT MAX(timestamp)
1227-
FROM msgs
1228-
WHERE chat_id=? AND hidden=0 AND state>?
1229-
HAVING COUNT(*) > 0",
1230-
(
1231-
self,
1232-
match incoming {
1233-
true => MessageState::InFresh,
1234-
false => MessageState::InSeen,
1235-
},
1230+
&format!(
1231+
"SELECT MAX(timestamp) FROM msgs
1232+
WHERE state IN ({states}) AND hidden=0 AND chat_id=?
1233+
HAVING COUNT(*) > 0"
12361234
),
1235+
(self,),
12371236
|row| {
12381237
let ts: i64 = row.get(0)?;
12391238
Ok(ts)

src/message.rs

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

13631363
/// Outgoing message read by the recipient (two checkmarks; this
1364-
/// requires goodwill on the receiver's side). Not used in the db for new messages.
1364+
/// requires goodwill on the receiver's side). API-only, not used in the db.
13651365
OutMdnRcvd = 28,
13661366
}
13671367

src/sql/migrations.rs

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

1331+
inc_and_check(&mut migration_version, 138)?;
1332+
if dbversion < migration_version {
1333+
// Tweak the index for `chat::calc_sort_timestamp()`.
1334+
sql.execute_migration(
1335+
"UPDATE msgs SET state=26 WHERE state=28;
1336+
DROP INDEX IF EXISTS msgs_index7;
1337+
CREATE INDEX msgs_index7 ON msgs (state, hidden, chat_id, timestamp);",
1338+
migration_version,
1339+
)
1340+
.await?;
1341+
}
1342+
13311343
let new_version = sql
13321344
.get_raw_config_int(VERSION_CFG)
13331345
.await?

src/tests/verified_chats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ async fn test_old_message_5() -> Result<()> {
277277
.await?
278278
.unwrap();
279279

280-
assert!(msg_sent.sort_timestamp == msg_incoming.sort_timestamp);
280+
assert_eq!(msg_sent.sort_timestamp, msg_incoming.sort_timestamp);
281281
alice
282282
.golden_test_chat(msg_sent.chat_id, "test_old_message_5")
283283
.await;

0 commit comments

Comments
 (0)