From 7a368b9b24ea02f54a420794bc0adedab4ae4f22 Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Tue, 6 Feb 2024 12:23:10 +0900 Subject: [PATCH 01/12] init algorithm::Base Signed-off-by: Kosuke Morimoto --- rust/Cargo.lock | 4 ++++ rust/Cargo.toml | 2 +- rust/libs/algorithm/Cargo.toml | 8 ++++++++ rust/libs/algorithm/src/base.rs | 19 +++++++++++++++++++ rust/libs/algorithm/src/lib.rs | 14 ++++++++++++++ 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 rust/libs/algorithm/Cargo.toml create mode 100644 rust/libs/algorithm/src/base.rs create mode 100644 rust/libs/algorithm/src/lib.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9c97873377..623805bf50 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -21,6 +21,10 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" name = "agent" version = "0.1.0" +[[package]] +name = "algorithm" +version = "0.1.0" + [[package]] name = "anyhow" version = "1.0.79" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 082e358313..a0cbcd4a97 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -14,4 +14,4 @@ # limitations under the License. # [workspace] -members = ["libs/proto", "libs/ngt", "bin/agent"] +members = ["libs/proto", "libs/ngt", "bin/agent", "libs/algorithm"] diff --git a/rust/libs/algorithm/Cargo.toml b/rust/libs/algorithm/Cargo.toml new file mode 100644 index 0000000000..67fcb329e1 --- /dev/null +++ b/rust/libs/algorithm/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "algorithm" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs new file mode 100644 index 0000000000..ef52bddec3 --- /dev/null +++ b/rust/libs/algorithm/src/base.rs @@ -0,0 +1,19 @@ +pub mod algorithm { + pub trait Base { + fn search(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; + fn insert(&self, v: &Vec, p: Option<&Param>) -> Result<()>; + fn update(&self, id: &U, v: &Vec, p: Option<&Param>) -> Result<()>; + fn remove(&self, id: &U, p: Option<&Param>) -> Result<()>; + fn commit(&self, p: Option<&Param>) -> Result<()>; + + fn new(p: Option<&Param>) -> Result<&Self>; + fn open(p: &str) -> Result<&Self>; + fn save(p: &str) -> Result<&Self>; + } + + pub trait Param { + type A; + fn get_parameter() -> Result<&A>; + fn set_parameter(a: &A) -> Result<()>; + } +} diff --git a/rust/libs/algorithm/src/lib.rs b/rust/libs/algorithm/src/lib.rs new file mode 100644 index 0000000000..7d12d9af81 --- /dev/null +++ b/rust/libs/algorithm/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} From 08b25c0efcc5f97a96ec98a6d6995276cd6d3c27 Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Tue, 6 Feb 2024 21:07:50 +0900 Subject: [PATCH 02/12] add 2 pattern Signed-off-by: Kosuke Morimoto --- rust/libs/algorithm/Cargo.toml | 1 + rust/libs/algorithm/src/base.rs | 59 +++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/rust/libs/algorithm/Cargo.toml b/rust/libs/algorithm/Cargo.toml index 67fcb329e1..01d5cf2bea 100644 --- a/rust/libs/algorithm/Cargo.toml +++ b/rust/libs/algorithm/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.79" diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs index ef52bddec3..8229662b51 100644 --- a/rust/libs/algorithm/src/base.rs +++ b/rust/libs/algorithm/src/base.rs @@ -1,19 +1,64 @@ -pub mod algorithm { +use anyhow::Result; + +pub mod algorithm1 { pub trait Base { fn search(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; - fn insert(&self, v: &Vec, p: Option<&Param>) -> Result<()>; - fn update(&self, id: &U, v: &Vec, p: Option<&Param>) -> Result<()>; - fn remove(&self, id: &U, p: Option<&Param>) -> Result<()>; - fn commit(&self, p: Option<&Param>) -> Result<()>; + fn insert(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; + fn update(&self, id: &U, v: &Vec, p: Option<&Param>) -> Result<&Response>; + fn remove(&self, id: &U, p: Option<&Param>) -> Result<&Response>; + fn commit(&self, p: Option<&Param>) -> Result<&Response>; + + fn new(p: Option<&Param>) -> Result<&Self>; + fn open(p: &str) -> Result<&Self>; + fn save(p: &str) -> Result<&Self>; + } + + pub trait Param { + type A; + fn get_parameters() -> Result<&A>; + fn set_parameters(a: &A) -> Result<()>; + } + + pub trait Response { + type Status; + type Result; + fn get_status() -> Status; + fn get_result() -> Result; + } +} + +pub mod algorithm2 { + pub trait Base { + fn search(&self, q: &Query) -> Result<&Response>; + fn insert(&self, q: &Query) -> Result<&Response>; + fn update(&self, q: &Query) -> Result<&Response>; + fn remove(&self, q: &Query) -> Result<&Response>; + fn commit(&self, q: &Query) -> Result<&Response>; fn new(p: Option<&Param>) -> Result<&Self>; fn open(p: &str) -> Result<&Self>; fn save(p: &str) -> Result<&Self>; } + pub trait Query { + type V; + fn get_query() -> Result<&Self::V>; + fn set_query(v: &V) -> Result<()>; + type P; + fn get_parameter() -> Result<&V>; + fn set_parameter(a: &Self::P) -> Result<()>; + } + pub trait Param { type A; - fn get_parameter() -> Result<&A>; - fn set_parameter(a: &A) -> Result<()>; + fn get_parameters() -> Result<&Self::A>; + fn set_parameters(a: &Self::A) -> Result<()>; + } + + pub trait Response { + type Status; + type Result; + fn get_status() -> Status; + fn get_result() -> Result; } } From faf1b3df2b192908aa538c722762e99edc943e41 Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Fri, 9 Feb 2024 22:01:55 +0900 Subject: [PATCH 03/12] update dependencies Signed-off-by: Kosuke Morimoto --- rust/Cargo.lock | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 623805bf50..a706d3c872 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -24,6 +24,9 @@ version = "0.1.0" [[package]] name = "algorithm" version = "0.1.0" +dependencies = [ + "anyhow", +] [[package]] name = "anyhow" From 96e96ee9503d8ba1ad560f343f6f9481d1b2494f Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Tue, 13 Feb 2024 18:33:35 +0900 Subject: [PATCH 04/12] add serde Signed-off-by: Kosuke Morimoto --- rust/Cargo.lock | 1 + rust/libs/algorithm/Cargo.toml | 1 + rust/libs/algorithm/src/base.rs | 6 ++++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index a706d3c872..016a66bb75 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -26,6 +26,7 @@ name = "algorithm" version = "0.1.0" dependencies = [ "anyhow", + "serde", ] [[package]] diff --git a/rust/libs/algorithm/Cargo.toml b/rust/libs/algorithm/Cargo.toml index 01d5cf2bea..3596e09c58 100644 --- a/rust/libs/algorithm/Cargo.toml +++ b/rust/libs/algorithm/Cargo.toml @@ -7,3 +7,4 @@ edition = "2021" [dependencies] anyhow = "1.0.79" +serde = { version = "1.0.196", features = ["derive"] } diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs index 8229662b51..9a073af78f 100644 --- a/rust/libs/algorithm/src/base.rs +++ b/rust/libs/algorithm/src/base.rs @@ -1,7 +1,9 @@ use anyhow::Result; +use serde::Serialize; +use serde::Deserialize; pub mod algorithm1 { - pub trait Base { + pub trait Base { fn search(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; fn insert(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; fn update(&self, id: &U, v: &Vec, p: Option<&Param>) -> Result<&Response>; @@ -28,7 +30,7 @@ pub mod algorithm1 { } pub mod algorithm2 { - pub trait Base { + pub trait Base { fn search(&self, q: &Query) -> Result<&Response>; fn insert(&self, q: &Query) -> Result<&Response>; fn update(&self, q: &Query) -> Result<&Response>; From aa795240743977b7af8b4ade24d6776cb8c2e87b Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Tue, 13 Feb 2024 19:52:08 +0900 Subject: [PATCH 05/12] fix Signed-off-by: Kosuke Morimoto --- rust/libs/algorithm/src/base.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs index 9a073af78f..fae2c4ca6d 100644 --- a/rust/libs/algorithm/src/base.rs +++ b/rust/libs/algorithm/src/base.rs @@ -1,9 +1,9 @@ use anyhow::Result; -use serde::Serialize; use serde::Deserialize; +use serde::Serialize; pub mod algorithm1 { - pub trait Base { + pub trait Base { fn search(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; fn insert(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; fn update(&self, id: &U, v: &Vec, p: Option<&Param>) -> Result<&Response>; @@ -30,7 +30,7 @@ pub mod algorithm1 { } pub mod algorithm2 { - pub trait Base { + pub trait Base { fn search(&self, q: &Query) -> Result<&Response>; fn insert(&self, q: &Query) -> Result<&Response>; fn update(&self, q: &Query) -> Result<&Response>; From 1f85d2bf5b033f81be571fd5e24715d411c78ada Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Wed, 14 Feb 2024 13:33:20 +0900 Subject: [PATCH 06/12] add example Signed-off-by: Kosuke Morimoto --- rust/libs/algorithm/src/base.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs index fae2c4ca6d..59a75ff118 100644 --- a/rust/libs/algorithm/src/base.rs +++ b/rust/libs/algorithm/src/base.rs @@ -42,25 +42,17 @@ pub mod algorithm2 { fn save(p: &str) -> Result<&Self>; } - pub trait Query { - type V; - fn get_query() -> Result<&Self::V>; - fn set_query(v: &V) -> Result<()>; - type P; - fn get_parameter() -> Result<&V>; - fn set_parameter(a: &Self::P) -> Result<()>; + // example like NGT + #[derive(Serialize, Deserialize, Debug)] + struct SearchQuery { + vector: Vec, + size: u64, + epsilon: f32, + radius: f32, } - pub trait Param { - type A; - fn get_parameters() -> Result<&Self::A>; - fn set_parameters(a: &Self::A) -> Result<()>; - } - - pub trait Response { - type Status; - type Result; - fn get_status() -> Status; - fn get_result() -> Result; + #[derive(Serialize, Deserialize, Debug)] + struct InsertQuery { + vector: Vec, } } From 14496089620c6478354bb80a03b792cf49e1b63d Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Tue, 20 Feb 2024 15:06:06 +0900 Subject: [PATCH 07/12] add KVS trait Signed-off-by: Kosuke Morimoto --- rust/Cargo.lock | 4 +++ rust/Cargo.toml | 2 +- rust/libs/algorithm/src/base.rs | 47 ++------------------------------- rust/libs/kvs/Cargo.toml | 8 ++++++ rust/libs/kvs/src/kvs.rs | 12 +++++++++ rust/libs/kvs/src/lib.rs | 14 ++++++++++ 6 files changed, 41 insertions(+), 46 deletions(-) create mode 100644 rust/libs/kvs/Cargo.toml create mode 100644 rust/libs/kvs/src/kvs.rs create mode 100644 rust/libs/kvs/src/lib.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 016a66bb75..1a0d9d7821 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -374,6 +374,10 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "kvs" +version = "0.1.0" + [[package]] name = "libc" version = "0.2.153" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index a0cbcd4a97..61fee32e22 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -14,4 +14,4 @@ # limitations under the License. # [workspace] -members = ["libs/proto", "libs/ngt", "bin/agent", "libs/algorithm"] +members = ["libs/proto", "libs/ngt", "bin/agent", "libs/algorithm", "libs/kvs"] diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs index 59a75ff118..bf3493e3d0 100644 --- a/rust/libs/algorithm/src/base.rs +++ b/rust/libs/algorithm/src/base.rs @@ -1,11 +1,9 @@ use anyhow::Result; -use serde::Deserialize; -use serde::Serialize; -pub mod algorithm1 { +pub mod algorithm { pub trait Base { fn search(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; - fn insert(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; + fn insert(&self, id: &U, v: &Vec, p: Option<&Param>) -> Result<&Response>; fn update(&self, id: &U, v: &Vec, p: Option<&Param>) -> Result<&Response>; fn remove(&self, id: &U, p: Option<&Param>) -> Result<&Response>; fn commit(&self, p: Option<&Param>) -> Result<&Response>; @@ -14,45 +12,4 @@ pub mod algorithm1 { fn open(p: &str) -> Result<&Self>; fn save(p: &str) -> Result<&Self>; } - - pub trait Param { - type A; - fn get_parameters() -> Result<&A>; - fn set_parameters(a: &A) -> Result<()>; - } - - pub trait Response { - type Status; - type Result; - fn get_status() -> Status; - fn get_result() -> Result; - } -} - -pub mod algorithm2 { - pub trait Base { - fn search(&self, q: &Query) -> Result<&Response>; - fn insert(&self, q: &Query) -> Result<&Response>; - fn update(&self, q: &Query) -> Result<&Response>; - fn remove(&self, q: &Query) -> Result<&Response>; - fn commit(&self, q: &Query) -> Result<&Response>; - - fn new(p: Option<&Param>) -> Result<&Self>; - fn open(p: &str) -> Result<&Self>; - fn save(p: &str) -> Result<&Self>; - } - - // example like NGT - #[derive(Serialize, Deserialize, Debug)] - struct SearchQuery { - vector: Vec, - size: u64, - epsilon: f32, - radius: f32, - } - - #[derive(Serialize, Deserialize, Debug)] - struct InsertQuery { - vector: Vec, - } } diff --git a/rust/libs/kvs/Cargo.toml b/rust/libs/kvs/Cargo.toml new file mode 100644 index 0000000000..c63821f865 --- /dev/null +++ b/rust/libs/kvs/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "kvs" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/libs/kvs/src/kvs.rs b/rust/libs/kvs/src/kvs.rs new file mode 100644 index 0000000000..b8f7f8ee27 --- /dev/null +++ b/rust/libs/kvs/src/kvs.rs @@ -0,0 +1,12 @@ +use anyhow::Result; + +pub mod kvs { + pub trait KVS { + fn set(k: &K, v: &V) -> Result<()>; + fn get_value(k: &K) -> Result<&V>; + fn get_key(v: &V) -> Result<&K>; + + fn new(p: &str) -> Result<&Self>; + fn open(p: &str) -> Result<&Self>; + } +} diff --git a/rust/libs/kvs/src/lib.rs b/rust/libs/kvs/src/lib.rs new file mode 100644 index 0000000000..7d12d9af81 --- /dev/null +++ b/rust/libs/kvs/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} From eab39eab503bbea69f2f4b32d469912b254aea5b Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Tue, 20 Feb 2024 23:45:29 +0900 Subject: [PATCH 08/12] impl Signed-off-by: Kosuke Morimoto --- rust/Cargo.lock | 373 ++++++++++++++++++++++++++++++-- rust/Cargo.toml | 8 +- rust/libs/algorithm/Cargo.toml | 1 + rust/libs/algorithm/src/base.rs | 18 +- rust/libs/algorithm/src/ngt.rs | 48 ++++ rust/libs/ngt-rs/Cargo.toml | 11 + rust/libs/ngt-rs/build.rs | 14 ++ rust/libs/ngt-rs/src/input.cpp | 42 ++++ rust/libs/ngt-rs/src/input.h | 29 +++ rust/libs/ngt-rs/src/lib.rs | 54 +++++ rust/libs/ngt/Cargo.toml | 23 -- rust/libs/ngt/src/lib.rs | 29 --- 12 files changed, 573 insertions(+), 77 deletions(-) create mode 100644 rust/libs/algorithm/src/ngt.rs create mode 100644 rust/libs/ngt-rs/Cargo.toml create mode 100644 rust/libs/ngt-rs/build.rs create mode 100644 rust/libs/ngt-rs/src/input.cpp create mode 100644 rust/libs/ngt-rs/src/input.h create mode 100644 rust/libs/ngt-rs/src/lib.rs delete mode 100644 rust/libs/ngt/Cargo.toml delete mode 100644 rust/libs/ngt/src/lib.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 1a0d9d7821..6471bbc010 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -26,6 +26,7 @@ name = "algorithm" version = "0.1.0" dependencies = [ "anyhow", + "ngt-rs", "serde", ] @@ -82,7 +83,7 @@ checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" dependencies = [ "async-trait", "axum-core", - "bitflags", + "bitflags 1.3.2", "bytes", "futures-util", "http", @@ -134,6 +135,15 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "backtrace-ext" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "537beee3be4a18fb023b570f80e3ae28003db9167a751266b259926e25539d50" +dependencies = [ + "backtrace", +] + [[package]] name = "base64" version = "0.21.7" @@ -146,6 +156,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitflags" +version = "2.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" + [[package]] name = "bytes" version = "1.5.0" @@ -164,6 +180,60 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + +[[package]] +name = "cxx" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c15f3b597018782655a05d417f28bac009f6eb60f4b6703eb818998c1aaa16a" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81699747d109bba60bd6f87e7cb24b626824b8427b32f199b95c7faa06ee3dc9" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a7eb4c4fd18505f5a935f9c2ee77780350dcdb56da7cd037634e806141c5c43" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d914fcc6452d133236ee067a9538be25ba6a644a450e1a6c617da84bf029854" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "either" version = "1.10.0" @@ -176,6 +246,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" +[[package]] +name = "errno" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "fnv" version = "1.0.7" @@ -359,6 +439,12 @@ dependencies = [ "hashbrown 0.14.3", ] +[[package]] +name = "is_ci" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45" + [[package]] name = "itertools" version = "0.11.0" @@ -384,6 +470,21 @@ version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" +[[package]] +name = "link-cplusplus" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d240c6f7e1ba3a28b0249f774e6a9dd0175054b52dfbb61b16eb8505c3785c9" +dependencies = [ + "cc", +] + +[[package]] +name = "linux-raw-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" + [[package]] name = "matchit" version = "0.7.3" @@ -396,6 +497,36 @@ version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +[[package]] +name = "miette" +version = "7.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baed61d13cc3723ee6dbed730a82bfacedc60a85d81da2d77e9c3e8ebc0b504a" +dependencies = [ + "backtrace", + "backtrace-ext", + "miette-derive", + "owo-colors", + "supports-color", + "supports-hyperlinks", + "supports-unicode", + "terminal_size", + "textwrap", + "thiserror", + "unicode-width", +] + +[[package]] +name = "miette-derive" +version = "7.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f301c3f54f98abc6c212ee722f5e5c62e472a334415840669e356f04850051ec" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "mime" version = "0.3.17" @@ -419,12 +550,17 @@ checksum = "8f3d0b296e374a4e6f3c7b0a1f5a51d748a0d34c85e7dc48fc3fa9a87657fe09" dependencies = [ "libc", "wasi", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] -name = "ngt" +name = "ngt-rs" version = "0.1.0" +dependencies = [ + "cxx", + "cxx-build", + "miette", +] [[package]] name = "object" @@ -441,6 +577,12 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "owo-colors" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f" + [[package]] name = "percent-encoding" version = "2.3.1" @@ -581,12 +723,31 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustix" +version = "0.38.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" +dependencies = [ + "bitflags 2.4.2", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + [[package]] name = "rustversion" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" +[[package]] +name = "scratch" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3cf7c11c38cb994f3d40e8a8cde3bbd1f72a435e4c49e85d6553d8312306152" + [[package]] name = "serde" version = "1.0.196" @@ -616,6 +777,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smawk" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c" + [[package]] name = "socket2" version = "0.5.5" @@ -623,9 +790,30 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" dependencies = [ "libc", - "windows-sys", + "windows-sys 0.48.0", +] + +[[package]] +name = "supports-color" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9829b314621dfc575df4e409e79f9d6a66a3bd707ab73f23cb4aa3a854ac854f" +dependencies = [ + "is_ci", ] +[[package]] +name = "supports-hyperlinks" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c0a1e5168041f5f3ff68ff7d95dcb9c8749df29f6e7e89ada40dd4c9de404ee" + +[[package]] +name = "supports-unicode" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7401a30af6cb5818bb64852270bb722533397edcfc7344954a38f420819ece2" + [[package]] name = "syn" version = "2.0.48" @@ -643,6 +831,56 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "terminal_size" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" +dependencies = [ + "rustix", + "windows-sys 0.48.0", +] + +[[package]] +name = "textwrap" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" +dependencies = [ + "smawk", + "unicode-linebreak", + "unicode-width", +] + +[[package]] +name = "thiserror" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio" version = "1.36.0" @@ -655,7 +893,7 @@ dependencies = [ "mio", "pin-project-lite", "socket2", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -806,6 +1044,18 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-linebreak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" + +[[package]] +name = "unicode-width" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" + [[package]] name = "want" version = "0.3.1" @@ -821,13 +1071,53 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[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-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[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" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ - "windows-targets", + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.0", ] [[package]] @@ -836,13 +1126,28 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" +dependencies = [ + "windows_aarch64_gnullvm 0.52.0", + "windows_aarch64_msvc 0.52.0", + "windows_i686_gnu 0.52.0", + "windows_i686_msvc 0.52.0", + "windows_x86_64_gnu 0.52.0", + "windows_x86_64_gnullvm 0.52.0", + "windows_x86_64_msvc 0.52.0", ] [[package]] @@ -851,38 +1156,80 @@ version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" + [[package]] name = "windows_aarch64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" + [[package]] name = "windows_i686_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" +[[package]] +name = "windows_i686_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" + [[package]] name = "windows_i686_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" +[[package]] +name = "windows_i686_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" + [[package]] name = "windows_x86_64_gnu" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" + [[package]] name = "windows_x86_64_gnullvm" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" + [[package]] name = "windows_x86_64_msvc" version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 61fee32e22..daf9b04ec7 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -14,4 +14,10 @@ # limitations under the License. # [workspace] -members = ["libs/proto", "libs/ngt", "bin/agent", "libs/algorithm", "libs/kvs"] +members = [ + "libs/proto", + "bin/agent", + "libs/algorithm", + "libs/kvs", + "libs/ngt-rs", +] diff --git a/rust/libs/algorithm/Cargo.toml b/rust/libs/algorithm/Cargo.toml index 3596e09c58..64035841f6 100644 --- a/rust/libs/algorithm/Cargo.toml +++ b/rust/libs/algorithm/Cargo.toml @@ -8,3 +8,4 @@ edition = "2021" [dependencies] anyhow = "1.0.79" serde = { version = "1.0.196", features = ["derive"] } +ngt-rs = { path = "../ngt-rs" } diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs index bf3493e3d0..fc5342c210 100644 --- a/rust/libs/algorithm/src/base.rs +++ b/rust/libs/algorithm/src/base.rs @@ -1,15 +1,11 @@ -use anyhow::Result; - pub mod algorithm { - pub trait Base { - fn search(&self, v: &Vec, p: Option<&Param>) -> Result<&Response>; - fn insert(&self, id: &U, v: &Vec, p: Option<&Param>) -> Result<&Response>; - fn update(&self, id: &U, v: &Vec, p: Option<&Param>) -> Result<&Response>; - fn remove(&self, id: &U, p: Option<&Param>) -> Result<&Response>; - fn commit(&self, p: Option<&Param>) -> Result<&Response>; + use anyhow::Result; - fn new(p: Option<&Param>) -> Result<&Self>; - fn open(p: &str) -> Result<&Self>; - fn save(p: &str) -> Result<&Self>; + pub trait Base { + fn search(&self, v: &Vec, p: Option<&SP>) -> Result<&SR>; + fn insert(&self, v: &Vec, id: &U, p: Option<&IP>) -> Result<&IR>; + fn update(&self, v: &Vec, id: &U, p: Option<&UP>) -> Result<&UR>; + fn remove(&self, id: &U, p: Option<&RP>) -> Result<&RR>; + fn commit(&self, p: Option<&CP>) -> Result<&CR>; } } diff --git a/rust/libs/algorithm/src/ngt.rs b/rust/libs/algorithm/src/ngt.rs new file mode 100644 index 0000000000..f1b7ebf8d0 --- /dev/null +++ b/rust/libs/algorithm/src/ngt.rs @@ -0,0 +1,48 @@ +pub mod ngt { + use crate::algorithm::Base; + use anyhow::{anyhow, Ok, Result}; + + #[derive(Debug)] + struct Index {} + + #[derive(Debug)] + pub struct NGT { + index: Index, + } + #[derive(Debug)] + pub struct SearchResult { + id: String, + distance: f32, + } + + #[derive(Debug)] + pub struct SearchResponse { + results: Vec, + } + + #[derive(Debug)] + pub struct SearchParam { + epsilon: f32, + radius: f32, + num: u32, + } + + #[derive(Debug)] + pub struct InsertParam {} + + #[derive(Debug)] + pub struct UpdateParam {} + + #[derive(Debug)] + pub struct RemoveParam {} + + #[derive(Debug)] + pub struct CommitParam{} + + #[derive(Debug)] + pub struct NGTParam {} + + impl Base for NGT { + fn search() + } +} \ No newline at end of file diff --git a/rust/libs/ngt-rs/Cargo.toml b/rust/libs/ngt-rs/Cargo.toml new file mode 100644 index 0000000000..92f108c09d --- /dev/null +++ b/rust/libs/ngt-rs/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "ngt-rs" +version = "0.1.0" +edition = "2021" + +[dependencies] +cxx = { version = "1.0.117", features = ["c++20"] } + +[build-dependencies] +cxx-build = "1.0.117" +miette = { version = "7.1.0", features = ["fancy"] } diff --git a/rust/libs/ngt-rs/build.rs b/rust/libs/ngt-rs/build.rs new file mode 100644 index 0000000000..714dc09d87 --- /dev/null +++ b/rust/libs/ngt-rs/build.rs @@ -0,0 +1,14 @@ +fn main() -> miette::Result<()> {let current_dir = std::env::current_dir().unwrap(); + println!("cargo:rustc-link-search=native={}", current_dir.display()); + println!("cargo:rustc-link-search=native={}", current_dir.display()); + + cxx_build::bridge("src/lib.rs") + .file("src/input.cpp") + .flag_if_supported("-std=c++20") + .compile("ngt-rs"); + + println!("cargo:rustc-link-lib=static=ngt"); + println!("cargo:rustc-link-lib=static=gomp"); + println!("cargo:rerun-if-changed=src/*"); + Ok(()) +} \ No newline at end of file diff --git a/rust/libs/ngt-rs/src/input.cpp b/rust/libs/ngt-rs/src/input.cpp new file mode 100644 index 0000000000..b882a68cdb --- /dev/null +++ b/rust/libs/ngt-rs/src/input.cpp @@ -0,0 +1,42 @@ +#include "ngt-rs/src/input.h" +#include "ngt-rs/src/lib.rs.h" + +Property::Property(): p() {} + +void Property::set_dimension(rust::i32 dimension) { + p.dimension = dimension; +} + +void Property::set_distance_type(const DistanceType t) { + p.distanceType = static_cast(t); +} + +void Property::set_object_type(const ObjectType t) { + p.objectType = static_cast(t); +} + +Index::Index(Property& p) { + index = new NGT::GraphAndTreeIndex(p.p); +} + +rust::u32 Index::insert(const rust::Vec& object) { + std::vector v; + std::copy(object.begin(), object.end(), std::back_inserter(v)); + return index->insert(v); +} + +void Index::create_index(rust::u32 pool_size) { + index->createIndex(pool_size); +} + +void Index::remove(rust::u32 id) { + index->remove(id); +} + +std::unique_ptr new_property() { + return std::make_unique(); +} + +std::unique_ptr new_index(Property& p) { + return std::make_unique(p); +} \ No newline at end of file diff --git a/rust/libs/ngt-rs/src/input.h b/rust/libs/ngt-rs/src/input.h new file mode 100644 index 0000000000..5b1e14de46 --- /dev/null +++ b/rust/libs/ngt-rs/src/input.h @@ -0,0 +1,29 @@ +#pragma once + +#include "NGT/Index.h" +#include +#include "rust/cxx.h" + +enum class DistanceType; +enum class ObjectType; + +class Property { +public: + NGT::Property p; + Property(); + void set_dimension(rust::i32); + void set_object_type(const ObjectType); + void set_distance_type(const DistanceType); +}; + +class Index { + NGT::Index* index; +public: + Index(Property&); + rust::u32 insert(const rust::Vec& object); + void create_index(rust::u32); + void remove(rust::u32); +}; + +std::unique_ptr new_property(); +std::unique_ptr new_index(Property& p); \ No newline at end of file diff --git a/rust/libs/ngt-rs/src/lib.rs b/rust/libs/ngt-rs/src/lib.rs new file mode 100644 index 0000000000..eb4056a41d --- /dev/null +++ b/rust/libs/ngt-rs/src/lib.rs @@ -0,0 +1,54 @@ +#[cxx::bridge] +pub mod ffi { + #[repr(i32)] + enum ObjectType { + None = 0, + Uint8 = 1, + Float = 2, + Float16 = 3, + } + + #[repr(i32)] + enum DistanceType { + None = -1, + L1 = 0, + L2 = 1, + Hamming = 2, + Angle = 3, + Cosine = 4, + NormalizedAngle = 5, + NormalizedCosine = 6, + Jaccard = 7, + SparseJaccard = 8, + NormalizedL2 = 9, + Poincare = 100, + Lorentz = 101, + } + + unsafe extern "C++" { + include!("ngt-rs/src/input.h"); + + type Property; + fn new_property() -> UniquePtr; + fn set_dimension(self: Pin<&mut Property>, dimension: i32); + fn set_object_type(self: Pin<&mut Property>, t: ObjectType); + fn set_distance_type(self: Pin<&mut Property>, t: DistanceType); + + type Index; + fn new_index(p: Pin<&mut Property>) -> UniquePtr; + fn insert(self: Pin<&mut Index>, v: &Vec) -> u32; + fn create_index(self: Pin<&mut Index>, pool_size: u32); + fn remove(self: Pin<&mut Index>, id: u32); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/rust/libs/ngt/Cargo.toml b/rust/libs/ngt/Cargo.toml deleted file mode 100644 index cf9c8f82fe..0000000000 --- a/rust/libs/ngt/Cargo.toml +++ /dev/null @@ -1,23 +0,0 @@ -# -# Copyright (C) 2019-2024 vdaas.org vald team -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# You may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -[package] -name = "ngt" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/rust/libs/ngt/src/lib.rs b/rust/libs/ngt/src/lib.rs deleted file mode 100644 index 958ec94a9e..0000000000 --- a/rust/libs/ngt/src/lib.rs +++ /dev/null @@ -1,29 +0,0 @@ -// -// Copyright (C) 2019-2024 vdaas.org vald team -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// You may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// https://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -pub fn add(left: usize, right: usize) -> usize { - left + right -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); - } -} From dbf693fd5aad2bdcfbc0f79d6021657c590293e3 Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Wed, 21 Feb 2024 12:14:39 +0900 Subject: [PATCH 09/12] fix base trait Signed-off-by: Kosuke Morimoto --- rust/libs/algorithm/src/base.rs | 14 ++++++++------ rust/libs/algorithm/src/ngt.rs | 34 +++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs index fc5342c210..5435aa8194 100644 --- a/rust/libs/algorithm/src/base.rs +++ b/rust/libs/algorithm/src/base.rs @@ -1,11 +1,13 @@ pub mod algorithm { use anyhow::Result; - pub trait Base { - fn search(&self, v: &Vec, p: Option<&SP>) -> Result<&SR>; - fn insert(&self, v: &Vec, id: &U, p: Option<&IP>) -> Result<&IR>; - fn update(&self, v: &Vec, id: &U, p: Option<&UP>) -> Result<&UR>; - fn remove(&self, id: &U, p: Option<&RP>) -> Result<&RR>; - fn commit(&self, p: Option<&CP>) -> Result<&CR>; + pub trait Base { + fn search(&self, v: &Vec, p: Option) -> Result; + fn insert(&self, v: &Vec, id: &U, p: Option) -> Result; + fn update(&self, v: &Vec, id: &U, p: Option) -> Result; + fn remove(&self, id: &U, p: Option) -> Result; + fn commit(&self, p: Option) -> Result; + + fn new(p: Option

) -> Result; } } diff --git a/rust/libs/algorithm/src/ngt.rs b/rust/libs/algorithm/src/ngt.rs index f1b7ebf8d0..c3f562b463 100644 --- a/rust/libs/algorithm/src/ngt.rs +++ b/rust/libs/algorithm/src/ngt.rs @@ -27,9 +27,9 @@ pub mod ngt { num: u32, } - #[derive(Debug)] + #[derive(Debug)] pub struct InsertParam {} - + #[derive(Debug)] pub struct UpdateParam {} @@ -37,12 +37,34 @@ pub mod ngt { pub struct RemoveParam {} #[derive(Debug)] - pub struct CommitParam{} + pub struct CommitParam {} #[derive(Debug)] pub struct NGTParam {} - impl Base for NGT { - fn search() + impl + Base< + f32, + String, + SearchParam, + SearchResponse, + InsertParam, + (), + UpdateParam, + (), + RemoveParam, + (), + CommitParam, + (), + NGTParam, + > for NGT + { + fn search(&self, v: &Vec, p: Option) -> Result; + fn insert(&self, v: &Vec, id: &U, p: Option) -> Result<()>; + fn update(&self, v: &Vec, id: &U, p: Option) -> Result<()>; + fn remove(&self, id: &U, p: Option) -> Result<()>; + fn commit(&self, p: Option) -> Result<()>; + + fn new(p: Option) -> Result; } -} \ No newline at end of file +} From 42175a2a9692c72f03f921c8eba8e1c95c3049fe Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Thu, 7 Mar 2024 17:28:54 +0900 Subject: [PATCH 10/12] add new idea of Base trait Signed-off-by: Kosuke Morimoto --- rust/libs/algorithm/src/base.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs index 5435aa8194..165f5964d8 100644 --- a/rust/libs/algorithm/src/base.rs +++ b/rust/libs/algorithm/src/base.rs @@ -11,3 +11,27 @@ pub mod algorithm { fn new(p: Option

) -> Result; } } + +pub mod algorithm2 { + use anyhow::Result; + + pub trait Search { + fn search(&self, v: &[T], p: Option

) -> Result; + } + + pub trait Insert { + fn insert(&self, v: &[T], id: &U, p: Option

) -> Result; + } + + pub trait Update { + fn update(&self, v: &[T], id: &U, p: Option

) -> Result; + } + + pub trait Remove { + fn remove(&self, id: &U, p: Option

) -> Result; + } + + pub trait Commit { + fn commit(&self, p: Option

) -> Result; + } +} From 78218d221ef504ef68262c327aeb43d497f7ce68 Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Fri, 8 Mar 2024 17:53:21 +0900 Subject: [PATCH 11/12] add new idea of Base trait Signed-off-by: Kosuke Morimoto --- rust/libs/algorithm/Cargo.toml | 1 + rust/libs/algorithm/src/base.rs | 43 ++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/rust/libs/algorithm/Cargo.toml b/rust/libs/algorithm/Cargo.toml index 64035841f6..c501005b70 100644 --- a/rust/libs/algorithm/Cargo.toml +++ b/rust/libs/algorithm/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" anyhow = "1.0.79" serde = { version = "1.0.196", features = ["derive"] } ngt-rs = { path = "../ngt-rs" } +num-traits = "0.2.18" diff --git a/rust/libs/algorithm/src/base.rs b/rust/libs/algorithm/src/base.rs index 165f5964d8..a5bc0735f2 100644 --- a/rust/libs/algorithm/src/base.rs +++ b/rust/libs/algorithm/src/base.rs @@ -1,37 +1,56 @@ -pub mod algorithm { +pub mod algorithm1 { use anyhow::Result; - pub trait Base { - fn search(&self, v: &Vec, p: Option) -> Result; - fn insert(&self, v: &Vec, id: &U, p: Option) -> Result; - fn update(&self, v: &Vec, id: &U, p: Option) -> Result; + pub trait Base: Send + Sync + where + T: num::Num, + U: num::sign::Unsigned, + { + fn search(&self, v: &[T], p: Option) -> Result; + fn insert(&self, v: &[T], id: &U, p: Option) -> Result; + fn update(&self, v: &[T], id: &U, p: Option) -> Result; fn remove(&self, id: &U, p: Option) -> Result; fn commit(&self, p: Option) -> Result; - - fn new(p: Option

) -> Result; } } pub mod algorithm2 { use anyhow::Result; - pub trait Search { + pub trait Search: Send + Sync { fn search(&self, v: &[T], p: Option

) -> Result; } - pub trait Insert { + pub trait Insert: Send + Sync { fn insert(&self, v: &[T], id: &U, p: Option

) -> Result; } - pub trait Update { + pub trait Update: Send + Sync { fn update(&self, v: &[T], id: &U, p: Option

) -> Result; } - pub trait Remove { + pub trait Remove: Send + Sync { fn remove(&self, id: &U, p: Option

) -> Result; } - pub trait Commit { + pub trait Commit: Send + Sync { fn commit(&self, p: Option

) -> Result; } } + +// inspired by https://github.com/jonluca/hora +pub mod algorithm3 { + use anyhow::Result; + + pub trait Base: Send + Sync + where + T: num::Num, + U: num::sign::Unsigned, + { + fn search(&self, v: &[T], p: Option) -> Result>; + fn insert(&mut self, v: &[T], id: &U, p: Option) -> Result<()>; + fn update(&mut self, v: &[T], id: &U, p: Option) -> Result<()>; + fn remove(&mut self, id: &U, p: Option) -> Result<()>; + fn commit(&mut self, p: Option) -> Result<()>; + } +} From f395ae7ba29d818a1950895f6749d387a99f8467 Mon Sep 17 00:00:00 2001 From: Kosuke Morimoto Date: Fri, 8 Mar 2024 17:54:05 +0900 Subject: [PATCH 12/12] update dependency Signed-off-by: Kosuke Morimoto --- rust/Cargo.lock | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 6471bbc010..9aca34e1f5 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -27,6 +27,7 @@ version = "0.1.0" dependencies = [ "anyhow", "ngt-rs", + "num-traits", "serde", ] @@ -562,6 +563,15 @@ dependencies = [ "miette", ] +[[package]] +name = "num-traits" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.32.2"