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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::core::search::doc_id_set_iterator::{AllDISI, DocIdSetIterator, EmptyDISI};
use crate::core::util::accountable::Accountable;
use crate::core::util::bits::{Bits, MatchAllBits};
use crate::core::util::dummy::dummy_bits::DummyBits;
use crate::core::util::error::lucene_error::Result;
/// A [`DocIdSet`] contains a set of document IDs.
/// Implementing types must provide an [`iterator`](DocIdSet::iterator) method
/// to access the set.
pub trait DocIdSet: Accountable {
type DocIdSetIterator: DocIdSetIterator;
fn iterator(&self) -> Result<Self::DocIdSetIterator>;
/// Optionally provides a [`Bits`] view for random access to matching
/// documents.
///
/// # Returns
/// * `None` if this [`DocIdSet`] does not support random access.
///
/// Note that, unlike [`iterator`](DocIdSet::iterator), a return value of
/// `None` **does not** imply that no documents match the filter!
///
/// The default implementation does not provide random access, so you only
/// need to implement this method if your [`DocIdSet`] can guarantee
/// random access to every document ID in `O(1)` time without external
/// disk access because the [`Bits`] trait cannot return an I/O error.
/// This is generally true for bit sets like
/// [`FixedBitSet`](crate::core::util::fixed_bit_set::FixedBitSet),
/// which return themselves if used as a [`DocIdSet`].
type Bits: Bits + Clone;
fn bits(&self) -> Option<Self::Bits>;
/// Some implementations require calling the finish method before invoking iterator.
/// # See
/// [`DocsWithFieldSet`](crate::core::index::docs_with_field_set::DocsWithFieldSet)
fn finish(&mut self) {}
}
/// A [`DocIdSet`] that matches all document IDs up to a specified document
/// (exclusive).
pub struct All {
max_doc: i32,
bits: Option<MatchAllBits>,
}
impl All {
fn new(max_doc: i32) -> Self {
let bits = Some(MatchAllBits::new(max_doc as usize));
All { max_doc, bits }
}
}
/// Returns a [`DocIdSet`] that matches all document IDs below `max_doc`.
pub fn all(max_doc: i32) -> All {
All::new(max_doc)
}
/// A [`DocIdSet`] that matches all doc ids up to a specified doc (exclusive).
impl DocIdSet for All {
type DocIdSetIterator = AllDISI;
fn iterator(&self) -> Result<Self::DocIdSetIterator> {
Ok(AllDISI::new(self.max_doc))
}
type Bits = MatchAllBits;
fn bits(&self) -> Option<Self::Bits> {
self.bits.clone()
}
}
impl Accountable for All {
fn ram_bytes_used(&self) -> Result<i64> {
Ok(0)
}
}
pub struct EmptyDocIdSet;
impl Accountable for EmptyDocIdSet {
fn ram_bytes_used(&self) -> Result<i64> {
Ok(0)
}
}
impl DocIdSet for EmptyDocIdSet {
type DocIdSetIterator = EmptyDISI;
fn iterator(&self) -> Result<Self::DocIdSetIterator> {
Ok(EmptyDISI::new())
}
type Bits = DummyBits;
fn bits(&self) -> Option<Self::Bits> {
None
}
}