From 07f8ac96ef11a46ae5b481ff3d4655b56bccdf39 Mon Sep 17 00:00:00 2001 From: Stu Hood Date: Mon, 15 Dec 2025 15:08:34 -0800 Subject: [PATCH] Rename to `SortByErasedType`. --- src/collector/sort_key/mod.rs | 8 +-- ...ort_by_owned.rs => sort_by_erased_type.rs} | 55 ++++++++++--------- 2 files changed, 32 insertions(+), 31 deletions(-) rename src/collector/sort_key/{sort_by_owned.rs => sort_by_erased_type.rs} (89%) diff --git a/src/collector/sort_key/mod.rs b/src/collector/sort_key/mod.rs index 7c02e4731..3c3ddbd5c 100644 --- a/src/collector/sort_key/mod.rs +++ b/src/collector/sort_key/mod.rs @@ -1,12 +1,12 @@ mod order; -mod sort_by_owned; +mod sort_by_erased_type; mod sort_by_score; mod sort_by_static_fast_value; mod sort_by_string; mod sort_key_computer; pub use order::*; -pub use sort_by_owned::SortByOwnedValue; +pub use sort_by_erased_type::SortByErasedType; pub use sort_by_score::SortBySimilarityScore; pub use sort_by_static_fast_value::SortByStaticFastValue; pub use sort_by_string::SortByString; @@ -18,7 +18,7 @@ mod tests { use std::ops::Range; use crate::collector::sort_key::{ - SortByOwnedValue, SortBySimilarityScore, SortByStaticFastValue, SortByString, + SortByErasedType, SortBySimilarityScore, SortByStaticFastValue, SortByString, }; use crate::collector::{ComparableDoc, DocSetCollector, TopDocs}; use crate::indexer::NoMergePolicy; @@ -341,7 +341,7 @@ mod tests { let top_collector = TopDocs::with_limit(4).order_by::<(Score, OwnedValue)>(( (SortBySimilarityScore, score_order), - (SortByOwnedValue::for_field("city"), city_order), + (SortByErasedType::for_field("city"), city_order), )); let results: Vec<((Score, OwnedValue), DocAddress)> = searcher.search(&AllQuery, &top_collector)?; diff --git a/src/collector/sort_key/sort_by_owned.rs b/src/collector/sort_key/sort_by_erased_type.rs similarity index 89% rename from src/collector/sort_key/sort_by_owned.rs rename to src/collector/sort_key/sort_by_erased_type.rs index 27eed1ac8..4f9365d06 100644 --- a/src/collector/sort_key/sort_by_owned.rs +++ b/src/collector/sort_key/sort_by_erased_type.rs @@ -14,12 +14,14 @@ use crate::{DateTime, DocId, Score}; /// are not known until runtime. But it comes with a performance cost: wherever possible, prefer to /// use a SortKeyComputer implementation with a known-type at compile time. #[derive(Debug, Clone)] -pub enum SortByOwnedValue { +pub enum SortByErasedType { + /// Sort by a fast field Field(String), + /// Sort by score Score, } -impl SortByOwnedValue { +impl SortByErasedType { /// Creates a new sort key computer which will sort by the given fast field column, with type /// erasure. pub fn for_field(column_name: impl ToString) -> Self { @@ -32,18 +34,17 @@ impl SortByOwnedValue { } } -/// TODO: Rename to Boxed...? Or Owned? -trait AnySegmentSortKeyComputer: Send + Sync { +trait ErasedSegmentSortKeyComputer: Send + Sync { fn segment_sort_key(&mut self, doc: DocId, score: Score) -> Option; fn convert_segment_sort_key(&self, sort_key: Option) -> OwnedValue; } -struct AnySegmentSortKeyComputerWrapper { +struct ErasedSegmentSortKeyComputerWrapper { inner: C, converter: F, } -impl AnySegmentSortKeyComputer for AnySegmentSortKeyComputerWrapper +impl ErasedSegmentSortKeyComputer for ErasedSegmentSortKeyComputerWrapper where C: SegmentSortKeyComputer> + Send + Sync, F: Fn(C::SortKey) -> OwnedValue + Send + Sync + 'static, @@ -62,7 +63,7 @@ struct ScoreSegmentSortKeyComputer { segment_computer: SortBySimilarityScore, } -impl AnySegmentSortKeyComputer for ScoreSegmentSortKeyComputer { +impl ErasedSegmentSortKeyComputer for ScoreSegmentSortKeyComputer { fn segment_sort_key(&mut self, doc: DocId, score: Score) -> Option { let score_value: f64 = self.segment_computer.segment_sort_key(doc, score).into(); Some(score_value.to_u64()) @@ -74,9 +75,9 @@ impl AnySegmentSortKeyComputer for ScoreSegmentSortKeyComputer { } } -impl SortKeyComputer for SortByOwnedValue { +impl SortKeyComputer for SortByErasedType { type SortKey = OwnedValue; - type Child = ByOwnedValueColumnSegmentSortKeyComputer; + type Child = ErasedColumnSegmentSortKeyComputer; type Comparator = NaturalComparator; fn requires_scoring(&self) -> bool { @@ -87,7 +88,7 @@ impl SortKeyComputer for SortByOwnedValue { &self, segment_reader: &crate::SegmentReader, ) -> crate::Result { - let inner: Box = match self { + let inner: Box = match self { Self::Field(column_name) => { let fast_fields = segment_reader.fast_fields(); // TODO: We currently double-open the column to avoid relying on the implementation @@ -106,7 +107,7 @@ impl SortKeyComputer for SortByOwnedValue { ColumnType::Str => { let computer = SortByString::for_field(column_name); let inner = computer.segment_sort_key_computer(segment_reader)?; - Box::new(AnySegmentSortKeyComputerWrapper { + Box::new(ErasedSegmentSortKeyComputerWrapper { inner, converter: |val: Option| { val.map(OwnedValue::Str).unwrap_or(OwnedValue::Null) @@ -116,7 +117,7 @@ impl SortKeyComputer for SortByOwnedValue { ColumnType::U64 => { let computer = SortByStaticFastValue::::for_field(column_name); let inner = computer.segment_sort_key_computer(segment_reader)?; - Box::new(AnySegmentSortKeyComputerWrapper { + Box::new(ErasedSegmentSortKeyComputerWrapper { inner, converter: |val: Option| { val.map(OwnedValue::U64).unwrap_or(OwnedValue::Null) @@ -126,7 +127,7 @@ impl SortKeyComputer for SortByOwnedValue { ColumnType::I64 => { let computer = SortByStaticFastValue::::for_field(column_name); let inner = computer.segment_sort_key_computer(segment_reader)?; - Box::new(AnySegmentSortKeyComputerWrapper { + Box::new(ErasedSegmentSortKeyComputerWrapper { inner, converter: |val: Option| { val.map(OwnedValue::I64).unwrap_or(OwnedValue::Null) @@ -136,7 +137,7 @@ impl SortKeyComputer for SortByOwnedValue { ColumnType::F64 => { let computer = SortByStaticFastValue::::for_field(column_name); let inner = computer.segment_sort_key_computer(segment_reader)?; - Box::new(AnySegmentSortKeyComputerWrapper { + Box::new(ErasedSegmentSortKeyComputerWrapper { inner, converter: |val: Option| { val.map(OwnedValue::F64).unwrap_or(OwnedValue::Null) @@ -146,7 +147,7 @@ impl SortKeyComputer for SortByOwnedValue { ColumnType::Bool => { let computer = SortByStaticFastValue::::for_field(column_name); let inner = computer.segment_sort_key_computer(segment_reader)?; - Box::new(AnySegmentSortKeyComputerWrapper { + Box::new(ErasedSegmentSortKeyComputerWrapper { inner, converter: |val: Option| { val.map(OwnedValue::Bool).unwrap_or(OwnedValue::Null) @@ -156,7 +157,7 @@ impl SortKeyComputer for SortByOwnedValue { ColumnType::DateTime => { let computer = SortByStaticFastValue::::for_field(column_name); let inner = computer.segment_sort_key_computer(segment_reader)?; - Box::new(AnySegmentSortKeyComputerWrapper { + Box::new(ErasedSegmentSortKeyComputerWrapper { inner, converter: |val: Option| { val.map(OwnedValue::Date).unwrap_or(OwnedValue::Null) @@ -176,15 +177,15 @@ impl SortKeyComputer for SortByOwnedValue { segment_computer: SortBySimilarityScore, }), }; - Ok(ByOwnedValueColumnSegmentSortKeyComputer { inner }) + Ok(ErasedColumnSegmentSortKeyComputer { inner }) } } -pub struct ByOwnedValueColumnSegmentSortKeyComputer { - inner: Box, +pub struct ErasedColumnSegmentSortKeyComputer { + inner: Box, } -impl SegmentSortKeyComputer for ByOwnedValueColumnSegmentSortKeyComputer { +impl SegmentSortKeyComputer for ErasedColumnSegmentSortKeyComputer { type SortKey = OwnedValue; type SegmentSortKey = Option; type SegmentComparator = NaturalComparator; @@ -201,7 +202,7 @@ impl SegmentSortKeyComputer for ByOwnedValueColumnSegmentSortKeyComputer { #[cfg(test)] mod tests { - use crate::collector::sort_key::{ComparatorEnum, SortByOwnedValue}; + use crate::collector::sort_key::{ComparatorEnum, SortByErasedType}; use crate::collector::TopDocs; use crate::query::AllQuery; use crate::schema::{OwnedValue, Schema, FAST, TEXT}; @@ -223,7 +224,7 @@ mod tests { let searcher = reader.searcher(); let collector = TopDocs::with_limit(10) - .order_by((SortByOwnedValue::for_field("id"), ComparatorEnum::Natural)); + .order_by((SortByErasedType::for_field("id"), ComparatorEnum::Natural)); let top_docs = searcher.search(&AllQuery, &collector).unwrap(); let values: Vec = top_docs.into_iter().map(|(key, _)| key).collect(); @@ -234,7 +235,7 @@ mod tests { ); let collector = TopDocs::with_limit(10).order_by(( - SortByOwnedValue::for_field("id"), + SortByErasedType::for_field("id"), ComparatorEnum::ReverseNoneLower, )); let top_docs = searcher.search(&AllQuery, &collector).unwrap(); @@ -263,7 +264,7 @@ mod tests { let searcher = reader.searcher(); let collector = TopDocs::with_limit(10).order_by(( - SortByOwnedValue::for_field("city"), + SortByErasedType::for_field("city"), ComparatorEnum::ReverseNoneLower, )); let top_docs = searcher.search(&AllQuery, &collector).unwrap(); @@ -296,7 +297,7 @@ mod tests { let searcher = reader.searcher(); let collector = TopDocs::with_limit(10) - .order_by((SortByOwnedValue::for_field("id"), ComparatorEnum::Reverse)); + .order_by((SortByErasedType::for_field("id"), ComparatorEnum::Reverse)); let top_docs = searcher.search(&AllQuery, &collector).unwrap(); let values: Vec = top_docs.into_iter().map(|(key, _)| key).collect(); @@ -325,7 +326,7 @@ mod tests { // Sort by score descending (Natural) let collector = TopDocs::with_limit(10) - .order_by((SortByOwnedValue::for_score(), ComparatorEnum::Natural)); + .order_by((SortByErasedType::for_score(), ComparatorEnum::Natural)); let top_docs = searcher.search(&query, &collector).unwrap(); let values: Vec = top_docs @@ -341,7 +342,7 @@ mod tests { // Sort by score ascending (ReverseNoneLower) let collector = TopDocs::with_limit(10).order_by(( - SortByOwnedValue::for_score(), + SortByErasedType::for_score(), ComparatorEnum::ReverseNoneLower, )); let top_docs = searcher.search(&query, &collector).unwrap();