Lucene.Net.Misc.Index.Sorter: Reviewed EarlyTerminatingSortingCollector, Sorter, SortingAtomicReader, and SortingMergePolicy line-by-line and fixed some formatting issues, naming issues, and documentation issues
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/3b122d0c Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/3b122d0c Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/3b122d0c Branch: refs/heads/api-work Commit: 3b122d0c42942d4322e11adbcbf9bd81c2afe3be Parents: f6c7515 Author: Shad Storhaug <[email protected]> Authored: Fri Mar 24 04:28:07 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Fri Mar 24 06:28:23 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net.Misc/Index/Sorter/Sorter.cs | 35 ++-- .../Index/Sorter/SortingAtomicReader.cs | 168 +++++++++---------- .../Index/Sorter/SortingMergePolicy.cs | 14 +- 3 files changed, 102 insertions(+), 115 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/3b122d0c/src/Lucene.Net.Misc/Index/Sorter/Sorter.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Misc/Index/Sorter/Sorter.cs b/src/Lucene.Net.Misc/Index/Sorter/Sorter.cs index 841bead..3af8d32 100644 --- a/src/Lucene.Net.Misc/Index/Sorter/Sorter.cs +++ b/src/Lucene.Net.Misc/Index/Sorter/Sorter.cs @@ -32,7 +32,7 @@ namespace Lucene.Net.Index.Sorter /// </summary> internal sealed class Sorter { - internal readonly Sort sort_Renamed; + internal readonly Sort sort; /// <summary> /// Creates a new Sorter to sort the index with <paramref name="sort"/>. @@ -43,20 +43,19 @@ namespace Lucene.Net.Index.Sorter { throw new System.ArgumentException("Cannot sort an index with a Sort that refers to the relevance score"); } - this.sort_Renamed = sort; + this.sort = sort; } /// <summary> /// A permutation of doc IDs. For every document ID between <c>0</c> and - /// <see cref="IndexReader.MaxDoc"/>, <code>OldToNew(NewToOld(docID))</code> must - /// return <code>docID</code>. + /// <see cref="IndexReader.MaxDoc"/>, <c>OldToNew(NewToOld(docID))</c> must + /// return <c>docID</c>. /// </summary> internal abstract class DocMap { - /// <summary> /// Given a doc ID from the original index, return its ordinal in the - /// sorted index. + /// sorted index. /// </summary> public abstract int OldToNew(int docID); @@ -67,7 +66,7 @@ namespace Lucene.Net.Index.Sorter /// <summary> /// Return the number of documents in this map. This must be equal to the - /// <see cref="AtomicReader.LiveDocs">number of documents</see> of the + /// <see cref="IndexReader.MaxDoc"/> number of documents of the /// <see cref="AtomicReader"/> which is sorted. /// </summary> public abstract int Count { get; } @@ -98,23 +97,21 @@ namespace Lucene.Net.Index.Sorter /// </summary> internal abstract class DocComparer : IComparer<int> { - /// <summary> - /// Compare docID1 against docID2. The contract for the return value is the + /// Compare <paramref name="docID1"/> against <paramref name="docID2"/>. The contract for the return value is the /// same as <see cref="IComparer{T}.Compare(T, T)"/>. /// </summary> public abstract int Compare(int docID1, int docID2); - } private sealed class DocValueSorter : TimSorter { + private readonly int[] docs; + private readonly Sorter.DocComparer comparer; + private readonly int[] tmp; - internal readonly int[] docs; - internal readonly Sorter.DocComparer comparer; - internal readonly int[] tmp; - - internal DocValueSorter(int[] docs, Sorter.DocComparer comparer) : base(docs.Length / 64) + internal DocValueSorter(int[] docs, Sorter.DocComparer comparer) + : base(docs.Length / 64) { this.docs = docs; this.comparer = comparer; @@ -257,9 +254,8 @@ namespace Lucene.Net.Index.Sorter /// </summary> internal DocMap Sort(AtomicReader reader) { - SortField[] fields = sort_Renamed.GetSort(); + SortField[] fields = sort.GetSort(); int[] reverseMul = new int[fields.Length]; - FieldComparer[] comparers = new FieldComparer[fields.Length]; for (int i = 0; i < fields.Length; i++) @@ -324,7 +320,7 @@ namespace Lucene.Net.Index.Sorter { get { - return sort_Renamed.ToString(); + return sort.ToString(); } } @@ -337,7 +333,8 @@ namespace Lucene.Net.Index.Sorter private class ScorerAnonymousInnerClassHelper : Scorer { - public ScorerAnonymousInnerClassHelper() : base(null) + public ScorerAnonymousInnerClassHelper() + : base(null) { } http://git-wip-us.apache.org/repos/asf/lucenenet/blob/3b122d0c/src/Lucene.Net.Misc/Index/Sorter/SortingAtomicReader.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Misc/Index/Sorter/SortingAtomicReader.cs b/src/Lucene.Net.Misc/Index/Sorter/SortingAtomicReader.cs index e874b88..f14f71d 100644 --- a/src/Lucene.Net.Misc/Index/Sorter/SortingAtomicReader.cs +++ b/src/Lucene.Net.Misc/Index/Sorter/SortingAtomicReader.cs @@ -1,10 +1,10 @@ -using System; -using System.Diagnostics; -using Lucene.Net.Search; +using Lucene.Net.Search; using Lucene.Net.Store; +using Lucene.Net.Support; using Lucene.Net.Util; using Lucene.Net.Util.Automaton; -using Lucene.Net.Support; +using System; +using System.Diagnostics; namespace Lucene.Net.Index.Sorter { @@ -43,15 +43,13 @@ namespace Lucene.Net.Index.Sorter /// </summary> public class SortingAtomicReader : FilterAtomicReader { - private class SortingFields : FilterFields { + private readonly Sorter.DocMap docMap; + private readonly FieldInfos infos; - internal readonly Sorter.DocMap docMap; - internal readonly FieldInfos infos; - - public SortingFields(Fields @in, FieldInfos infos, Sorter.DocMap docMap) - : base(@in) + public SortingFields(Fields input, FieldInfos infos, Sorter.DocMap docMap) + : base(input) { this.docMap = docMap; this.infos = infos; @@ -73,11 +71,11 @@ namespace Lucene.Net.Index.Sorter private class SortingTerms : FilterTerms { + private readonly Sorter.DocMap docMap; + private readonly IndexOptions indexOptions; - internal readonly Sorter.DocMap docMap; - internal readonly IndexOptions indexOptions; - - public SortingTerms(Terms input, IndexOptions indexOptions, Sorter.DocMap docMap) : base(input) + public SortingTerms(Terms input, IndexOptions indexOptions, Sorter.DocMap docMap) + : base(input) { this.docMap = docMap; this.indexOptions = indexOptions; @@ -96,12 +94,11 @@ namespace Lucene.Net.Index.Sorter private class SortingTermsEnum : FilterTermsEnum { + private readonly Sorter.DocMap docMap; // pkg-protected to avoid synthetic accessor methods + private readonly IndexOptions indexOptions; - internal readonly Sorter.DocMap docMap; // pkg-protected to avoid synthetic accessor methods - internal readonly IndexOptions indexOptions; - - public SortingTermsEnum(TermsEnum @in, Sorter.DocMap docMap, IndexOptions indexOptions) - : base(@in) + public SortingTermsEnum(TermsEnum input, Sorter.DocMap docMap, IndexOptions indexOptions) + : base(input) { this.docMap = docMap; this.indexOptions = indexOptions; @@ -196,13 +193,12 @@ namespace Lucene.Net.Index.Sorter private class SortingBinaryDocValues : BinaryDocValues { + private readonly BinaryDocValues @in; + private readonly Sorter.DocMap docMap; - internal readonly BinaryDocValues @in; - internal readonly Sorter.DocMap docMap; - - internal SortingBinaryDocValues(BinaryDocValues @in, Sorter.DocMap docMap) + internal SortingBinaryDocValues(BinaryDocValues input, Sorter.DocMap docMap) { - this.@in = @in; + this.@in = input; this.docMap = docMap; } @@ -214,13 +210,12 @@ namespace Lucene.Net.Index.Sorter private class SortingNumericDocValues : NumericDocValues { + private readonly NumericDocValues @in; + private readonly Sorter.DocMap docMap; - internal readonly NumericDocValues @in; - internal readonly Sorter.DocMap docMap; - - public SortingNumericDocValues(NumericDocValues @in, Sorter.DocMap docMap) + public SortingNumericDocValues(NumericDocValues input, Sorter.DocMap docMap) { - this.@in = @in; + this.@in = input; this.docMap = docMap; } @@ -232,13 +227,12 @@ namespace Lucene.Net.Index.Sorter private class SortingBits : IBits { + private readonly IBits @in; + private readonly Sorter.DocMap docMap; - internal readonly IBits @in; - internal readonly Sorter.DocMap docMap; - - public SortingBits(IBits @in, Sorter.DocMap docMap) + public SortingBits(IBits input, Sorter.DocMap docMap) { - this.@in = @in; + this.@in = input; this.docMap = docMap; } @@ -255,13 +249,12 @@ namespace Lucene.Net.Index.Sorter private class SortingSortedDocValues : SortedDocValues { + private readonly SortedDocValues @in; + private readonly Sorter.DocMap docMap; - internal readonly SortedDocValues @in; - internal readonly Sorter.DocMap docMap; - - internal SortingSortedDocValues(SortedDocValues @in, Sorter.DocMap docMap) + internal SortingSortedDocValues(SortedDocValues input, Sorter.DocMap docMap) { - this.@in = @in; + this.@in = input; this.docMap = docMap; } @@ -296,13 +289,12 @@ namespace Lucene.Net.Index.Sorter private class SortingSortedSetDocValues : SortedSetDocValues { + private readonly SortedSetDocValues @in; + private readonly Sorter.DocMap docMap; - internal readonly SortedSetDocValues @in; - internal readonly Sorter.DocMap docMap; - - internal SortingSortedSetDocValues(SortedSetDocValues @in, Sorter.DocMap docMap) + internal SortingSortedSetDocValues(SortedSetDocValues input, Sorter.DocMap docMap) { - this.@in = @in; + this.@in = input; this.docMap = docMap; } @@ -337,16 +329,15 @@ namespace Lucene.Net.Index.Sorter internal class SortingDocsEnum : FilterDocsEnum { - - internal sealed class DocFreqSorter : TimSorter + private sealed class DocFreqSorter : TimSorter { + private int[] docs; + private int[] freqs; + private readonly int[] tmpDocs; + private int[] tmpFreqs; - internal int[] docs; - internal int[] freqs; - internal readonly int[] tmpDocs; - internal int[] tmpFreqs; - - public DocFreqSorter(int maxDoc) : base(maxDoc / 64) + public DocFreqSorter(int maxDoc) + : base(maxDoc / 64) { this.tmpDocs = new int[maxDoc / 64]; } @@ -413,16 +404,16 @@ namespace Lucene.Net.Index.Sorter } } - internal readonly int maxDoc; - internal readonly DocFreqSorter sorter; - internal int[] docs; - internal int[] freqs; - internal int docIt = -1; - internal readonly int upto; - internal readonly bool withFreqs; + private readonly int maxDoc; + private readonly DocFreqSorter sorter; + private int[] docs; + private int[] freqs; + private int docIt = -1; + private readonly int upto; + private readonly bool withFreqs; - internal SortingDocsEnum(int maxDoc, SortingDocsEnum reuse, DocsEnum @in, bool withFreqs, Sorter.DocMap docMap) - : base(@in) + internal SortingDocsEnum(int maxDoc, SortingDocsEnum reuse, DocsEnum input, bool withFreqs, Sorter.DocMap docMap) + : base(input) { this.maxDoc = maxDoc; this.withFreqs = withFreqs; @@ -453,7 +444,7 @@ namespace Lucene.Net.Index.Sorter { freqs = new int[docs.Length]; } - while ((doc = @in.NextDoc()) != DocIdSetIterator.NO_MORE_DOCS) + while ((doc = input.NextDoc()) != DocIdSetIterator.NO_MORE_DOCS) { if (i >= docs.Length) { @@ -461,14 +452,14 @@ namespace Lucene.Net.Index.Sorter freqs = ArrayUtil.Grow(freqs, freqs.Length + 1); } docs[i] = docMap.OldToNew(doc); - freqs[i] = @in.Freq; + freqs[i] = input.Freq; ++i; } } else { freqs = null; - while ((doc = @in.NextDoc()) != DocIdSetIterator.NO_MORE_DOCS) + while ((doc = input.NextDoc()) != DocIdSetIterator.NO_MORE_DOCS) { if (i >= docs.Length) { @@ -533,7 +524,6 @@ namespace Lucene.Net.Index.Sorter internal class SortingDocsAndPositionsEnum : FilterDocsAndPositionsEnum { - /// <summary> /// A <see cref="TimSorter"/> which sorts two parallel arrays of doc IDs and /// offsets in one go. Everytime a doc ID is 'swapped', its correponding offset @@ -541,13 +531,13 @@ namespace Lucene.Net.Index.Sorter /// </summary> internal sealed class DocOffsetSorter : TimSorter { + private int[] docs; + private long[] offsets; + private readonly int[] tmpDocs; + private readonly long[] tmpOffsets; - internal int[] docs; - internal long[] offsets; - internal readonly int[] tmpDocs; - internal readonly long[] tmpOffsets; - - public DocOffsetSorter(int maxDoc) : base(maxDoc / 64) + public DocOffsetSorter(int maxDoc) + : base(maxDoc / 64) { this.tmpDocs = new int[maxDoc / 64]; this.tmpOffsets = new long[maxDoc / 64]; @@ -599,23 +589,23 @@ namespace Lucene.Net.Index.Sorter } } - internal readonly int maxDoc; - internal readonly DocOffsetSorter sorter; - internal int[] docs; - internal long[] offsets; - internal readonly int upto; + private readonly int maxDoc; + private readonly DocOffsetSorter sorter; + private int[] docs; + private long[] offsets; + private readonly int upto; - internal readonly IndexInput postingInput; - internal readonly bool storeOffsets; + private readonly IndexInput postingInput; + private readonly bool storeOffsets; - internal int docIt = -1; - internal int pos; - internal int startOffset = -1; - internal int endOffset = -1; - internal readonly BytesRef payload; - internal int currFreq; + private int docIt = -1; + private int pos; + private int startOffset = -1; + private int endOffset = -1; + private readonly BytesRef payload; + private int currFreq; - internal readonly RAMFile file; + private readonly RAMFile file; internal SortingDocsAndPositionsEnum(int maxDoc, SortingDocsAndPositionsEnum reuse, DocsAndPositionsEnum @in, Sorter.DocMap docMap, bool storeOffsets) : base(@in) @@ -679,7 +669,7 @@ namespace Lucene.Net.Index.Sorter return docs == ((SortingDocsAndPositionsEnum)other).docs; } - internal virtual void AddPositions(DocsAndPositionsEnum @in, IndexOutput @out) + private void AddPositions(DocsAndPositionsEnum @in, IndexOutput @out) { int freq = @in.Freq; @out.WriteVInt32(freq); @@ -801,7 +791,7 @@ namespace Lucene.Net.Index.Sorter /// </summary> public static AtomicReader Wrap(AtomicReader reader, Sort sort) { - return Wrap(reader, (new Sorter(sort)).Sort(reader)); + return Wrap(reader, new Sorter(sort).Sort(reader)); } /// <summary> @@ -816,13 +806,13 @@ namespace Lucene.Net.Index.Sorter } if (reader.MaxDoc != docMap.Count) { - throw new System.ArgumentException("reader.maxDoc() should be equal to docMap.size(), got" + reader.MaxDoc + " != " + docMap.Count); + throw new System.ArgumentException("reader.MaxDoc should be equal to docMap.Count, got" + reader.MaxDoc + " != " + docMap.Count); } Debug.Assert(Sorter.IsConsistent(docMap)); return new SortingAtomicReader(reader, docMap); } - internal readonly Sorter.DocMap docMap; // pkg-protected to avoid synthetic accessor methods + private readonly Sorter.DocMap docMap; // pkg-protected to avoid synthetic accessor methods private SortingAtomicReader(AtomicReader @in, Sorter.DocMap docMap) : base(@in) { http://git-wip-us.apache.org/repos/asf/lucenenet/blob/3b122d0c/src/Lucene.Net.Misc/Index/Sorter/SortingMergePolicy.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Misc/Index/Sorter/SortingMergePolicy.cs b/src/Lucene.Net.Misc/Index/Sorter/SortingMergePolicy.cs index 770c00b..2853157 100644 --- a/src/Lucene.Net.Misc/Index/Sorter/SortingMergePolicy.cs +++ b/src/Lucene.Net.Misc/Index/Sorter/SortingMergePolicy.cs @@ -1,6 +1,5 @@ using Lucene.Net.Search; using Lucene.Net.Store; -using Lucene.Net.Support; using Lucene.Net.Util; using Lucene.Net.Util.Packed; using System; @@ -51,7 +50,7 @@ namespace Lucene.Net.Index.Sorter /// Put in the <see cref="SegmentInfo.Diagnostics">diagnostics</see> to denote that /// this segment is sorted. /// </summary> - public const string SORTER_ID_PROP = "sorter"; + public static readonly string SORTER_ID_PROP = "sorter"; internal class SortingOneMerge : OneMerge { @@ -132,7 +131,7 @@ namespace Lucene.Net.Index.Sorter { if (unsortedReaders == null) { - throw new InvalidOperationException("Invalid state"); + throw new InvalidOperationException(); } if (docMap == null) { @@ -164,7 +163,6 @@ namespace Lucene.Net.Index.Sorter return mergeState.DocMaps[0].Get(newWithDeletes); } } - } internal class SortingMergeSpecification : MergeSpecification @@ -184,7 +182,7 @@ namespace Lucene.Net.Index.Sorter public override string SegString(Directory dir) { - return "SortingMergeSpec(" + base.SegString(dir) + ", sorter=" + outerInstance.sorter + ")"; + return "SortingMergeSpecification(" + base.SegString(dir) + ", sorter=" + outerInstance.sorter + ")"; } } @@ -198,8 +196,10 @@ namespace Lucene.Net.Index.Sorter { SegmentReader segReader = (SegmentReader)reader; IDictionary<string, string> diagnostics = segReader.SegmentInfo.Info.Diagnostics; - var diagnosticsSort = diagnostics.ContainsKey(SORTER_ID_PROP) ? diagnostics[SORTER_ID_PROP] : null; - if (diagnostics != null && sort.ToString().Equals(diagnosticsSort)) + string diagnosticsSort; + if (diagnostics != null + && diagnostics.TryGetValue(SORTER_ID_PROP, out diagnosticsSort) + && sort.ToString().Equals(diagnosticsSort)) { return true; }
