Blob cantine_derive/src/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
use std::{marker::PhantomData, ops::Range}; use serde::Serialize; use tantivy::{ collector::{Collector, SegmentCollector}, DocId, Result, Score, SegmentLocalId, SegmentReader, }; pub use cantine_derive_internal::FilterAndAggregation; #[derive(Serialize, Debug, Clone)] pub struct RangeStats<T> { pub min: T, pub max: T, pub count: u64, } impl<T> RangeStats<T> where T: PartialOrd + Copy, { pub fn collect(&mut self, value: T) { if self.min > value { self.min = value; } if self.max < value { self.max = value; } self.count += 1; } pub fn merge(&mut self, other: &Self) { if self.min > other.min { self.min = other.min; } if self.max < other.max { self.max = other.max; } self.count += other.count; } } impl<T> From<&Range<T>> for RangeStats<T> where T: PartialOrd + Copy, { fn from(src: &Range<T>) -> Self { Self { min: src.end, max: src.start, count: 0, } } } pub trait Mergeable: Send + Sync { fn merge_same_size(&mut self, other: &Self); } pub trait Feature<TQuery>: Sync { type Agg: Mergeable + for<'a> From<&'a TQuery>; fn collect_into(&self, query: &TQuery, agg: &mut Self::Agg); } pub trait FeatureForSegment<T>: Sync { type Output: FeatureForDoc<T>; fn for_segment(&self, reader: &SegmentReader) -> Self::Output; } impl<T, F, O> FeatureForSegment<T> for F where F: Sync + Fn(&SegmentReader) -> O, O: FeatureForDoc<T>, { type Output = O; fn for_segment(&self, reader: &SegmentReader) -> Self::Output { (self)(reader) } } struct FeatureCollector<T, Q, F> { query: Q, reader_factory: F, _marker: PhantomData<T>, } impl<T, A, Q, F, O> FeatureCollector<T, Q, F> where T: 'static + Feature<Q, Agg = A>, Q: 'static + Clone + Sync, A: 'static + Mergeable + for<'a> From<&'a Q>, F: FeatureForSegment<T, Output = O>, O: 'static + FeatureForDoc<T>, { pub fn new(query: Q, reader_factory: F) -> Self { Self { query, reader_factory, _marker: PhantomData, } } } impl<T, A, Q, F, O> Collector for FeatureCollector<T, Q, F> where T: 'static + Feature<Q, Agg = A>, Q: 'static + Clone + Sync, A: 'static + Mergeable + for<'a> From<&'a Q>, F: FeatureForSegment<T, Output = O>, O: 'static + FeatureForDoc<T>, { type Fruit = A; type Child = FeatureSegmentCollector<T, A, Q, O>; fn for_segment( &self, _segment_id: SegmentLocalId, segment_reader: &SegmentReader, ) -> Result<Self::Child> { Ok(FeatureSegmentCollector { agg: A::from(&self.query), query: self.query.clone(), reader: self.reader_factory.for_segment(segment_reader), _marker: PhantomData, }) } fn requires_scoring(&self) -> bool { false } fn merge_fruits(&self, fruits: Vec<Self::Fruit>) -> Result<Self::Fruit> { let mut iter = fruits.into_iter(); let mut first = iter.next().unwrap_or_else(|| A::from(&self.query)); for fruit in iter { first.merge_same_size(&fruit); } Ok(first) } } pub trait FeatureForDoc<T> { fn for_doc(&self, doc: DocId) -> Option<T>; } impl<T, F> FeatureForDoc<T> for F where F: Fn(DocId) -> Option<T>, { fn for_doc(&self, doc: DocId) -> Option<T> { (self)(doc) } } struct FeatureSegmentCollector<T, A, Q, F> { agg: A, query: Q, reader: F, _marker: PhantomData<T>, } impl<T, A, Q, F> SegmentCollector for FeatureSegmentCollector<T, A, Q, F> where T: 'static + Feature<Q, Agg = A>, Q: 'static, A: 'static + Mergeable + for<'a> From<&'a Q>, F: 'static + FeatureForDoc<T>, { type Fruit = A; fn collect(&mut self, doc: DocId, _score: Score) { if let Some(item) = self.reader.for_doc(doc) { item.collect_into(&self.query, &mut self.agg); } } fn harvest(self) -> Self::Fruit { self.agg } } #[cfg(test)] mod tests { use super::*; use std::{convert::TryInto, ops::Range}; use tantivy::{query::AllQuery, schema::SchemaBuilder, Document, Index}; struct Metadata { a: i16, b: u16, } // XXX Who will test the tests? impl Metadata { pub fn as_bytes(&self) -> [u8; 4] { let mut out = [0u8; 4]; out[0..2].copy_from_slice(&self.a.to_le_bytes()); out[2..].copy_from_slice(&self.b.to_le_bytes()); out } pub fn from_bytes(src: [u8; 4]) -> Self { let a = i16::from_le_bytes(src[0..2].try_into().unwrap()); let b = u16::from_le_bytes(src[2..].try_into().unwrap()); Self { a, b } } } #[derive(Debug, Default)] struct MetaAgg { a: usize, b: usize, } impl Mergeable for MetaAgg { fn merge_same_size(&mut self, other: &Self) { self.a += other.a; self.b += other.b; } } #[derive(Clone)] struct LessThanMetaQuery { a: i16, b: u16, } impl From<&LessThanMetaQuery> for MetaAgg { fn from(_src: &LessThanMetaQuery) -> Self { Self::default() } } impl Feature<LessThanMetaQuery> for Metadata { type Agg = MetaAgg; fn collect_into(&self, query: &LessThanMetaQuery, agg: &mut Self::Agg) { if self.a < query.a { agg.a += 1; } if self.b < query.b { agg.b += 1; } } } #[derive(Clone)] struct CountARangesQuery(Vec<Range<i16>>); impl From<&CountARangesQuery> for Vec<i16> { fn from(src: &CountARangesQuery) -> Self { vec![0; src.0.len()] } } impl Mergeable for Vec<i16> { fn merge_same_size(&mut self, other: &Self) { for (idx, tally) in other.iter().enumerate() { self[idx] += tally; } } } impl Feature<CountARangesQuery> for Metadata { type Agg = Vec<i16>; fn collect_into(&self, query: &CountARangesQuery, agg: &mut Self::Agg) { for (idx, range) in query.0.iter().enumerate() { if range.contains(&self.a) { agg[idx] += 1; } } } } #[test] fn pass() -> Result<()> { let mut builder = SchemaBuilder::new(); let bytes_field = builder.add_bytes_field("metadata_as_bytes"); let index = Index::create_in_ram(builder.build()); let mut writer = index.writer_with_num_threads(1, 3_000_000)?; let add_doc = |meta: Metadata| { let mut doc = Document::new(); doc.add_bytes(bytes_field, meta.as_bytes().to_vec()); writer.add_document(doc); }; add_doc(Metadata { a: -1, b: 1 }); add_doc(Metadata { a: -2, b: 2 }); add_doc(Metadata { a: -3, b: 3 }); add_doc(Metadata { a: -4, b: 4 }); writer.commit()?; let reader = index.reader()?; let searcher = reader.searcher(); let less_than_collector = FeatureCollector::<Metadata, _, _>::new( // So we want count: // * Every document that has "a" < -1 // * Every document that has "b" < 2 LessThanMetaQuery { a: -1, b: 2 }, move |reader: &SegmentReader| { let bytes_reader = reader.fast_fields().bytes(bytes_field).unwrap(); move |doc_id| { let metadata_bytes = bytes_reader.get_bytes(doc_id); metadata_bytes.try_into().ok().map(Metadata::from_bytes) } }, ); let a_ranges_collector = FeatureCollector::<Metadata, _, _>::new( // And here we'll get a count for: // * Every doc that a is within -10..0 (4) // * Every doc that a is within 0..10 (0) // * Every doc that a is within -2..4 (2) CountARangesQuery(vec![-10..0, 0..10, -2..4]), move |reader: &SegmentReader| { let bytes_reader = reader.fast_fields().bytes(bytes_field).unwrap(); move |doc_id| { let metadata_bytes = bytes_reader.get_bytes(doc_id); metadata_bytes.try_into().ok().map(Metadata::from_bytes) } }, ); let (agg, a_range_counts) = searcher.search(&AllQuery, &(less_than_collector, a_ranges_collector))?; assert_eq!(3, agg.a); assert_eq!(1, agg.b); assert_eq!(vec![4, 0, 2], a_range_counts); Ok(()) } } |