forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyDocument.cs
More file actions
402 lines (359 loc) · 18.1 KB
/
Copy pathLazyDocument.cs
File metadata and controls
402 lines (359 loc) · 18.1 KB
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
using Lucene.Net.Analysis;
using Lucene.Net.Diagnostics;
using Lucene.Net.Index;
using Lucene.Net.Support.Threading;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Documents
{
/*
* 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.
*/
/// <summary>
/// Defers actually loading a field's value until you ask
/// for it. You must not use the returned Field instances
/// after the provided reader has been closed. </summary>
/// <seealso cref="GetField(FieldInfo)"/>
public class LazyDocument
{
private readonly IndexReader reader;
private readonly int docID;
// null until first field is loaded
private Document doc;
private readonly IDictionary<int, IList<LazyField>> fields = new JCG.Dictionary<int, IList<LazyField>>(); // LUCENENET: marked readonly
private readonly ISet<string> fieldNames = new JCG.HashSet<string>(); // LUCENENET: marked readonly
public LazyDocument(IndexReader reader, int docID)
{
this.reader = reader;
this.docID = docID;
}
/// <summary>
/// Creates an IndexableField whose value will be lazy loaded if and
/// when it is used.
/// <para>
/// <b>NOTE:</b> This method must be called once for each value of the field
/// name specified in sequence that the values exist. This method may not be
/// used to generate multiple, lazy, IndexableField instances referring to
/// the same underlying IndexableField instance.
/// </para>
/// <para>
/// The lazy loading of field values from all instances of IndexableField
/// objects returned by this method are all backed by a single Document
/// per LazyDocument instance.
/// </para>
/// </summary>
public virtual IIndexableField GetField(FieldInfo fieldInfo)
{
fieldNames.Add(fieldInfo.Name);
if (!fields.TryGetValue(fieldInfo.Number, out IList<LazyField> values) || null == values)
{
values = new JCG.List<LazyField>();
fields[fieldInfo.Number] = values;
}
LazyField value = new LazyField(this, fieldInfo.Name, fieldInfo.Number);
values.Add(value);
UninterruptableMonitor.Enter(this);
try
{
// edge case: if someone asks this LazyDoc for more LazyFields
// after other LazyFields from the same LazyDoc have been
// actuallized, we need to force the doc to be re-fetched
// so the new LazyFields are also populated.
doc = null;
}
finally
{
UninterruptableMonitor.Exit(this);
}
return value;
}
/// <summary>
/// non-private for test only access
/// @lucene.internal
/// </summary>
internal virtual Document GetDocument()
{
UninterruptableMonitor.Enter(this);
try
{
if (doc is null)
{
try
{
doc = reader.Document(docID, fieldNames);
}
catch (Exception ioe) when (ioe.IsIOException())
{
throw IllegalStateException.Create("unable to load document", ioe);
}
}
return doc;
}
finally
{
UninterruptableMonitor.Exit(this);
}
}
// :TODO: synchronize to prevent redundant copying? (sync per field name?)
private void FetchRealValues(string name, int fieldNum)
{
Document d = GetDocument();
fields.TryGetValue(fieldNum, out IList<LazyField> lazyValues);
IIndexableField[] realValues = d.GetFields(name);
if (Debugging.AssertsEnabled) Debugging.Assert(realValues.Length <= lazyValues.Count,
"More lazy values then real values for field: {0}", name);
for (int i = 0; i < lazyValues.Count; i++)
{
LazyField f = lazyValues[i];
if (null != f)
{
f.realValue = realValues[i];
}
}
}
/// <summary>
/// @lucene.internal
/// </summary>
public class LazyField : IIndexableField, IFormattable
{
private readonly LazyDocument outerInstance;
internal readonly string name; // LUCENENET: marked readonly
internal readonly int fieldNum; // LUCENENET: marked readonly
internal volatile IIndexableField realValue = null;
internal LazyField(LazyDocument outerInstance, string name, int fieldNum)
{
this.outerInstance = outerInstance;
this.name = name;
this.fieldNum = fieldNum;
}
/// <summary>
/// non-private for test only access
/// @lucene.internal
/// </summary>
public virtual bool HasBeenLoaded => null != realValue;
internal virtual IIndexableField GetRealValue()
{
if (null == realValue)
{
outerInstance.FetchRealValues(name, fieldNum);
}
if (Debugging.AssertsEnabled)
{
Debugging.Assert(HasBeenLoaded, "field value was not lazy loaded");
Debugging.Assert(realValue.Name.Equals(Name, StringComparison.Ordinal), "realvalue name != name: {0} != {1}", realValue.Name, Name);
}
return realValue;
}
/// <summary>
/// The field's name
/// </summary>
public virtual string Name => name;
/// <summary>
/// Gets the boost factor on this field.
/// </summary>
public virtual float Boost => 1.0f;
/// <summary>
/// Non-null if this field has a binary value. </summary>
public virtual BytesRef GetBinaryValue()
{
return GetRealValue().GetBinaryValue();
}
/// <summary>
/// The value of the field as a <see cref="string"/>, or <c>null</c>. If <c>null</c>, the <see cref="TextReader"/> value or
/// binary value is used. Exactly one of <see cref="GetStringValue()"/>, <see cref="GetReaderValue()"/>, and
/// <see cref="GetBinaryValue()"/> must be set.
/// </summary>
/// <returns>The string representation of the value if it is either a <see cref="string"/> or numeric type.</returns>
public virtual string GetStringValue()
{
return GetRealValue().GetStringValue();
}
/// <summary>
/// The value of the field as a <see cref="string"/>, or <c>null</c>. If <c>null</c>, the <see cref="TextReader"/> value or
/// binary value is used. Exactly one of <see cref="GetStringValue()"/>, <see cref="GetReaderValue()"/>, and
/// <see cref="GetBinaryValue()"/> must be set.
/// </summary>
/// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param>
/// <returns>The string representation of the value if it is either a <see cref="string"/> or numeric type.</returns>
// LUCENENET specific - created overload so we can format an underlying numeric type using specified provider
public virtual string GetStringValue(IFormatProvider provider)
{
return GetRealValue().GetStringValue(provider);
}
/// <summary>
/// The value of the field as a <see cref="string"/>, or <c>null</c>. If <c>null</c>, the <see cref="TextReader"/> value or
/// binary value is used. Exactly one of <see cref="GetStringValue()"/>, <see cref="GetReaderValue()"/>, and
/// <see cref="GetBinaryValue()"/> must be set.
/// </summary>
/// <param name="format">A standard or custom numeric format string. This parameter has no effect if this field is non-numeric.</param>
/// <returns>The string representation of the value if it is either a <see cref="string"/> or numeric type.</returns>
// LUCENENET specific - created overload so we can format an underlying numeric type using specified format
public virtual string GetStringValue(string format)
{
return GetRealValue().GetStringValue(format);
}
/// <summary>
/// The value of the field as a <see cref="string"/>, or <c>null</c>. If <c>null</c>, the <see cref="TextReader"/> value or
/// binary value is used. Exactly one of <see cref="GetStringValue()"/>, <see cref="GetReaderValue()"/>, and
/// <see cref="GetBinaryValue()"/> must be set.
/// </summary>
/// <param name="format">A standard or custom numeric format string. This parameter has no effect if this field is non-numeric.</param>
/// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param>
/// <returns>The string representation of the value if it is either a <see cref="string"/> or numeric type.</returns>
// LUCENENET specific - created overload so we can format an underlying numeric type using specified format and provider
public virtual string GetStringValue(string format, IFormatProvider provider)
{
return GetRealValue().GetStringValue(format, provider);
}
/// <summary>
/// The value of the field as a <see cref="TextReader"/>, or <c>null</c>. If <c>null</c>, the <see cref="string"/> value or
/// binary value is used. Exactly one of <see cref="GetStringValue()"/>, <see cref="GetReaderValue()"/>, and
/// <see cref="GetBinaryValue()"/> must be set.
/// </summary>
public virtual TextReader GetReaderValue()
{
return GetRealValue().GetReaderValue();
}
[Obsolete("In .NET, use of this method will cause boxing/unboxing. Instead, use the NumericType property to check the underlying type and call the appropriate GetXXXValue() method to retrieve the value.")]
public virtual object GetNumericValue()
{
return GetRealValue().GetNumericValue();
}
/// <summary>
/// Gets the <see cref="NumericFieldType"/> of the underlying value, or <see cref="NumericFieldType.NONE"/> if the value is not set or non-numeric.
/// <para/>
/// Expert: The difference between this property and <see cref="FieldType.NumericType"/> is
/// this is represents the current state of the field (whether being written or read) and the
/// <see cref="FieldType"/> property represents instructions on how the field will be written,
/// but does not re-populate when reading back from an index (it is write-only).
/// <para/>
/// In Java, the numeric type was determined by checking the type of
/// <see cref="GetNumericValue()"/>. However, since there are no reference number
/// types in .NET, using <see cref="GetNumericValue()"/> so will cause boxing/unboxing. It is
/// therefore recommended to use this property to check the underlying type and the corresponding
/// <c>Get*Value()</c> method to retrieve the value.
/// <para/>
/// NOTE: Since Lucene codecs do not support <see cref="NumericFieldType.BYTE"/> or <see cref="NumericFieldType.INT16"/>,
/// fields created with these types will always be <see cref="NumericFieldType.INT32"/> when read back from the index.
/// </summary>
// LUCENENET specific - Since we have no numeric reference types in .NET, this method was added to check
// the numeric type of the inner field without boxing/unboxing.
public virtual NumericFieldType NumericType => GetRealValue().NumericType;
/// <summary>
/// Returns the field value as <see cref="byte"/> or <c>null</c> if the type
/// is non-numeric.
/// </summary>
/// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
// LUCENENET specific - created overload for Byte, since we have no Number class in .NET
public virtual byte? GetByteValue()
{
return GetRealValue().GetByteValue();
}
/// <summary>
/// Returns the field value as <see cref="short"/> or <c>null</c> if the type
/// is non-numeric.
/// </summary>
/// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
// LUCENENET specific - created overload for Short, since we have no Number class in .NET
public virtual short? GetInt16Value()
{
return GetRealValue().GetInt16Value();
}
/// <summary>
/// Returns the field value as <see cref="int"/> or <c>null</c> if the type
/// is non-numeric.
/// </summary>
/// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
// LUCENENET specific - created overload for Int32, since we have no Number class in .NET
public virtual int? GetInt32Value()
{
return GetRealValue().GetInt32Value();
}
/// <summary>
/// Returns the field value as <see cref="long"/> or <c>null</c> if the type
/// is non-numeric.
/// </summary>
/// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
// LUCENENET specific - created overload for Int64, since we have no Number class in .NET
public virtual long? GetInt64Value()
{
return GetRealValue().GetInt64Value();
}
/// <summary>
/// Returns the field value as <see cref="float"/> or <c>null</c> if the type
/// is non-numeric.
/// </summary>
/// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
// LUCENENET specific - created overload for Single, since we have no Number class in .NET
public virtual float? GetSingleValue()
{
return GetRealValue().GetSingleValue();
}
/// <summary>
/// Returns the field value as <see cref="double"/> or <c>null</c> if the type
/// is non-numeric.
/// </summary>
/// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
// LUCENENET specific - created overload for Double, since we have no Number class in .NET
public virtual double? GetDoubleValue()
{
return GetRealValue().GetDoubleValue();
}
/// <summary>
/// Returns the <see cref="Documents.FieldType"/> for this field as type <see cref="Documents.FieldType"/>. </summary>
public virtual FieldType FieldType => GetRealValue().IndexableFieldType as FieldType;
/// <summary>
/// Returns the <see cref="Documents.FieldType"/> for this field as type <see cref="IIndexableFieldType"/>. </summary>
public virtual IIndexableFieldType IndexableFieldType => GetRealValue().IndexableFieldType;
public virtual TokenStream GetTokenStream(Analyzer analyzer)
{
return GetRealValue().GetTokenStream(analyzer);
}
// LUCENENET specific - method added for better .NET compatibility
public override string ToString()
{
return ToString(null, J2N.Text.StringFormatter.CurrentCulture);
}
// LUCENENET specific - method added for better .NET compatibility
public virtual string ToString(string format)
{
return ToString(format, J2N.Text.StringFormatter.CurrentCulture);
}
// LUCENENET specific - method added for better .NET compatibility
public virtual string ToString(IFormatProvider provider)
{
return ToString(null, provider);
}
// LUCENENET specific - method added for better .NET compatibility
public virtual string ToString(string format, IFormatProvider provider)
{
IIndexableField rv = GetRealValue();
if (rv is IFormattable formattable)
{
return formattable.ToString(format, provider);
}
else
{
return rv.ToString();
}
}
}
}
}