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

Make RangeKey an enum with default ordering #2

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Changes from all commits
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
69 changes: 9 additions & 60 deletions components/raftstore/src/coprocessor/region_info_accessor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.

use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::collections::Bound::{Excluded, Included, Unbounded};
use std::collections::Bound::{Excluded, Unbounded};
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::sync::{mpsc, Mutex};
use std::time::Duration;
Expand Down Expand Up @@ -72,76 +71,26 @@ type RegionRangesMap = BTreeMap<RangeKey, u64>;
// RangeKey is a wrapper used to unify the comparsion between region start key
// and region end key. Region end key is special as empty stands for the infinite,
// so we need to take special care for cases where the end key is empty.
#[derive(Clone, Debug)]
pub struct RangeKey {
key: Vec<u8>,
is_end_key: bool,
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum RangeKey {
Finite(Vec<u8>),
Infinite,
}

impl RangeKey {
pub fn from_start_key(key: Vec<u8>) -> Self {
Self {
key: key.to_vec(),
is_end_key: false,
}
RangeKey::Finite(key.to_vec())
}

pub fn from_end_key(key: Vec<u8>) -> Self {
Self {
key: key.to_vec(),
is_end_key: true,
}
}
}

impl PartialEq for RangeKey {
fn eq(&self, other: &Self) -> bool {
if self.key.is_empty() && other.key.is_empty() {
self.is_end_key == other.is_end_key
} else {
self.key.eq(&other.key)
}
}
}

impl Eq for RangeKey {}

impl Ord for RangeKey {
fn cmp(&self, other: &Self) -> Ordering {
if self.is_end_key && other.is_end_key {
if self.key.is_empty() && other.key.is_empty() {
Ordering::Equal
} else if self.key.is_empty() {
Ordering::Greater
} else if other.key.is_empty() {
Ordering::Less
} else {
self.key.cmp(&other.key)
}
} else if self.is_end_key && !other.is_end_key {
if self.key.is_empty() {
Ordering::Greater
} else {
self.key.cmp(&other.key)
}
} else if !self.is_end_key && other.is_end_key {
if other.key.is_empty() {
Ordering::Less
} else {
self.key.cmp(&other.key)
}
if key.is_empty() {
RangeKey::Infinite
} else {
self.key.cmp(&other.key)
RangeKey::Finite(key.to_vec())
}
}
}

impl PartialOrd for RangeKey {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

pub type Callback<T> = Box<dyn FnOnce(T) + Send>;
pub type SeekRegionCallback = Box<dyn FnOnce(&mut dyn Iterator<Item = &RegionInfo>) + Send>;

Expand Down