Skip to content

Commit

Permalink
feat: implemented get and new
Browse files Browse the repository at this point in the history
  • Loading branch information
smorihira committed Sep 9, 2024
1 parent 5636a10 commit b90df91
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 7 deletions.
155 changes: 152 additions & 3 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions rust/bin/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ version = "0.1.0"
edition = "2021"

[dependencies]
kv = "0.24.0"
prost-types = "0.13.2"
proto = { version = "0.1.0", path = "../../libs/proto" }
sled = "0.34.7"
tokio = { version = "1.40.0", features = ["full"] }
tonic = "0.12.2"
18 changes: 16 additions & 2 deletions rust/bin/meta/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,25 @@
//
mod meta;

#[derive(Default, Debug)]
use std::sync::Arc;
use kv::*;

// #[derive(Debug)] // TODO: Debug外す?
pub struct Meta {

store: Arc<Store>,
bucket: Bucket<'static, Raw, Raw>,
}

impl Meta {
pub fn new() -> Result<Self, kv::Error> {
let cfg_path = "/var/lib/meta/database"; // TODO: pathはこれでよい?
let cfg = Config::new(cfg_path);
let store = Arc::new(Store::new(cfg)?);
let bucket = store.bucket::<Raw, Raw>(Some("meta_bucket"))?;

Ok(Meta { store, bucket })
}
}



21 changes: 20 additions & 1 deletion rust/bin/meta/src/handler/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,33 @@
//

use proto::{meta::v1::meta_server, payload::v1::{meta, Empty}};
use kv::*;

#[tonic::async_trait]
impl meta_server::Meta for super::Meta {
async fn get(
&self,
request: tonic::Request<meta::Key>,
) -> std::result::Result<tonic::Response<meta::Value>, tonic::Status> {
todo!()
let key = request.into_inner().key;
let raw_key = Raw::from(key.as_bytes());

match self.bucket.get(&raw_key) {
Ok(Some(value_bytes)) => {
let any_value = prost_types::Any {
type_url: "type.googleapis.com/your.package.MessageType".to_string(),
value: value_bytes.to_vec(),
};

let response = meta::Value {
value: Some(any_value),
};

Ok(tonic::Response::new(response))
},
Ok(None) => Err(tonic::Status::not_found("Key not found")),
Err(e) => Err(tonic::Status::internal(format!("Database error: {}", e))),
}
}
async fn set(
&self,
Expand Down
2 changes: 1 addition & 1 deletion rust/bin/meta/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod handler;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = "[::1]:8081".parse()?;
let meta = handler::Meta::default();
let meta = handler::Meta::new().expect("Failed to initialize Meta service");

tonic::transport::Server::builder()
.add_service(proto::meta::v1::meta_server::MetaServer::new(meta))
Expand Down

0 comments on commit b90df91

Please sign in to comment.