Skip to content

Commit cb526f5

Browse files
committed
dev: move MapApiExt from trait to a method container struct, still
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` ```
1 parent 19c53ad commit cb526f5

File tree

3 files changed

+40
-74
lines changed

3 files changed

+40
-74
lines changed

src/meta/raft-store/src/sm_v002/leveled_store/leveled_map.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ impl<'d> LeveledRef<'d> {
6161
impl<'d, K> MapApiRO<'d, K> for LeveledRef<'d>
6262
where
6363
K: MapKey + fmt::Debug,
64+
// &'d LevelData: MapApiRO<'d, K>,
6465
for<'him> &'him LevelData: MapApiRO<'him, K>,
6566
{
6667
type GetFut<'f, Q> = impl Future<Output = Marked<K::V>> + 'f
@@ -110,12 +111,13 @@ where
110111
{
111112
// TODO: &LeveledRef use LeveledRef
112113

113-
let levels = self.iter_levels();
114+
// let levels = self.iter_levels();
114115

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

118-
for api in levels {
119+
// for api in levels {
120+
for api in self.iter_levels() {
119121
let a = api.range(range.clone()).await;
120122
km = km.merge(a);
121123
}

src/meta/raft-store/src/sm_v002/leveled_store/map_api.rs

Lines changed: 27 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -112,93 +112,52 @@ where K: MapKey
112112
fn set<'f>(self, key: K, value: Option<(K::V, Option<KVMeta>)>) -> Self::SetFut<'f>;
113113
}
114114

115-
#[async_trait::async_trait]
116-
pub(in crate::sm_v002) trait MapApiExt<'d, K>
117-
where K: MapKey
118-
{
119-
// TODO:
115+
pub(in crate::sm_v002) struct MapApiExt;
116+
117+
impl MapApiExt {
120118
/// Update only the meta associated to an entry and keeps the value unchanged.
121119
/// If the entry does not exist, nothing is done.
122-
async fn update_meta(self, key: K, meta: Option<KVMeta>) -> (Marked<K::V>, Marked<K::V>);
123-
124-
// TODO:
125-
/// Update only the value and keeps the meta unchanged.
126-
/// If the entry does not exist, create one.
127-
async fn upsert_value(&mut self, key: K, value: K::V) -> (Marked<K::V>, Marked<K::V>);
128-
}
129-
130-
/// Update only the meta associated to an entry and keeps the value unchanged.
131-
/// If the entry does not exist, nothing is done.
132-
async fn update_meta<'d, K, T>(
133-
s: &'d mut T,
134-
key: K,
135-
meta: Option<KVMeta>,
136-
) -> (Marked<K::V>, Marked<K::V>)
137-
where
138-
K: MapKey,
139-
&'d mut T: MapApi<'d, K>,
140-
{
141-
//
142-
let got = s.to_ro().get(&key).await;
143-
if got.is_tomb_stone() {
144-
return (got.clone(), got.clone());
145-
}
146-
147-
// Safe unwrap(), got is Normal
148-
let (v, _) = got.unpack_ref().unwrap();
149-
150-
s.set(key, Some((v.clone(), meta))).await
151-
}
152-
153-
/// Update only the value and keeps the meta unchanged.
154-
/// If the entry does not exist, create one.
155-
async fn upsert_value<'d, K, T>(s: &'d mut T, key: K, value: K::V) -> (Marked<K::V>, Marked<K::V>)
156-
where
157-
K: MapKey,
158-
&'d mut T: MapApi<'d, K>,
159-
{
160-
let got = s.to_ro().get(&key).await;
161-
162-
let meta = if let Some((_, meta)) = got.unpack_ref() {
163-
meta
164-
} else {
165-
None
166-
};
167-
168-
s.set(key, Some((value, meta.cloned()))).await
169-
}
170-
171-
#[async_trait::async_trait]
172-
impl<'d, K, T> MapApiExt<'d, K> for &'d mut T
173-
where
174-
&'d mut T: MapApi<'d, K>,
175-
K: MapKey,
176-
{
177-
// Not work: it requires MapApiRO::GetFut to be Send, but adding `Send` to MapApiRO::GetFut cause an compilation error:
178-
// https://github.com/rust-lang/rust/issues/100013
179-
async fn update_meta(self, key: K, meta: Option<KVMeta>) -> (Marked<K::V>, Marked<K::V>) {
120+
pub(in crate::sm_v002) async fn update_meta<'d, K, T>(
121+
s: &'d mut T,
122+
key: K,
123+
meta: Option<KVMeta>,
124+
) -> (Marked<K::V>, Marked<K::V>)
125+
where
126+
K: MapKey,
127+
&'d mut T: MapApi<'d, K>,
128+
{
180129
//
181-
let got = self.to_ro().get(&key).await;
130+
let got = s.to_ro().get(&key).await;
182131
if got.is_tomb_stone() {
183132
return (got.clone(), got.clone());
184133
}
185134

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

189-
self.set(key, Some((v.clone(), meta))).await
138+
s.set(key, Some((v.clone(), meta))).await
190139
}
191140

192-
async fn upsert_value(&mut self, key: K, value: K::V) -> (Marked<K::V>, Marked<K::V>) {
193-
let got = self.get(&key).await;
141+
/// Update only the value and keeps the meta unchanged.
142+
/// If the entry does not exist, create one.
143+
pub(in crate::sm_v002) async fn upsert_value<'d, K, T>(
144+
s: &'d mut T,
145+
key: K,
146+
value: K::V,
147+
) -> (Marked<K::V>, Marked<K::V>)
148+
where
149+
K: MapKey,
150+
&'d mut T: MapApi<'d, K>,
151+
{
152+
let got = s.to_ro().get(&key).await;
194153

195154
let meta = if let Some((_, meta)) = got.unpack_ref() {
196155
meta
197156
} else {
198157
None
199158
};
200159

201-
self.set(key, Some((value, meta.cloned()))).await
160+
s.set(key, Some((value, meta.cloned()))).await
202161
}
203162
}
204163

src/meta/raft-store/src/sm_v002/sm_v002.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ impl SMV002 {
250250
///
251251
/// It does not check expiration of the returned entry.
252252
pub async fn get_kv(&self, key: &str) -> Option<SeqV> {
253-
let got = MapApiRO::<String>::get(&self.levels, key).await;
253+
let got = MapApiRO::<String>::get(self.levels.leveled_ref(), key).await;
254+
// let got = self.levels.leveled_ref().get(key).await;
255+
// let got = MapApiRO::<String>::get(&self.levels, key).await;
254256
Into::<Option<SeqV>>::into(got)
255257
}
256258

@@ -408,9 +410,12 @@ impl SMV002 {
408410
}
409411
Operation::Delete => self.levels.set(upsert_kv.key.clone(), None).await,
410412
Operation::AsIs => {
411-
self.levels
412-
.update_meta(upsert_kv.key.clone(), upsert_kv.value_meta.clone())
413-
.await
413+
MapApiExt::update_meta(
414+
&mut self.levels,
415+
upsert_kv.key.clone(),
416+
upsert_kv.value_meta.clone(),
417+
)
418+
.await
414419
}
415420
};
416421

0 commit comments

Comments
 (0)