Skip to content

Commit adff64f

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 d0f8e10 commit adff64f

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
@@ -1229,20 +1229,19 @@ impl ChatId {
12291229
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
12301230
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
12311231
// fresh ones, they rather mean that older incoming messages are actually seen as well.
1232+
let states = match incoming {
1233+
true => "13, 16, 18, 20, 24, 26", // `> MessageState::InFresh`
1234+
false => "18, 20, 24, 26", // `> MessageState::InSeen`
1235+
};
12321236
context
12331237
.sql
12341238
.query_row_optional(
1235-
"SELECT MAX(timestamp)
1236-
FROM msgs
1237-
WHERE chat_id=? AND hidden=0 AND state>?
1238-
HAVING COUNT(*) > 0",
1239-
(
1240-
self,
1241-
match incoming {
1242-
true => MessageState::InFresh,
1243-
false => MessageState::InSeen,
1244-
},
1239+
&format!(
1240+
"SELECT MAX(timestamp) FROM msgs
1241+
WHERE state IN ({states}) AND hidden=0 AND chat_id=?
1242+
HAVING COUNT(*) > 0"
12451243
),
1244+
(self,),
12461245
|row| {
12471246
let ts: i64 = row.get(0)?;
12481247
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
@@ -282,7 +282,7 @@ async fn test_old_message_5() -> Result<()> {
282282
.await?
283283
.unwrap();
284284

285-
assert!(msg_sent.sort_timestamp == msg_incoming.sort_timestamp);
285+
assert_eq!(msg_sent.sort_timestamp, msg_incoming.sort_timestamp);
286286
alice
287287
.golden_test_chat(msg_sent.chat_id, "test_old_message_5")
288288
.await;

0 commit comments

Comments
 (0)