forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParallelAtomicReader.cs
403 lines (365 loc) · 14.3 KB
/
ParallelAtomicReader.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
using J2N.Runtime.CompilerServices;
using Lucene.Net.Support;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using JCG = J2N.Collections.Generic;
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 IBits = Lucene.Net.Util.IBits;
/// <summary>
/// An <see cref="AtomicReader"/> which reads multiple, parallel indexes. Each index
/// added must have the same number of documents, but typically each contains
/// different fields. Deletions are taken from the first reader.
/// Each document contains the union of the fields of all documents
/// with the same document number. When searching, matches for a
/// query term are from the first index added that has the field.
///
/// <para/>This is useful, e.g., with collections that have large fields which
/// change rarely and small fields that change more frequently. The smaller
/// fields may be re-indexed in a new index and both indexes may be searched
/// together.
///
/// <para/><strong>Warning:</strong> It is up to you to make sure all indexes
/// are created and modified the same way. For example, if you add
/// documents to one index, you need to add the same documents in the
/// same order to the other indexes. <em>Failure to do so will result in
/// undefined behavior</em>.
/// </summary>
public class ParallelAtomicReader : AtomicReader
{
private void InitializeInstanceFields()
{
fields = new ParallelFields(this);
}
private readonly FieldInfos fieldInfos;
private ParallelFields fields;
private readonly AtomicReader[] parallelReaders, storedFieldsReaders;
private readonly ISet<AtomicReader> completeReaderSet = new JCG.HashSet<AtomicReader>(IdentityEqualityComparer<AtomicReader>.Default);
private readonly bool closeSubReaders;
private readonly int maxDoc, numDocs;
private readonly bool hasDeletions;
// LUCENENET specific: Use StringComparer.Ordinal to get the same ordering as Java
private readonly IDictionary<string, AtomicReader> fieldToReader = new JCG.SortedDictionary<string, AtomicReader>(StringComparer.Ordinal);
private readonly IDictionary<string, AtomicReader> tvFieldToReader = new JCG.SortedDictionary<string, AtomicReader>(StringComparer.Ordinal);
/// <summary>
/// Create a <see cref="ParallelAtomicReader"/> based on the provided
/// readers; auto-disposes the given <paramref name="readers"/> on <see cref="IndexReader.Dispose()"/>.
/// </summary>
public ParallelAtomicReader(params AtomicReader[] readers)
: this(true, readers)
{
}
/// <summary>
/// Create a <see cref="ParallelAtomicReader"/> based on the provided
/// <paramref name="readers"/>.
/// </summary>
public ParallelAtomicReader(bool closeSubReaders, params AtomicReader[] readers)
: this(closeSubReaders, readers, readers)
{
}
/// <summary>
/// Expert: create a <see cref="ParallelAtomicReader"/> based on the provided
/// <paramref name="readers"/> and <paramref name="storedFieldsReaders"/>; when a document is
/// loaded, only <paramref name="storedFieldsReaders"/> will be used.
/// </summary>
public ParallelAtomicReader(bool closeSubReaders, AtomicReader[] readers, AtomicReader[] storedFieldsReaders)
{
InitializeInstanceFields();
this.closeSubReaders = closeSubReaders;
if (readers.Length == 0 && storedFieldsReaders.Length > 0)
{
throw new System.ArgumentException("There must be at least one main reader if storedFieldsReaders are used.");
}
this.parallelReaders = (AtomicReader[])readers.Clone();
this.storedFieldsReaders = (AtomicReader[])storedFieldsReaders.Clone();
if (parallelReaders.Length > 0)
{
AtomicReader first = parallelReaders[0];
this.maxDoc = first.MaxDoc;
this.numDocs = first.NumDocs;
this.hasDeletions = first.HasDeletions;
}
else
{
this.maxDoc = this.numDocs = 0;
this.hasDeletions = false;
}
completeReaderSet.UnionWith(this.parallelReaders);
completeReaderSet.UnionWith(this.storedFieldsReaders);
// check compatibility:
foreach (AtomicReader reader in completeReaderSet)
{
if (reader.MaxDoc != maxDoc)
{
throw new System.ArgumentException("All readers must have same MaxDoc: " + maxDoc + "!=" + reader.MaxDoc);
}
}
// TODO: make this read-only in a cleaner way?
FieldInfos.Builder builder = new FieldInfos.Builder();
// build FieldInfos and fieldToReader map:
foreach (AtomicReader reader in this.parallelReaders)
{
FieldInfos readerFieldInfos = reader.FieldInfos;
foreach (FieldInfo fieldInfo in readerFieldInfos)
{
// NOTE: first reader having a given field "wins":
if (!fieldToReader.ContainsKey(fieldInfo.Name))
{
builder.Add(fieldInfo);
fieldToReader[fieldInfo.Name] = reader;
if (fieldInfo.HasVectors)
{
tvFieldToReader[fieldInfo.Name] = reader;
}
}
}
}
fieldInfos = builder.Finish();
// build Fields instance
foreach (AtomicReader reader in this.parallelReaders)
{
Fields readerFields = reader.Fields;
if (readerFields != null)
{
foreach (string field in readerFields)
{
// only add if the reader responsible for that field name is the current:
if (fieldToReader[field].Equals(reader))
{
this.fields.AddField(field, readerFields.GetTerms(field));
}
}
}
}
// do this finally so any Exceptions occurred before don't affect refcounts:
foreach (AtomicReader reader in completeReaderSet)
{
if (!closeSubReaders)
{
reader.IncRef();
}
reader.RegisterParentReader(this);
}
}
public override string ToString()
{
StringBuilder buffer = new StringBuilder("ParallelAtomicReader(");
bool removeLastCommaSpace = false;
foreach (AtomicReader reader in completeReaderSet)
{
buffer.Append(reader);
buffer.Append(", ");
removeLastCommaSpace = true;
}
if (removeLastCommaSpace)
{
buffer.Remove(buffer.Length - 2, 2);
}
return buffer.Append(')').ToString();
}
// Single instance of this, per ParallelReader instance
private sealed class ParallelFields : Fields
{
private readonly ParallelAtomicReader outerInstance;
// LUCENENET specific: Use StringComparer.Ordinal to get the same ordering as Java
internal readonly IDictionary<string, Terms> fields = new JCG.SortedDictionary<string, Terms>(StringComparer.Ordinal);
internal ParallelFields(ParallelAtomicReader outerInstance)
{
this.outerInstance = outerInstance;
}
internal void AddField(string fieldName, Terms terms)
{
fields[fieldName] = terms;
}
public override IEnumerator<string> GetEnumerator()
{
return fields.Keys.GetEnumerator();
}
public override Terms GetTerms(string field)
{
Terms result;
fields.TryGetValue(field, out result);
return result;
}
public override int Count
{
get { return fields.Count; }
}
}
/// <summary>
/// Get the <see cref="Index.FieldInfos"/> describing all fields in
/// this reader.
/// <para/>
/// NOTE: the returned field numbers will likely not
/// correspond to the actual field numbers in the underlying
/// readers, and codec metadata (<see cref="FieldInfo.GetAttribute(string)"/>
/// will be unavailable.
/// </summary>
public override FieldInfos FieldInfos
{
get
{
return fieldInfos;
}
}
public override IBits LiveDocs
{
get
{
EnsureOpen();
return hasDeletions ? parallelReaders[0].LiveDocs : null;
}
}
public override Fields Fields
{
get
{
EnsureOpen();
return fields;
}
}
public override int NumDocs
{
get
{
// Don't call ensureOpen() here (it could affect performance)
return numDocs;
}
}
public override int MaxDoc
{
get
{
// Don't call ensureOpen() here (it could affect performance)
return maxDoc;
}
}
public override void Document(int docID, StoredFieldVisitor visitor)
{
EnsureOpen();
foreach (AtomicReader reader in storedFieldsReaders)
{
reader.Document(docID, visitor);
}
}
public override Fields GetTermVectors(int docID)
{
EnsureOpen();
ParallelFields fields = null;
foreach (KeyValuePair<string, AtomicReader> ent in tvFieldToReader/*.EntrySet()*/)
{
string fieldName = ent.Key;
Terms vector = ent.Value.GetTermVector(docID, fieldName);
if (vector != null)
{
if (fields == null)
{
fields = new ParallelFields(this);
}
fields.AddField(fieldName, vector);
}
}
return fields;
}
protected internal override void DoClose()
{
lock (this)
{
IOException ioe = null;
foreach (AtomicReader reader in completeReaderSet)
{
try
{
if (closeSubReaders)
{
reader.Dispose();
}
else
{
reader.DecRef();
}
}
catch (IOException e)
{
if (ioe == null)
{
ioe = e;
}
}
}
// throw the first exception
if (ioe != null)
{
throw ioe;
}
}
}
public override NumericDocValues GetNumericDocValues(string field)
{
EnsureOpen();
AtomicReader reader;
return fieldToReader.TryGetValue(field, out reader) ? reader.GetNumericDocValues(field) : null;
}
public override BinaryDocValues GetBinaryDocValues(string field)
{
EnsureOpen();
AtomicReader reader;
return fieldToReader.TryGetValue(field, out reader) ? reader.GetBinaryDocValues(field) : null;
}
public override SortedDocValues GetSortedDocValues(string field)
{
EnsureOpen();
AtomicReader reader;
return fieldToReader.TryGetValue(field, out reader) ? reader.GetSortedDocValues(field) : null;
}
public override SortedSetDocValues GetSortedSetDocValues(string field)
{
EnsureOpen();
AtomicReader reader;
return fieldToReader.TryGetValue(field, out reader) ? reader.GetSortedSetDocValues(field) : null;
}
public override IBits GetDocsWithField(string field)
{
EnsureOpen();
AtomicReader reader;
return fieldToReader.TryGetValue(field, out reader) ? reader.GetDocsWithField(field) : null;
}
public override NumericDocValues GetNormValues(string field)
{
EnsureOpen();
AtomicReader reader;
NumericDocValues values = null;
if (fieldToReader.TryGetValue(field, out reader))
{
values = reader.GetNormValues(field);
}
return values;
}
public override void CheckIntegrity()
{
EnsureOpen();
foreach (AtomicReader reader in completeReaderSet)
{
reader.CheckIntegrity();
}
}
}
}