diff --git a/src/common/meta/src/kv_backend/memory.rs b/src/common/meta/src/kv_backend/memory.rs index f07ac929fb93..dde70642e25c 100644 --- a/src/common/meta/src/kv_backend/memory.rs +++ b/src/common/meta/src/kv_backend/memory.rs @@ -13,10 +13,12 @@ // limitations under the License. use std::any::Any; +use std::borrow::BorrowMut; use std::collections::btree_map::Entry; use std::collections::BTreeMap; use std::fmt::{Display, Formatter}; use std::marker::PhantomData; +use std::ops::RangeBounds; use std::sync::RwLock; use async_trait::async_trait; @@ -93,13 +95,13 @@ impl KvBackend for MemoryKvBackend { let iter = kvs.range(range); let mut more = false; - let mut counter = 0usize; + let mut took = 0; let kvs = iter .take_while(|_| { - let take = counter != limit as usize || limit == 0; - counter += 1; - more = counter > limit as usize && limit != 0; + let take = limit == 0 || took != limit; + took += 1; + more = limit > 0 && took > limit; take }) @@ -216,25 +218,11 @@ impl KvBackend for MemoryKvBackend { let mut kvs = self.kvs.write().unwrap(); - let keys = kvs - .range(range) - .map(|(key, _)| key.clone()) + let prev_kvs = kvs + .extract_if(|key, _| range.contains(key)) + .map(Into::into) .collect::>(); - let mut prev_kvs = if prev_kv { - Vec::with_capacity(keys.len()) - } else { - vec![] - }; - - for key in keys { - if let Some(value) = kvs.remove(&key) { - if prev_kv { - prev_kvs.push((key.clone(), value).into()) - } - } - } - Ok(DeleteRangeResponse { deleted: prev_kvs.len() as i64, prev_kvs: if prev_kv { prev_kvs } else { vec![] }, diff --git a/src/log-store/src/raft_engine/backend.rs b/src/log-store/src/raft_engine/backend.rs index 187bce847b42..99fb72f28b2e 100644 --- a/src/log-store/src/raft_engine/backend.rs +++ b/src/log-store/src/raft_engine/backend.rs @@ -145,9 +145,7 @@ impl KvBackend for RaftEngineBackend { } = req; let (start_key, end_key) = match (start, end) { - (Included(start), Included(_)) => { - (Some(start.clone()), Some(get_next_prefix_key(&start))) - } + (Included(start), Included(end)) => (Some(start), Some(get_next_prefix_key(&end))), (Unbounded, Unbounded) => (None, None), (Included(start), Excluded(end)) => (Some(start), Some(end)), (Included(start), Unbounded) => (Some(start), None),