From b90df9150e2f7f0cd9bb5fa1bcbd346c1b12df66 Mon Sep 17 00:00:00 2001 From: smorihira Date: Mon, 9 Sep 2024 12:52:54 +0900 Subject: [PATCH] feat: implemented get and new --- rust/Cargo.lock | 155 +++++++++++++++++++++++++++++- rust/bin/meta/Cargo.toml | 3 + rust/bin/meta/src/handler.rs | 18 +++- rust/bin/meta/src/handler/meta.rs | 21 +++- rust/bin/meta/src/main.rs | 2 +- 5 files changed, 192 insertions(+), 7 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9fcf401f91..8be889e8c5 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -248,6 +248,30 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + [[package]] name = "either" version = "1.13.0" @@ -284,6 +308,16 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fs2" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "futures-channel" version = "0.3.30" @@ -356,6 +390,15 @@ dependencies = [ "slab", ] +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + [[package]] name = "getrandom" version = "0.2.15" @@ -623,6 +666,15 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "instant" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" +dependencies = [ + "cfg-if", +] + [[package]] name = "ipnet" version = "2.9.0" @@ -662,6 +714,19 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "kv" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "620727085ac39ee9650b373fe6d8073a0aee6f99e52a9c72b25f7671078039ab" +dependencies = [ + "pin-project-lite", + "serde", + "sled", + "thiserror", + "toml", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -706,7 +771,10 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" name = "meta" version = "0.1.0" dependencies = [ + "kv", + "prost-types", "proto", + "sled", "tokio", "tonic 0.12.2", ] @@ -875,6 +943,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core 0.8.6", +] + [[package]] name = "parking_lot" version = "0.12.3" @@ -882,7 +961,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" dependencies = [ "lock_api", - "parking_lot_core", + "parking_lot_core 0.9.10", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall 0.2.16", + "smallvec", + "winapi", ] [[package]] @@ -893,7 +986,7 @@ checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" dependencies = [ "cfg-if", "libc", - "redox_syscall", + "redox_syscall 0.5.3", "smallvec", "windows-targets 0.52.6", ] @@ -1065,6 +1158,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "redox_syscall" version = "0.5.3" @@ -1202,6 +1304,22 @@ dependencies = [ "autocfg", ] +[[package]] +name = "sled" +version = "0.34.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f96b4737c2ce5987354855aed3797279def4ebf734436c6aa4552cf8e169935" +dependencies = [ + "crc32fast", + "crossbeam-epoch", + "crossbeam-utils", + "fs2", + "fxhash", + "libc", + "log", + "parking_lot 0.11.2", +] + [[package]] name = "smallvec" version = "1.13.2" @@ -1307,7 +1425,7 @@ dependencies = [ "bytes", "libc", "mio", - "parking_lot", + "parking_lot 0.12.3", "pin-project-lite", "signal-hook-registry", "socket2", @@ -1360,6 +1478,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "tonic" version = "0.11.0" @@ -1621,6 +1748,28 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/rust/bin/meta/Cargo.toml b/rust/bin/meta/Cargo.toml index abb129eabf..99e3bb95a2 100644 --- a/rust/bin/meta/Cargo.toml +++ b/rust/bin/meta/Cargo.toml @@ -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" diff --git a/rust/bin/meta/src/handler.rs b/rust/bin/meta/src/handler.rs index fc7e547286..a84f37f6f8 100644 --- a/rust/bin/meta/src/handler.rs +++ b/rust/bin/meta/src/handler.rs @@ -15,11 +15,25 @@ // mod meta; -#[derive(Default, Debug)] +use std::sync::Arc; +use kv::*; + +// #[derive(Debug)] // TODO: Debug外す? pub struct Meta { - + store: Arc, + bucket: Bucket<'static, Raw, Raw>, } +impl Meta { + pub fn new() -> Result { + 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::(Some("meta_bucket"))?; + + Ok(Meta { store, bucket }) + } +} diff --git a/rust/bin/meta/src/handler/meta.rs b/rust/bin/meta/src/handler/meta.rs index 9133b5e514..152c46801a 100644 --- a/rust/bin/meta/src/handler/meta.rs +++ b/rust/bin/meta/src/handler/meta.rs @@ -15,6 +15,7 @@ // use proto::{meta::v1::meta_server, payload::v1::{meta, Empty}}; +use kv::*; #[tonic::async_trait] impl meta_server::Meta for super::Meta { @@ -22,7 +23,25 @@ impl meta_server::Meta for super::Meta { &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.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, diff --git a/rust/bin/meta/src/main.rs b/rust/bin/meta/src/main.rs index df0c321fb3..27ebe77fb4 100644 --- a/rust/bin/meta/src/main.rs +++ b/rust/bin/meta/src/main.rs @@ -19,7 +19,7 @@ mod handler; #[tokio::main] async fn main() -> Result<(), Box> { 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))