forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectDocValuesConsumer.cs
More file actions
394 lines (352 loc) · 13.6 KB
/
Copy pathDirectDocValuesConsumer.cs
File metadata and controls
394 lines (352 loc) · 13.6 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
using Lucene.Net.Diagnostics;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Lucene.Net.Codecs.Memory
{
/*
* 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 BytesRef = Util.BytesRef;
using FieldInfo = Index.FieldInfo;
using IndexFileNames = Index.IndexFileNames;
using IndexOutput = Store.IndexOutput;
using IOUtils = Util.IOUtils;
using SegmentWriteState = Index.SegmentWriteState;
/// <summary>
/// Writer for <see cref="DirectDocValuesFormat"/>.
/// </summary>
internal class DirectDocValuesConsumer : DocValuesConsumer
{
#pragma warning disable CA2213 // Disposable fields should be disposed
private IndexOutput data, meta;
#pragma warning restore CA2213 // Disposable fields should be disposed
//private readonly int maxDoc; // LUCENENET: Not used
internal DirectDocValuesConsumer(SegmentWriteState state, string dataCodec, string dataExtension,
string metaCodec, string metaExtension)
{
//maxDoc = state.SegmentInfo.DocCount; // LUCENENET: Not used
bool success = false;
try
{
string dataName = IndexFileNames.SegmentFileName(state.SegmentInfo.Name, state.SegmentSuffix,
dataExtension);
data = state.Directory.CreateOutput(dataName, state.Context);
CodecUtil.WriteHeader(data, dataCodec, DirectDocValuesProducer.VERSION_CURRENT);
string metaName = IndexFileNames.SegmentFileName(state.SegmentInfo.Name, state.SegmentSuffix,
metaExtension);
meta = state.Directory.CreateOutput(metaName, state.Context);
CodecUtil.WriteHeader(meta, metaCodec, DirectDocValuesProducer.VERSION_CURRENT);
success = true;
}
finally
{
if (!success)
{
IOUtils.DisposeWhileHandlingException(this);
}
}
}
public override void AddNumericField(FieldInfo field, IEnumerable<long?> values)
{
meta.WriteVInt32(field.Number);
meta.WriteByte(MemoryDocValuesProducer.NUMBER);
AddNumericFieldValues(field, values);
}
private void AddNumericFieldValues(FieldInfo field, IEnumerable<long?> values)
{
meta.WriteInt64(data.Position); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
long minValue = long.MaxValue;
long maxValue = long.MinValue;
bool missing = false;
long count = 0;
foreach (var nv in values)
{
if (nv != null)
{
var v = nv.Value;
minValue = Math.Min(minValue, v);
maxValue = Math.Max(maxValue, v);
}
else
{
missing = true;
}
count++;
if (count >= DirectDocValuesFormat.MAX_SORTED_SET_ORDS)
{
throw new ArgumentException("DocValuesField \"" + field.Name + "\" is too large, must be <= " +
DirectDocValuesFormat.MAX_SORTED_SET_ORDS + " values/total ords");
}
}
meta.WriteInt32((int)count);
if (missing)
{
long start = data.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
WriteMissingBitset(values);
meta.WriteInt64(start);
meta.WriteInt64(data.Position - start); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
}
else
{
meta.WriteInt64(-1L);
}
byte byteWidth;
if (minValue >= sbyte.MinValue && maxValue <= sbyte.MaxValue)
{
byteWidth = 1;
}
else if (minValue >= short.MinValue && maxValue <= short.MaxValue)
{
byteWidth = 2;
}
else if (minValue >= int.MinValue && maxValue <= int.MaxValue)
{
byteWidth = 4;
}
else
{
byteWidth = 8;
}
meta.WriteByte(byteWidth);
foreach (var nv in values)
{
long v = nv.GetValueOrDefault();
switch (byteWidth)
{
case 1:
data.WriteByte((byte)v);
break;
case 2:
data.WriteInt16((short)v);
break;
case 4:
data.WriteInt32((int)v);
break;
case 8:
data.WriteInt64(v);
break;
}
}
}
protected override void Dispose(bool disposing)
{
if (!disposing) return;
var success = false;
try
{
if (meta != null)
{
meta.WriteVInt32(-1); // write EOF marker
CodecUtil.WriteFooter(meta); // write checksum
}
if (data != null)
{
CodecUtil.WriteFooter(data);
}
success = true;
}
finally
{
if (success)
{
IOUtils.Dispose(data, meta);
}
else
{
IOUtils.DisposeWhileHandlingException(data, meta);
}
data = meta = null;
}
}
public override void AddBinaryField(FieldInfo field, IEnumerable<BytesRef> values)
{
meta.WriteVInt32(field.Number);
meta.WriteByte(MemoryDocValuesProducer.BYTES);
AddBinaryFieldValues(field, values);
}
private void AddBinaryFieldValues(FieldInfo field, IEnumerable<BytesRef> values)
{
// write the byte[] data
long startFP = data.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
bool missing = false;
long totalBytes = 0;
int count = 0;
foreach (BytesRef v in values)
{
if (v != null)
{
data.WriteBytes(v.Bytes, v.Offset, v.Length);
totalBytes += v.Length;
if (totalBytes > DirectDocValuesFormat.MAX_TOTAL_BYTES_LENGTH)
{
throw new ArgumentException("DocValuesField \"" + field.Name +
"\" is too large, cannot have more than DirectDocValuesFormat.MAX_TOTAL_BYTES_LENGTH (" +
DirectDocValuesFormat.MAX_TOTAL_BYTES_LENGTH + ") bytes");
}
}
else
{
missing = true;
}
count++;
}
meta.WriteInt64(startFP);
meta.WriteInt32((int)totalBytes);
meta.WriteInt32(count);
if (missing)
{
long start = data.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
WriteMissingBitset(values);
meta.WriteInt64(start);
meta.WriteInt64(data.Position - start); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
}
else
{
meta.WriteInt64(-1L);
}
int addr = 0;
foreach (BytesRef v in values)
{
data.WriteInt32(addr);
if (v != null)
{
addr += v.Length;
}
}
data.WriteInt32(addr);
}
// TODO: in some cases representing missing with minValue-1 wouldn't take up additional space and so on,
// but this is very simple, and algorithms only check this for values of 0 anyway (doesn't slow down normal decode)
internal virtual void WriteMissingBitset<T1>(IEnumerable<T1> values)
{
long bits = 0;
int count = 0;
foreach (object v in values)
{
if (count == 64)
{
data.WriteInt64(bits);
count = 0;
bits = 0;
}
if (v != null)
{
bits |= 1L << (count & 0x3f);
}
count++;
}
if (count > 0)
{
data.WriteInt64(bits);
}
}
public override void AddSortedField(FieldInfo field, IEnumerable<BytesRef> values, IEnumerable<long?> docToOrd)
{
meta.WriteVInt32(field.Number);
meta.WriteByte((byte)DirectDocValuesProducer.SORTED);
// write the ordinals as numerics
AddNumericFieldValues(field, docToOrd);
// write the values as binary
AddBinaryFieldValues(field, values);
}
// note: this might not be the most efficient... but its fairly simple
public override void AddSortedSetField(FieldInfo field, IEnumerable<BytesRef> values,
IEnumerable<long?> docToOrdCount, IEnumerable<long?> ords)
{
meta.WriteVInt32(field.Number);
meta.WriteByte((byte)DirectDocValuesProducer.SORTED_SET);
// First write docToOrdCounts, except we "aggregate" the
// counts so they turn into addresses, and add a final
// value = the total aggregate:
AddNumericFieldValues(field, new EnumerableAnonymousClass(docToOrdCount));
// Write ordinals for all docs, appended into one big
// numerics:
AddNumericFieldValues(field, ords);
// write the values as binary
AddBinaryFieldValues(field, values);
}
private sealed class EnumerableAnonymousClass : IEnumerable<long?>
{
private readonly IEnumerable<long?> _docToOrdCount;
public EnumerableAnonymousClass(IEnumerable<long?> docToOrdCount)
{
_docToOrdCount = docToOrdCount;
}
// Just aggregates the count values so they become
// "addresses", and adds one more value in the end
// (the final sum):
public IEnumerator<long?> GetEnumerator()
{
return new Enumerator(_docToOrdCount);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private sealed class Enumerator : IEnumerator<long?>
{
private readonly IEnumerator<long?> iter;
public Enumerator(IEnumerable<long?> docToOrdCount)
{
this.iter = docToOrdCount.GetEnumerator();
}
private long sum;
private bool ended;
private long toReturn;
public long? Current => toReturn;
object IEnumerator.Current => Current;
// LUCENENET: Remove() not supported by .NET
public bool MoveNext()
{
if (ended) return false;
toReturn = sum;
if (iter.MoveNext())
{
long? n = iter.Current;
if (n.HasValue)
{
sum += n.Value;
}
return true;
}
else if (!ended)
{
ended = true;
return true;
}
else
{
if (Debugging.AssertsEnabled) Debugging.Assert(false);
return false;
}
}
public void Reset()
{
throw UnsupportedOperationException.Create();
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
public void Dispose()
{
if (!disposedValue)
{
iter.Dispose();
disposedValue = true;
}
}
#endregion
}
}
}
}