mirror of https://github.com/tikv/client-rust.git
Merge pull request #152 from ekexium/remove_value_type
set Value type to an alias of Vec<u8>
This commit is contained in:
commit
b1bdbb34ef
|
@ -43,7 +43,7 @@ async fn main() -> Result<()> {
|
|||
// Above, you saw we can use a `&'static str`, this is primarily for making examples short.
|
||||
// This type is practical to use for real things, and usage forces an internal copy.
|
||||
//
|
||||
// It is best to pass a `Vec<u8>` in terms of explictness and speed. `String`s and a few other
|
||||
// It is best to pass a `Vec<u8>` in terms of explicitness and speed. `String`s and a few other
|
||||
// types are supported as well, but it all ends up as `Vec<u8>` in the end.
|
||||
let value: Option<Value> = client.get(KEY.to_owned()).await?;
|
||||
assert_eq!(value, Some(Value::from(VALUE.to_owned())));
|
||||
|
@ -114,17 +114,20 @@ async fn main() -> Result<()> {
|
|||
.batch_scan(batch_scan_keys.to_owned(), 10)
|
||||
.await
|
||||
.expect("Could not batch scan");
|
||||
let vals: Vec<_> = kv_pairs.into_iter().map(|p| p.1).collect();
|
||||
let vals: Vec<_> = kv_pairs
|
||||
.into_iter()
|
||||
.map(|p| String::from_utf8(p.1).unwrap().to_owned())
|
||||
.collect();
|
||||
assert_eq!(
|
||||
&vals,
|
||||
&[
|
||||
"v1".to_owned().into(),
|
||||
"v2".to_owned().into(),
|
||||
"v2".to_owned().into(),
|
||||
"v3".to_owned().into(),
|
||||
"v1".to_owned().into(),
|
||||
"v2".to_owned().into(),
|
||||
"v3".to_owned().into()
|
||||
"v1".to_owned(),
|
||||
"v2".to_owned(),
|
||||
"v2".to_owned(),
|
||||
"v3".to_owned(),
|
||||
"v1".to_owned(),
|
||||
"v2".to_owned(),
|
||||
"v3".to_owned()
|
||||
]
|
||||
);
|
||||
println!(
|
||||
|
|
|
@ -95,7 +95,7 @@ impl Into<(Key, Value)> for KvPair {
|
|||
|
||||
impl From<kvrpcpb::KvPair> for KvPair {
|
||||
fn from(mut pair: kvrpcpb::KvPair) -> Self {
|
||||
KvPair(Key::from(pair.take_key()), Value::from(pair.take_value()))
|
||||
KvPair(Key::from(pair.take_key()), pair.take_value())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,7 @@ impl Into<kvrpcpb::KvPair> for KvPair {
|
|||
let mut result = kvrpcpb::KvPair::default();
|
||||
let (key, value) = self.into();
|
||||
result.set_key(key.into());
|
||||
result.set_value(value.into());
|
||||
result.set_value(value);
|
||||
result
|
||||
}
|
||||
}
|
||||
|
@ -124,9 +124,9 @@ impl AsRef<Value> for KvPair {
|
|||
impl fmt::Debug for KvPair {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let KvPair(key, value) = self;
|
||||
match str::from_utf8(&value.0) {
|
||||
match str::from_utf8(&value) {
|
||||
Ok(s) => write!(f, "KvPair({}, {:?})", HexRepr(&key.0), s),
|
||||
Err(_) => write!(f, "KvPair({}, {})", HexRepr(&key.0), HexRepr(&value.0)),
|
||||
Err(_) => write!(f, "KvPair({}, {})", HexRepr(&key.0), HexRepr(&value)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
|
||||
|
||||
use super::HexRepr;
|
||||
#[cfg(test)]
|
||||
use proptest::{arbitrary::any_with, collection::size_range};
|
||||
#[cfg(test)]
|
||||
use proptest_derive::Arbitrary;
|
||||
use std::{fmt, str, u8};
|
||||
use std::u8;
|
||||
|
||||
/// The value part of a key/value pair.
|
||||
///
|
||||
|
@ -48,54 +43,5 @@ use std::{fmt, str, u8};
|
|||
///
|
||||
/// Many functions which accept a `Value` accept an `Into<Value>`, which means all of the above types
|
||||
/// can be passed directly to those functions.
|
||||
#[derive(Default, Clone, Eq, PartialEq, Hash)]
|
||||
#[cfg_attr(test, derive(Arbitrary))]
|
||||
pub struct Value(
|
||||
#[cfg_attr(
|
||||
test,
|
||||
proptest(
|
||||
strategy = "any_with::<Vec<u8>>((size_range(crate::proptests::PROPTEST_VALUE_MAX), ()))"
|
||||
)
|
||||
)]
|
||||
pub(super) Vec<u8>,
|
||||
);
|
||||
|
||||
impl Value {
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<u8>> for Value {
|
||||
fn from(v: Vec<u8>) -> Self {
|
||||
Value(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Value {
|
||||
fn from(v: String) -> Value {
|
||||
Value(v.into_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<Vec<u8>> for Value {
|
||||
fn into(self) -> Vec<u8> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Into<&'a [u8]> for &'a Value {
|
||||
fn into(self) -> &'a [u8] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Value {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match str::from_utf8(&self.0) {
|
||||
Ok(s) => write!(f, "Value({:?})", s),
|
||||
Err(_) => write!(f, "Value({})", HexRepr(&self.0)),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub type Value = Vec<u8>;
|
||||
|
|
|
@ -11,7 +11,6 @@ mod raw;
|
|||
|
||||
pub(crate) const ENV_PD_ADDRS: &str = "PD_ADDRS";
|
||||
pub(crate) const PROPTEST_KEY_MAX: usize = 1024 * 2; // 2 KB
|
||||
pub(crate) const PROPTEST_VALUE_MAX: usize = 1024 * 16; // 16 KB
|
||||
pub(crate) const PROPTEST_BATCH_SIZE_MAX: usize = 16;
|
||||
|
||||
pub fn arb_batch<T: core::fmt::Debug>(
|
||||
|
|
|
@ -21,7 +21,11 @@ proptest! {
|
|||
let out_value = block_on(
|
||||
client.get(pair.key().clone())
|
||||
).unwrap();
|
||||
assert_eq!(Some(Value::from(pair.value().clone())), out_value);
|
||||
|
||||
match out_value {
|
||||
None => assert!(pair.value().is_empty()),
|
||||
Some(out) => assert_eq!(Value::from(pair.value().clone()), out)
|
||||
}
|
||||
|
||||
block_on(
|
||||
client.delete(pair.key().clone())
|
||||
|
|
|
@ -27,14 +27,6 @@ impl KvRequest for kvrpcpb::RawGetRequest {
|
|||
const REQUEST_NAME: &'static str = "raw_get";
|
||||
const RPC_FN: RpcFnType<Self, Self::RpcResponse> = TikvClient::raw_get_async_opt;
|
||||
|
||||
fn make_rpc_request<KvC: KvClient>(&self, key: Self::KeyData, store: &Store<KvC>) -> Self {
|
||||
let mut req = store.request::<Self>();
|
||||
req.set_key(key.into());
|
||||
req.set_cf(self.cf.clone());
|
||||
|
||||
req
|
||||
}
|
||||
|
||||
fn store_stream<PdC: PdClient>(
|
||||
&mut self,
|
||||
pd_client: Arc<PdC>,
|
||||
|
@ -43,8 +35,16 @@ impl KvRequest for kvrpcpb::RawGetRequest {
|
|||
store_stream_for_key(key, pd_client)
|
||||
}
|
||||
|
||||
fn make_rpc_request<KvC: KvClient>(&self, key: Self::KeyData, store: &Store<KvC>) -> Self {
|
||||
let mut req = store.request::<Self>();
|
||||
req.set_key(key.into());
|
||||
req.set_cf(self.cf.clone());
|
||||
|
||||
req
|
||||
}
|
||||
|
||||
fn map_result(mut resp: Self::RpcResponse) -> Self::Result {
|
||||
let result: Value = resp.take_value().into();
|
||||
let result: Value = resp.take_value();
|
||||
if result.is_empty() {
|
||||
None
|
||||
} else {
|
||||
|
@ -128,7 +128,7 @@ impl KvRequest for kvrpcpb::RawPutRequest {
|
|||
fn make_rpc_request<KvC: KvClient>(&self, key: Self::KeyData, store: &Store<KvC>) -> Self {
|
||||
let mut req = store.request::<Self>();
|
||||
req.set_key(key.0.into());
|
||||
req.set_value(key.1.into());
|
||||
req.set_value(key.1);
|
||||
req.set_cf(self.cf.clone());
|
||||
|
||||
req
|
||||
|
@ -163,7 +163,7 @@ pub fn new_raw_put_request(
|
|||
) -> kvrpcpb::RawPutRequest {
|
||||
let mut req = kvrpcpb::RawPutRequest::default();
|
||||
req.set_key(key.into().into());
|
||||
req.set_value(value.into().into());
|
||||
req.set_value(value.into());
|
||||
req.maybe_set_cf(cf);
|
||||
|
||||
req
|
||||
|
|
|
@ -139,7 +139,7 @@ impl Mutation {
|
|||
Mutation::Cached(_) => return None,
|
||||
Mutation::Put(v) => {
|
||||
pb.set_op(kvrpcpb::Op::Put);
|
||||
pb.set_value(v.clone().into());
|
||||
pb.set_value(v.clone());
|
||||
}
|
||||
Mutation::Del => pb.set_op(kvrpcpb::Op::Del),
|
||||
Mutation::Lock => pb.set_op(kvrpcpb::Op::Lock),
|
||||
|
@ -192,7 +192,7 @@ mod tests {
|
|||
block_on(buffer.get_or_else(b"key1".to_vec().into(), move |_| ready(panic!())))
|
||||
.unwrap()
|
||||
.unwrap(),
|
||||
b"value1".to_vec().into()
|
||||
b"value1".to_vec()
|
||||
);
|
||||
|
||||
buffer.delete(b"key2".to_vec().into());
|
||||
|
|
|
@ -39,7 +39,7 @@ impl KvRequest for kvrpcpb::GetRequest {
|
|||
}
|
||||
|
||||
fn map_result(mut resp: Self::RpcResponse) -> Self::Result {
|
||||
let result: Value = resp.take_value().into();
|
||||
let result: Value = resp.take_value();
|
||||
if resp.not_found {
|
||||
None
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue