mirror of https://github.com/tikv/client-rust.git
batch_get returns an iterator
Signed-off-by: Yilin Chen <sticnarf@gmail.com>
This commit is contained in:
parent
a74e1abd1a
commit
a601c39c1f
|
|
@ -15,10 +15,7 @@ pub use self::requests::Scanner;
|
||||||
use crate::{Key, Result, Value};
|
use crate::{Key, Result, Value};
|
||||||
use derive_new::new;
|
use derive_new::new;
|
||||||
use kvproto::kvrpcpb;
|
use kvproto::kvrpcpb;
|
||||||
use std::{
|
use std::{collections::BTreeMap, ops::RangeBounds};
|
||||||
collections::{BTreeMap, HashMap},
|
|
||||||
ops::RangeBounds,
|
|
||||||
};
|
|
||||||
|
|
||||||
mod client;
|
mod client;
|
||||||
pub(crate) mod requests;
|
pub(crate) mod requests;
|
||||||
|
|
@ -119,8 +116,8 @@ impl Transaction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the values associated with the given keys. Non-existent keys are not included in the
|
/// Gets the values associated with the given keys. The returned iterator is in the same order
|
||||||
/// result.
|
/// as the given keys.
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # #![feature(async_await)]
|
/// # #![feature(async_await)]
|
||||||
|
|
@ -132,7 +129,11 @@ impl Transaction {
|
||||||
/// # let connected_client = connecting_client.await.unwrap();
|
/// # let connected_client = connecting_client.await.unwrap();
|
||||||
/// let mut txn = connected_client.begin().await.unwrap();
|
/// let mut txn = connected_client.begin().await.unwrap();
|
||||||
/// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
|
/// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
|
||||||
/// let result: HashMap<Key, Value> = txn.batch_get(keys).await.unwrap();
|
/// let result: HashMap<Key, Value> = txn
|
||||||
|
/// .batch_get(keys)
|
||||||
|
/// .await
|
||||||
|
/// .unwrap()
|
||||||
|
/// .filter_map(|(k, v)| v.map(move |v| (k, v))).collect();
|
||||||
/// // Finish the transaction...
|
/// // Finish the transaction...
|
||||||
/// txn.commit().await.unwrap();
|
/// txn.commit().await.unwrap();
|
||||||
/// # });
|
/// # });
|
||||||
|
|
@ -140,25 +141,37 @@ impl Transaction {
|
||||||
pub async fn batch_get(
|
pub async fn batch_get(
|
||||||
&self,
|
&self,
|
||||||
keys: impl IntoIterator<Item = impl Into<Key>>,
|
keys: impl IntoIterator<Item = impl Into<Key>>,
|
||||||
) -> Result<HashMap<Key, Value>> {
|
) -> Result<impl Iterator<Item = (Key, Option<Value>)>> {
|
||||||
let mut result = HashMap::new();
|
|
||||||
let mut keys_from_snapshot = Vec::new();
|
let mut keys_from_snapshot = Vec::new();
|
||||||
|
let mut results_in_buffer = keys
|
||||||
// Try to get the result from buffered mutations first
|
.into_iter()
|
||||||
for key in keys {
|
.map(|key| {
|
||||||
let key = key.into();
|
let key = key.into();
|
||||||
match self.get_from_mutations(&key) {
|
let mutation_value = self.get_from_mutations(&key);
|
||||||
MutationValue::Determined(Some(value)) => {
|
if let MutationValue::Undetermined = mutation_value {
|
||||||
result.insert(key, value);
|
keys_from_snapshot.push(key.clone());
|
||||||
}
|
}
|
||||||
MutationValue::Determined(None) => {}
|
(key, mutation_value)
|
||||||
MutationValue::Undetermined => keys_from_snapshot.push(key),
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.into_iter();
|
||||||
|
let mut results_from_snapshot = self
|
||||||
|
.snapshot
|
||||||
|
.batch_get(keys_from_snapshot)
|
||||||
|
.await?
|
||||||
|
.peekable();
|
||||||
|
Ok(std::iter::from_fn(move || {
|
||||||
|
let (key, mutation_value) = results_in_buffer.next()?;
|
||||||
|
match mutation_value {
|
||||||
|
MutationValue::Determined(value) => Some((key, value)),
|
||||||
|
MutationValue::Undetermined => match results_from_snapshot.peek() {
|
||||||
|
Some((key_from_snapshot, _)) if &key == key_from_snapshot => {
|
||||||
|
results_from_snapshot.next()
|
||||||
|
}
|
||||||
|
_ => Some((key, None)),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}))
|
||||||
|
|
||||||
// Get others from snapshot
|
|
||||||
result.extend(self.snapshot.batch_get(keys_from_snapshot).await?);
|
|
||||||
Ok(result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scan(&self, _range: impl RangeBounds<Key>) -> Scanner {
|
pub fn scan(&self, _range: impl RangeBounds<Key>) -> Scanner {
|
||||||
|
|
@ -349,8 +362,8 @@ impl Snapshot {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the values associated with the given keys. Non-existent keys are not included in the
|
/// Gets the values associated with the given keys. The returned iterator is in the same order
|
||||||
/// result.
|
/// as the given keys.
|
||||||
///
|
///
|
||||||
/// ```rust,no_run
|
/// ```rust,no_run
|
||||||
/// # #![feature(async_await)]
|
/// # #![feature(async_await)]
|
||||||
|
|
@ -362,14 +375,18 @@ impl Snapshot {
|
||||||
/// # let connected_client = connecting_client.await.unwrap();
|
/// # let connected_client = connecting_client.await.unwrap();
|
||||||
/// let snapshot = connected_client.snapshot().await.unwrap();
|
/// let snapshot = connected_client.snapshot().await.unwrap();
|
||||||
/// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
|
/// let keys = vec!["TiKV".to_owned(), "TiDB".to_owned()];
|
||||||
/// let result: HashMap<Key, Value> = snapshot.batch_get(keys).await.unwrap();
|
/// let result: HashMap<Key, Value> = snapshot
|
||||||
|
/// .batch_get(keys)
|
||||||
|
/// .await
|
||||||
|
/// .unwrap()
|
||||||
|
/// .filter_map(|(k, v)| v.map(move |v| (k, v))).collect();
|
||||||
/// # });
|
/// # });
|
||||||
/// ```
|
/// ```
|
||||||
pub async fn batch_get(
|
pub async fn batch_get(
|
||||||
&self,
|
&self,
|
||||||
_keys: impl IntoIterator<Item = impl Into<Key>>,
|
_keys: impl IntoIterator<Item = impl Into<Key>>,
|
||||||
) -> Result<HashMap<Key, Value>> {
|
) -> Result<impl Iterator<Item = (Key, Option<Value>)>> {
|
||||||
unimplemented!()
|
Ok(std::iter::repeat_with(|| unimplemented!()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scan(&self, range: impl RangeBounds<Key>) -> Scanner {
|
pub fn scan(&self, range: impl RangeBounds<Key>) -> Scanner {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue