Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More cleanup of unused code #6903

Merged
merged 3 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 4 additions & 11 deletions backend/libraries/chat_events/src/chat_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::expiring_events::ExpiringEvents;
use crate::last_updated_timestamps::LastUpdatedTimestamps;
use crate::metrics::{ChatMetricsInternal, MetricKey};
use crate::search_index::SearchIndex;
use crate::stable_storage::key::KeyPrefix;
use crate::stable_memory::key::KeyPrefix;
use crate::*;
use event_store_producer::{EventBuilder, EventStoreClient, Runtime};
use rand::rngs::StdRng;
Expand Down Expand Up @@ -77,19 +77,12 @@ impl ChatEvents {
false
}

pub fn init_maps(&mut self) {
self.main.init_hybrid_map(self.chat, None);
for (message_index, thread) in self.threads.iter_mut() {
thread.init_hybrid_map(self.chat, Some(*message_index));
}
}

pub fn import_events(chat: Chat, events: Vec<(EventContext, ByteBuf)>) {
stable_storage::write_events_as_bytes(chat, events);
stable_memory::write_events_as_bytes(chat, events);
}

pub fn garbage_collect_stable_memory(prefix: KeyPrefix) -> Result<u32, u32> {
stable_storage::garbage_collect(prefix)
stable_memory::garbage_collect(prefix)
}

pub fn set_stable_memory_key_prefixes(&mut self) {
Expand Down Expand Up @@ -174,7 +167,7 @@ impl ChatEvents {
}

pub fn read_events_as_bytes_from_stable_memory(&self, after: Option<EventContext>) -> Vec<(EventContext, ByteBuf)> {
stable_storage::read_events_as_bytes(self.chat, after, 1_000_000)
stable_memory::read_events_as_bytes(self.chat, after, 1_000_000)
}

pub fn iter_recently_updated_events(
Expand Down
43 changes: 15 additions & 28 deletions backend/libraries/chat_events/src/chat_events_list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::hybrid_map::HybridMap;
use crate::last_updated_timestamps::LastUpdatedTimestamps;
use crate::stable_storage::ChatEventsStableStorage;
use crate::stable_memory::ChatEventsStableStorage;
use crate::{ChatEventInternal, EventKey, EventOrExpiredRangeInternal, EventsMap, MessageInternal};
use candid::Principal;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::Entry::Vacant;
Expand All @@ -15,41 +14,29 @@ use types::{

#[derive(Serialize, Deserialize)]
pub struct ChatEventsList {
#[serde(skip_deserializing, default = "default_stable_events_map")]
stable_events_map: HybridMap<ChatEventsStableStorage>,
events_map: HybridMap<ChatEventsStableStorage>,
message_id_map: HashMap<MessageId, EventIndex>,
message_event_indexes: Vec<EventIndex>,
latest_event_index: Option<EventIndex>,
latest_event_timestamp: Option<TimestampMillis>,
}

fn default_stable_events_map() -> HybridMap<ChatEventsStableStorage> {
HybridMap::new(Chat::Group(Principal::anonymous().into()), None)
}

impl ChatEventsList {
pub fn init_hybrid_map(&mut self, chat: Chat, thread_root_message_index: Option<MessageIndex>) {
self.stable_events_map = HybridMap::new(chat, thread_root_message_index);
self.stable_events_map
.populate_fast_map(self.latest_event_index.unwrap_or_default());
}

pub fn update_event_in_stable_memory(&mut self, event_key: EventKey) {
pub fn update_event_in_memory(&mut self, event_key: EventKey) {
if let Some(event_index) = self.event_index(event_key) {
if let Some(event) = self.stable_events_map.get(event_index) {
self.stable_events_map.insert(event);
if let Some(event) = self.events_map.get(event_index) {
self.events_map.insert(event);
}
}
}

pub fn set_stable_memory_prefix(&mut self, chat: Chat, thread_root_message_index: Option<MessageIndex>) {
self.stable_events_map
.set_stable_memory_prefix(chat, thread_root_message_index);
self.events_map.set_stable_memory_prefix(chat, thread_root_message_index);
}

pub fn new(chat: Chat, thread_root_message_index: Option<MessageIndex>) -> Self {
ChatEventsList {
stable_events_map: HybridMap::new(chat, thread_root_message_index),
events_map: HybridMap::new(chat, thread_root_message_index),
message_id_map: HashMap::new(),
message_event_indexes: Vec::new(),
latest_event_index: None,
Expand Down Expand Up @@ -81,7 +68,7 @@ impl ChatEventsList {
expires_at,
event,
};
self.stable_events_map.insert(event_wrapper);
self.events_map.insert(event_wrapper);

self.latest_event_index = Some(event_index);
self.latest_event_timestamp = Some(now);
Expand Down Expand Up @@ -121,7 +108,7 @@ impl ChatEventsList {
if let Some(mut event) = self.get_event(event_key, EventIndex::default()) {
update_event_fn(&mut event).map(|result| {
let event_index = event.index;
self.stable_events_map.insert(event);
self.events_map.insert(event);
(result, event_index)
})
} else {
Expand Down Expand Up @@ -153,7 +140,7 @@ impl ChatEventsList {
(min_visible_event_index, self.latest_event_index.unwrap_or_default())
};

let iter = self.stable_events_map.range(min..=max);
let iter = self.events_map.range(min..=max);

if ascending {
Box::new(ChatEventsListIterator {
Expand Down Expand Up @@ -206,7 +193,7 @@ impl ChatEventsList {
}

pub(crate) fn event_count_since<F: Fn(&ChatEventInternal) -> bool>(&self, since: TimestampMillis, filter: &F) -> usize {
self.stable_events_map
self.events_map
.iter()
.rev()
.take_while(|e| e.timestamp > since)
Expand All @@ -215,7 +202,7 @@ impl ChatEventsList {
}

pub fn remove(&mut self, event_index: EventIndex) -> Option<EventWrapperInternal<ChatEventInternal>> {
self.stable_events_map.remove(event_index)
self.events_map.remove(event_index)
}

pub fn latest_event_index(&self) -> Option<EventIndex> {
Expand Down Expand Up @@ -243,7 +230,7 @@ impl ChatEventsList {
}

pub fn last(&self) -> Option<EventWrapperInternal<ChatEventInternal>> {
self.stable_events_map.iter().next_back()
self.events_map.iter().next_back()
}

pub fn contains_message_id(&self, message_id: MessageId) -> bool {
Expand All @@ -262,12 +249,12 @@ impl ChatEventsList {
&self,
event_index: EventIndex,
) -> Result<EventWrapperInternal<ChatEventInternal>, (Option<EventIndex>, Option<EventIndex>)> {
let next_key = match self.stable_events_map.range(event_index..).next() {
let next_key = match self.events_map.range(event_index..).next() {
Some(v) if v.index == event_index => return Ok(v),
Some(v) => Some(v.index),
None => None,
};
let previous_key = self.stable_events_map.range(..event_index).next_back().map(|e| e.index);
let previous_key = self.events_map.range(..event_index).next_back().map(|e| e.index);

Err((previous_key, next_key))
}
Expand Down
10 changes: 1 addition & 9 deletions backend/libraries/chat_events/src/hybrid_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stable_storage::ChatEventsStableStorage;
use crate::stable_memory::ChatEventsStableStorage;
use crate::{ChatEventInternal, EventsMap};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -26,14 +26,6 @@ pub struct HybridMap<EventsMap> {
}

impl<MSlow: EventsMap> HybridMap<MSlow> {
// TODO: Remove this once everything is migrated over
pub fn populate_fast_map(&mut self, latest_event_index: EventIndex) {
for event in self.slow.iter().rev().take(self.max_events_in_fast_map as usize) {
self.fast.insert(event.index, event);
}
self.latest_event_index = latest_event_index;
}

fn fast_enabled(&self) -> bool {
self.max_events_in_fast_map > 0
}
Expand Down
4 changes: 2 additions & 2 deletions backend/libraries/chat_events/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ mod last_updated_timestamps;
mod message_content_internal;
mod metrics;
mod search_index;
mod stable_storage;
mod stable_memory;

pub use crate::chat_event_internal::*;
pub use crate::chat_events::*;
pub use crate::chat_events_list::*;
pub use crate::events_map::*;
pub use crate::message_content_internal::*;
pub use crate::metrics::*;
pub use crate::stable_storage::key::*;
pub use crate::stable_memory::key::*;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stable_storage::key::{Key, KeyPrefix};
use crate::stable_memory::key::{Key, KeyPrefix};
use crate::{ChatEventInternal, EventsMap};
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::message_content_internal::icrc1::AccountInternal;
use crate::stable_storage::tests::test_values::{
use crate::stable_memory::tests::test_values::{
AUDIO1, CRYPTO1, CUSTOM1, DELETED1, FILE1, GIPHY1, GOVERNANCE_PROPOSAL1, IMAGE1, MESSAGE_REMINDER1,
MESSAGE_REMINDER_CREATED1, P2P_SWAP1, POLL1, PRIZE1, PRIZE_WINNER1, REPORTED_MESSAGE1, TEXT1, VIDEO1, VIDEO_CALL1,
};
use crate::stable_storage::{bytes_to_event, event_to_bytes};
use crate::stable_memory::{bytes_to_event, event_to_bytes};
use crate::{
AudioContentInternal, BlobReferenceInternal, CallParticipantInternal, ChatEventInternal, ChatInternal,
CompletedCryptoTransactionInternal, CryptoContentInternal, CustomContentInternal, DeletedByInternal, FileContentInternal,
Expand Down
2 changes: 0 additions & 2 deletions backend/libraries/group_chat_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ candid = { workspace = true }
chat_events = { path = "../chat_events" }
event_store_producer = { workspace = true }
group_community_common = { path = "../group_community_common" }
ic-cdk = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
regex-lite = { workspace = true }
search = { path = "../search" }
serde = { workspace = true }
serde_repr = { workspace = true }
tracing = { workspace = true }
types = { path = "../types" }
utils = { path = "../utils" }

Expand Down
6 changes: 1 addition & 5 deletions backend/libraries/group_chat_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use regex_lite::Regex;
use search::Query;
use serde::{Deserialize, Serialize};
use std::cmp::{max, min, Reverse};
use std::collections::{BTreeMap, BTreeSet, HashSet, VecDeque};
use std::collections::{BTreeMap, BTreeSet, HashSet};
use types::{
AccessGate, AccessGateConfig, AccessGateConfigInternal, AvatarChanged, ContentValidationError, CustomPermission, Document,
EventIndex, EventOrExpiredRange, EventWrapper, EventsResponse, ExternalUrlUpdated, FieldTooLongResult, FieldTooShortResult,
Expand Down Expand Up @@ -61,10 +61,7 @@ pub struct GroupChatCore {
pub invited_users: InvitedUsers,
pub min_visible_indexes_for_new_members: Option<(EventIndex, MessageIndex)>,
pub external_url: Timestamped<Option<String>>,
#[serde(default)]
at_everyone_mentions: BTreeMap<TimestampMillis, AtEveryoneMention>,
#[serde(default)]
pub dedupe_at_everyone_mentions_queue: VecDeque<UserId>,
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -121,7 +118,6 @@ impl GroupChatCore {
min_visible_indexes_for_new_members: None,
external_url: Timestamped::new(external_url, now),
at_everyone_mentions: BTreeMap::new(),
dedupe_at_everyone_mentions_queue: VecDeque::new(),
}
}

Expand Down
20 changes: 6 additions & 14 deletions backend/libraries/group_chat_core/src/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::ser::SerializeSeq;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::cmp::max;
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Formatter;
use types::{
is_default, EventIndex, GroupMember, GroupPermissions, MessageIndex, TimestampMillis, Timestamped, UserId, UserType,
Expand All @@ -23,7 +23,7 @@ const MAX_MEMBERS_PER_GROUP: u32 = 100_000;
#[derive(Serialize, Deserialize, Default)]
pub struct GroupMembers {
#[serde(serialize_with = "serialize_members", deserialize_with = "deserialize_members")]
members: HashMap<UserId, GroupMemberInternal>,
members: BTreeMap<UserId, GroupMemberInternal>,
member_ids: BTreeSet<UserId>,
owners: BTreeSet<UserId>,
admins: BTreeSet<UserId>,
Expand Down Expand Up @@ -185,14 +185,6 @@ impl GroupMembers {
&self.member_ids
}

pub fn iter(&self) -> impl Iterator<Item = &GroupMemberInternal> {
self.members.values()
}

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut GroupMemberInternal> {
self.members.values_mut()
}

pub fn get(&self, user_id: &UserId) -> Option<&GroupMemberInternal> {
self.members.get(user_id)
}
Expand Down Expand Up @@ -662,22 +654,22 @@ fn mentions_are_empty(value: &Mentions) -> bool {
value.is_empty()
}

fn serialize_members<S: Serializer>(value: &HashMap<UserId, GroupMemberInternal>, serializer: S) -> Result<S::Ok, S::Error> {
fn serialize_members<S: Serializer>(value: &BTreeMap<UserId, GroupMemberInternal>, serializer: S) -> Result<S::Ok, S::Error> {
let mut seq = serializer.serialize_seq(Some(value.len()))?;
for member in value.values() {
seq.serialize_element(member)?;
}
seq.end()
}

fn deserialize_members<'de, D: Deserializer<'de>>(deserializer: D) -> Result<HashMap<UserId, GroupMemberInternal>, D::Error> {
fn deserialize_members<'de, D: Deserializer<'de>>(deserializer: D) -> Result<BTreeMap<UserId, GroupMemberInternal>, D::Error> {
deserializer.deserialize_seq(GroupMembersMapVisitor)
}

struct GroupMembersMapVisitor;

impl<'de> Visitor<'de> for GroupMembersMapVisitor {
type Value = HashMap<UserId, GroupMemberInternal>;
type Value = BTreeMap<UserId, GroupMemberInternal>;

fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
formatter.write_str("a sequence")
Expand All @@ -687,7 +679,7 @@ impl<'de> Visitor<'de> for GroupMembersMapVisitor {
where
A: SeqAccess<'de>,
{
let mut map = seq.size_hint().map_or_else(HashMap::new, HashMap::with_capacity);
let mut map = BTreeMap::new();
while let Some(next) = seq.next_element::<GroupMemberInternal>()? {
map.insert(next.user_id, next);
}
Expand Down
Loading