Skip to content

Commit

Permalink
Merge pull request #2 from frew/fix-gc-keys
Browse files Browse the repository at this point in the history
Make RangeKey an enum with default ordering
  • Loading branch information
Connor1996 authored Nov 5, 2021
2 parents a0825b1 + d5985cf commit c410ef1
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.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

0 comments on commit c410ef1

Please sign in to comment.