Skip to content

Commit

Permalink
refactor(xlineapi): introduce BytesAffineRef and KeyRangeRef
Browse files Browse the repository at this point in the history
Signed-off-by: lxl66566 <lxl66566@gmail.com>
  • Loading branch information
lxl66566 committed Nov 4, 2024
1 parent bdd59e4 commit 3024155
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 60 deletions.
32 changes: 16 additions & 16 deletions crates/xline/src/storage/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use crossbeam_skiplist::{map::Entry, SkipMap};
use itertools::Itertools;
use parking_lot::{Mutex, RwLock};
use utils::parking_lot_lock::RwLockMap;
use xlineapi::keyrange::{BytesAffine, EtcdKeyRange, KeyRange, StdBoundRange};
use xlineapi::keyrange::{BytesAffine, EtcdKeyRange, KeyRange, KeyRangeRef, StdBoundRange};

use super::revision::{KeyRevision, Revision};

/// Operations for `Index`
pub(crate) trait IndexOperate {
/// Get `Revision` of keys, get the latest `Revision` when revision <= 0
fn get(&self, keyrange: KeyRange, revision: i64) -> Vec<Revision>;
fn get(&self, keyrange: KeyRangeRef, revision: i64) -> Vec<Revision>;

/// Register a new `KeyRevision` of the given key
///
Expand Down Expand Up @@ -258,22 +258,22 @@ where
}

impl IndexOperate for Index {
fn get(&self, keyrange: KeyRange, revision: i64) -> Vec<Revision> {
fn get(&self, keyrange: KeyRangeRef, revision: i64) -> Vec<Revision> {
match keyrange {
KeyRange::OneKey(key) => self
KeyRangeRef::OneKey(key) => self
.inner
.get(&key)
.get(key)
.and_then(fmap_value(|revs| Index::get_revision(revs, revision)))
.map(|rev| vec![rev])
.unwrap_or_default(),
KeyRange::Range(_) if keyrange.is_all_keys() => self
KeyRangeRef::Range(_) if keyrange.is_all_keys() => self
.inner
.iter()
.filter_map(fmap_value(|revs| Index::get_revision(revs, revision)))
.collect(),
KeyRange::Range(_) => self
KeyRangeRef::Range(_) => self
.inner
.range(keyrange)
.range(keyrange.to_owned())
.filter_map(fmap_value(|revs| Index::get_revision(revs, revision)))
.collect(),
}
Expand Down Expand Up @@ -504,20 +504,20 @@ impl IndexState<'_> {
}

impl IndexOperate for IndexState<'_> {
fn get(&self, keyrange: KeyRange, revision: i64) -> Vec<Revision> {
fn get(&self, keyrange: KeyRangeRef, revision: i64) -> Vec<Revision> {
match keyrange {
KeyRange::OneKey(key) => {
Index::get_revision(&self.one_key_revisions(&key, &self.state.lock()), revision)
KeyRangeRef::OneKey(key) => {
Index::get_revision(&self.one_key_revisions(key, &self.state.lock()), revision)
.map(|rev| vec![rev])
.unwrap_or_default()
}
KeyRange::Range(_) if keyrange.is_all_keys() => self
KeyRangeRef::Range(_) if keyrange.is_all_keys() => self
.all_key_revisions()
.into_iter()
.filter_map(|(_, revs)| Index::get_revision(revs.as_ref(), revision))
.collect(),
KeyRange::Range(_) => self
.range_key_revisions(keyrange)
KeyRangeRef::Range(_) => self
.range_key_revisions(keyrange.to_owned())
.into_iter()
.filter_map(|(_, revs)| Index::get_revision(revs.as_ref(), revision))
.collect(),
Expand Down Expand Up @@ -674,11 +674,11 @@ mod test {
let index = init_and_test_insert();
let txn = index.state();
assert_eq!(
txn.get(KeyRange::new_one_key("key"), 0),
txn.get(KeyRange::new_one_key("key").to_ref(), 0),
vec![Revision::new(3, 1)]
);
assert_eq!(
txn.get(KeyRange::new_one_key("key"), 1),
txn.get(KeyRange::new_one_key("key").to_ref(), 1),
vec![Revision::new(1, 3)]
);
txn.commit();
Expand Down
32 changes: 16 additions & 16 deletions crates/xline/src/storage/kv_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use utils::table_names::{KV_TABLE, META_TABLE};
use xlineapi::{
command::{CommandResponse, SyncResponse},
execute_error::ExecuteError,
keyrange::KeyRange,
keyrange::{KeyRange, KeyRangeRef},
};

use super::{
Expand Down Expand Up @@ -109,7 +109,7 @@ impl KvStoreInner {
fn get_range<T>(
txn_db: &T,
index: &dyn IndexOperate,
keyrange: KeyRange,
keyrange: KeyRangeRef,
revision: i64,
) -> Result<Vec<KeyValue>, ExecuteError>
where
Expand All @@ -124,7 +124,7 @@ impl KvStoreInner {
fn get_range_with_opts<T>(
txn_db: &T,
index: &dyn IndexOperate,
keyrange: KeyRange,
keyrange: KeyRangeRef,
revision: i64,
limit: usize,
count_only: bool,
Expand All @@ -149,7 +149,7 @@ impl KvStoreInner {
Self::get_range(
self.db.as_ref(),
self.index.as_ref(),
KeyRange::OneKey(kv.key.clone()),
KeyRangeRef::OneKey(&kv.key),
kv.mod_revision.overflow_sub(1),
)
.ok()?
Expand Down Expand Up @@ -473,7 +473,7 @@ impl KvStore {
let kvs = KvStoreInner::get_range(
txn_db,
index,
KeyRange::new_etcd(cmp.key.clone(), cmp.range_end.clone()),
KeyRangeRef::new_etcd(&cmp.key, &cmp.range_end),
0,
)
.unwrap_or_default();
Expand Down Expand Up @@ -628,7 +628,7 @@ impl KvStore {
let (mut kvs, total) = KvStoreInner::get_range_with_opts(
tnx_db,
index,
KeyRange::new_etcd(req.key.clone(), req.range_end.clone()),
KeyRangeRef::new_etcd(&req.key, &req.range_end),
req.revision,
storage_fetch_limit.numeric_cast(),
req.count_only,
Expand Down Expand Up @@ -767,7 +767,7 @@ impl KvStore {
let prev_kvs = KvStoreInner::get_range(
txn_db,
index,
KeyRange::new_etcd(req.key.clone(), req.range_end.clone()),
KeyRangeRef::new_etcd(&req.key, &req.range_end),
0,
)?;
let mut response = DeleteRangeResponse {
Expand Down Expand Up @@ -1692,14 +1692,14 @@ mod test {
let txn_db = store.inner.db.transaction();
let index = store.inner.index.state();
assert_eq!(
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a"), 2)
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a").to_ref(), 2)
.unwrap()
.len(),
1,
"(a, 1) should not be removed"
);
assert_eq!(
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("b"), 3)
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("b").to_ref(), 3)
.unwrap()
.len(),
1,
Expand All @@ -1709,20 +1709,20 @@ mod test {
let target_revisions = index_compact(&store, 4);
store.compact(target_revisions.as_ref())?;
assert!(
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a"), 2)
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a").to_ref(), 2)
.unwrap()
.is_empty(),
"(a, 1) should be removed"
);
assert_eq!(
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("b"), 3)
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("b").to_ref(), 3)
.unwrap()
.len(),
1,
"(b, 2) should not be removed"
);
assert_eq!(
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a"), 4)
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a").to_ref(), 4)
.unwrap()
.len(),
1,
Expand All @@ -1732,26 +1732,26 @@ mod test {
let target_revisions = index_compact(&store, 5);
store.compact(target_revisions.as_ref())?;
assert!(
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a"), 2)
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a").to_ref(), 2)
.unwrap()
.is_empty(),
"(a, 1) should be removed"
);
assert_eq!(
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("b"), 3)
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("b").to_ref(), 3)
.unwrap()
.len(),
1,
"(b, 2) should not be removed"
);
assert!(
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a"), 4)
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a").to_ref(), 4)
.unwrap()
.is_empty(),
"(a, 3) should be removed"
);
assert!(
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a"), 5)
KvStoreInner::get_range(&txn_db, &index, KeyRange::new_one_key("a").to_ref(), 5)
.unwrap()
.is_empty(),
"(a, 4) should be removed"
Expand Down
Loading

0 comments on commit 3024155

Please sign in to comment.