Skip to content

Commit

Permalink
close tikv#11217 simplify rangekey
Browse files Browse the repository at this point in the history
Signed-off-by: Connor1996 <zbk602423539@gmail.com>
  • Loading branch information
Connor1996 committed Nov 5, 2021
1 parent a0825b1 commit 5f9fef3
Showing 1 changed file with 9 additions and 60 deletions.
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)
}

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)
}
}
}

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

0 comments on commit 5f9fef3

Please sign in to comment.