Skip to content

Commit

Permalink
dev: move MapApiExt from trait to a method container struct, still
Browse files Browse the repository at this point in the history
lifetime error:

```
error: implementation of `MapApiRO` is not general enough
  --> src/meta/raft-store/src/sm_v002/sm_v002.rs:80:74
   |
80 |       async fn get_kv(&self, key: &str) -> Result<GetKVReply, Self::Error> {
   |  __________________________________________________________________________^
81 | |         let got = self.sm.get_kv(key).await;
82 | |
83 | |         let local_now_ms = SeqV::<()>::now_ms();
84 | |         let got = Self::non_expired(got, local_now_ms);
85 | |         Ok(got)
86 | |     }
   | |_____^ implementation of `MapApiRO` is not general enough
   |
   = note: `MapApiRO<'1, std::string::String>` would have to be implemented for the type `&'0 LevelData`, for any two lifetimes `'0` and `'1`...
   = note: ...but `MapApiRO<'2, std::string::String>` is actually implemented for the type `&'2 LevelData`, for some specific lifetime `'2`
```
  • Loading branch information
drmingdrmer committed Sep 27, 2023
1 parent 19c53ad commit cb526f5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 74 deletions.
6 changes: 4 additions & 2 deletions src/meta/raft-store/src/sm_v002/leveled_store/leveled_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl<'d> LeveledRef<'d> {
impl<'d, K> MapApiRO<'d, K> for LeveledRef<'d>
where
K: MapKey + fmt::Debug,
// &'d LevelData: MapApiRO<'d, K>,
for<'him> &'him LevelData: MapApiRO<'him, K>,
{
type GetFut<'f, Q> = impl Future<Output = Marked<K::V>> + 'f
Expand Down Expand Up @@ -110,12 +111,13 @@ where
{
// TODO: &LeveledRef use LeveledRef

let levels = self.iter_levels();
// let levels = self.iter_levels();

async move {
let mut km = KMerge::by(util::by_key_seq);

for api in levels {
// for api in levels {
for api in self.iter_levels() {
let a = api.range(range.clone()).await;
km = km.merge(a);
}
Expand Down
95 changes: 27 additions & 68 deletions src/meta/raft-store/src/sm_v002/leveled_store/map_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,93 +112,52 @@ where K: MapKey
fn set<'f>(self, key: K, value: Option<(K::V, Option<KVMeta>)>) -> Self::SetFut<'f>;
}

#[async_trait::async_trait]
pub(in crate::sm_v002) trait MapApiExt<'d, K>
where K: MapKey
{
// TODO:
pub(in crate::sm_v002) struct MapApiExt;

impl MapApiExt {
/// Update only the meta associated to an entry and keeps the value unchanged.
/// If the entry does not exist, nothing is done.
async fn update_meta(self, key: K, meta: Option<KVMeta>) -> (Marked<K::V>, Marked<K::V>);

// TODO:
/// Update only the value and keeps the meta unchanged.
/// If the entry does not exist, create one.
async fn upsert_value(&mut self, key: K, value: K::V) -> (Marked<K::V>, Marked<K::V>);
}

/// Update only the meta associated to an entry and keeps the value unchanged.
/// If the entry does not exist, nothing is done.
async fn update_meta<'d, K, T>(
s: &'d mut T,
key: K,
meta: Option<KVMeta>,
) -> (Marked<K::V>, Marked<K::V>)
where
K: MapKey,
&'d mut T: MapApi<'d, K>,
{
//
let got = s.to_ro().get(&key).await;
if got.is_tomb_stone() {
return (got.clone(), got.clone());
}

// Safe unwrap(), got is Normal
let (v, _) = got.unpack_ref().unwrap();

s.set(key, Some((v.clone(), meta))).await
}

/// Update only the value and keeps the meta unchanged.
/// If the entry does not exist, create one.
async fn upsert_value<'d, K, T>(s: &'d mut T, key: K, value: K::V) -> (Marked<K::V>, Marked<K::V>)
where
K: MapKey,
&'d mut T: MapApi<'d, K>,
{
let got = s.to_ro().get(&key).await;

let meta = if let Some((_, meta)) = got.unpack_ref() {
meta
} else {
None
};

s.set(key, Some((value, meta.cloned()))).await
}

#[async_trait::async_trait]
impl<'d, K, T> MapApiExt<'d, K> for &'d mut T
where
&'d mut T: MapApi<'d, K>,
K: MapKey,
{
// Not work: it requires MapApiRO::GetFut to be Send, but adding `Send` to MapApiRO::GetFut cause an compilation error:
// https://github.com/rust-lang/rust/issues/100013
async fn update_meta(self, key: K, meta: Option<KVMeta>) -> (Marked<K::V>, Marked<K::V>) {
pub(in crate::sm_v002) async fn update_meta<'d, K, T>(
s: &'d mut T,
key: K,
meta: Option<KVMeta>,
) -> (Marked<K::V>, Marked<K::V>)
where
K: MapKey,
&'d mut T: MapApi<'d, K>,
{
//
let got = self.to_ro().get(&key).await;
let got = s.to_ro().get(&key).await;
if got.is_tomb_stone() {
return (got.clone(), got.clone());
}

// Safe unwrap(), got is Normal
let (v, _) = got.unpack_ref().unwrap();

self.set(key, Some((v.clone(), meta))).await
s.set(key, Some((v.clone(), meta))).await
}

async fn upsert_value(&mut self, key: K, value: K::V) -> (Marked<K::V>, Marked<K::V>) {
let got = self.get(&key).await;
/// Update only the value and keeps the meta unchanged.
/// If the entry does not exist, create one.
pub(in crate::sm_v002) async fn upsert_value<'d, K, T>(
s: &'d mut T,
key: K,
value: K::V,
) -> (Marked<K::V>, Marked<K::V>)
where
K: MapKey,
&'d mut T: MapApi<'d, K>,
{
let got = s.to_ro().get(&key).await;

let meta = if let Some((_, meta)) = got.unpack_ref() {
meta
} else {
None
};

self.set(key, Some((value, meta.cloned()))).await
s.set(key, Some((value, meta.cloned()))).await
}
}

Expand Down
13 changes: 9 additions & 4 deletions src/meta/raft-store/src/sm_v002/sm_v002.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ impl SMV002 {
///
/// It does not check expiration of the returned entry.
pub async fn get_kv(&self, key: &str) -> Option<SeqV> {
let got = MapApiRO::<String>::get(&self.levels, key).await;
let got = MapApiRO::<String>::get(self.levels.leveled_ref(), key).await;
// let got = self.levels.leveled_ref().get(key).await;
// let got = MapApiRO::<String>::get(&self.levels, key).await;
Into::<Option<SeqV>>::into(got)
}

Expand Down Expand Up @@ -408,9 +410,12 @@ impl SMV002 {
}
Operation::Delete => self.levels.set(upsert_kv.key.clone(), None).await,
Operation::AsIs => {
self.levels
.update_meta(upsert_kv.key.clone(), upsert_kv.value_meta.clone())
.await
MapApiExt::update_meta(
&mut self.levels,
upsert_kv.key.clone(),
upsert_kv.value_meta.clone(),
)
.await
}
};

Expand Down

0 comments on commit cb526f5

Please sign in to comment.