Skip to content

Commit 21b1abb

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 e286d72 commit 21b1abb

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
@@ -1227,20 +1227,19 @@ impl ChatId {
12271227
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
12281228
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
12291229
// fresh ones, they rather mean that older incoming messages are actually seen as well.
1230+
let states = match incoming {
1231+
true => "13, 16, 18, 20, 24, 26", // `> MessageState::InFresh`
1232+
false => "18, 20, 24, 26", // `> MessageState::InSeen`
1233+
};
12301234
context
12311235
.sql
12321236
.query_row_optional(
1233-
"SELECT MAX(timestamp)
1234-
FROM msgs
1235-
WHERE chat_id=? AND hidden=0 AND state>?
1236-
HAVING COUNT(*) > 0",
1237-
(
1238-
self,
1239-
match incoming {
1240-
true => MessageState::InFresh,
1241-
false => MessageState::InSeen,
1242-
},
1237+
&format!(
1238+
"SELECT MAX(timestamp) FROM msgs
1239+
WHERE state IN ({states}) AND hidden=0 AND chat_id=?
1240+
HAVING COUNT(*) > 0"
12431241
),
1242+
(self,),
12441243
|row| {
12451244
let ts: i64 = row.get(0)?;
12461245
Ok(ts)

src/message.rs

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

13681368
/// Outgoing message read by the recipient (two checkmarks; this
1369-
/// requires goodwill on the receiver's side). Not used in the db for new messages.
1369+
/// requires goodwill on the receiver's side). API-only, not used in the db.
13701370
OutMdnRcvd = 28,
13711371
}
13721372

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
@@ -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)