From 9d3c0a8fd34ec25b6599433c800420b510230bd7 Mon Sep 17 00:00:00 2001 From: smorihira Date: Mon, 9 Sep 2024 14:55:33 +0900 Subject: [PATCH] feat: implement delete and set --- rust/bin/meta/src/handler.rs | 1 - rust/bin/meta/src/handler/meta.rs | 31 +++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/rust/bin/meta/src/handler.rs b/rust/bin/meta/src/handler.rs index a84f37f6f8..41e0853b06 100644 --- a/rust/bin/meta/src/handler.rs +++ b/rust/bin/meta/src/handler.rs @@ -18,7 +18,6 @@ mod meta; use std::sync::Arc; use kv::*; -// #[derive(Debug)] // TODO: Debug外す? pub struct Meta { store: Arc, bucket: Bucket<'static, Raw, Raw>, diff --git a/rust/bin/meta/src/handler/meta.rs b/rust/bin/meta/src/handler/meta.rs index 152c46801a..bf8c00aecd 100644 --- a/rust/bin/meta/src/handler/meta.rs +++ b/rust/bin/meta/src/handler/meta.rs @@ -47,13 +47,40 @@ impl meta_server::Meta for super::Meta { &self, request: tonic::Request, ) -> std::result::Result, 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, ) -> std::result::Result, 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))), + } } } \ No newline at end of file