store_api/storage/
types.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Common types.
16
17use datatypes::arrow::array::{BooleanArray, Datum, UInt64Array};
18
19/// Represents a sequence number of data in storage. The offset of logstore can be used
20/// as a sequence number.
21pub type SequenceNumber = u64;
22
23/// A range of sequence numbers.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub enum SequenceRange {
26    Gt {
27        /// Exclusive lower bound
28        min: SequenceNumber,
29    },
30    LtEq {
31        /// Inclusive upper bound
32        max: SequenceNumber,
33    },
34    GtLtEq {
35        /// Exclusive lower bound
36        min: SequenceNumber,
37        /// Inclusive upper bound
38        max: SequenceNumber,
39    },
40}
41
42impl SequenceRange {
43    pub fn new(min: Option<SequenceNumber>, max: Option<SequenceNumber>) -> Option<Self> {
44        match (min, max) {
45            (Some(min), Some(max)) => Some(SequenceRange::GtLtEq { min, max }),
46            (Some(min), None) => Some(SequenceRange::Gt { min }),
47            (None, Some(max)) => Some(SequenceRange::LtEq { max }),
48            (None, None) => None,
49        }
50    }
51
52    pub fn filter(
53        &self,
54        seqs: &dyn Datum,
55    ) -> Result<BooleanArray, datatypes::arrow::error::ArrowError> {
56        match self {
57            SequenceRange::Gt { min } => {
58                let min = UInt64Array::new_scalar(*min);
59                let pred = datafusion_common::arrow::compute::kernels::cmp::gt(seqs, &min)?;
60                Ok(pred)
61            }
62            SequenceRange::LtEq { max } => {
63                let max = UInt64Array::new_scalar(*max);
64                let pred = datafusion_common::arrow::compute::kernels::cmp::lt_eq(seqs, &max)?;
65                Ok(pred)
66            }
67            SequenceRange::GtLtEq { min, max } => {
68                let min = UInt64Array::new_scalar(*min);
69                let max = UInt64Array::new_scalar(*max);
70                let pred_min = datafusion_common::arrow::compute::kernels::cmp::gt(seqs, &min)?;
71                let pred_max = datafusion_common::arrow::compute::kernels::cmp::lt_eq(seqs, &max)?;
72                datafusion_common::arrow::compute::kernels::boolean::and(&pred_min, &pred_max)
73            }
74        }
75    }
76}