Skip to content

Commit

Permalink
feat: add BytesAffine implementation
Browse files Browse the repository at this point in the history
Signed-off-by: bsbds <[email protected]>
  • Loading branch information
bsbds committed Mar 18, 2024
1 parent 0cfa232 commit 55b8ee0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/xlineapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ prost = "0.12.3"
serde = { version = "1.0.137", features = ["derive"] }
thiserror = "1.0.37"
tonic = { version = "0.4.1", package = "madsim-tonic" }
utils = { path = "../utils" }
workspace-hack = { version = "0.1", path = "../../workspace-hack" }

[build-dependencies]
Expand Down
76 changes: 76 additions & 0 deletions crates/xlineapi/src/interval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::cmp;

use utils::interval_map::Interval;

use crate::command::KeyRange;

impl From<KeyRange> for Interval<BytesAffine> {
fn from(range: KeyRange) -> Self {
let start = range.range_start().to_vec();
let end = match range.range_end() {
&[] => {
let mut end = start.clone();
end.push(0);
BytesAffine::Bytes(end)
}
&[0] => BytesAffine::Unbounded,
bytes => BytesAffine::Bytes(bytes.to_vec()),
};
Interval::new(BytesAffine::Bytes(start), end)
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BytesAffine {
/// Bytes
Bytes(Vec<u8>),
/// Unbounded
Unbounded,
}

impl BytesAffine {
pub fn new_key(bytes: impl Into<Vec<u8>>) -> Self {
Self::Bytes(bytes.into())
}

pub fn new_unbounded() -> Self {
Self::Unbounded
}
}

impl PartialOrd for BytesAffine {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
match (self, other) {
(BytesAffine::Bytes(x), BytesAffine::Bytes(y)) => x.partial_cmp(y),
(BytesAffine::Bytes(_), BytesAffine::Unbounded) => Some(cmp::Ordering::Less),
(BytesAffine::Unbounded, BytesAffine::Bytes(_)) => Some(cmp::Ordering::Greater),
(BytesAffine::Unbounded, BytesAffine::Unbounded) => Some(cmp::Ordering::Equal),
}
}
}

impl Ord for BytesAffine {
fn cmp(&self, other: &Self) -> cmp::Ordering {
match (self, other) {
(BytesAffine::Bytes(x), BytesAffine::Bytes(y)) => x.cmp(y),
(BytesAffine::Bytes(_), BytesAffine::Unbounded) => cmp::Ordering::Less,
(BytesAffine::Unbounded, BytesAffine::Bytes(_)) => cmp::Ordering::Greater,
(BytesAffine::Unbounded, BytesAffine::Unbounded) => cmp::Ordering::Equal,
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn bytes_affine_cmp_is_ok() {
assert_eq!(BytesAffine::new_key("abc"), BytesAffine::new_key("abc"));
assert!(BytesAffine::new_key("a") < BytesAffine::new_key("b"));
assert!(BytesAffine::new_key("abcd") < BytesAffine::new_key("b"));
assert!(BytesAffine::new_key("abcd") < BytesAffine::new_unbounded());
assert!(BytesAffine::new_key("123") < BytesAffine::new_unbounded());
assert_eq!(BytesAffine::new_unbounded(), BytesAffine::new_unbounded());
}
}
1 change: 1 addition & 0 deletions crates/xlineapi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@

pub mod command;
pub mod execute_error;
pub mod interval;
pub mod request_validation;

mod etcdserverpb {
Expand Down

0 comments on commit 55b8ee0

Please sign in to comment.