use crate::convert::*;
use crate::error::Result;
use kstone_core::Item;
use kstone_proto::{self as proto, keystone_db_client::KeystoneDbClient};
use tonic::transport::Channel;
pub struct RemoteBatchGetRequest {
keys: Vec<proto::Key>,
}
impl RemoteBatchGetRequest {
pub fn new() -> Self {
Self { keys: Vec::new() }
}
pub fn add_key(mut self, pk: &[u8]) -> Self {
self.keys.push(proto::Key {
partition_key: pk.to_vec(),
sort_key: None,
});
self
}
pub fn add_key_with_sk(mut self, pk: &[u8], sk: &[u8]) -> Self {
self.keys.push(proto::Key {
partition_key: pk.to_vec(),
sort_key: Some(sk.to_vec()),
});
self
}
pub async fn execute(self, client: &mut KeystoneDbClient<Channel>) -> Result<RemoteBatchGetResponse> {
let request = proto::BatchGetRequest {
keys: self.keys,
};
let response = client
.batch_get(request)
.await?
.into_inner();
let items: Vec<Item> = response
.items
.into_iter()
.map(|proto_item| proto_item_to_ks(proto_item).expect("Invalid item from server"))
.collect();
Ok(RemoteBatchGetResponse {
items,
count: response.count as usize,
})
}
}
impl Default for RemoteBatchGetRequest {
fn default() -> Self {
Self::new()
}
}
pub struct RemoteBatchGetResponse {
pub items: Vec<Item>,
pub count: usize,
}
pub struct RemoteBatchWriteRequest {
writes: Vec<proto::WriteRequest>,
}
impl RemoteBatchWriteRequest {
pub fn new() -> Self {
Self { writes: Vec::new() }
}
pub fn put(mut self, pk: &[u8], item: Item) -> Self {
self.writes.push(proto::WriteRequest {
request: Some(proto::write_request::Request::Put(proto::PutItem {
partition_key: pk.to_vec(),
sort_key: None,
item: Some(ks_item_to_proto(&item)),
})),
});
self
}
pub fn put_with_sk(mut self, pk: &[u8], sk: &[u8], item: Item) -> Self {
self.writes.push(proto::WriteRequest {
request: Some(proto::write_request::Request::Put(proto::PutItem {
partition_key: pk.to_vec(),
sort_key: Some(sk.to_vec()),
item: Some(ks_item_to_proto(&item)),
})),
});
self
}
pub fn delete(mut self, pk: &[u8]) -> Self {
self.writes.push(proto::WriteRequest {
request: Some(proto::write_request::Request::Delete(proto::DeleteKey {
partition_key: pk.to_vec(),
sort_key: None,
})),
});
self
}
pub fn delete_with_sk(mut self, pk: &[u8], sk: &[u8]) -> Self {
self.writes.push(proto::WriteRequest {
request: Some(proto::write_request::Request::Delete(proto::DeleteKey {
partition_key: pk.to_vec(),
sort_key: Some(sk.to_vec()),
})),
});
self
}
pub async fn execute(self, client: &mut KeystoneDbClient<Channel>) -> Result<RemoteBatchWriteResponse> {
let request = proto::BatchWriteRequest {
writes: self.writes,
};
let response = client
.batch_write(request)
.await?
.into_inner();
Ok(RemoteBatchWriteResponse {
success: response.success,
})
}
}
impl Default for RemoteBatchWriteRequest {
fn default() -> Self {
Self::new()
}
}
pub struct RemoteBatchWriteResponse {
pub success: bool,
}