Skip to content

Commit

Permalink
Reinstate code to update thread summaries (#6754)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Nov 7, 2024
1 parent c5a9e10 commit bccb53b
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions backend/libraries/chat_events/src/chat_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use search::{Document, Query};
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use sha2::{Digest, Sha256};
use std::cmp::max;
use std::cmp::{max, min};
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::mem;
use std::ops::DerefMut;
use tracing::info;
use types::{
AcceptP2PSwapResult, CallParticipant, CancelP2PSwapResult, CanisterId, Chat, ChatType, CompleteP2PSwapResult,
CompletedCryptoTransaction, Cryptocurrency, DirectChatCreated, EventContext, EventIndex, EventWrapper,
Expand Down Expand Up @@ -76,7 +77,23 @@ impl ChatEvents {
}

pub fn migrate_next_batch_of_events_to_stable_storage(&mut self) -> bool {
self.thread_messages_to_update_in_stable_memory.is_empty() && self.next_event_to_migrate_to_stable_memory.is_none()
while !self.thread_messages_to_update_in_stable_memory.is_empty() {
if ic_cdk::api::instruction_counter() > 1_000_000_000 {
return false;
}

let batch: Vec<_> = self
.thread_messages_to_update_in_stable_memory
.drain(..min(100, self.thread_messages_to_update_in_stable_memory.len()))
.collect();

let count = batch.len();
for message_index in batch {
self.update_event_in_stable_memory(message_index.into());
}
info!(chat = ?self.chat, count, "Updated threads in stable memory");
}
self.next_event_to_migrate_to_stable_memory.is_none()
}

pub fn new_direct_chat(
Expand Down

0 comments on commit bccb53b

Please sign in to comment.