Skip to content

Commit a99e545

Browse files
committed
return result from segment collector
1 parent 3f88718 commit a99e545

13 files changed

+61
-42
lines changed

examples/custom_collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ struct StatsSegmentCollector {
102102
impl SegmentCollector for StatsSegmentCollector {
103103
type Fruit = Option<Stats>;
104104

105-
fn collect(&mut self, doc: u32, _score: Score) {
105+
fn collect(&mut self, doc: u32, _score: Score) -> crate::Result<()> {
106106
let value = self.fast_field_reader.get(doc) as f64;
107107
self.stats.count += 1;
108108
self.stats.sum += value;
109109
self.stats.squared_sum += value * value;
110+
Ok(())
110111
}
111112

112113
fn harvest(self) -> <Self as SegmentCollector>::Fruit {

src/aggregation/collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ impl SegmentCollector for AggregationSegmentCollector {
132132
type Fruit = crate::Result<IntermediateAggregationResults>;
133133

134134
#[inline]
135-
fn collect(&mut self, doc: crate::DocId, _score: crate::Score) {
135+
fn collect(&mut self, doc: crate::DocId, _score: crate::Score) -> crate::Result<()> {
136136
self.result.collect(doc, &self.aggs_with_accessor);
137+
Ok(())
137138
}
138139

139140
fn harvest(mut self) -> Self::Fruit {

src/collector/count_collector.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ pub struct SegmentCountCollector {
6565
impl SegmentCollector for SegmentCountCollector {
6666
type Fruit = usize;
6767

68-
fn collect(&mut self, _: DocId, _: Score) {
68+
fn collect(&mut self, _: DocId, _: Score) -> crate::Result<()> {
6969
self.count += 1;
70+
Ok(())
7071
}
7172

7273
fn harvest(self) -> usize {
@@ -92,18 +93,18 @@ mod tests {
9293
}
9394
{
9495
let mut count_collector = SegmentCountCollector::default();
95-
count_collector.collect(0u32, 1.0);
96+
count_collector.collect(0u32, 1.0).unwrap();
9697
assert_eq!(count_collector.harvest(), 1);
9798
}
9899
{
99100
let mut count_collector = SegmentCountCollector::default();
100-
count_collector.collect(0u32, 1.0);
101+
count_collector.collect(0u32, 1.0).unwrap();
101102
assert_eq!(count_collector.harvest(), 1);
102103
}
103104
{
104105
let mut count_collector = SegmentCountCollector::default();
105-
count_collector.collect(0u32, 1.0);
106-
count_collector.collect(1u32, 1.0);
106+
count_collector.collect(0u32, 1.0).unwrap();
107+
count_collector.collect(1u32, 1.0).unwrap();
107108
assert_eq!(count_collector.harvest(), 2);
108109
}
109110
}

src/collector/custom_score_top_collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ where
9090
{
9191
type Fruit = Vec<(TScore, DocAddress)>;
9292

93-
fn collect(&mut self, doc: DocId, _score: Score) {
93+
fn collect(&mut self, doc: DocId, _score: Score) -> crate::Result<()> {
9494
let score = self.segment_scorer.score(doc);
9595
self.segment_collector.collect(doc, score);
96+
Ok(())
9697
}
9798

9899
fn harvest(self) -> Vec<(TScore, DocAddress)> {

src/collector/docset_collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ pub struct DocSetChildCollector {
5050
impl SegmentCollector for DocSetChildCollector {
5151
type Fruit = (u32, HashSet<DocId>);
5252

53-
fn collect(&mut self, doc: crate::DocId, _score: Score) {
53+
fn collect(&mut self, doc: crate::DocId, _score: Score) -> crate::Result<()> {
5454
self.docs.insert(doc);
55+
Ok(())
5556
}
5657

5758
fn harvest(self) -> (u32, HashSet<DocId>) {

src/collector/facet_collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl Collector for FacetCollector {
333333
impl SegmentCollector for FacetSegmentCollector {
334334
type Fruit = FacetCounts;
335335

336-
fn collect(&mut self, doc: DocId, _: Score) {
336+
fn collect(&mut self, doc: DocId, _: Score) -> crate::Result<()> {
337337
self.reader.facet_ords(doc, &mut self.facet_ords_buf);
338338
let mut previous_collapsed_ord: usize = usize::MAX;
339339
for &facet_ord in &self.facet_ords_buf {
@@ -345,6 +345,7 @@ impl SegmentCollector for FacetSegmentCollector {
345345
};
346346
previous_collapsed_ord = collapsed_ord;
347347
}
348+
Ok(())
348349
}
349350

350351
/// Returns the results of the collection.

src/collector/filter_collector_wrapper.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,12 @@ where
173173
{
174174
type Fruit = TSegmentCollector::Fruit;
175175

176-
fn collect(&mut self, doc: u32, score: Score) {
176+
fn collect(&mut self, doc: u32, score: Score) -> crate::Result<()> {
177177
let value = self.fast_field_reader.get(doc);
178178
if (self.predicate)(value) {
179-
self.segment_collector.collect(doc, score)
179+
self.segment_collector.collect(doc, score)?;
180180
}
181+
Ok(())
181182
}
182183

183184
fn harvest(self) -> <TSegmentCollector as SegmentCollector>::Fruit {

src/collector/histogram_collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,10 @@ pub struct SegmentHistogramCollector {
9191
impl SegmentCollector for SegmentHistogramCollector {
9292
type Fruit = Vec<u64>;
9393

94-
fn collect(&mut self, doc: DocId, _score: Score) {
94+
fn collect(&mut self, doc: DocId, _score: Score) -> crate::Result<()> {
9595
let value = self.ff_reader.get(doc);
9696
self.histogram_computer.add_value(value);
97+
Ok(())
9798
}
9899

99100
fn harvest(self) -> Self::Fruit {

src/collector/mod.rs

+21-17
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ pub trait Collector: Sync + Send {
175175
if let Some(alive_bitset) = reader.alive_bitset() {
176176
weight.for_each(reader, &mut |doc, score| {
177177
if alive_bitset.is_alive(doc) {
178-
segment_collector.collect(doc, score);
178+
segment_collector.collect(doc, score).unwrap(); // TODO
179179
}
180180
})?;
181181
} else {
182182
weight.for_each(reader, &mut |doc, score| {
183-
segment_collector.collect(doc, score);
183+
segment_collector.collect(doc, score).unwrap(); // TODO
184184
})?;
185185
}
186186
Ok(segment_collector.harvest())
@@ -190,10 +190,11 @@ pub trait Collector: Sync + Send {
190190
impl<TSegmentCollector: SegmentCollector> SegmentCollector for Option<TSegmentCollector> {
191191
type Fruit = Option<TSegmentCollector::Fruit>;
192192

193-
fn collect(&mut self, doc: DocId, score: Score) {
193+
fn collect(&mut self, doc: DocId, score: Score) -> crate::Result<()> {
194194
if let Some(segment_collector) = self {
195-
segment_collector.collect(doc, score);
195+
segment_collector.collect(doc, score)?;
196196
}
197+
Ok(())
197198
}
198199

199200
fn harvest(self) -> Self::Fruit {
@@ -253,7 +254,7 @@ pub trait SegmentCollector: 'static {
253254
type Fruit: Fruit;
254255

255256
/// The query pushes the scored document to the collector via this method.
256-
fn collect(&mut self, doc: DocId, score: Score);
257+
fn collect(&mut self, doc: DocId, score: Score) -> crate::Result<()>;
257258

258259
/// Extract the fruit of the collection from the `SegmentCollector`.
259260
fn harvest(self) -> Self::Fruit;
@@ -308,9 +309,10 @@ where
308309
{
309310
type Fruit = (Left::Fruit, Right::Fruit);
310311

311-
fn collect(&mut self, doc: DocId, score: Score) {
312-
self.0.collect(doc, score);
313-
self.1.collect(doc, score);
312+
fn collect(&mut self, doc: DocId, score: Score) -> crate::Result<()> {
313+
self.0.collect(doc, score)?;
314+
self.1.collect(doc, score)?;
315+
Ok(())
314316
}
315317

316318
fn harvest(self) -> <Self as SegmentCollector>::Fruit {
@@ -372,10 +374,11 @@ where
372374
{
373375
type Fruit = (One::Fruit, Two::Fruit, Three::Fruit);
374376

375-
fn collect(&mut self, doc: DocId, score: Score) {
376-
self.0.collect(doc, score);
377-
self.1.collect(doc, score);
378-
self.2.collect(doc, score);
377+
fn collect(&mut self, doc: DocId, score: Score) -> crate::Result<()> {
378+
self.0.collect(doc, score)?;
379+
self.1.collect(doc, score)?;
380+
self.2.collect(doc, score)?;
381+
Ok(())
379382
}
380383

381384
fn harvest(self) -> <Self as SegmentCollector>::Fruit {
@@ -446,11 +449,12 @@ where
446449
{
447450
type Fruit = (One::Fruit, Two::Fruit, Three::Fruit, Four::Fruit);
448451

449-
fn collect(&mut self, doc: DocId, score: Score) {
450-
self.0.collect(doc, score);
451-
self.1.collect(doc, score);
452-
self.2.collect(doc, score);
453-
self.3.collect(doc, score);
452+
fn collect(&mut self, doc: DocId, score: Score) -> crate::Result<()> {
453+
self.0.collect(doc, score)?;
454+
self.1.collect(doc, score)?;
455+
self.2.collect(doc, score)?;
456+
self.3.collect(doc, score)?;
457+
Ok(())
454458
}
455459

456460
fn harvest(self) -> <Self as SegmentCollector>::Fruit {

src/collector/multi_collector.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ impl<TCollector: Collector> Collector for CollectorWrapper<TCollector> {
5252
impl SegmentCollector for Box<dyn BoxableSegmentCollector> {
5353
type Fruit = Box<dyn Fruit>;
5454

55-
fn collect(&mut self, doc: u32, score: Score) {
56-
self.as_mut().collect(doc, score);
55+
fn collect(&mut self, doc: u32, score: Score) -> crate::Result<()> {
56+
self.as_mut().collect(doc, score)?;
57+
Ok(())
5758
}
5859

5960
fn harvest(self) -> Box<dyn Fruit> {
@@ -62,7 +63,7 @@ impl SegmentCollector for Box<dyn BoxableSegmentCollector> {
6263
}
6364

6465
pub trait BoxableSegmentCollector {
65-
fn collect(&mut self, doc: u32, score: Score);
66+
fn collect(&mut self, doc: u32, score: Score) -> crate::Result<()>;
6667
fn harvest_from_box(self: Box<Self>) -> Box<dyn Fruit>;
6768
}
6869

@@ -71,8 +72,8 @@ pub struct SegmentCollectorWrapper<TSegmentCollector: SegmentCollector>(TSegment
7172
impl<TSegmentCollector: SegmentCollector> BoxableSegmentCollector
7273
for SegmentCollectorWrapper<TSegmentCollector>
7374
{
74-
fn collect(&mut self, doc: u32, score: Score) {
75-
self.0.collect(doc, score);
75+
fn collect(&mut self, doc: u32, score: Score) -> crate::Result<()> {
76+
self.0.collect(doc, score)
7677
}
7778

7879
fn harvest_from_box(self: Box<Self>) -> Box<dyn Fruit> {
@@ -228,10 +229,11 @@ pub struct MultiCollectorChild {
228229
impl SegmentCollector for MultiCollectorChild {
229230
type Fruit = MultiFruit;
230231

231-
fn collect(&mut self, doc: DocId, score: Score) {
232+
fn collect(&mut self, doc: DocId, score: Score) -> crate::Result<()> {
232233
for child in &mut self.children {
233-
child.collect(doc, score);
234+
child.collect(doc, score)?;
234235
}
236+
Ok(())
235237
}
236238

237239
fn harvest(self) -> MultiFruit {

src/collector/tests.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ impl Collector for TestCollector {
138138
impl SegmentCollector for TestSegmentCollector {
139139
type Fruit = TestFruit;
140140

141-
fn collect(&mut self, doc: DocId, score: Score) {
141+
fn collect(&mut self, doc: DocId, score: Score) -> crate::Result<()> {
142142
self.fruit.docs.push(DocAddress::new(self.segment_id, doc));
143143
self.fruit.scores.push(score);
144+
Ok(())
144145
}
145146

146147
fn harvest(self) -> <Self as SegmentCollector>::Fruit {
@@ -198,9 +199,10 @@ impl Collector for FastFieldTestCollector {
198199
impl SegmentCollector for FastFieldSegmentCollector {
199200
type Fruit = Vec<u64>;
200201

201-
fn collect(&mut self, doc: DocId, _score: Score) {
202+
fn collect(&mut self, doc: DocId, _score: Score) -> crate::Result<()> {
202203
let val = self.reader.get(doc);
203204
self.vals.push(val);
205+
Ok(())
204206
}
205207

206208
fn harvest(self) -> Vec<u64> {
@@ -255,9 +257,10 @@ impl Collector for BytesFastFieldTestCollector {
255257
impl SegmentCollector for BytesFastFieldSegmentCollector {
256258
type Fruit = Vec<u8>;
257259

258-
fn collect(&mut self, doc: u32, _score: Score) {
260+
fn collect(&mut self, doc: u32, _score: Score) -> crate::Result<()> {
259261
let data = self.reader.get_bytes(doc);
260262
self.vals.extend(data);
263+
Ok(())
261264
}
262265

263266
fn harvest(self) -> <Self as SegmentCollector>::Fruit {

src/collector/top_score_collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,9 @@ pub struct TopScoreSegmentCollector(TopSegmentCollector<Score>);
699699
impl SegmentCollector for TopScoreSegmentCollector {
700700
type Fruit = Vec<(Score, DocAddress)>;
701701

702-
fn collect(&mut self, doc: DocId, score: Score) {
702+
fn collect(&mut self, doc: DocId, score: Score) -> crate::Result<()> {
703703
self.0.collect(doc, score);
704+
Ok(())
704705
}
705706

706707
fn harvest(self) -> Vec<(Score, DocAddress)> {

src/collector/tweak_score_top_collector.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ where
9393
{
9494
type Fruit = Vec<(TScore, DocAddress)>;
9595

96-
fn collect(&mut self, doc: DocId, score: Score) {
96+
fn collect(&mut self, doc: DocId, score: Score) -> crate::Result<()> {
9797
let score = self.segment_scorer.score(doc, score);
9898
self.segment_collector.collect(doc, score);
99+
Ok(())
99100
}
100101

101102
fn harvest(self) -> Vec<(TScore, DocAddress)> {

0 commit comments

Comments
 (0)