Skip to content

Commit

Permalink
feat: implement delete and set
Browse files Browse the repository at this point in the history
  • Loading branch information
smorihira committed Sep 9, 2024
1 parent b90df91 commit 9d3c0a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 0 additions & 1 deletion rust/bin/meta/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mod meta;
use std::sync::Arc;
use kv::*;

// #[derive(Debug)] // TODO: Debug外す?
pub struct Meta {
store: Arc<Store>,
bucket: Bucket<'static, Raw, Raw>,
Expand Down
31 changes: 29 additions & 2 deletions rust/bin/meta/src/handler/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,40 @@ impl meta_server::Meta for super::Meta {
&self,
request: tonic::Request<meta::KeyValue>,
) -> std::result::Result<tonic::Response<Empty>, tonic::Status> {
todo!()
let key_value = request.into_inner();

let key = match key_value.key {
Some(k) => k.key,
None => return Err(tonic::Status::invalid_argument("Key is missing")),
};

let value = match key_value.value {
Some(v) => match v.value {
Some(any_value) => any_value.value,
None => return Err(tonic::Status::invalid_argument("Value is missing")),
},
None => return Err(tonic::Status::invalid_argument("Value is missing")),
};

let raw_key = Raw::from(key.as_bytes());
let raw_value = sled::IVec::from(value);

match self.bucket.set(&raw_key, &raw_value) {
Ok(_) => Ok(tonic::Response::new(Empty {})),
Err(e) => Err(tonic::Status::internal(format!("Failed to set value: {}", e))),
}
}

async fn delete(
&self,
request: tonic::Request<meta::Key>,
) -> std::result::Result<tonic::Response<Empty>, tonic::Status> {
todo!()
let key = request.into_inner().key;
let raw_key = Raw::from(key.as_bytes());

match self.bucket.remove(&raw_key) {
Ok(_) => Ok(tonic::Response::new(Empty {})),
Err(e) => Err(tonic::Status::internal(format!("Failed to delete key: {}", e))),
}
}
}

0 comments on commit 9d3c0a8

Please sign in to comment.