diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9c97873377..9aca34e1f5 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -21,6 +21,16 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" name = "agent" version = "0.1.0" +[[package]] +name = "algorithm" +version = "0.1.0" +dependencies = [ + "anyhow", + "ngt-rs", + "num-traits", + "serde", +] + [[package]] name = "anyhow" version = "1.0.79" @@ -74,7 +84,7 @@ checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" dependencies = [ "async-trait", "axum-core", - "bitflags", + "bitflags 1.3.2", "bytes", "futures-util", "http", @@ -126,6 +136,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" @@ -138,6 +157,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" @@ -156,6 +181,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" @@ -168,6 +247,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" @@ -351,6 +440,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" @@ -366,12 +461,31 @@ 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" 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" @@ -384,6 +498,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" @@ -407,12 +551,26 @@ 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 = "num-traits" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" +dependencies = [ + "autocfg", +] [[package]] name = "object" @@ -429,6 +587,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" @@ -569,12 +733,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" @@ -604,6 +787,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" @@ -611,9 +800,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" @@ -631,6 +841,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" @@ -643,7 +903,7 @@ dependencies = [ "mio", "pin-project-lite", "socket2", - "windows-sys", + "windows-sys 0.48.0", ] [[package]] @@ -794,6 +1054,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" @@ -809,13 +1081,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]] @@ -824,13 +1136,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]] @@ -839,38 +1166,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 082e358313..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"] +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 new file mode 100644 index 0000000000..c501005b70 --- /dev/null +++ b/rust/libs/algorithm/Cargo.toml @@ -0,0 +1,12 @@ +[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] +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 new file mode 100644 index 0000000000..a5bc0735f2 --- /dev/null +++ b/rust/libs/algorithm/src/base.rs @@ -0,0 +1,56 @@ +pub mod algorithm1 { + 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(&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; + } +} + +pub mod algorithm2 { + use anyhow::Result; + + pub trait Search: Send + Sync { + fn search(&self, v: &[T], p: Option

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

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

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

) -> Result; + } + + 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<()>; + } +} 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); + } +} diff --git a/rust/libs/algorithm/src/ngt.rs b/rust/libs/algorithm/src/ngt.rs new file mode 100644 index 0000000000..c3f562b463 --- /dev/null +++ b/rust/libs/algorithm/src/ngt.rs @@ -0,0 +1,70 @@ +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< + 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; + } +} 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); + } +} 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); - } -}