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
/*
* 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::index::index_reader::{CompositeReaderContextKind, IndexReader};
use crate::core::index::leaf_reader::LeafReader;
use crate::core::util::error::lucene_error::Result;
use std::rc::Rc;
use std::sync::Arc;
/// A reader composed from sub-readers.
///
/// Instances of this reader type can only be used to get stored fields from the
/// underlying [`LeafReader`]s, and it is not possible to directly retrieve
/// postings. To do that, get the
/// [`LeafReaderContext`](crate::core::index::leaf_reader_context::LeafReaderContext)
/// for all sub-readers via
/// [`IndexReaderContext::leaves`](crate::core::index::index_reader_context::IndexReaderContext::leaves).
///
/// [`IndexReader`] instances for indexes on disk are usually constructed with a
/// call to one of the [`directory_reader::open`](crate::core::index::directory_reader::open) methods, for example
/// [`directory_reader::open`](crate::core::index::directory_reader::open).
/// [`DirectoryReader`](crate::core::index::directory_reader::DirectoryReader)
/// implements the [`CompositeReader`] trait, so it is not possible to
/// directly get postings from it.
///
/// Concrete implementations are usually constructed with an opening function such as
/// [`directory_reader::open`](crate::core::index::directory_reader::open).
///
/// For efficiency, this API often refers to documents via document numbers:
/// non-negative integers that each name a unique document in the index. These
/// document numbers are ephemeral and may change as documents are added to and
/// deleted from an index. Clients should not rely on a document having the same
/// number between sessions.
///
/// NOTE: [`IndexReader`] instances are completely thread safe, meaning multiple
/// threads can call any of their methods concurrently. If your application
/// requires external synchronization, do not synchronize on the reader instance;
/// use your own non-Lucene objects instead.
pub trait CompositeReader: IndexReader<ContextKind = CompositeReaderContextKind> + Sized {
type LeafReader: LeafReader + Clone;
type SubReader: IndexReader;
/// Expert: returns the sequential sub-readers that this reader is logically
/// composed of.
///
/// This method may not return `None`.
///
/// NOTE: In contrast to previous Lucene versions, code that wants to get all
/// [`LeafReader`]s this composite is composed of should use
/// [`IndexReaderContext::leaves`](crate::core::index::index_reader_context::IndexReaderContext::leaves).
fn get_sequential_sub_readers(&self) -> &[Self::SubReader];
#[doc(hidden)]
fn visit_leaves<F>(&self, visitor: &mut F) -> Result<()>
where
F: FnMut(&Self::LeafReader) -> Result<()>;
fn to_string(&self) -> String {
String::new()
}
}
impl<CR> CompositeReader for &CR
where
CR: CompositeReader,
{
type LeafReader = CR::LeafReader;
type SubReader = CR::SubReader;
fn get_sequential_sub_readers(&self) -> &[Self::SubReader] {
(**self).get_sequential_sub_readers()
}
fn visit_leaves<F>(&self, visitor: &mut F) -> Result<()>
where
F: FnMut(&Self::LeafReader) -> Result<()>,
{
(**self).visit_leaves(visitor)
}
fn to_string(&self) -> String {
(**self).to_string()
}
}
impl<CR> CompositeReader for Arc<CR>
where
CR: CompositeReader,
{
type LeafReader = CR::LeafReader;
type SubReader = CR::SubReader;
fn get_sequential_sub_readers(&self) -> &[Self::SubReader] {
(**self).get_sequential_sub_readers()
}
fn visit_leaves<F>(&self, visitor: &mut F) -> Result<()>
where
F: FnMut(&Self::LeafReader) -> Result<()>,
{
(**self).visit_leaves(visitor)
}
fn to_string(&self) -> String {
(**self).to_string()
}
}
impl<CR> CompositeReader for Rc<CR>
where
CR: CompositeReader,
{
type LeafReader = CR::LeafReader;
type SubReader = CR::SubReader;
fn get_sequential_sub_readers(&self) -> &[Self::SubReader] {
(**self).get_sequential_sub_readers()
}
fn visit_leaves<F>(&self, visitor: &mut F) -> Result<()>
where
F: FnMut(&Self::LeafReader) -> Result<()>,
{
(**self).visit_leaves(visitor)
}
fn to_string(&self) -> String {
(**self).to_string()
}
}