Skip to content
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

feat: add clear methods #478

Merged
merged 3 commits into from
Sep 25, 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
65 changes: 62 additions & 3 deletions crates/loro-internal/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
diff::{myers_diff, DiffHandler, OperateProxy},
event::{Diff, TextDiffItem},
op::ListSlice,
state::{ContainerState, IndexType, State, TreeParentId},
state::{IndexType, State, TreeParentId},
txn::EventHint,
utils::{string_slice::StringSlice, utf16::count_utf16_len},
};
Expand Down Expand Up @@ -815,6 +815,7 @@ impl HandlerTrait for ListHandler {
}
}
}

#[derive(Clone)]
pub struct UnknownHandler {
inner: BasicHandler,
Expand Down Expand Up @@ -2717,6 +2718,21 @@ impl ListHandler {
MaybeDetached::Attached(a) => a.is_deleted(),
}
}

pub fn clear(&self) -> LoroResult<()> {
match &self.inner {
MaybeDetached::Detached(l) => {
let mut l = l.try_lock().unwrap();
l.value.clear();
Ok(())
}
MaybeDetached::Attached(a) => a.with_txn(|txn| self.clear_with_txn(txn)),
}
}

pub fn clear_with_txn(&self, txn: &mut Transaction) -> LoroResult<()> {
self.delete_with_txn(txn, 0, self.len())
}
}

impl MovableListHandler {
Expand Down Expand Up @@ -3234,8 +3250,8 @@ impl MovableListHandler {
let a = state.as_movable_list_state().unwrap();
match a.get(index, IndexType::ForUser) {
Some(v) => {
if let LoroValue::Container(id) = v {
Some(ValueOrHandler::Handler(create_handler(m, id.clone())))
if let LoroValue::Container(c) = v {
Some(ValueOrHandler::Handler(create_handler(m, c.clone())))
} else {
Some(ValueOrHandler::Value(v.clone()))
}
Expand Down Expand Up @@ -3356,6 +3372,21 @@ impl MovableListHandler {
MaybeDetached::Attached(a) => a.is_deleted(),
}
}

pub fn clear(&self) -> LoroResult<()> {
match &self.inner {
MaybeDetached::Detached(d) => {
let mut d = d.lock().unwrap();
d.value.clear();
Ok(())
}
MaybeDetached::Attached(a) => a.with_txn(|txn| self.clear_with_txn(txn)),
}
}

pub fn clear_with_txn(&self, txn: &mut Transaction) -> LoroResult<()> {
self.delete_with_txn(txn, 0, self.len())
}
}

impl MapHandler {
Expand Down Expand Up @@ -3665,6 +3696,34 @@ impl MapHandler {
MaybeDetached::Attached(a) => a.is_deleted(),
}
}

pub fn clear(&self) -> LoroResult<()> {
match &self.inner {
MaybeDetached::Detached(m) => {
let mut m = m.try_lock().unwrap();
m.value.clear();
Ok(())
}
MaybeDetached::Attached(a) => a.with_txn(|txn| self.clear_with_txn(txn)),
}
}

pub fn clear_with_txn(&self, txn: &mut Transaction) -> LoroResult<()> {
let keys: Vec<InternalString> = self.inner.try_attached_state()?.with_state(|state| {
state
.as_map_state()
.unwrap()
.iter()
.map(|(k, _)| k.clone())
.collect()
});

for key in keys {
self.delete_with_txn(txn, &key)?;
}

Ok(())
}
}

#[inline(always)]
Expand Down
18 changes: 18 additions & 0 deletions crates/loro-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,12 @@ impl LoroMap {
};
handler_to_js_value(Handler::Map(h), self.doc.clone()).into()
}

/// Delete all key-value pairs in the map.
pub fn clear(&self) -> JsResult<()> {
self.handler.clear()?;
Ok(())
}
}

impl Default for LoroMap {
Expand Down Expand Up @@ -2743,6 +2749,12 @@ impl LoroList {
Ok(None)
}
}

/// Delete all elements in the list.
pub fn clear(&self) -> JsResult<()> {
self.handler.clear()?;
Ok(())
}
}

impl Default for LoroList {
Expand Down Expand Up @@ -3115,6 +3127,12 @@ impl LoroMovableList {
v.into()
}))
}

/// Delete all elements in the list.
pub fn clear(&self) -> JsResult<()> {
self.handler.clear()?;
Ok(())
}
}

/// The handler of a tree(forest) container.
Expand Down
15 changes: 15 additions & 0 deletions crates/loro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,11 @@ impl LoroList {
pub fn to_vec(&self) -> Vec<LoroValue> {
Arc::unwrap_or_clone(self.get_value().into_list().unwrap())
}

/// Delete all elements in the list.
pub fn clear(&self) -> LoroResult<()> {
self.handler.clear()
}
}

impl Default for LoroList {
Expand Down Expand Up @@ -1229,6 +1234,11 @@ impl LoroMap {
.get_or_create_container(key, child.to_handler())?,
))
}

/// Delete all key-value pairs in the map.
pub fn clear(&self) -> LoroResult<()> {
self.handler.clear()
}
}

impl Default for LoroMap {
Expand Down Expand Up @@ -2097,6 +2107,11 @@ impl LoroMovableList {
pub fn to_vec(&self) -> Vec<LoroValue> {
Arc::unwrap_or_clone(self.get_value().into_list().unwrap())
}

/// Delete all elements in the list.
pub fn clear(&self) -> LoroResult<()> {
self.handler.clear()
}
}

impl Default for LoroMovableList {
Expand Down
Loading