denormalize to reduce load on visible message view

This commit is contained in:
2026-04-08 01:08:45 -04:00
parent f20f811175
commit 036c14f658
3 changed files with 52 additions and 21 deletions
+42 -10
View File
@@ -622,8 +622,11 @@ pub fn create_thread_with_message(ctx: &ReducerContext, name: String, channel_id
});
}
let channel = ctx.db.channel().id().find(channel_id).expect("Channel not found");
ctx.db.recent_message().insert(RecentMessage {
id: 0,
server_id: channel.server_id,
channel_id,
message_id: msg.id,
sender: ctx.sender(),
@@ -633,10 +636,15 @@ pub fn create_thread_with_message(ctx: &ReducerContext, name: String, channel_id
seq_id,
});
let mut recent: Vec<_> = ctx.db.recent_message().channel_id().filter(channel_id).collect();
recent.sort_by_key(|m| m.sent);
if recent.len() > 100 {
ctx.db.recent_message().id().delete(recent[0].id);
if seq_id > 100 {
let old_seq_id = seq_id - 100;
let to_delete: Vec<_> = ctx.db.recent_message().channel_id().filter(channel_id)
.filter(|m| m.seq_id <= old_seq_id)
.map(|m| m.id)
.collect();
for id in to_delete {
ctx.db.recent_message().id().delete(id);
}
}
}
@@ -676,8 +684,11 @@ pub fn send_message(ctx: &ReducerContext, text: String, channel_id: u64, thread_
});
}
let channel = ctx.db.channel().id().find(channel_id).expect("Channel not found");
ctx.db.recent_message().insert(RecentMessage {
id: 0,
server_id: channel.server_id,
channel_id,
message_id: msg.id,
sender: ctx.sender(),
@@ -687,10 +698,15 @@ pub fn send_message(ctx: &ReducerContext, text: String, channel_id: u64, thread_
seq_id,
});
let mut recent: Vec<_> = ctx.db.recent_message().channel_id().filter(channel_id).collect();
recent.sort_by_key(|m| m.sent);
if recent.len() > 100 {
ctx.db.recent_message().id().delete(recent[0].id);
if seq_id > 100 {
let old_seq_id = seq_id - 100;
let to_delete: Vec<_> = ctx.db.recent_message().channel_id().filter(channel_id)
.filter(|m| m.seq_id <= old_seq_id)
.map(|m| m.id)
.collect();
for id in to_delete {
ctx.db.recent_message().id().delete(id);
}
}
}
@@ -702,9 +718,10 @@ pub fn bootstrap_sequences(ctx: &ReducerContext) {
let hwm_ids: Vec<_> = ctx.db.channel_high_water_mark().iter().map(|r| r.channel_id).collect();
for id in hwm_ids { ctx.db.channel_high_water_mark().channel_id().delete(id); }
let mut all_messages: Vec<_> = ctx.db.message().iter().collect();
all_messages.sort_by_key(|m| m.sent);
let rm_ids: Vec<_> = ctx.db.recent_message().iter().map(|r| r.id).collect();
for id in rm_ids { ctx.db.recent_message().id().delete(id); }
let all_messages: Vec<_> = ctx.db.message().iter().collect();
for msg in all_messages {
let seq_id = get_next_seq_id(&ctx.db, msg.channel_id);
ctx.db.channel_message_sequence().insert(ChannelMessageSequence {
@@ -712,6 +729,20 @@ pub fn bootstrap_sequences(ctx: &ReducerContext) {
channel_id: msg.channel_id,
seq_id,
});
if let Some(channel) = ctx.db.channel().id().find(msg.channel_id) {
ctx.db.recent_message().insert(RecentMessage {
id: 0,
server_id: channel.server_id,
channel_id: msg.channel_id,
message_id: msg.id,
sender: msg.sender,
text: msg.text.clone(),
thread_id: msg.thread_id,
sent: msg.sent,
seq_id,
});
}
}
}
@@ -745,6 +776,7 @@ pub fn open_direct_message(ctx: &ReducerContext, recipient: Identity) {
kind: ChannelKind::Text,
});
ctx.db.direct_message().insert(DirectMessage {
id: 0,
channel_id: chan.id,
+2
View File
@@ -300,6 +300,8 @@ pub struct RecentMessage {
#[auto_inc]
pub id: u64,
#[index(btree)]
pub server_id: u64, // 0 if DM
#[index(btree)]
pub channel_id: u64,
pub message_id: u64,
pub sender: Identity,
+8 -11
View File
@@ -54,11 +54,9 @@ pub fn get_visible_message_ids(db: &Local, identity: Identity) -> HashMap<u64, u
}
}
for member in db.server_member().identity().filter(identity) {
for chan in db.channel().server_id().filter(member.server_id) {
for rm in db.recent_message().channel_id().filter(chan.id) {
if !result.contains_key(&rm.message_id) {
result.insert(rm.message_id, rm.seq_id);
}
for rm in db.recent_message().server_id().filter(member.server_id) {
if !result.contains_key(&rm.message_id) {
result.insert(rm.message_id, rm.seq_id);
}
}
}
@@ -95,11 +93,9 @@ pub fn get_visible_message_ids_read_only(db: &LocalReadOnly, identity: Identity)
}
}
for member in db.server_member().identity().filter(identity) {
for chan in db.channel().server_id().filter(member.server_id) {
for rm in db.recent_message().channel_id().filter(chan.id) {
if !result.contains_key(&rm.message_id) {
result.insert(rm.message_id, rm.seq_id);
}
for rm in db.recent_message().server_id().filter(member.server_id) {
if !result.contains_key(&rm.message_id) {
result.insert(rm.message_id, rm.seq_id);
}
}
}
@@ -144,7 +140,8 @@ pub fn get_visible_image_ids(db: &Local, identity: Identity) -> HashSet<u64> {
}
}
}
for ce in db.custom_emoji().iter() {
// Optimized custom emoji scan
for ce in db.custom_emoji().name().filter(""..) {
ids.insert(ce.id);
}
let visible_msg_ids = get_visible_message_ids(db, identity);