8000 Channel History by roobscoob · Pull Request #33 · scopeclient/scope · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Channel History #33

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

Merged
merged 16 commits into from
Nov 19, 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
22 changes: 22 additions & 0 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[workspace]
resolver = "2"
members = [
"src/ui"
]
members = ["src/ui", "src/cache", "src/chat", "src/discord"]

[workspace.dependencies]
chrono = "0.4.38"
13 changes: 13 additions & 0 deletions src/cache/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "scope-backend-cache"
version = "0.1.0"
edition = "2021"

[dependencies]
gpui = { git = "https://github.com/huacnlee/zed.git", branch = "export-platform-window", default-features = false, features = [
"http_client",
"font-kit",
] }
rand = "0.8.5"
scope-chat = { version = "0.1.0", path = "../chat" }
tokio = "1.41.1"
83 changes: 83 additions & 0 deletions src/cache/src/async_list/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
pub mod refcache;
pub mod refcacheslice;
pub mod tests;

use std::collections::HashMap;

use refcache::CacheReferences;
use refcacheslice::Exists;
use scope_chat::async_list::{AsyncListIndex, AsyncListItem, AsyncListResult};

pub struct AsyncListCache<I: AsyncListItem> {
cache_refs: CacheReferences<I::Identifier>,
cache_map: HashMap<I::Identifier, I>,
}

impl<I: AsyncListItem> Default for AsyncListCache<I> {
fn default() -> Self {
Self::new()
}
}

impl<I: AsyncListItem> AsyncListCache<I> {
pub fn new() -> Self {
Self {
cache_refs: CacheReferences::new(),
cache_map: HashMap::new(),
}
}

pub fn append_bottom(&mut self, value: I) {
let identifier = value.get_list_identifier();

self.cache_refs.append_bottom(identifier.clone());
self.cache_map.insert(identifier, value);
}

pub fn insert(&mut self, index: AsyncListIndex<I::Identifier>, value: I, is_top: bool, is_bottom: bool) {
let identifier = value.get_list_identifier();

self.cache_map.insert(identifier.clone(), value);
self.cache_refs.insert(index, identifier.clone(), is_top, is_bottom);
}

/// you mut **KNOW** that the item you are inserting is not:
/// - directly next to (Before or After) **any** item in the list
/// - the first or last item in the list
pub fn insert_detached(&mut self, value: I) {
let identifier = value.get_list_identifier();

self.cache_map.insert(identifier.clone(), value);
self.cache_refs.insert_detached(identifier);
}

pub fn bounded_at_top_by(&self) -> Option<I::Identifier> {
self.cache_refs.top_bound()
}

pub fn bounded_at_bottom_by(&self) -> Option<I::Identifier> {
self.cache_refs.bottom_bound()
}

pub fn get(&self, index: AsyncListIndex<I::Identifier>) -> Exists<AsyncListResult<I>> {
let cache_result = self.cache_refs.get(index.clone());

if let Exists::Yes(cache_result) = cache_result {
let content = self.cache_map.get(&cache_result).unwrap().clone();
let is_top = self.cache_refs.top_bound().map(|v| v == content.get_list_identifier()).unwrap_or(false);
let is_bottom = self.cache_refs.bottom_bound().map(|v| v == content.get_list_identifier()).unwrap_or(false);

return Exists::Yes(AsyncListResult { content, is_top, is_bottom });
};

if let Exists::No = cache_result {
return Exists::No;
}

Exists::Unknown
}

pub fn find(&self, identifier: &I::Identifier) -> Option<I> {
self.cache_map.get(identifier).cloned()
}
}
203 changes: 203 additions & 0 deletions src/cache/src/async_list/refcache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
use std::{collections::HashMap, fmt::Debug};

use scope_chat::async_list::AsyncListIndex;

use super::refcacheslice::{self, CacheReferencesSlice, Exists};

pub struct CacheReferences<I: Clone + Eq + PartialEq> {
// dense segments are unordered (spooky!) slices of content we do! know about.
// the u64 in the hashmap represents a kind of "segment identifier"
dense_segments: HashMap<u64, CacheReferencesSlice<I>>,

top_bounded_identifier: Option<u64>,
bottom_bounded_identifier: Option<u64>,
}

impl<I: Clone + Eq + PartialEq> Default for CacheReferences<I> {
fn default() -> Self {
Self::new()
}
}

impl<I: Clone + Eq + PartialEq> CacheReferences<I> {
pub fn new() -> Self {
Self {
dense_segments: HashMap::new(),
top_bounded_identifier: None,
bottom_bounded_identifier: None,
}
}

pub fn append_bottom(&mut self, identifier: I) {
let mut id = None;

for (segment_id, segment) in self.dense_segments.iter() {
if let Exists::Yes(_) = segment.get(AsyncListIndex::RelativeToBottom(0)) {
if id.is_some() {
panic!("There should only be one bottom bound segment");
}

id = Some(*segment_id)
}
}

if let Some(id) = id {
self.dense_segments.get_mut(&id).unwrap().append_bottom(identifier);
} else {
self.insert(AsyncListIndex::RelativeToBottom(0), identifier, false, true);
}
}

pub fn top_bound(&self) -> Option<I> {
let index = self.top_bounded_identifier?;
let top_bound = self.dense_segments.get(&index).unwrap();

assert!(top_bound.is_bounded_at_top);

Some(top_bound.item_references.first().unwrap().clone())
}

pub fn bottom_bound(&self) -> Option<I> {
let index = self.bottom_bounded_identifier?;
let bottom_bound = self.dense_segments.get(&index).unwrap();

assert!(bottom_bound.is_bounded_at_bottom);

Some(bottom_bound.item_references.last().unwrap().clone())
}

pub fn get(&self, index: AsyncListIndex<I>) -> Exists<I> {
for segment in self.dense_segments.values() {
let result = segment.get(index.clone());

if let Exists::Yes(value) = result {
return Exists::Yes(value);
} else if let Exists::No = result {
return Exists::No;
}
}

Exists::Unknown
}

/// you mut **KNOW** that the item you are inserting is not:
/// - directly next to (Before or After) **any** item in the list
/// - the first or last item in the list
pub fn insert_detached(&mut self, item: I) {
self.dense_segments.insert(
rand::random(),
CacheReferencesSlice {
is_bounded_at_top: false,
is_bounded_at_bottom: false,

item_references: vec![item],
},
);
}

pub fn insert(&mut self, index: AsyncListIndex<I>, item: I, is_top: bool, is_bottom: bool) {
// insert routine is really complex:
// an insert can "join" together 2 segments
// an insert can append to a segment
// or an insert can construct a new segment

let mut segments = vec![];

for (i, segment) in self.dense_segments.iter() {
if let Some(position) = segment.can_insert(index.clone()) {
segments.push((position, *i));
}
}

if segments.is_empty() {
let id = rand::random();

self.dense_segments.insert(
id,
CacheReferencesSlice {
is_bounded_at_top: is_top,
is_bounded_at_bottom: is_bottom,

item_references: vec![item],
},
);

if is_bottom {
self.bottom_bounded_identifier = Some(id);
}

if is_top {
self.top_bounded_identifier = Some(id);
}
} else if segments.len() == 1 {
self.dense_segments.get_mut(&segments[0].1).unwrap().insert(index.clone(), item, is_bottom, is_top);

if is_top {
self.top_bounded_identifier = Some(segments[0].1)
}
if is_bottom {
self.bottom_bounded_identifier = Some(segments[0].1)
}
} else if segments.len() == 2 {
assert!(!is_top);
assert!(!is_bottom);

let (li, ri) = match (segments[0], segments[1]) {
((refcacheslice::Position::After, lp), (refcacheslice::Position::Before, rp)) => (lp, rp),
((refcacheslice::Position::Before, rp), (refcacheslice::Position::After, lp)) => (lp, rp),

_ => panic!("How are there two candidates that aren't (Before, After) or (After, Before)?"),
};

let (left, right) = if li < ri {
let right = self.dense_segments.remove(&ri).unwrap();
let left = self.dense_segments.remove(&li).unwrap();

(left, right)
} else {
let left = self.dense_segments.remove(&li).unwrap();
let right = self.dense_segments.remove(&ri).unwrap();

(left, right)
};

let mut merged = left.item_references;

merged.push(item);

merged.extend(right.item_references);

let id = rand::random();

self.dense_segments.insert(
id,
CacheReferencesSlice {
is_bounded_at_top: left.is_bounded_at_top,
is_bounded_at_bottom: right.is_bounded_at_bottom,

item_references: merged,
},
);

if left.is_bounded_at_top {
self.top_bounded_identifier = Some(id);
}

if right.is_bounded_at_bottom {
self.bottom_bounded_identifier = Some(id);
}
} else {
panic!("Impossible state")
}
}
}

impl<I: Clone + Eq + PartialEq + Debug> Debug for CacheReferences<I> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CacheReferences")
.field("top_bounded_segment", &self.top_bounded_identifier)
.field("bottom_bounded_segment", &self.bottom_bounded_identifier)
.field("dense_segments", &self.dense_segments)
.finish()
}
}
Loading
0