mirror of
https://github.com/chatmail/core.git
synced 2026-05-05 22:36:30 +03:00
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.
This commit is contained in:
19
src/chat.rs
19
src/chat.rs
@@ -1227,20 +1227,19 @@ impl ChatId {
|
|||||||
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
|
// NB: Received outgoing messages may break sorting of fresh incoming ones, but this
|
||||||
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
|
// shouldn't happen frequently. Seen incoming messages don't really break sorting of
|
||||||
// fresh ones, they rather mean that older incoming messages are actually seen as well.
|
// fresh ones, they rather mean that older incoming messages are actually seen as well.
|
||||||
|
let states = match incoming {
|
||||||
|
true => "13, 16, 18, 20, 24, 26", // `> MessageState::InFresh`
|
||||||
|
false => "18, 20, 24, 26", // `> MessageState::InSeen`
|
||||||
|
};
|
||||||
context
|
context
|
||||||
.sql
|
.sql
|
||||||
.query_row_optional(
|
.query_row_optional(
|
||||||
"SELECT MAX(timestamp)
|
&format!(
|
||||||
FROM msgs
|
"SELECT MAX(timestamp) FROM msgs
|
||||||
WHERE chat_id=? AND hidden=0 AND state>?
|
WHERE state IN ({states}) AND hidden=0 AND chat_id=?
|
||||||
HAVING COUNT(*) > 0",
|
HAVING COUNT(*) > 0"
|
||||||
(
|
|
||||||
self,
|
|
||||||
match incoming {
|
|
||||||
true => MessageState::InFresh,
|
|
||||||
false => MessageState::InSeen,
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
|
(self,),
|
||||||
|row| {
|
|row| {
|
||||||
let ts: i64 = row.get(0)?;
|
let ts: i64 = row.get(0)?;
|
||||||
Ok(ts)
|
Ok(ts)
|
||||||
|
|||||||
@@ -1366,7 +1366,7 @@ pub enum MessageState {
|
|||||||
OutDelivered = 26,
|
OutDelivered = 26,
|
||||||
|
|
||||||
/// Outgoing message read by the recipient (two checkmarks; this
|
/// Outgoing message read by the recipient (two checkmarks; this
|
||||||
/// requires goodwill on the receiver's side). Not used in the db for new messages.
|
/// requires goodwill on the receiver's side). API-only, not used in the db.
|
||||||
OutMdnRcvd = 28,
|
OutMdnRcvd = 28,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1271,6 +1271,18 @@ CREATE INDEX gossip_timestamp_index ON gossip_timestamp (chat_id, fingerprint);
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inc_and_check(&mut migration_version, 135)?;
|
||||||
|
if dbversion < migration_version {
|
||||||
|
// Tweak the index for `chat::calc_sort_timestamp()`.
|
||||||
|
sql.execute_migration(
|
||||||
|
"UPDATE msgs SET state=26 WHERE state=28;
|
||||||
|
DROP INDEX IF EXISTS msgs_index7;
|
||||||
|
CREATE INDEX msgs_index7 ON msgs (state, hidden, chat_id, timestamp);",
|
||||||
|
migration_version,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
let new_version = sql
|
let new_version = sql
|
||||||
.get_raw_config_int(VERSION_CFG)
|
.get_raw_config_int(VERSION_CFG)
|
||||||
.await?
|
.await?
|
||||||
|
|||||||
@@ -277,7 +277,7 @@ async fn test_old_message_5() -> Result<()> {
|
|||||||
.await?
|
.await?
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
assert!(msg_sent.sort_timestamp == msg_incoming.sort_timestamp);
|
assert_eq!(msg_sent.sort_timestamp, msg_incoming.sort_timestamp);
|
||||||
alice
|
alice
|
||||||
.golden_test_chat(msg_sent.chat_id, "test_old_message_5")
|
.golden_test_chat(msg_sent.chat_id, "test_old_message_5")
|
||||||
.await;
|
.await;
|
||||||
|
|||||||
Reference in New Issue
Block a user