| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use crate::clubcard::ClubcardIndexEntry; |
| 6 |
use crate::equation::Equation; |
| 7 |
|
| 8 |
pub trait AsQuery<const W: usize> { |
| 9 |
/// Hash this item to a homogeneous equation (s, a) such that |
| 10 |
/// (1) s is uniform in {0, 1, ..., m-1}, |
| 11 |
/// (2) a satisfies the alignment requirement (a\[0\] & 1 == 1) but is otherwise uniformly random, |
| 12 |
fn as_query(&self, m: usize) -> Equation<W>; |
| 13 |
|
| 14 |
/// The block that this item belongs in. |
| 15 |
fn block(&self) -> &[u8]; |
| 16 |
|
| 17 |
/// A unique identifier for this item. If this item cannot be inserted into the linear system, |
| 18 |
/// then we will store its `included()` status in a secondary retrieval mechanism keyed by |
| 19 |
/// `discriminant()`. |
| 20 |
fn discriminant(&self) -> &[u8]; |
| 21 |
|
| 22 |
#[doc(hidden)] |
| 23 |
fn as_approx_query(&self, meta: &ClubcardIndexEntry) -> Equation<W> { |
| 24 |
let mut approx_eq = self.as_query(meta.approx_filter_m); |
| 25 |
approx_eq.s += meta.approx_filter_offset; |
| 26 |
approx_eq |
| 27 |
} |
| 28 |
|
| 29 |
#[doc(hidden)] |
| 30 |
fn as_exact_query(&self, meta: &ClubcardIndexEntry) -> Equation<W> { |
| 31 |
let mut exact_eq = self.as_query(meta.exact_filter_m); |
| 32 |
exact_eq.s += meta.exact_filter_offset; |
| 33 |
exact_eq |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/// A Filterable is an item that can be inserted into a RibbonBuilder. |
| 38 |
pub trait Filterable<const W: usize>: AsQuery<W> { |
| 39 |
/// Whether this item should be included in an exact filter. |
| 40 |
fn included(&self) -> bool { |
| 41 |
false |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/// A Queryable is an item that can be passed to Clubcard::contains. |
| 46 |
pub trait Queryable<const W: usize>: AsQuery<W> { |
| 47 |
/// The universe metadata is stored in the Clubcard and passed to Queryable::in_universe before |
| 48 |
/// a query is performed. The query is performed only if Queryable::in_universe returns true. |
| 49 |
type UniverseMetadata; |
| 50 |
|
| 51 |
/// The partition metadata is stored in the Clubcard and is used to construct a Queryable with |
| 52 |
/// the correct block identifier. For example, a clubcard that encodes a subset of the integers |
| 53 |
/// {0, ..., n} might have a set of named non-overlapping intervals as its partition metadata: |
| 54 |
/// { "a": {0, ..., 32}, "b": {33, ..., 100}, ... }. To perform a membership test for 37, |
| 55 |
/// the user would use the partition metadata to construct a Queryable with block = "b" and |
| 56 |
/// discriminant = 37. |
| 57 |
type PartitionMetadata; |
| 58 |
|
| 59 |
fn in_universe(&self, meta: &Self::UniverseMetadata) -> bool; |
| 60 |
} |