-
Notifications
You must be signed in to change notification settings - Fork 623
/
OfflineSorter.cs
688 lines (620 loc) · 27.9 KB
/
OfflineSorter.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
using Lucene.Net.Diagnostics;
using Lucene.Net.Store;
using Lucene.Net.Support.IO;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using JCG = J2N.Collections.Generic;
namespace Lucene.Net.Util
{
/*
* 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>
/// On-disk sorting of byte arrays. Each byte array (entry) is a composed of the following
/// fields:
/// <list type="bullet">
/// <item><description>(two bytes) length of the following byte array,</description></item>
/// <item><description>exactly the above count of bytes for the sequence to be sorted.</description></item>
/// </list>
/// </summary>
public sealed class OfflineSorter
{
/// <summary>
/// Convenience constant for megabytes </summary>
public const long MB = 1024 * 1024;
/// <summary>
/// Convenience constant for gigabytes </summary>
public const long GB = MB * 1024;
/// <summary>
/// Minimum recommended buffer size for sorting.
/// </summary>
public const long MIN_BUFFER_SIZE_MB = 32;
/// <summary>
/// Absolute minimum required buffer size for sorting.
/// </summary>
public const long ABSOLUTE_MIN_SORT_BUFFER_SIZE = MB / 2;
private const string MIN_BUFFER_SIZE_MSG = "At least 0.5MB RAM buffer is needed";
/// <summary>
/// Maximum number of temporary files before doing an intermediate merge.
/// </summary>
public const int MAX_TEMPFILES = 128;
/// <summary>
/// A bit more descriptive unit for constructors.
/// </summary>
/// <seealso cref="Automatic()"/>
/// <seealso cref="Megabytes(long)"/>
public sealed class BufferSize
{
internal readonly int bytes;
private BufferSize(long bytes)
{
if (bytes > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(bytes), "Buffer too large for .NET (" + (int.MaxValue / MB) + "mb max): " + bytes); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention)
}
if (bytes < ABSOLUTE_MIN_SORT_BUFFER_SIZE)
{
throw new ArgumentOutOfRangeException(nameof(bytes), MIN_BUFFER_SIZE_MSG + ": " + bytes); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention)
}
this.bytes = (int)bytes;
}
/// <summary>
/// Creates a <see cref="BufferSize"/> in MB. The given
/// values must be > 0 and < 2048.
/// </summary>
public static BufferSize Megabytes(long mb)
{
return new BufferSize(mb * MB);
}
/// <summary>
/// Approximately half of the currently available free heap, but no less
/// than <see cref="ABSOLUTE_MIN_SORT_BUFFER_SIZE"/>. However if current heap allocation
/// is insufficient or if there is a large portion of unallocated heap-space available
/// for sorting consult with max allowed heap size.
/// </summary>
public static BufferSize Automatic()
{
long max, total, free;
using (var proc = Process.GetCurrentProcess())
{
// take sizes in "conservative" order
max = proc.PeakVirtualMemorySize64; // max allocated; java has it as Runtime.maxMemory();
total = proc.VirtualMemorySize64; // currently allocated; java has it as Runtime.totalMemory();
free = proc.PrivateMemorySize64; // unused portion of currently allocated; java has it as Runtime.freeMemory();
}
long totalAvailableBytes = max - total + free;
// by free mem (attempting to not grow the heap for this)
long sortBufferByteSize = free / 2;
long minBufferSizeBytes = MIN_BUFFER_SIZE_MB * MB;
if (sortBufferByteSize < minBufferSizeBytes || totalAvailableBytes > 10 * minBufferSizeBytes) // lets see if we need/should to grow the heap
{
if (totalAvailableBytes / 2 > minBufferSizeBytes) // there is enough mem for a reasonable buffer
{
sortBufferByteSize = totalAvailableBytes / 2; // grow the heap
}
else
{
//heap seems smallish lets be conservative fall back to the free/2
sortBufferByteSize = Math.Max(ABSOLUTE_MIN_SORT_BUFFER_SIZE, sortBufferByteSize);
}
}
return new BufferSize(Math.Min((long)int.MaxValue, sortBufferByteSize));
}
}
/// <summary>
/// Sort info (debugging mostly).
/// </summary>
public class SortInfo
{
/// <summary>
/// Number of temporary files created when merging partitions </summary>
public int TempMergeFiles { get; set; }
/// <summary>
/// Number of partition merges </summary>
public int MergeRounds { get; set; }
/// <summary>
/// Number of lines of data read </summary>
public int Lines { get; set; }
/// <summary>
/// Time spent merging sorted partitions (in milliseconds) </summary>
public long MergeTime { get; set; }
/// <summary>
/// Time spent sorting data (in milliseconds) </summary>
public long SortTime { get; set; }
/// <summary>
/// Total time spent (in milliseconds) </summary>
public long TotalTime { get; set; }
/// <summary>
/// Time spent in i/o read (in milliseconds) </summary>
public long ReadTime { get; set; }
/// <summary>
/// Read buffer size (in bytes) </summary>
public long BufferSize { get; private set; }
/// <summary>
/// Create a new <see cref="SortInfo"/> (with empty statistics) for debugging. </summary>
public SortInfo(OfflineSorter offlineSorter)
{
BufferSize = offlineSorter.ramBufferSize.bytes;
}
/// <summary>
/// Returns a string representation of this object.
/// </summary>
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture,
"time={0:0.00} sec. total ({1:0.00} reading, {2:0.00} sorting, {3:0.00} merging), lines={4}, temp files={5}, merges={6}, soft ram limit={7:0.00} MB",
TotalTime / 1000.0d, ReadTime / 1000.0d, SortTime / 1000.0d, MergeTime / 1000.0d,
Lines, TempMergeFiles, MergeRounds,
(double)BufferSize / MB);
}
}
private readonly BufferSize ramBufferSize;
private readonly Counter bufferBytesUsed = Counter.NewCounter();
private readonly BytesRefArray buffer;
private SortInfo sortInfo;
private readonly int maxTempFiles;
private readonly IComparer<BytesRef> comparer;
/// <summary>
/// Default comparer: sorts in binary (codepoint) order </summary>
public static readonly IComparer<BytesRef> DEFAULT_COMPARER = Utf8SortedAsUnicodeComparer.Instance;
/// <summary>
/// Defaults constructor.
/// </summary>
/// <seealso cref="DefaultTempDir()"/>
/// <seealso cref="BufferSize.Automatic()"/>
public OfflineSorter()
: this(DEFAULT_COMPARER, BufferSize.Automatic(), DefaultTempDir(), MAX_TEMPFILES)
{
}
/// <summary>
/// Defaults constructor with a custom comparer.
/// </summary>
/// <seealso cref="DefaultTempDir()"/>
/// <seealso cref="BufferSize.Automatic()"/>
public OfflineSorter(IComparer<BytesRef> comparer)
: this(comparer, BufferSize.Automatic(), DefaultTempDir(), MAX_TEMPFILES)
{
}
/// <summary>
/// All-details constructor.
/// </summary>
#pragma warning disable IDE0060 // Remove unused parameter
public OfflineSorter(IComparer<BytesRef> comparer, BufferSize ramBufferSize, DirectoryInfo tempDirectory, int maxTempfiles)
#pragma warning restore IDE0060 // Remove unused parameter
{
buffer = new BytesRefArray(bufferBytesUsed);
if (ramBufferSize.bytes < ABSOLUTE_MIN_SORT_BUFFER_SIZE)
{
throw new ArgumentException(MIN_BUFFER_SIZE_MSG + ": " + ramBufferSize.bytes);
}
if (maxTempfiles < 2)
{
throw new ArgumentOutOfRangeException(nameof(maxTempfiles), "maxTempFiles must be >= 2"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention)
}
this.ramBufferSize = ramBufferSize;
this.maxTempFiles = maxTempfiles;
this.comparer = comparer;
}
/// <summary>
/// Sort input to output, explicit hint for the buffer size. The amount of allocated
/// memory may deviate from the hint (may be smaller or larger).
/// </summary>
public SortInfo Sort(FileInfo input, FileInfo output)
{
sortInfo = new SortInfo(this) { TotalTime = J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond }; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
output.Delete();
var merges = new JCG.List<FileInfo>();
bool success2 = false;
try
{
var inputStream = new ByteSequencesReader(input);
bool success = false;
try
{
int lines = 0;
while ((lines = ReadPartition(inputStream)) > 0)
{
merges.Add(SortPartition(/*lines*/)); // LUCENENET specific - removed unused parameter
sortInfo.TempMergeFiles++;
sortInfo.Lines += lines;
// Handle intermediate merges.
if (merges.Count == maxTempFiles)
{
var intermediate = new FileInfo(Path.GetTempFileName());
try
{
MergePartitions(merges, intermediate);
}
finally
{
foreach (var file in merges)
{
file.Delete();
}
merges.Clear();
merges.Add(intermediate);
}
sortInfo.TempMergeFiles++;
}
}
success = true;
}
finally
{
if (success)
{
IOUtils.Dispose(inputStream);
}
else
{
IOUtils.DisposeWhileHandlingException(inputStream);
}
}
// One partition, try to rename or copy if unsuccessful.
if (merges.Count == 1)
{
FileInfo single = merges[0];
Copy(single, output);
try
{
File.Delete(single.FullName);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch
{
// ignored
}
#pragma warning restore CA1031 // Do not catch general exception types
}
else
{
// otherwise merge the partitions with a priority queue.
MergePartitions(merges, output);
}
success2 = true;
}
finally
{
foreach (FileInfo file in merges)
{
file.Delete();
}
if (!success2)
{
output.Delete();
}
}
sortInfo.TotalTime = ((J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) - sortInfo.TotalTime); // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
return sortInfo;
}
/// <summary>
/// Returns the default temporary directory. By default, the System's temp folder. If not accessible
/// or not available, an <see cref="IOException"/> is thrown.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DirectoryInfo DefaultTempDir()
{
return new DirectoryInfo(Path.GetTempPath());
}
/// <summary>
/// Copies one file to another.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Copy(FileInfo file, FileInfo output)
{
using Stream inputStream = file.OpenRead();
using Stream outputStream = output.OpenWrite();
inputStream.CopyTo(outputStream);
}
/// <summary>
/// Sort a single partition in-memory. </summary>
private FileInfo SortPartition(/*int len*/) // LUCENENET NOTE: made private, since protected is not valid in a sealed class. Also eliminated unused parameter.
{
var data = this.buffer;
FileInfo tempFile = FileSupport.CreateTempFile("sort", "partition", DefaultTempDir());
long start = J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
sortInfo.SortTime += ((J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) - start); // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
using (var @out = new ByteSequencesWriter(tempFile))
{
IBytesRefEnumerator iter = buffer.GetEnumerator(comparer);
while (iter.MoveNext())
{
if (Debugging.AssertsEnabled) Debugging.Assert(iter.Current.Length <= ushort.MaxValue);
@out.Write(iter.Current);
}
}
// Clean up the buffer for the next partition.
data.Clear();
return tempFile;
}
/// <summary>
/// Merge a list of sorted temporary files (partitions) into an output file. </summary>
internal void MergePartitions(IList<FileInfo> merges, FileInfo outputFile)
{
long start = J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
var @out = new ByteSequencesWriter(outputFile);
PriorityQueue<FileAndTop> queue = new PriorityQueueAnonymousClass(this, merges.Count);
var streams = new ByteSequencesReader[merges.Count];
try
{
// Open streams and read the top for each file
for (int i = 0; i < merges.Count; i++)
{
streams[i] = new ByteSequencesReader(merges[i]);
byte[] line = streams[i].Read();
if (line != null)
{
queue.InsertWithOverflow(new FileAndTop(i, line));
}
}
// Unix utility sort() uses ordered array of files to pick the next line from, updating
// it as it reads new lines. The PQ used here is a more elegant solution and has
// a nicer theoretical complexity bound :) The entire sorting process is I/O bound anyway
// so it shouldn't make much of a difference (didn't check).
FileAndTop top;
while ((top = queue.Top) != null)
{
@out.Write(top.Current);
if (!streams[top.Fd].Read(top.Current))
{
queue.Pop();
}
else
{
queue.UpdateTop();
}
}
sortInfo.MergeTime += (J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) - start; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
sortInfo.MergeRounds++;
}
finally
{
// The logic below is: if an exception occurs in closing out, it has a priority over exceptions
// happening in closing streams.
try
{
IOUtils.Dispose(streams);
}
finally
{
IOUtils.Dispose(@out);
}
}
}
private class PriorityQueueAnonymousClass : PriorityQueue<FileAndTop>
{
private readonly OfflineSorter outerInstance;
public PriorityQueueAnonymousClass(OfflineSorter outerInstance, int size)
: base(size)
{
this.outerInstance = outerInstance;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected internal override bool LessThan(FileAndTop a, FileAndTop b)
{
return outerInstance.comparer.Compare(a.Current, b.Current) < 0;
}
}
/// <summary>
/// Read in a single partition of data. </summary>
internal int ReadPartition(ByteSequencesReader reader)
{
long start = J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond; // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
var scratch = new BytesRef();
while ((scratch.Bytes = reader.Read()) != null)
{
scratch.Length = scratch.Bytes.Length;
buffer.Append(scratch);
// Account for the created objects.
// (buffer slots do not account to buffer size.)
if (ramBufferSize.bytes < bufferBytesUsed)
{
break;
}
}
sortInfo.ReadTime += ((J2N.Time.NanoTime() / J2N.Time.MillisecondsPerNanosecond) - start); // LUCENENET: Use NanoTime() rather than CurrentTimeMilliseconds() for more accurate/reliable results
return buffer.Length;
}
internal class FileAndTop
{
internal int Fd { get; private set; }
internal BytesRef Current { get; private set; }
internal FileAndTop(int fd, byte[] firstLine)
{
this.Fd = fd;
this.Current = new BytesRef(firstLine);
}
}
/// <summary>
/// Utility class to emit length-prefixed <see cref="T:byte[]"/> entries to an output stream for sorting.
/// Complementary to <see cref="ByteSequencesReader"/>.
/// </summary>
public class ByteSequencesWriter : IDisposable
{
private readonly DataOutput os;
/// <summary>
/// Constructs a <see cref="ByteSequencesWriter"/> to the provided <see cref="FileInfo"/>. </summary>
public ByteSequencesWriter(FileInfo file)
: this(NewBinaryWriterDataOutput(file))
{
}
/// <summary>
/// Constructs a <see cref="ByteSequencesWriter"/> to the provided <see cref="DataOutput"/>. </summary>
public ByteSequencesWriter(DataOutput os)
{
this.os = os;
}
/// <summary>
/// LUCENENET specific - ensures the file has been created with no BOM
/// if it doesn't already exist and opens the file for writing.
/// Java doesn't use a BOM by default.
/// </summary>
private static BinaryWriterDataOutput NewBinaryWriterDataOutput(FileInfo file)
{
string fileName = file.FullName;
// Create the file (without BOM) if it doesn't already exist
if (!File.Exists(fileName))
{
// Create the file
File.WriteAllText(fileName, string.Empty, new UTF8Encoding(false) /* No BOM */);
}
return new BinaryWriterDataOutput(new BinaryWriter(new FileStream(fileName, FileMode.Open, FileAccess.Write)));
}
/// <summary>
/// Writes a <see cref="BytesRef"/>. </summary>
/// <seealso cref="Write(byte[], int, int)"/>
public virtual void Write(BytesRef @ref)
{
if (Debugging.AssertsEnabled) Debugging.Assert(@ref != null);
Write(@ref.Bytes, @ref.Offset, @ref.Length);
}
/// <summary>
/// Writes a byte array. </summary>
/// <seealso cref="Write(byte[], int, int)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public virtual void Write(byte[] bytes)
{
Write(bytes, 0, bytes.Length);
}
/// <summary>
/// Writes a byte array.
/// <para/>
/// The length is written as a <see cref="short"/>, followed
/// by the bytes.
/// </summary>
public virtual void Write(byte[] bytes, int off, int len)
{
if (Debugging.AssertsEnabled)
{
Debugging.Assert(bytes != null);
Debugging.Assert(off >= 0 && off + len <= bytes.Length);
Debugging.Assert(len >= 0);
}
os.WriteInt16((short)len);
os.WriteBytes(bytes, off, len); // LUCENENET NOTE: We call WriteBytes, since there is no Write() on Lucene's version of DataOutput
}
/// <summary>
/// Disposes the provided <see cref="DataOutput"/> if it is <see cref="IDisposable"/>.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Disposes the provided <see cref="DataOutput"/> if it is <see cref="IDisposable"/>.
/// </summary>
protected virtual void Dispose(bool disposing) // LUCENENET specific - implemented proper dispose pattern
{
if (disposing && this.os is IDisposable disposable)
disposable.Dispose();
}
}
/// <summary>
/// Utility class to read length-prefixed <see cref="T:byte[]"/> entries from an input.
/// Complementary to <see cref="ByteSequencesWriter"/>.
/// </summary>
public class ByteSequencesReader : IDisposable
{
private readonly DataInput inputStream;
/// <summary>
/// Constructs a <see cref="ByteSequencesReader"/> from the provided <see cref="FileInfo"/>. </summary>
public ByteSequencesReader(FileInfo file)
: this(new BinaryReaderDataInput(new BinaryReader(new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))))
{
}
/// <summary>
/// Constructs a <see cref="ByteSequencesReader"/> from the provided <see cref="DataInput"/>. </summary>
public ByteSequencesReader(DataInput inputStream)
{
this.inputStream = inputStream;
}
/// <summary>
/// Reads the next entry into the provided <see cref="BytesRef"/>. The internal
/// storage is resized if needed.
/// </summary>
/// <returns> Returns <c>false</c> if EOF occurred when trying to read
/// the header of the next sequence. Returns <c>true</c> otherwise. </returns>
/// <exception cref="EndOfStreamException"> If the file ends before the full sequence is read. </exception>
public virtual bool Read(BytesRef @ref)
{
ushort length;
try
{
length = (ushort)inputStream.ReadInt16();
}
catch (Exception e) when (e.IsEOFException())
{
return false;
}
@ref.Grow(length);
@ref.Offset = 0;
@ref.Length = length;
inputStream.ReadBytes(@ref.Bytes, 0, length);
return true;
}
/// <summary>
/// Reads the next entry and returns it if successful.
/// </summary>
/// <seealso cref="Read(BytesRef)"/>
/// <returns> Returns <c>null</c> if EOF occurred before the next entry
/// could be read. </returns>
/// <exception cref="EndOfStreamException"> If the file ends before the full sequence is read. </exception>
public virtual byte[] Read()
{
ushort length;
try
{
length = (ushort)inputStream.ReadInt16();
}
catch (Exception e) when (e.IsEOFException())
{
return null;
}
if (Debugging.AssertsEnabled) Debugging.Assert(length >= 0, "Sanity: sequence length < 0: {0}", length);
byte[] result = new byte[length];
inputStream.ReadBytes(result, 0, length);
return result;
}
/// <summary>
/// Disposes the provided <see cref="DataInput"/> if it is <see cref="IDisposable"/>.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) // LUCENENET specific - implemented proper dispose pattern
{
if (disposing)
{
if (inputStream is IDisposable disposable)
{
disposable.Dispose();
}
}
}
}
/// <summary>
/// Returns the comparer in use to sort entries </summary>
public IComparer<BytesRef> Comparer => comparer;
}
}