Add move, link, and unlink operations

This commit is contained in:
Mikayla 2023-09-09 13:24:04 -07:00
parent 77cdbdb12a
commit 439f627d9a
No known key found for this signature in database
3 changed files with 275 additions and 69 deletions

View file

@ -146,7 +146,7 @@ impl ChannelStore {
})
}
pub fn channel_at_index(&self, ix: usize) -> Option<(&Arc<Channel>, &Arc<[ChannelId]>)> {
pub fn channel_at_index(&self, ix: usize) -> Option<(&Arc<Channel>, &ChannelPath)> {
let path = self.channel_index.get(ix)?;
let id = path.last().unwrap();
let channel = self.channel_for_id(*id).unwrap();

View file

@ -1,13 +1,38 @@
use std::sync::Arc;
use std::{sync::Arc, ops::Deref};
use collections::HashMap;
use rpc::proto;
use serde_derive::{Serialize, Deserialize};
use crate::{ChannelId, Channel};
pub type ChannelPath = Arc<[ChannelId]>;
pub type ChannelsById = HashMap<ChannelId, Arc<Channel>>;
#[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]
})
}
}
impl Default for ChannelPath {
fn default() -> Self {
ChannelPath(Arc::from([]))
}
}
#[derive(Default, Debug)]
pub struct ChannelIndex {
paths: Vec<ChannelPath>,
@ -99,7 +124,7 @@ impl<'a> ChannelPathsUpsertGuard<'a> {
if path.ends_with(&[parent_id]) {
let mut new_path = path.to_vec();
new_path.push(channel_id);
self.paths.insert(ix + 1, new_path.into());
self.paths.insert(ix + 1, ChannelPath(new_path.into()));
ix += 1;
}
ix += 1;
@ -107,7 +132,7 @@ impl<'a> ChannelPathsUpsertGuard<'a> {
}
fn insert_root(&mut self, channel_id: ChannelId) {
self.paths.push(Arc::from([channel_id]));
self.paths.push(ChannelPath(Arc::from([channel_id])));
}
}