Skip to content

Commit

Permalink
add KVS trait
Browse files Browse the repository at this point in the history
Signed-off-by: Kosuke Morimoto <[email protected]>
  • Loading branch information
kmrmt committed Feb 20, 2024
1 parent 1f85d2b commit 1449608
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 46 deletions.
4 changes: 4 additions & 0 deletions rust/Cargo.lock

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

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
47 changes: 2 additions & 45 deletions rust/libs/algorithm/src/base.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use anyhow::Result;
use serde::Deserialize;
use serde::Serialize;

pub mod algorithm1 {
pub mod algorithm {
pub trait Base<T, U, Param: Deserialize, Response: Serialize> {
fn search(&self, v: &Vec<T>, p: Option<&Param>) -> Result<&Response>;
fn insert(&self, v: &Vec<T>, p: Option<&Param>) -> Result<&Response>;
fn insert(&self, id: &U, v: &Vec<T>, p: Option<&Param>) -> Result<&Response>;
fn update(&self, id: &U, v: &Vec<T>, p: Option<&Param>) -> Result<&Response>;
fn remove(&self, id: &U, p: Option<&Param>) -> Result<&Response>;
fn commit(&self, p: Option<&Param>) -> Result<&Response>;
Expand All @@ -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<Query: Deserialize, Response: Serialize, Param: Deserialize> {
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<f32>,
size: u64,
epsilon: f32,
radius: f32,
}

#[derive(Serialize, Deserialize, Debug)]
struct InsertQuery {
vector: Vec<f32>,
}
}
8 changes: 8 additions & 0 deletions rust/libs/kvs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
12 changes: 12 additions & 0 deletions rust/libs/kvs/src/kvs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use anyhow::Result;

pub mod kvs {
pub trait KVS<K, V> {
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>;
}
}
14 changes: 14 additions & 0 deletions rust/libs/kvs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 1449608

Please sign in to comment.