zed/crates/channel/src/channel_store/channel_index.rs

167 lines
4.8 KiB
Rust
Raw Normal View History

use std::{ops::Deref, sync::Arc};
use collections::HashMap;
use rpc::proto;
use serde_derive::{Deserialize, Serialize};
use crate::{Channel, ChannelId};
pub type ChannelsById = HashMap<ChannelId, Arc<Channel>>;
2023-09-09 20:24:04 +00:00
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
pub struct ChannelPath(Arc<[ChannelId]>);
impl Deref for ChannelPath {
type Target = [ChannelId];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl ChannelPath {
pub fn parent_id(&self) -> Option<ChannelId> {
self.0.len().checked_sub(2).map(|i| self.0[i])
2023-09-09 20:24:04 +00:00
}
}
impl Default for ChannelPath {
fn default() -> Self {
ChannelPath(Arc::from([]))
}
}
#[derive(Default, Debug)]
pub struct ChannelIndex {
paths: Vec<ChannelPath>,
channels_by_id: ChannelsById,
}
impl ChannelIndex {
pub fn by_id(&self) -> &ChannelsById {
&self.channels_by_id
}
pub fn clear(&mut self) {
self.paths.clear();
self.channels_by_id.clear();
}
/// Delete the given channels from this index.
pub fn delete_channels(&mut self, channels: &[ChannelId]) {
self.channels_by_id
.retain(|channel_id, _| !channels.contains(channel_id));
self.paths.retain(|path| {
path.iter()
.all(|channel_id| self.channels_by_id.contains_key(channel_id))
});
2023-09-09 19:10:18 +00:00
}
pub fn bulk_edit(&mut self) -> ChannelPathsEditGuard {
ChannelPathsEditGuard {
paths: &mut self.paths,
channels_by_id: &mut self.channels_by_id,
}
2023-09-09 19:10:18 +00:00
}
}
impl Deref for ChannelIndex {
type Target = [ChannelPath];
2023-09-09 19:10:18 +00:00
fn deref(&self) -> &Self::Target {
&self.paths
2023-09-09 19:10:18 +00:00
}
}
/// A guard for ensuring that the paths index maintains its sort and uniqueness
/// invariants after a series of insertions
pub struct ChannelPathsEditGuard<'a> {
paths: &'a mut Vec<ChannelPath>,
channels_by_id: &'a mut ChannelsById,
}
2023-09-09 19:10:18 +00:00
impl<'a> ChannelPathsEditGuard<'a> {
/// Remove the given edge from this index. This will not remove the channel.
/// If this operation would result in a dangling edge, re-insert it.
pub fn delete_edge(&mut self, parent_id: ChannelId, channel_id: ChannelId) {
self.paths.retain(|path| {
!path
.windows(2)
.any(|window| window == [parent_id, channel_id])
});
// Ensure that there is at least one channel path in the index
if !self
.paths
.iter()
.any(|path| path.iter().any(|id| id == &channel_id))
{
self.insert_root(channel_id);
}
}
2023-09-09 01:47:59 +00:00
pub fn upsert(&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;
2023-09-09 01:47:59 +00:00
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);
}
}
}
2023-09-09 01:47:59 +00:00
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;
while ix < self.paths.len() {
let path = &self.paths[ix];
if path.ends_with(&[parent_id]) {
2023-09-09 01:47:59 +00:00
let mut new_path = path.to_vec();
new_path.push(channel_id);
2023-09-09 20:24:04 +00:00
self.paths.insert(ix + 1, ChannelPath(new_path.into()));
ix += 2;
} else if path.get(0) == Some(&channel_id) {
// Clear out any paths that have this chahnnel as their root
2023-09-15 18:06:15 +00:00
self.paths.swap_remove(ix);
} else {
ix += 1;
}
}
}
2023-09-09 01:47:59 +00:00
fn insert_root(&mut self, channel_id: ChannelId) {
2023-09-09 20:24:04 +00:00
self.paths.push(ChannelPath(Arc::from([channel_id])));
}
}
impl<'a> Drop for ChannelPathsEditGuard<'a> {
fn drop(&mut self) {
self.paths.sort_by(|a, b| {
let a = channel_path_sorting_key(a, &self.channels_by_id);
let b = channel_path_sorting_key(b, &self.channels_by_id);
a.cmp(b)
});
self.paths.dedup();
}
}
fn channel_path_sorting_key<'a>(
path: &'a [ChannelId],
channels_by_id: &'a ChannelsById,
) -> impl 'a + Iterator<Item = Option<&'a str>> {
path.iter()
.map(|id| Some(channels_by_id.get(id)?.name.as_str()))
}