Skip to content

Commit

Permalink
chore: make map on YType required
Browse files Browse the repository at this point in the history
  • Loading branch information
forehalo authored and darkskygit committed Sep 1, 2023
1 parent 4312e2d commit 0710cda
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 56 deletions.
4 changes: 3 additions & 1 deletion y-octo/src/doc/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use crate::sync::{Arc, RwLock};
/// [DocOptions] used to create a new [Doc]
///
/// ```
/// use y_octo::DocOptions;
///
/// let doc = DocOptions::new()
/// .with_client_id(1)
/// .with_guid("guid".into())
/// .auto_gc(true)
/// .build();
///
/// assert!(doc.guid(), "guid")
/// assert_eq!(doc.guid(), "guid")
/// ```
#[derive(Clone, Debug)]
pub struct DocOptions {
Expand Down
37 changes: 12 additions & 25 deletions y-octo/src/doc/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,7 @@ impl DocStore {
let mut conflict = if let Some(left) = left.get() {
left.right.as_ref().into()
} else if let Some(parent_sub) = &this.parent_sub {
parent
.map
.as_ref()
.and_then(|m| m.get(parent_sub).cloned())
.unwrap_or(Somr::none())
parent.map.get(parent_sub).cloned().unwrap_or(Somr::none())
} else {
parent.start.clone()
};
Expand Down Expand Up @@ -495,12 +491,7 @@ impl DocStore {
} else {
// no left, parent.start = this
right = if let Some(parent_sub) = &this.parent_sub {
parent
.map
.as_ref()
.and_then(|map| map.get(parent_sub))
.map(|n| Node::Item(n.clone()).head())
.into()
parent.map.get(parent_sub).map(|n| Node::Item(n.clone()).head()).into()
} else {
mem::replace(&mut parent.start, item.clone())
};
Expand All @@ -518,10 +509,7 @@ impl DocStore {
} else {
// no right, parent.start = this, delete this.left
if let Some(parent_sub) = &this.parent_sub {
parent
.map
.get_or_insert(HashMap::default())
.insert(parent_sub.to_string(), item.clone());
parent.map.insert(parent_sub.to_string(), item.clone());

if let Some(left) = &this.left {
self.delete_node(left, Some(parent));
Expand Down Expand Up @@ -592,11 +580,10 @@ impl DocStore {
}
}

if let Some(map) = ty.map.take() {
for (_, item) in map {
if let Some(item) = item.get() {
Self::delete_item_inner(delete_set, item, Some(&mut ty));
}
let map_values = ty.map.values().cloned().collect::<Vec<_>>();
for item in map_values {
if let Some(item) = item.get() {
Self::delete_item_inner(delete_set, item, Some(&mut ty));
}
}
}
Expand Down Expand Up @@ -841,13 +828,13 @@ impl DocStore {
}
ty.start = Somr::none();

if let Some(map) = ty.map.take() {
for (_, item) in map {
if let Some(item) = item.get() {
Self::gc_item_by_id(items, item.id, true)?;
}
for (_, item) in &ty.map {
if let Some(item) = item.get() {
Self::gc_item_by_id(items, item.id, true)?;
}
}

ty.map.clear();
}
}

Expand Down
6 changes: 6 additions & 0 deletions y-octo/src/doc/types/list/search_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ impl DerefMut for MarkerList {
}
}

impl Default for MarkerList {
fn default() -> Self {
Self::new()
}
}

impl MarkerList {
pub fn new() -> Self {
MarkerList(RefCell::new(VecDeque::new()))
Expand Down
48 changes: 20 additions & 28 deletions y-octo/src/doc/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl_type!(Map);
pub(crate) trait MapType: AsInner<Inner = YTypeRef> {
fn insert(&mut self, key: impl AsRef<str>, value: impl Into<Content>) -> JwstCodecResult {
if let Some((mut store, mut ty)) = self.as_inner().write() {
let left = ty.map.as_ref().and_then(|map| map.get(key.as_ref())).cloned();
let left = ty.map.get(key.as_ref()).cloned();

let item = store.create_item(
value.into(),
Expand All @@ -30,7 +30,7 @@ pub(crate) trait MapType: AsInner<Inner = YTypeRef> {

fn keys(&self) -> Vec<String> {
if let Some(ty) = self.as_inner().ty() {
ty.map.as_ref().map_or(Vec::new(), |map| map.keys().cloned().collect())
ty.map.keys().cloned().collect()
} else {
vec![]
}
Expand All @@ -39,8 +39,7 @@ pub(crate) trait MapType: AsInner<Inner = YTypeRef> {
fn get(&self, key: impl AsRef<str>) -> Option<Arc<Content>> {
self.as_inner().ty().and_then(|ty| {
ty.map
.as_ref()
.and_then(|map| map.get(key.as_ref()))
.get(key.as_ref())
.filter(|item| item.get().map(|item| !item.deleted()).unwrap_or(false))
.map(|item| item.get().unwrap().content.clone())
})
Expand All @@ -50,11 +49,9 @@ pub(crate) trait MapType: AsInner<Inner = YTypeRef> {
let mut ret = HashMap::default();

if let Some(ty) = self.as_inner().ty() {
if let Some(map) = ty.map.as_ref() {
for key in map.keys() {
if let Some(content) = self.get(key) {
ret.insert(key.clone(), content);
}
for key in ty.map.keys() {
if let Some(content) = self.get(key) {
ret.insert(key.clone(), content);
}
}
}
Expand All @@ -65,8 +62,7 @@ pub(crate) trait MapType: AsInner<Inner = YTypeRef> {
fn contains_key(&self, key: impl AsRef<str>) -> bool {
if let Some(ty) = self.as_inner().ty() {
ty.map
.as_ref()
.and_then(|map| map.get(key.as_ref()))
.get(key.as_ref())
.and_then(|item| item.get())
.map_or(false, |item| !item.deleted())
} else {
Expand All @@ -76,9 +72,9 @@ pub(crate) trait MapType: AsInner<Inner = YTypeRef> {

fn remove(&mut self, key: impl AsRef<str>) -> bool {
if let Some((mut store, mut ty)) = self.as_inner().write() {
let node = ty.map.as_ref().and_then(|map| map.get(key.as_ref()));
if let Some(node) = node {
if let Some(item) = node.clone().get() {
let item = ty.map.get(key.as_ref()).cloned();
if let Some(item) = item {
if let Some(item) = item.get() {
store.delete_item(item, Some(&mut ty));
return true;
}
Expand All @@ -92,27 +88,23 @@ pub(crate) trait MapType: AsInner<Inner = YTypeRef> {
self.as_inner()
.ty()
.map(|ty| {
ty.map.as_ref().map_or(0, |map| {
map.values()
.filter(|v| v.get().map(|item| !item.deleted()).unwrap_or(false))
.count()
}) as u64
ty.map
.values()
.filter(|v| v.get().map(|item| !item.deleted()).unwrap_or(false))
.count() as u64
})
.unwrap_or(0)
}

fn iter(&self) -> MapIterator {
let inner = self.as_inner().ty().unwrap();
let map = inner.map.as_ref().map(|map| {
map.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect::<Vec<(String, Somr<Item>)>>()
});
let map = inner
.map
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect::<Vec<(String, Somr<Item>)>>();

MapIterator {
nodes: map.unwrap_or_default(),
index: 0,
}
MapIterator { nodes: map, index: 0 }
}
}

Expand Down
4 changes: 2 additions & 2 deletions y-octo/src/doc/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
pub(crate) struct YType {
pub start: Somr<Item>,
pub item: Somr<Item>,
pub map: Option<HashMap<String, Somr<Item>>>,
pub map: HashMap<String, Somr<Item>>,
pub len: u64,
/// The tag name of XMLElement and XMLHook type
pub name: Option<String>,
Expand All @@ -46,7 +46,7 @@ impl PartialEq for YType {
fn eq(&self, other: &Self) -> bool {
self.root_name == other.root_name
|| (self.start.is_some() && self.start == other.start)
|| (self.map.is_some() && self.map == other.map)
|| self.map == other.map
}
}

Expand Down

0 comments on commit 0710cda

Please sign in to comment.