Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add bytes affine #667

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
bsbds marked this conversation as resolved.
Show resolved Hide resolved
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());
bsbds marked this conversation as resolved.
Show resolved Hide resolved
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
Loading