WIP: Send the channel name and the channel edges seperately, so we're not repeating them constantly

This commit is currently broken and includes debug data for a failed attempt at rewriting the insert_edge logic
This commit is contained in:
Mikayla
2023-09-15 17:57:23 -07:00
parent 363867c65b
commit 5f9c56c8b0
13 changed files with 437 additions and 760 deletions
+46 -8
View File
@@ -3,12 +3,25 @@ mod channel_index;
use crate::{channel_buffer::ChannelBuffer, channel_chat::ChannelChat};
use anyhow::{anyhow, Result};
use client::{Client, Subscription, User, UserId, UserStore};
use collections::{hash_map::{self, DefaultHasher}, HashMap, HashSet};
use collections::{
hash_map::{self, DefaultHasher},
HashMap, HashSet,
};
use futures::{channel::mpsc, future::Shared, Future, FutureExt, StreamExt};
use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task, WeakModelHandle};
use rpc::{proto, TypedEnvelope};
use rpc::{
proto::{self, ChannelEdge, ChannelPermission},
TypedEnvelope,
};
use serde_derive::{Deserialize, Serialize};
use std::{mem, ops::Deref, sync::Arc, time::Duration, borrow::Cow, hash::{Hash, Hasher}};
use std::{
borrow::Cow,
hash::{Hash, Hasher},
mem,
ops::Deref,
sync::Arc,
time::Duration,
};
use util::ResultExt;
use self::channel_index::ChannelIndex;
@@ -301,18 +314,33 @@ impl ChannelStore {
let client = self.client.clone();
let name = name.trim_start_matches("#").to_owned();
cx.spawn(|this, mut cx| async move {
let channel = client
let response = client
.request(proto::CreateChannel { name, parent_id })
.await?
.await?;
let channel = response
.channel
.ok_or_else(|| anyhow!("missing channel in response"))?;
let channel_id = channel.id;
let parent_edge = if let Some(parent_id) = parent_id {
vec![ChannelEdge {
channel_id: channel.id,
parent_id,
}]
} else {
vec![]
};
this.update(&mut cx, |this, cx| {
let task = this.update_channels(
proto::UpdateChannels {
channels: vec![channel],
insert_edge: parent_edge,
channel_permissions: vec![ChannelPermission {
channel_id,
is_admin: true,
}],
..Default::default()
},
cx,
@@ -730,6 +758,8 @@ impl ChannelStore {
payload: proto::UpdateChannels,
cx: &mut ModelContext<ChannelStore>,
) -> Option<Task<Result<()>>> {
dbg!(self.client.user_id(), &payload);
if !payload.remove_channel_invitations.is_empty() {
self.channel_invitations
.retain(|channel| !payload.remove_channel_invitations.contains(&channel.id));
@@ -752,7 +782,9 @@ impl ChannelStore {
let channels_changed = !payload.channels.is_empty()
|| !payload.delete_channels.is_empty()
|| !payload.insert_edge.is_empty()
|| !payload.delete_edge.is_empty();
if channels_changed {
if !payload.delete_channels.is_empty() {
self.channel_index.delete_channels(&payload.delete_channels);
@@ -774,14 +806,20 @@ impl ChannelStore {
}
let mut index_edit = self.channel_index.bulk_edit();
dbg!(&index_edit);
for channel in payload.channels {
index_edit.upsert(channel)
index_edit.insert(channel)
}
for edge in payload.insert_edge {
index_edit.insert_edge(edge.parent_id, edge.channel_id);
}
for edge in payload.delete_edge {
index_edit.delete_edge(edge.parent_id, edge.channel_id);
}
drop(index_edit);
dbg!(&self.channel_index);
}
for permission in payload.channel_permissions {
@@ -53,6 +53,7 @@ impl Deref for ChannelIndex {
/// A guard for ensuring that the paths index maintains its sort and uniqueness
/// invariants after a series of insertions
#[derive(Debug)]
pub struct ChannelPathsEditGuard<'a> {
paths: &'a mut Vec<ChannelPath>,
channels_by_id: &'a mut ChannelsById,
@@ -78,42 +79,81 @@ impl<'a> ChannelPathsEditGuard<'a> {
}
}
pub fn upsert(&mut self, channel_proto: proto::Channel) {
pub fn insert(&mut self, channel_proto: proto::Channel) {
if let Some(existing_channel) = self.channels_by_id.get_mut(&channel_proto.id) {
Arc::make_mut(existing_channel).name = channel_proto.name;
if let Some(parent_id) = channel_proto.parent_id {
self.insert_edge(parent_id, channel_proto.id)
}
} else {
let channel = Arc::new(Channel {
id: channel_proto.id,
name: channel_proto.name,
});
self.channels_by_id.insert(channel.id, channel.clone());
if let Some(parent_id) = channel_proto.parent_id {
self.insert_edge(parent_id, channel.id);
} else {
self.insert_root(channel.id);
}
self.channels_by_id.insert(
channel_proto.id,
Arc::new(Channel {
id: channel_proto.id,
name: channel_proto.name,
}),
);
self.insert_root(channel_proto.id);
}
}
fn insert_edge(&mut self, parent_id: ChannelId, channel_id: ChannelId) {
pub fn insert_edge(&mut self, parent_id: ChannelId, channel_id: ChannelId) {
debug_assert!(self.channels_by_id.contains_key(&parent_id));
let mut ix = 0;
println!("*********** INSERTING EDGE {}, {} ***********", channel_id, parent_id);
dbg!(&self.paths);
while ix < self.paths.len() {
let path = &self.paths[ix];
println!("*********");
dbg!(path);
if path.ends_with(&[parent_id]) {
let mut new_path = path.to_vec();
dbg!("Appending to parent path");
let mut new_path = Vec::with_capacity(path.len() + 1);
new_path.extend_from_slice(path);
new_path.push(channel_id);
self.paths.insert(ix + 1, ChannelPath::new(new_path.into()));
self.paths.insert(ix + 1, dbg!(ChannelPath::new(new_path.into())));
ix += 2;
} else if path.get(0) == Some(&channel_id) {
// Clear out any paths that have this chahnnel as their root
self.paths.swap_remove(ix);
} else if let Some(path_ix) = path.iter().position(|c| c == &channel_id) {
if path.contains(&parent_id) {
dbg!("Doing nothing");
ix += 1;
continue;
}
if path_ix == 0 && path.len() == 1 {
dbg!("Removing path that is just this");
self.paths.swap_remove(ix);
continue;
}
// This is the busted section rn
// We're trying to do this weird, unsorted context
// free insertion thing, but we can't insert 'parent_id',
// we have to _prepend_ with _parent path to_,
// or something like that.
// It's a bit busted rn, I think I need to keep this whole thing
// sorted now, as this is a huge mess.
// Basically, we want to do the exact thing we do in the
// server, except explicitly.
// Also, rethink the bulk edit abstraction, it's use may no longer
// be as needed with the channel names and edges seperated.
dbg!("Expanding path which contains");
let (left, right) = path.split_at(path_ix);
let mut new_path = Vec::with_capacity(left.len() + right.len() + 1);
/// WRONG WRONG WRONG
new_path.extend_from_slice(left);
new_path.push(parent_id);
/// WRONG WRONG WRONG
new_path.extend_from_slice(right);
if path_ix == 0 {
dbg!("Replacing path that starts with this");
self.paths[ix] = dbg!(ChannelPath::new(new_path.into()));
} else {
dbg!("inserting new path");
self.paths.insert(ix + 1, dbg!(ChannelPath::new(new_path.into())));
ix += 1;
}
ix += 1;
} else {
dbg!("Doing nothing");
ix += 1;
}
}
+21 -8
View File
@@ -18,12 +18,11 @@ fn test_update_channels(cx: &mut AppContext) {
proto::Channel {
id: 1,
name: "b".to_string(),
parent_id: None,
},
proto::Channel {
id: 2,
name: "a".to_string(),
parent_id: None,
},
],
channel_permissions: vec![proto::ChannelPermission {
@@ -51,12 +50,20 @@ fn test_update_channels(cx: &mut AppContext) {
proto::Channel {
id: 3,
name: "x".to_string(),
parent_id: Some(1),
},
proto::Channel {
id: 4,
name: "y".to_string(),
parent_id: Some(2),
},
],
insert_edge: vec![
proto::ChannelEdge {
parent_id: 1,
channel_id: 3,
},
proto::ChannelEdge {
parent_id: 2,
channel_id: 4,
},
],
..Default::default()
@@ -86,17 +93,24 @@ fn test_dangling_channel_paths(cx: &mut AppContext) {
proto::Channel {
id: 0,
name: "a".to_string(),
parent_id: None,
},
proto::Channel {
id: 1,
name: "b".to_string(),
parent_id: Some(0),
},
proto::Channel {
id: 2,
name: "c".to_string(),
parent_id: Some(1),
},
],
insert_edge: vec![
proto::ChannelEdge {
parent_id: 0,
channel_id: 1,
},
proto::ChannelEdge {
parent_id: 1,
channel_id: 2,
},
],
channel_permissions: vec![proto::ChannelPermission {
@@ -145,7 +159,6 @@ async fn test_channel_messages(cx: &mut TestAppContext) {
channels: vec![proto::Channel {
id: channel_id,
name: "the-channel".to_string(),
parent_id: None,
}],
..Default::default()
});