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
/*
* 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::field_infos::FieldInfos;
use crate::core::store::IOContext;
use crate::core::store::directory::Directory;
use crate::core::util::error::lucene_error::Result;
use crate::core::util::fixed_bit_set::FixedBitSet;
use crate::core::util::info_stream::InfoStreamMT;
use std::sync::Arc;
/// Holder struct for common parameters used during write.
pub struct SegmentWriteState<'a, D> {
/// InfoStream used for debugging messages.
pub info_stream: InfoStreamMT,
/// Directory where this segment will be written to.
pub directory: &'a D,
/// FieldInfos describing all fields in this segment.
pub field_infos: Arc<FieldInfos>,
/// Number of deleted documents set while flushing the segment.
pub del_count_on_flush: i32,
/// Number of only soft deleted documents set while flushing the segment.
pub soft_del_count_on_flush: i32,
/// FixedBitSet recording live documents; this is only set if there
/// is one or more deleted documents.
pub live_docs: Option<Arc<FixedBitSet>>,
/// Unique suffix for any postings files written for this segment.
/// PerFieldPostingsFormat sets this for each of the postings formats it
/// wraps. If you create a new PostingsFormat, then any files you
/// write/read must be derived using this suffix (use
/// IndexFileNames::segment_file_name).
///
/// Note: the suffix must be either empty, or be a textual suffix
/// containing exactly two parts (separated by underscore), or be a
/// base36 generation.
pub segment_suffix: String,
/// IOContext for all writes; you should pass this to
/// [`Directory::create_output`].
pub context: &'a IOContext,
}
impl<'a, D> SegmentWriteState<'a, D> {
/// Creates an instance without a suffix.
pub(crate) fn new(
info_stream: InfoStreamMT,
directory: &'a D,
field_infos: Arc<FieldInfos>,
context: &'a IOContext,
) -> Self {
Self::with_suffix(info_stream, directory, field_infos, context, "")
}
/// Creates an instance with a segment suffix.
pub(crate) fn with_suffix<S>(
info_stream: InfoStreamMT,
directory: &'a D,
field_infos: Arc<FieldInfos>,
context: &'a IOContext,
segment_suffix: S,
) -> Self
where
S: AsRef<str> + Into<String>,
{
debug_assert!(Self::assert_segment_suffix(segment_suffix.as_ref()));
Self {
info_stream,
directory,
field_infos,
context,
segment_suffix: segment_suffix.into(),
del_count_on_flush: 0,
soft_del_count_on_flush: 0,
live_docs: None,
}
}
pub(crate) fn create_segment_output(
&self,
segment_name: &str,
extension: &str,
) -> Result<D::IndexOutput>
where
D: Directory,
{
crate::core::index::IndexFileNames::with_segment_file_name(
segment_name,
&self.segment_suffix,
extension,
|name| self.directory.create_output(name, self.context),
)
}
/// Create a shallow copy of SegmentWriteState with a new segment suffix.
pub fn copy_with_suffix<S>(state: &SegmentWriteState<'a, D>, segment_suffix: S) -> Self
where
S: Into<String>,
{
let segment_suffix = segment_suffix.into();
Self {
info_stream: state.info_stream.clone(),
directory: state.directory,
field_infos: Arc::clone(&state.field_infos),
context: state.context,
segment_suffix,
del_count_on_flush: state.del_count_on_flush,
soft_del_count_on_flush: state.soft_del_count_on_flush,
live_docs: state.live_docs.clone(),
}
}
// currently only used by assert? clean up and make real check?
// either it's a segment suffix (_X_Y) or it's a parsable generation
fn assert_segment_suffix(suffix: &str) -> bool {
if suffix.is_empty() {
return true;
}
let mut parts = suffix.split('_');
parts.next();
match (parts.next(), parts.next()) {
(Some(_), None) => true,
(None, None) => i64::from_str_radix(suffix, 36).is_ok(),
_ => false,
}
}
}