forked from apache/lucenenet
/
MergePolicy.cs
817 lines (750 loc) · 30.9 KB
/
MergePolicy.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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
using Lucene.Net.Support;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
#if FEATURE_SERIALIZABLE_EXCEPTIONS
using System.Runtime.Serialization;
#endif
using System.Text;
using System.Threading;
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 Directory = Lucene.Net.Store.Directory;
using FixedBitSet = Lucene.Net.Util.FixedBitSet;
using MergeInfo = Lucene.Net.Store.MergeInfo;
//using AlreadySetException = Lucene.Net.Util.SetOnce.AlreadySetException;
/// <summary>
/// <para>Expert: a <see cref="MergePolicy"/> determines the sequence of
/// primitive merge operations.</para>
///
/// <para>Whenever the segments in an index have been altered by
/// <see cref="IndexWriter"/>, either the addition of a newly
/// flushed segment, addition of many segments from
/// AddIndexes* calls, or a previous merge that may now need
/// to cascade, <see cref="IndexWriter"/> invokes <see cref="FindMerges(MergeTrigger, SegmentInfos)"/>
/// to give the <see cref="MergePolicy"/> a chance to pick
/// merges that are now required. This method returns a
/// <see cref="MergeSpecification"/> instance describing the set of
/// merges that should be done, or null if no merges are
/// necessary. When <see cref="IndexWriter.ForceMerge(int)"/> is called, it calls
/// <see cref="FindForcedMerges(SegmentInfos, int, IDictionary{SegmentCommitInfo, bool?})"/> and the <see cref="MergePolicy"/> should
/// then return the necessary merges.</para>
///
/// <para>Note that the policy can return more than one merge at
/// a time. In this case, if the writer is using
/// <see cref="SerialMergeScheduler"/>, the merges will be run
/// sequentially but if it is using
/// <see cref="ConcurrentMergeScheduler"/> they will be run concurrently.</para>
///
/// <para>The default MergePolicy is
/// <see cref="TieredMergePolicy"/>.</para>
///
/// @lucene.experimental
/// </summary>
public abstract class MergePolicy : IDisposable
#if FEATURE_CLONEABLE
, System.ICloneable
#endif
{
/// <summary>
/// A map of doc IDs. </summary>
public abstract class DocMap
{
/// <summary>
/// Sole constructor, typically invoked from sub-classes constructors. </summary>
protected DocMap()
{
}
/// <summary>
/// Return the new doc ID according to its old value. </summary>
public abstract int Map(int old);
/// <summary>
/// Useful from an assert. </summary>
internal virtual bool IsConsistent(int maxDoc)
{
FixedBitSet targets = new FixedBitSet(maxDoc);
for (int i = 0; i < maxDoc; ++i)
{
int target = Map(i);
if (target < 0 || target >= maxDoc)
{
Debug.Assert(false, "out of range: " + target + " not in [0-" + maxDoc + "[");
return false;
}
else if (targets.Get(target))
{
Debug.Assert(false, target + " is already taken (" + i + ")");
return false;
}
}
return true;
}
}
/// <summary>
/// OneMerge provides the information necessary to perform
/// an individual primitive merge operation, resulting in
/// a single new segment. The merge spec includes the
/// subset of segments to be merged as well as whether the
/// new segment should use the compound file format.
/// </summary>
public class OneMerge
{
internal SegmentCommitInfo info; // used by IndexWriter
internal bool registerDone; // used by IndexWriter
internal long mergeGen; // used by IndexWriter
internal bool isExternal; // used by IndexWriter
public int MaxNumSegments // used by IndexWriter
{
get { return maxNumSegments; }
set { maxNumSegments = value; }
}
private int maxNumSegments = -1;
/// <summary>
/// Estimated size in bytes of the merged segment. </summary>
public long EstimatedMergeBytes { get; internal set; } // used by IndexWriter // LUCENENET NOTE: original was volatile, but long cannot be in .NET
// Sum of sizeInBytes of all SegmentInfos; set by IW.mergeInit
internal long totalMergeBytes; // LUCENENET NOTE: original was volatile, but long cannot be in .NET
internal IList<SegmentReader> readers; // used by IndexWriter
/// <summary>
/// Segments to be merged. </summary>
public IList<SegmentCommitInfo> Segments { get; private set; }
/// <summary>
/// Number of documents in the merged segment. </summary>
public int TotalDocCount { get; private set; }
internal bool aborted;
internal Exception error;
internal bool paused;
/// <summary>
/// Sole constructor. </summary>
/// <param name="segments"> List of <seealso cref="SegmentCommitInfo"/>s
/// to be merged. </param>
public OneMerge(IList<SegmentCommitInfo> segments)
{
if (0 == segments.Count)
{
throw new Exception("segments must include at least one segment");
}
// clone the list, as the in list may be based off original SegmentInfos and may be modified
this.Segments = new List<SegmentCommitInfo>(segments);
int count = 0;
foreach (SegmentCommitInfo info in segments)
{
count += info.Info.DocCount;
}
TotalDocCount = count;
}
/// <summary>
/// Expert: Get the list of readers to merge. Note that this list does not
/// necessarily match the list of segments to merge and should only be used
/// to feed SegmentMerger to initialize a merge. When a <see cref="OneMerge"/>
/// reorders doc IDs, it must override <see cref="GetDocMap"/> too so that
/// deletes that happened during the merge can be applied to the newly
/// merged segment.
/// </summary>
public virtual IList<AtomicReader> GetMergeReaders()
{
if (this.readers == null)
{
throw new InvalidOperationException("IndexWriter has not initialized readers from the segment infos yet");
}
IList<AtomicReader> readers = new List<AtomicReader>(this.readers.Count);
foreach (AtomicReader reader in this.readers)
{
if (reader.NumDocs > 0)
{
readers.Add(reader);
}
}
return Collections.UnmodifiableList(readers);
}
/// <summary>
/// Expert: Sets the <see cref="SegmentCommitInfo"/> of this <see cref="OneMerge"/>.
/// Allows sub-classes to e.g. set diagnostics properties.
/// </summary>
public virtual SegmentCommitInfo Info
{
set
{
this.info = value;
}
get
{
return info;
}
}
/// <summary>
/// Expert: If <see cref="GetMergeReaders()"/> reorders document IDs, this method
/// must be overridden to return a mapping from the <i>natural</i> doc ID
/// (the doc ID that would result from a natural merge) to the actual doc
/// ID. This mapping is used to apply deletions that happened during the
/// merge to the new segment.
/// </summary>
public virtual DocMap GetDocMap(MergeState mergeState)
{
return new DocMapAnonymousInnerClassHelper(this);
}
private class DocMapAnonymousInnerClassHelper : DocMap
{
private readonly OneMerge outerInstance;
public DocMapAnonymousInnerClassHelper(OneMerge outerInstance)
{
this.outerInstance = outerInstance;
}
public override int Map(int docID)
{
return docID;
}
}
/// <summary>
/// Record that an exception occurred while executing
/// this merge
/// </summary>
internal virtual Exception Exception
{
set
{
lock (this)
{
this.error = value;
}
}
get
{
lock (this)
{
return error;
}
}
}
/// <summary>
/// Mark this merge as aborted. If this is called
/// before the merge is committed then the merge will
/// not be committed.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
internal virtual void Abort()
{
lock (this)
{
aborted = true;
Monitor.PulseAll(this);
}
}
/// <summary>
/// Returns <c>true</c> if this merge was aborted. </summary>
internal virtual bool IsAborted
{
get
{
lock (this)
{
return aborted;
}
}
}
/// <summary>
/// Called periodically by <see cref="IndexWriter"/> while
/// merging to see if the merge is aborted.
/// </summary>
public virtual void CheckAborted(Directory dir)
{
lock (this)
{
if (aborted)
{
throw new MergeAbortedException("merge is aborted: " + SegString(dir));
}
while (paused)
{
//#if !NETSTANDARD1_6
// try
// {
//#endif
// In theory we could wait() indefinitely, but we
// do 1000 msec, defensively
Monitor.Wait(this, TimeSpan.FromMilliseconds(1000));
//#if !NETSTANDARD1_6 // LUCENENET NOTE: Senseless to catch and rethrow the same exception type
// }
// catch (ThreadInterruptedException ie)
// {
// throw new Exception(ie.ToString(), ie);
// }
//#endif
if (aborted)
{
throw new MergeAbortedException("merge is aborted: " + SegString(dir));
}
}
}
}
/// <summary>
/// Set or clear whether this merge is paused paused (for example
/// <see cref="ConcurrentMergeScheduler"/> will pause merges
/// if too many are running).
/// </summary>
internal virtual void SetPause(bool paused)
{
lock (this)
{
this.paused = paused;
if (!paused)
{
// Wakeup merge thread, if it's waiting
Monitor.PulseAll(this);
}
}
}
/// <summary>
/// Returns <c>true</c> if this merge is paused.
/// </summary>
/// <seealso cref="SetPause(bool)"/>
internal virtual bool IsPaused
{
get
{
lock (this)
{
return paused;
}
}
}
/// <summary>
/// Returns a readable description of the current merge
/// state.
/// </summary>
public virtual string SegString(Directory dir)
{
StringBuilder b = new StringBuilder();
int numSegments = Segments.Count;
for (int i = 0; i < numSegments; i++)
{
if (i > 0)
{
b.Append(' ');
}
b.Append(Segments[i].ToString(dir, 0));
}
if (info != null)
{
b.Append(" into ").Append(info.Info.Name);
}
if (maxNumSegments != -1)
{
b.Append(" [maxNumSegments=" + maxNumSegments + "]");
}
if (aborted)
{
b.Append(" [ABORTED]");
}
return b.ToString();
}
/// <summary>
/// Returns the total size in bytes of this merge. Note that this does not
/// indicate the size of the merged segment, but the
/// input total size. This is only set once the merge is
/// initialized by <see cref="IndexWriter"/>.
/// </summary>
public virtual long TotalBytesSize
{
get { return totalMergeBytes; }
}
/// <summary>
/// Returns the total number of documents that are included with this merge.
/// Note that this does not indicate the number of documents after the merge.
/// </summary>
public virtual int TotalNumDocs
{
get
{
int total = 0;
foreach (SegmentCommitInfo info in Segments)
{
total += info.Info.DocCount;
}
return total;
}
}
/// <summary>
/// Return <see cref="Store.MergeInfo"/> describing this merge. </summary>
public virtual MergeInfo MergeInfo
{
get
{
return new MergeInfo(TotalDocCount, EstimatedMergeBytes, isExternal, maxNumSegments);
}
}
}
/// <summary>
/// A <see cref="MergeSpecification"/> instance provides the information
/// necessary to perform multiple merges. It simply
/// contains a list of <see cref="OneMerge"/> instances.
/// </summary>
public class MergeSpecification
{
/// <summary>
/// The subset of segments to be included in the primitive merge.
/// </summary>
public IList<OneMerge> Merges { get; private set; }
/// <summary>
/// Sole constructor. Use
/// <see cref="Add(OneMerge)"/> to add merges.
/// </summary>
public MergeSpecification()
{
Merges = new List<OneMerge>();
}
/// <summary>
/// Adds the provided <see cref="OneMerge"/> to this
/// specification.
/// </summary>
public virtual void Add(OneMerge merge)
{
Merges.Add(merge);
}
/// <summary>
/// Returns a description of the merges in this
/// specification.
/// </summary>
public virtual string SegString(Directory dir)
{
StringBuilder b = new StringBuilder();
b.Append("MergeSpec:\n");
int count = Merges.Count;
for (int i = 0; i < count; i++)
{
b.Append(" ").Append(1 + i).Append(": ").Append(Merges[i].SegString(dir));
}
return b.ToString();
}
}
/// <summary>
/// Exception thrown if there are any problems while
/// executing a merge.
/// </summary>
// LUCENENET: It is no longer good practice to use binary serialization.
// See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568
#if FEATURE_SERIALIZABLE_EXCEPTIONS
[Serializable]
#endif
public class MergeException : Exception
{
private Directory dir;
/// <summary>
/// Create a <see cref="MergeException"/>. </summary>
public MergeException(string message, Directory dir)
: base(message)
{
this.dir = dir;
}
/// <summary>
/// Create a <see cref="MergeException"/>. </summary>
public MergeException(Exception exc, Directory dir)
: base(exc.ToString(), exc)
{
this.dir = dir;
}
#if FEATURE_SERIALIZABLE_EXCEPTIONS
// For testing purposes
public MergeException(string message)
: base(message)
{
}
/// <summary>
/// Initializes a new instance of this class with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected MergeException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
/// <summary>
/// Returns the <see cref="Store.Directory"/> of the index that hit
/// the exception.
/// </summary>
public virtual Directory Directory
{
get
{
return dir;
}
}
}
/// <summary>
/// Thrown when a merge was explicity aborted because
/// <see cref="IndexWriter.Dispose(bool)"/> was called with
/// <c>false</c>. Normally this exception is
/// privately caught and suppresed by <see cref="IndexWriter"/>.
/// </summary>
// LUCENENET: It is no longer good practice to use binary serialization.
// See: https://github.com/dotnet/corefx/issues/23584#issuecomment-325724568
#if FEATURE_SERIALIZABLE_EXCEPTIONS
[Serializable]
#endif
public class MergeAbortedException : System.IO.IOException
{
/// <summary>
/// Create a <see cref="MergeAbortedException"/>. </summary>
public MergeAbortedException()
: base("merge is aborted")
{
}
/// <summary>
/// Create a <see cref="MergeAbortedException"/> with a
/// specified message.
/// </summary>
public MergeAbortedException(string message)
: base(message)
{
}
#if FEATURE_SERIALIZABLE_EXCEPTIONS
/// <summary>
/// Initializes a new instance of this class with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
protected MergeAbortedException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif
}
/// <summary>
/// Default ratio for compound file system usage. Set to <c>1.0</c>, always use
/// compound file system.
/// </summary>
protected static readonly double DEFAULT_NO_CFS_RATIO = 1.0;
/// <summary>
/// Default max segment size in order to use compound file system. Set to <see cref="long.MaxValue"/>.
/// </summary>
protected static readonly long DEFAULT_MAX_CFS_SEGMENT_SIZE = long.MaxValue;
/// <summary>
/// <see cref="IndexWriter"/> that contains this instance. </summary>
protected SetOnce<IndexWriter> m_writer;
/// <summary>
/// If the size of the merge segment exceeds this ratio of
/// the total index size then it will remain in
/// non-compound format
/// </summary>
protected double m_noCFSRatio = DEFAULT_NO_CFS_RATIO;
/// <summary>
/// If the size of the merged segment exceeds
/// this value then it will not use compound file format.
/// </summary>
protected long m_maxCFSSegmentSize = DEFAULT_MAX_CFS_SEGMENT_SIZE;
public virtual object Clone()
{
MergePolicy clone;
clone = (MergePolicy)base.MemberwiseClone();
clone.m_writer = new SetOnce<IndexWriter>();
return clone;
}
/// <summary>
/// Creates a new merge policy instance. Note that if you intend to use it
/// without passing it to <see cref="IndexWriter"/>, you should call
/// <see cref="SetIndexWriter(IndexWriter)"/>.
/// </summary>
public MergePolicy()
: this(DEFAULT_NO_CFS_RATIO, DEFAULT_MAX_CFS_SEGMENT_SIZE)
{
}
/// <summary>
/// Creates a new merge policy instance with default settings for <see cref="m_noCFSRatio"/>
/// and <see cref="m_maxCFSSegmentSize"/>. This ctor should be used by subclasses using different
/// defaults than the <see cref="MergePolicy"/>
/// </summary>
protected MergePolicy(double defaultNoCFSRatio, long defaultMaxCFSSegmentSize)
{
m_writer = new SetOnce<IndexWriter>();
this.m_noCFSRatio = defaultNoCFSRatio;
this.m_maxCFSSegmentSize = defaultMaxCFSSegmentSize;
}
/// <summary>
/// Sets the <see cref="IndexWriter"/> to use by this merge policy. This method is
/// allowed to be called only once, and is usually set by <see cref="IndexWriter"/>. If it is
/// called more than once, <see cref="AlreadySetException"/> is thrown.
/// </summary>
/// <seealso cref="SetOnce{T}"/>
public virtual void SetIndexWriter(IndexWriter writer)
{
this.m_writer.Set(writer);
}
/// <summary>
/// Determine what set of merge operations are now necessary on the index.
/// <see cref="IndexWriter"/> calls this whenever there is a change to the segments.
/// This call is always synchronized on the <see cref="IndexWriter"/> instance so
/// only one thread at a time will call this method. </summary>
/// <param name="mergeTrigger"> the event that triggered the merge </param>
/// <param name="segmentInfos">
/// the total set of segments in the index </param>
public abstract MergeSpecification FindMerges(MergeTrigger mergeTrigger, SegmentInfos segmentInfos);
/// <summary>
/// Determine what set of merge operations is necessary in
/// order to merge to <= the specified segment count. <see cref="IndexWriter"/> calls this when its
/// <see cref="IndexWriter.ForceMerge(int, bool)"/> method is called. This call is always
/// synchronized on the <see cref="IndexWriter"/> instance so only one thread at a
/// time will call this method.
/// </summary>
/// <param name="segmentInfos">
/// The total set of segments in the index </param>
/// <param name="maxSegmentCount">
/// Requested maximum number of segments in the index (currently this
/// is always 1) </param>
/// <param name="segmentsToMerge">
/// Contains the specific <see cref="SegmentInfo"/> instances that must be merged
/// away. This may be a subset of all
/// SegmentInfos. If the value is <c>true</c> for a
/// given <see cref="SegmentInfo"/>, that means this segment was
/// an original segment present in the
/// to-be-merged index; else, it was a segment
/// produced by a cascaded merge. </param>
public abstract MergeSpecification FindForcedMerges(SegmentInfos segmentInfos, int maxSegmentCount, IDictionary<SegmentCommitInfo, bool?> segmentsToMerge);
/// <summary>
/// Determine what set of merge operations is necessary in order to expunge all
/// deletes from the index.
/// </summary>
/// <param name="segmentInfos">
/// the total set of segments in the index </param>
public abstract MergeSpecification FindForcedDeletesMerges(SegmentInfos segmentInfos);
/// <summary>
/// Release all resources for the policy.
/// </summary>
// LUCENENET specific - implementing proper dispose pattern
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Release all resources for the policy.
/// </summary>
protected abstract void Dispose(bool disposing);
/// <summary>
/// Returns <c>true</c> if a new segment (regardless of its origin) should use the
/// compound file format. The default implementation returns <c>true</c>
/// iff the size of the given mergedInfo is less or equal to
/// <see cref="MaxCFSSegmentSizeMB"/> and the size is less or equal to the
/// TotalIndexSize * <see cref="NoCFSRatio"/> otherwise <code>false</code>.
/// </summary>
public virtual bool UseCompoundFile(SegmentInfos infos, SegmentCommitInfo mergedInfo)
{
if (NoCFSRatio == 0.0)
{
return false;
}
long mergedInfoSize = Size(mergedInfo);
if (mergedInfoSize > m_maxCFSSegmentSize)
{
return false;
}
if (NoCFSRatio >= 1.0)
{
return true;
}
long totalSize = 0;
foreach (SegmentCommitInfo info in infos.Segments)
{
totalSize += Size(info);
}
return mergedInfoSize <= NoCFSRatio * totalSize;
}
/// <summary>
/// Return the byte size of the provided
/// <see cref="SegmentCommitInfo"/>, pro-rated by percentage of
/// non-deleted documents is set.
/// </summary>
protected virtual long Size(SegmentCommitInfo info)
{
long byteSize = info.GetSizeInBytes();
int delCount = m_writer.Get().NumDeletedDocs(info);
double delRatio = (info.Info.DocCount <= 0 ? 0.0f : ((float)delCount / (float)info.Info.DocCount));
Debug.Assert(delRatio <= 1.0);
return (info.Info.DocCount <= 0 ? byteSize : (long)(byteSize * (1.0 - delRatio)));
}
/// <summary>
/// Returns <c>true</c> if this single info is already fully merged (has no
/// pending deletes, is in the same dir as the
/// writer, and matches the current compound file setting
/// </summary>
protected bool IsMerged(SegmentInfos infos, SegmentCommitInfo info)
{
IndexWriter w = m_writer.Get();
Debug.Assert(w != null);
bool hasDeletions = w.NumDeletedDocs(info) > 0;
return !hasDeletions
#pragma warning disable 612, 618
&& !info.Info.HasSeparateNorms
#pragma warning restore 612, 618
&& info.Info.Dir == w.Directory
&& UseCompoundFile(infos, info) == info.Info.UseCompoundFile;
}
/// <summary>
/// Gets or Sets current <see cref="m_noCFSRatio"/>.
/// <para/>
/// If a merged segment will be more than this percentage
/// of the total size of the index, leave the segment as
/// non-compound file even if compound file is enabled.
/// Set to 1.0 to always use CFS regardless of merge
/// size.
/// </summary>
public double NoCFSRatio
{
get
{
return m_noCFSRatio;
}
set
{
if (value < 0.0 || value > 1.0)
{
throw new System.ArgumentException("noCFSRatio must be 0.0 to 1.0 inclusive; got " + value);
}
this.m_noCFSRatio = value;
}
}
/// <summary>
/// Gets or Sets the largest size allowed for a compound file segment.
/// <para/>
/// If a merged segment will be more than this value,
/// leave the segment as
/// non-compound file even if compound file is enabled.
/// Set this to <see cref="double.PositiveInfinity"/> (default) and <see cref="NoCFSRatio"/> to 1.0
/// to always use CFS regardless of merge size.
/// </summary>
public double MaxCFSSegmentSizeMB
{
get
{
return m_maxCFSSegmentSize / 1024 / 1024.0;
}
set
{
if (value < 0.0)
{
throw new System.ArgumentException("maxCFSSegmentSizeMB must be >=0 (got " + value + ")");
}
value *= 1024 * 1024;
this.m_maxCFSSegmentSize = (value > long.MaxValue) ? long.MaxValue : (long)value;
}
}
}
}