caio.co/de/cantine

Rename `Feature*` to `Aggregable*`

I'm still missing something about lifetimes (perhaps just practice?) for
I can't seem to be able to rephrase all this into something that would
allow both owned and borrowed types- I mean, without inverting the
control and making the API terribly annoying to use...
Id
4221b887dc554ff57f16881c4dbd4508e0344728
Author
Caio
Commit time
2020-02-07T10:48:43+01:00

Modified cantine_derive/src/lib.rs

@@ -6,7 +6,7
DocId, Result, Score, SegmentLocalId, SegmentReader,
};

-pub use cantine_derive_internal::{AggregationQuery, FilterQuery};
+pub use cantine_derive_internal::{Aggregable, FilterQuery};

#[derive(Serialize, Debug, Clone)]
pub struct RangeStats<T> {
@@ -63,20 +63,20
fn from_query(query: &Q) -> Self;
}

-pub trait Feature: Sized + Sync {
+pub trait Aggregable: Sized + Sync {
type Query: Sync + Clone;
type Agg: Aggregator<Self::Query, Self>;
}

-pub trait FeatureForSegment<T>: Sync {
- type Output: FeatureForDoc<T>;
+pub trait AggregableForSegment<T>: Sync {
+ type Output: AggregableForDoc<T>;
fn for_segment(&self, reader: &SegmentReader) -> Self::Output;
}

-impl<T, F, O> FeatureForSegment<T> for F
+impl<T, F, O> AggregableForSegment<T> for F
where
F: Sync + Fn(&SegmentReader) -> O,
- O: FeatureForDoc<T>,
+ O: AggregableForDoc<T>,
{
type Output = O;

@@ -85,16 +85,16
}
}

-pub struct FeatureCollector<T: Feature, F> {
+pub struct AggregableCollector<T: Aggregable, F> {
query: T::Query,
reader_factory: F,
}

-impl<T, F, O> FeatureCollector<T, F>
+impl<T, F, O> AggregableCollector<T, F>
where
- T: 'static + Feature,
- F: FeatureForSegment<T, Output = O>,
- O: 'static + FeatureForDoc<T>,
+ T: 'static + Aggregable,
+ F: AggregableForSegment<T, Output = O>,
+ O: 'static + AggregableForDoc<T>,
{
pub fn new(query: T::Query, reader_factory: F) -> Self {
Self {
@@ -104,21 +104,21
}
}

-impl<T, F, O> Collector for FeatureCollector<T, F>
+impl<T, F, O> Collector for AggregableCollector<T, F>
where
- T: 'static + Feature,
- F: FeatureForSegment<T, Output = O>,
- O: 'static + FeatureForDoc<T>,
+ T: 'static + Aggregable,
+ F: AggregableForSegment<T, Output = O>,
+ O: 'static + AggregableForDoc<T>,
{
type Fruit = T::Agg;
- type Child = FeatureSegmentCollector<T, O>;
+ type Child = AggregableSegmentCollector<T, O>;

fn for_segment(
&self,
_segment_id: SegmentLocalId,
segment_reader: &SegmentReader,
) -> Result<Self::Child> {
- Ok(FeatureSegmentCollector {
+ Ok(AggregableSegmentCollector {
agg: T::Agg::from_query(&self.query),
query: self.query.clone(),
reader: self.reader_factory.for_segment(segment_reader),
@@ -144,11 +144,11
}
}

-pub trait FeatureForDoc<T> {
+pub trait AggregableForDoc<T> {
fn for_doc(&self, doc: DocId) -> Option<T>;
}

-impl<T, F> FeatureForDoc<T> for F
+impl<T, F> AggregableForDoc<T> for F
where
F: Fn(DocId) -> Option<T>,
{
@@ -157,16 +157,16
}
}

-pub struct FeatureSegmentCollector<T: Feature, F> {
+pub struct AggregableSegmentCollector<T: Aggregable, F> {
agg: T::Agg,
query: T::Query,
reader: F,
}

-impl<T, F> SegmentCollector for FeatureSegmentCollector<T, F>
+impl<T, F> SegmentCollector for AggregableSegmentCollector<T, F>
where
- T: 'static + Feature,
- F: 'static + FeatureForDoc<T>,
+ T: 'static + Aggregable,
+ F: 'static + AggregableForDoc<T>,
{
type Fruit = T::Agg;

@@ -209,7 +209,7
}
}

- impl Feature for i16 {
+ impl Aggregable for i16 {
type Query = Vec<Range<i16>>;
type Agg = Vec<i16>;
}
@@ -233,7 +233,7
let reader = index.reader()?;
let searcher = reader.searcher();

- let ranges_collector = FeatureCollector::<i16, _>::new(
+ let ranges_collector = AggregableCollector::<i16, _>::new(
vec![-10..0, 0..10, -2..4],
move |reader: &SegmentReader| {
let bytes_reader = reader.fast_fields().bytes(bytes_field).unwrap();

Modified cantine_derive/tests/aggregation_query.rs

@@ -1,11 +1,11
use bincode;

use serde::{Deserialize, Serialize};
use tantivy::{query::AllQuery, schema::SchemaBuilder, Document, Index, SegmentReader};

-use cantine_derive::{AggregationQuery, FeatureCollector, RangeStats};
+use cantine_derive::{Aggregable, AggregableCollector, RangeStats};

-#[derive(AggregationQuery, Serialize, Deserialize, Default)]
+#[derive(Aggregable, Serialize, Deserialize, Default)]
pub struct Feat {
pub a: u64,
pub b: Option<i16>,
@@ -156,10 +156,11
d: vec![42.0..100.0],
};

- let collector = FeatureCollector::<Feat, _>::new(query, move |seg_reader: &SegmentReader| {
- let reader = seg_reader.fast_fields().bytes(bytes_field).unwrap();
- move |doc| bincode::deserialize(reader.get_bytes(doc)).ok()
- });
+ let collector =
+ AggregableCollector::<Feat, _>::new(query, move |seg_reader: &SegmentReader| {
+ let reader = seg_reader.fast_fields().bytes(bytes_field).unwrap();
+ move |doc| bincode::deserialize(reader.get_bytes(doc)).ok()
+ });

let reader = index.reader()?;
let searcher = reader.searcher();

Modified cantine_derive/internal/src/lib.rs

@@ -18,7 +18,7
})
}

-#[proc_macro_derive(AggregationQuery)]
+#[proc_macro_derive(Aggregable)]
pub fn derive_agg(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);

@@ -327,7 +327,7
#(#fields),*
}

- impl cantine_derive::Feature for #feature {
+ impl cantine_derive::Aggregable for #feature {
type Query = #agg_query;
type Agg = #name;
}