use arrow::row::{Row, Rows};
use std::cmp::Ordering;
pub struct SortKeyCursor {
stream_idx: usize,
cur_row: usize,
num_rows: usize,
batch_id: usize,
rows: Rows,
}
impl std::fmt::Debug for SortKeyCursor {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.debug_struct("SortKeyCursor")
.field("cur_row", &self.cur_row)
.field("num_rows", &self.num_rows)
.field("batch_id", &self.batch_id)
.finish()
}
}
impl SortKeyCursor {
pub fn new(stream_idx: usize, batch_id: usize, rows: Rows) -> Self {
Self {
stream_idx,
cur_row: 0,
num_rows: rows.num_rows(),
batch_id,
rows,
}
}
#[inline(always)]
pub fn stream_idx(&self) -> usize {
self.stream_idx
}
#[inline(always)]
pub fn batch_id(&self) -> usize {
self.batch_id
}
#[inline(always)]
pub fn is_finished(&self) -> bool {
self.num_rows == self.cur_row
}
#[inline(always)]
pub fn advance(&mut self) -> usize {
assert!(!self.is_finished());
let t = self.cur_row;
self.cur_row += 1;
t
}
fn current(&self) -> Row<'_> {
self.rows.row(self.cur_row)
}
}
impl PartialEq for SortKeyCursor {
fn eq(&self, other: &Self) -> bool {
self.current() == other.current()
}
}
impl Eq for SortKeyCursor {}
impl PartialOrd for SortKeyCursor {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for SortKeyCursor {
fn cmp(&self, other: &Self) -> Ordering {
match (self.is_finished(), other.is_finished()) {
(true, true) => Ordering::Equal,
(_, true) => Ordering::Less,
(true, _) => Ordering::Greater,
_ => self
.current()
.cmp(&other.current())
.then_with(|| self.stream_idx.cmp(&other.stream_idx)),
}
}
}