-
Notifications
You must be signed in to change notification settings - Fork 624
/
SegmentInfo.cs
346 lines (309 loc) · 11.9 KB
/
SegmentInfo.cs
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
using J2N.Collections.Generic.Extensions;
using System;
using System.Collections.Generic;
using Lucene.Net.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
namespace Lucene.Net.Index
{
/*
* 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.
*/
using Codec = Lucene.Net.Codecs.Codec;
using Directory = Lucene.Net.Store.Directory;
using Lucene3xSegmentInfoFormat = Lucene.Net.Codecs.Lucene3x.Lucene3xSegmentInfoFormat;
using TrackingDirectoryWrapper = Lucene.Net.Store.TrackingDirectoryWrapper;
/// <summary>
/// Information about a segment such as it's name, directory, and files related
/// to the segment.
/// <para/>
/// @lucene.experimental
/// </summary>
public sealed class SegmentInfo
{
// TODO: remove these from this class, for now this is the representation
/// <summary>
/// Used by some member fields to mean not present (e.g.,
/// norms, deletions).
/// </summary>
public static readonly int NO = -1; // e.g. no norms; no deletes;
/// <summary>
/// Used by some member fields to mean present (e.g.,
/// norms, deletions).
/// </summary>
public static readonly int YES = 1; // e.g. have norms; have deletes;
/// <summary>
/// Unique segment name in the directory. </summary>
public string Name { get; private set; }
private int docCount; // number of docs in seg
/// <summary>
/// Where this segment resides. </summary>
public Directory Dir { get; private set; }
private bool isCompoundFile;
private Codec codec;
private IDictionary<string, string> diagnostics;
[Obsolete("not used anymore")]
private IDictionary<string, string> attributes;
// Tracks the Lucene version this segment was created with, since 3.1. Null
// indicates an older than 3.0 index, and it's used to detect a too old index.
// The format expected is "x.y" - "2.x" for pre-3.0 indexes (or null), and
// specific versions afterwards ("3.0", "3.1" etc.).
// see Constants.LUCENE_MAIN_VERSION.
private string version;
/// <summary>
/// Gets or Sets diagnostics saved into the segment when it was written.
/// </summary>
public IDictionary<string, string> Diagnostics
{
get => diagnostics;
set => this.diagnostics = value;
}
/// <summary>
/// Construct a new complete <see cref="SegmentInfo"/> instance from input.
/// <para>Note: this is public only to allow access from
/// the codecs package.</para>
/// </summary>
public SegmentInfo(Directory dir, string version, string name, int docCount, bool isCompoundFile, Codec codec, IDictionary<string, string> diagnostics)
: this(dir, version, name, docCount, isCompoundFile, codec, diagnostics, null)
{
}
/// <summary>
/// Construct a new complete <see cref="SegmentInfo"/> instance from input.
/// <para>Note: this is public only to allow access from
/// the codecs package.</para>
/// </summary>
public SegmentInfo(Directory dir, string version, string name, int docCount, bool isCompoundFile, Codec codec, IDictionary<string, string> diagnostics, IDictionary<string, string> attributes)
{
if (Debugging.AssertsEnabled) Debugging.Assert(!(dir is TrackingDirectoryWrapper));
this.Dir = dir;
this.version = version;
this.Name = name;
this.docCount = docCount;
this.isCompoundFile = isCompoundFile;
this.codec = codec;
this.diagnostics = diagnostics;
#pragma warning disable 612, 618
this.attributes = attributes;
#pragma warning restore 612, 618
}
[Obsolete("separate norms are not supported in >= 4.0")]
internal bool HasSeparateNorms => GetAttribute(Lucene3xSegmentInfoFormat.NORMGEN_KEY) != null;
/// <summary>
/// Gets or Sets whether this segment is stored as a compound file.
/// <c>true</c> if this is a compound file;
/// else, <c>false</c>
/// </summary>
public bool UseCompoundFile
{
get => isCompoundFile;
set => this.isCompoundFile = value;
}
/// <summary>
/// Gets or Sets <see cref="Codecs.Codec"/> that wrote this segment.
/// Setter can only be called once. </summary>
public Codec Codec
{
get => codec;
set
{
if (Debugging.AssertsEnabled) Debugging.Assert(this.codec is null);
this.codec = value ?? throw new ArgumentNullException(nameof(value), "Codec must be non-null");
}
}
/// <summary>
/// Returns number of documents in this segment (deletions
/// are not taken into account).
/// </summary>
public int DocCount
{
get
{
if (this.docCount == -1)
{
throw new InvalidOperationException("docCount isn't set yet");
}
return docCount;
}
internal set // NOTE: leave package private
{
if (this.docCount != -1)
{
throw new InvalidOperationException("docCount was already set");
}
this.docCount = value;
}
}
/// <summary>
/// Return all files referenced by this <see cref="SegmentInfo"/>. </summary>
public ISet<string> GetFiles()
{
if (setFiles == null)
{
throw new InvalidOperationException("files were not computed yet");
}
return setFiles.AsReadOnly();
}
public override string ToString()
{
return ToString(Dir, 0);
}
/// <summary>
/// Used for debugging. Format may suddenly change.
///
/// <para>Current format looks like
/// <c>_a(3.1):c45/4</c>, which means the segment's
/// name is <c>_a</c>; it was created with Lucene 3.1 (or
/// '?' if it's unknown); it's using compound file
/// format (would be <c>C</c> if not compound); it
/// has 45 documents; it has 4 deletions (this part is
/// left off when there are no deletions).</para>
/// </summary>
public string ToString(Directory dir, int delCount)
{
StringBuilder s = new StringBuilder();
s.Append(Name).Append('(').Append(version ?? "?").Append(')').Append(':');
char cfs = UseCompoundFile ? 'c' : 'C';
s.Append(cfs);
if (this.Dir != dir)
{
s.Append('x');
}
s.Append(docCount);
if (delCount != 0)
{
s.Append('/').Append(delCount);
}
// TODO: we could append toString of attributes() here?
return s.ToString();
}
/// <summary>
/// We consider another <see cref="SegmentInfo"/> instance equal if it
/// has the same dir and same name.
/// </summary>
public override bool Equals(object obj)
{
if (this == obj)
{
return true;
}
if (obj is SegmentInfo other)
{
return other.Dir == Dir && other.Name.Equals(Name, StringComparison.Ordinal);
}
else
{
return false;
}
}
public override int GetHashCode()
{
return Dir.GetHashCode() + Name.GetHashCode();
}
/// <summary>
/// Used by DefaultSegmentInfosReader to upgrade a 3.0 segment to record its
/// version is "3.0". this method can be removed when we're not required to
/// support 3x indexes anymore, e.g. in 5.0.
/// <para/>
/// <b>NOTE:</b> this method is used for internal purposes only - you should
/// not modify the version of a <see cref="SegmentInfo"/>, or it may result in unexpected
/// exceptions thrown when you attempt to open the index.
/// <para/>
/// @lucene.internal
/// </summary>
public string Version
{
get => version;
set => this.version = value;
}
private ISet<string> setFiles;
/// <summary>
/// Sets the files written for this segment. </summary>
public void SetFiles(ISet<string> files)
{
CheckFileNames(files);
setFiles = files;
}
/// <summary>
/// Add these files to the set of files written for this
/// segment.
/// </summary>
public void AddFiles(ICollection<string> files)
{
CheckFileNames(files);
setFiles.UnionWith(files);
}
/// <summary>
/// Add this file to the set of files written for this
/// segment.
/// </summary>
public void AddFile(string file)
{
CheckFileNames(new[] { file });
setFiles.Add(file);
}
private static void CheckFileNames(ICollection<string> files) // LUCENENET: CA1822: Mark members as static
{
Regex r = IndexFileNames.CODEC_FILE_PATTERN;
foreach (string file in files)
{
if (!r.IsMatch(file))
{
throw new ArgumentException("invalid codec filename '" + file + "', must match: " + IndexFileNames.CODEC_FILE_PATTERN.ToString());
}
}
}
/// <summary>
/// Get a codec attribute value, or null if it does not exist
/// </summary>
[Obsolete("no longer supported")]
public string GetAttribute(string key)
{
if (attributes == null)
{
return null;
}
else
{
attributes.TryGetValue(key, out string attribute);
return attribute;
}
}
/// <summary>
/// Puts a codec attribute value.
/// <para/>
/// This is a key-value mapping for the field that the codec can use to store
/// additional metadata, and will be available to the codec when reading the
/// segment via <see cref="GetAttribute(string)"/>
/// <para/>
/// If a value already exists for the field, it will be replaced with the new
/// value.
/// </summary>
[Obsolete("no longer supported")]
public string PutAttribute(string key, string value)
{
if (attributes == null)
{
attributes = new Dictionary<string, string>();
}
return attributes[key] = value;
}
/// <summary>
/// Returns the internal codec attributes map. May be null if no mappings exist.
/// </summary>
[Obsolete("no longer supported")]
public IDictionary<string, string> Attributes => attributes;
}
}