Lucene.Net.TestFramework: Renamed Codecs\asserting\ to Codecs\Asserting\
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/77e95ccc Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/77e95ccc Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/77e95ccc Branch: refs/heads/api-work Commit: 77e95ccc235a83696494c674f4945c42d80e9d61 Parents: 9682239 Author: Shad Storhaug <[email protected]> Authored: Sun Feb 26 02:44:43 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Mon Feb 27 06:17:55 2017 +0700 ---------------------------------------------------------------------- .../Codecs/Asserting/AssertingCodec.cs | 64 ++++ .../Asserting/AssertingDocValuesFormat.cs | 346 +++++++++++++++++++ .../Codecs/Asserting/AssertingNormsFormat.cs | 51 +++ .../Codecs/Asserting/AssertingPostingsFormat.cs | 318 +++++++++++++++++ .../Asserting/AssertingStoredFieldsFormat.cs | 154 +++++++++ .../Asserting/AssertingTermVectorsFormat.cs | 208 +++++++++++ .../Codecs/asserting/AssertingCodec.cs | 64 ---- .../asserting/AssertingDocValuesFormat.cs | 346 ------------------- .../Codecs/asserting/AssertingNormsFormat.cs | 51 --- .../Codecs/asserting/AssertingPostingsFormat.cs | 318 ----------------- .../asserting/AssertingStoredFieldsFormat.cs | 154 --------- .../asserting/AssertingTermVectorsFormat.cs | 208 ----------- .../Index/RandomCodec.cs | 4 +- .../Lucene.Net.TestFramework.csproj | 12 +- .../Index/TestBinaryDocValuesUpdates.cs | 2 +- .../Index/TestNumericDocValuesUpdates.cs | 2 +- 16 files changed, 1151 insertions(+), 1151 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingCodec.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingCodec.cs b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingCodec.cs new file mode 100644 index 0000000..5bf1179 --- /dev/null +++ b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingCodec.cs @@ -0,0 +1,64 @@ +namespace Lucene.Net.Codecs.Asserting +{ + /* + * 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 Lucene46Codec = Lucene.Net.Codecs.Lucene46.Lucene46Codec; + + /// <summary> + /// Acts like <seealso cref="Lucene46Codec"/> but with additional asserts. + /// </summary> + [CodecName("Asserting")] + public sealed class AssertingCodec : FilterCodec + { + private readonly PostingsFormat Postings = new AssertingPostingsFormat(); + private readonly TermVectorsFormat Vectors = new AssertingTermVectorsFormat(); + private readonly StoredFieldsFormat StoredFields = new AssertingStoredFieldsFormat(); + private readonly DocValuesFormat DocValues = new AssertingDocValuesFormat(); + private readonly NormsFormat Norms = new AssertingNormsFormat(); + + public AssertingCodec() + : base(new Lucene46Codec()) + { + } + + public override PostingsFormat PostingsFormat + { + get { return Postings; } + } + + public override TermVectorsFormat TermVectorsFormat + { + get { return Vectors; } + } + + public override StoredFieldsFormat StoredFieldsFormat + { + get { return StoredFields; } + } + + public override DocValuesFormat DocValuesFormat + { + get { return DocValues; } + } + + public override NormsFormat NormsFormat + { + get { return Norms; } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingDocValuesFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingDocValuesFormat.cs b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingDocValuesFormat.cs new file mode 100644 index 0000000..0d91fb0 --- /dev/null +++ b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingDocValuesFormat.cs @@ -0,0 +1,346 @@ +using System.Collections.Generic; +using System.Diagnostics; + +namespace Lucene.Net.Codecs.Asserting +{ + using System; + using AssertingAtomicReader = Lucene.Net.Index.AssertingAtomicReader; + using BinaryDocValues = Lucene.Net.Index.BinaryDocValues; + using IBits = Lucene.Net.Util.IBits; + using BytesRef = Lucene.Net.Util.BytesRef; + using DocValuesType = Lucene.Net.Index.DocValuesType; + using FieldInfo = Lucene.Net.Index.FieldInfo; + using FixedBitSet = Lucene.Net.Util.FixedBitSet; + using Int64BitSet = Lucene.Net.Util.Int64BitSet; + + /* + * 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 Lucene45DocValuesFormat = Lucene.Net.Codecs.Lucene45.Lucene45DocValuesFormat; + using NumericDocValues = Lucene.Net.Index.NumericDocValues; + using SegmentReadState = Lucene.Net.Index.SegmentReadState; + using SegmentWriteState = Lucene.Net.Index.SegmentWriteState; + using SortedDocValues = Lucene.Net.Index.SortedDocValues; + using SortedSetDocValues = Lucene.Net.Index.SortedSetDocValues; + + /// <summary> + /// Just like <seealso cref="Lucene45DocValuesFormat"/> but with additional asserts. + /// </summary> + [DocValuesFormatName("Asserting")] + public class AssertingDocValuesFormat : DocValuesFormat + { + private readonly DocValuesFormat @in = new Lucene45DocValuesFormat(); + + public AssertingDocValuesFormat() + : base() + { + } + + public override DocValuesConsumer FieldsConsumer(SegmentWriteState state) + { + DocValuesConsumer consumer = @in.FieldsConsumer(state); + Debug.Assert(consumer != null); + return new AssertingDocValuesConsumer(consumer, state.SegmentInfo.DocCount); + } + + public override DocValuesProducer FieldsProducer(SegmentReadState state) + { + Debug.Assert(state.FieldInfos.HasDocValues); + DocValuesProducer producer = @in.FieldsProducer(state); + Debug.Assert(producer != null); + return new AssertingDocValuesProducer(producer, state.SegmentInfo.DocCount); + } + + internal class AssertingDocValuesConsumer : DocValuesConsumer + { + internal readonly DocValuesConsumer @in; + internal readonly int MaxDoc; + + internal AssertingDocValuesConsumer(DocValuesConsumer @in, int maxDoc) + { + this.@in = @in; + this.MaxDoc = maxDoc; + } + + public override void AddNumericField(FieldInfo field, IEnumerable<long?> values) + { + int count = 0; + foreach (var v in values) + { + count++; + } + Debug.Assert(count == MaxDoc); + CheckIterator(values.GetEnumerator(), MaxDoc, true); + @in.AddNumericField(field, values); + } + + public override void AddBinaryField(FieldInfo field, IEnumerable<BytesRef> values) + { + int count = 0; + foreach (BytesRef b in values) + { + Debug.Assert(b == null || b.IsValid()); + count++; + } + Debug.Assert(count == MaxDoc); + CheckIterator(values.GetEnumerator(), MaxDoc, true); + @in.AddBinaryField(field, values); + } + + public override void AddSortedField(FieldInfo field, IEnumerable<BytesRef> values, IEnumerable<long?> docToOrd) + { + int valueCount = 0; + BytesRef lastValue = null; + foreach (BytesRef b in values) + { + Debug.Assert(b != null); + Debug.Assert(b.IsValid()); + if (valueCount > 0) + { + Debug.Assert(b.CompareTo(lastValue) > 0); + } + lastValue = BytesRef.DeepCopyOf(b); + valueCount++; + } + Debug.Assert(valueCount <= MaxDoc); + + FixedBitSet seenOrds = new FixedBitSet(valueCount); + + int count = 0; + foreach (long? v in docToOrd) + { + Debug.Assert(v != null); + int ord = (int)v.Value; + Debug.Assert(ord >= -1 && ord < valueCount); + if (ord >= 0) + { + seenOrds.Set(ord); + } + count++; + } + + Debug.Assert(count == MaxDoc); + Debug.Assert(seenOrds.Cardinality() == valueCount); + CheckIterator(values.GetEnumerator(), valueCount, false); + CheckIterator(docToOrd.GetEnumerator(), MaxDoc, false); + @in.AddSortedField(field, values, docToOrd); + } + + public override void AddSortedSetField(FieldInfo field, IEnumerable<BytesRef> values, IEnumerable<long?> docToOrdCount, IEnumerable<long?> ords) + { + long valueCount = 0; + BytesRef lastValue = null; + foreach (BytesRef b in values) + { + Debug.Assert(b != null); + Debug.Assert(b.IsValid()); + if (valueCount > 0) + { + Debug.Assert(b.CompareTo(lastValue) > 0); + } + lastValue = BytesRef.DeepCopyOf(b); + valueCount++; + } + + int docCount = 0; + long ordCount = 0; + Int64BitSet seenOrds = new Int64BitSet(valueCount); + IEnumerator<long?> ordIterator = ords.GetEnumerator(); + foreach (long? v in docToOrdCount) + { + Debug.Assert(v != null); + int count = (int)v.Value; + Debug.Assert(count >= 0); + docCount++; + ordCount += count; + + long lastOrd = -1; + for (int i = 0; i < count; i++) + { + ordIterator.MoveNext(); + long? o = ordIterator.Current; + Debug.Assert(o != null); + long ord = o.Value; + Debug.Assert(ord >= 0 && ord < valueCount); + Debug.Assert(ord > lastOrd, "ord=" + ord + ",lastOrd=" + lastOrd); + seenOrds.Set(ord); + lastOrd = ord; + } + } + Debug.Assert(ordIterator.MoveNext() == false); + + Debug.Assert(docCount == MaxDoc); + Debug.Assert(seenOrds.Cardinality() == valueCount); + CheckIterator(values.GetEnumerator(), valueCount, false); + CheckIterator(docToOrdCount.GetEnumerator(), MaxDoc, false); + CheckIterator(ords.GetEnumerator(), ordCount, false); + @in.AddSortedSetField(field, values, docToOrdCount, ords); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + @in.Dispose(); + } + } + + internal class AssertingNormsConsumer : DocValuesConsumer + { + internal readonly DocValuesConsumer @in; + internal readonly int MaxDoc; + + internal AssertingNormsConsumer(DocValuesConsumer @in, int maxDoc) + { + this.@in = @in; + this.MaxDoc = maxDoc; + } + + public override void AddNumericField(FieldInfo field, IEnumerable<long?> values) + { + int count = 0; + foreach (long? v in values) + { + Debug.Assert(v != null); + count++; + } + Debug.Assert(count == MaxDoc); + CheckIterator(values.GetEnumerator(), MaxDoc, false); + @in.AddNumericField(field, values); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + @in.Dispose(); + } + + public override void AddBinaryField(FieldInfo field, IEnumerable<BytesRef> values) + { + throw new InvalidOperationException(); + } + + public override void AddSortedField(FieldInfo field, IEnumerable<BytesRef> values, IEnumerable<long?> docToOrd) + { + throw new InvalidOperationException(); + } + + public override void AddSortedSetField(FieldInfo field, IEnumerable<BytesRef> values, IEnumerable<long?> docToOrdCount, IEnumerable<long?> ords) + { + throw new InvalidOperationException(); + } + } + + private static void CheckIterator<T>(IEnumerator<T> iterator, long expectedSize, bool allowNull) + { + for (long i = 0; i < expectedSize; i++) + { + bool hasNext = iterator.MoveNext(); + Debug.Assert(hasNext); + T v = iterator.Current; + Debug.Assert(allowNull || v != null); + try + { + iterator.Reset(); + throw new InvalidOperationException("broken iterator (supports remove): " + iterator); + } + catch (System.NotSupportedException) + { + // ok + } + } + Debug.Assert(!iterator.MoveNext()); + /*try + { + //iterator.next(); + throw new InvalidOperationException("broken iterator (allows next() when hasNext==false) " + iterator); + } + catch (Exception) + { + // ok + }*/ + } + + internal class AssertingDocValuesProducer : DocValuesProducer + { + internal readonly DocValuesProducer @in; + internal readonly int MaxDoc; + + internal AssertingDocValuesProducer(DocValuesProducer @in, int maxDoc) + { + this.@in = @in; + this.MaxDoc = maxDoc; + } + + public override NumericDocValues GetNumeric(FieldInfo field) + { + Debug.Assert(field.DocValuesType == DocValuesType.NUMERIC || field.NormType == DocValuesType.NUMERIC); + NumericDocValues values = @in.GetNumeric(field); + Debug.Assert(values != null); + return new AssertingAtomicReader.AssertingNumericDocValues(values, MaxDoc); + } + + public override BinaryDocValues GetBinary(FieldInfo field) + { + Debug.Assert(field.DocValuesType == DocValuesType.BINARY); + BinaryDocValues values = @in.GetBinary(field); + Debug.Assert(values != null); + return new AssertingAtomicReader.AssertingBinaryDocValues(values, MaxDoc); + } + + public override SortedDocValues GetSorted(FieldInfo field) + { + Debug.Assert(field.DocValuesType == DocValuesType.SORTED); + SortedDocValues values = @in.GetSorted(field); + Debug.Assert(values != null); + return new AssertingAtomicReader.AssertingSortedDocValues(values, MaxDoc); + } + + public override SortedSetDocValues GetSortedSet(FieldInfo field) + { + Debug.Assert(field.DocValuesType == DocValuesType.SORTED_SET); + SortedSetDocValues values = @in.GetSortedSet(field); + Debug.Assert(values != null); + return new AssertingAtomicReader.AssertingSortedSetDocValues(values, MaxDoc); + } + + public override IBits GetDocsWithField(FieldInfo field) + { + Debug.Assert(field.DocValuesType != null); + IBits bits = @in.GetDocsWithField(field); + Debug.Assert(bits != null); + Debug.Assert(bits.Length == MaxDoc); + return new AssertingAtomicReader.AssertingBits(bits); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + @in.Dispose(); + } + + public override long RamBytesUsed() + { + return @in.RamBytesUsed(); + } + + public override void CheckIntegrity() + { + @in.CheckIntegrity(); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingNormsFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingNormsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingNormsFormat.cs new file mode 100644 index 0000000..4bdc7a3 --- /dev/null +++ b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingNormsFormat.cs @@ -0,0 +1,51 @@ +using System.Diagnostics; + +namespace Lucene.Net.Codecs.Asserting +{ + using AssertingDocValuesProducer = Lucene.Net.Codecs.Asserting.AssertingDocValuesFormat.AssertingDocValuesProducer; + + /* + * 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 AssertingNormsConsumer = Lucene.Net.Codecs.Asserting.AssertingDocValuesFormat.AssertingNormsConsumer; + using Lucene42NormsFormat = Lucene.Net.Codecs.Lucene42.Lucene42NormsFormat; + using SegmentReadState = Lucene.Net.Index.SegmentReadState; + using SegmentWriteState = Lucene.Net.Index.SegmentWriteState; + + /// <summary> + /// Just like <seealso cref="Lucene42NormsFormat"/> but with additional asserts. + /// </summary> + public class AssertingNormsFormat : NormsFormat + { + private readonly NormsFormat @in = new Lucene42NormsFormat(); + + public override DocValuesConsumer NormsConsumer(SegmentWriteState state) + { + DocValuesConsumer consumer = @in.NormsConsumer(state); + Debug.Assert(consumer != null); + return new AssertingNormsConsumer(consumer, state.SegmentInfo.DocCount); + } + + public override DocValuesProducer NormsProducer(SegmentReadState state) + { + Debug.Assert(state.FieldInfos.HasNorms); + DocValuesProducer producer = @in.NormsProducer(state); + Debug.Assert(producer != null); + return new AssertingDocValuesProducer(producer, state.SegmentInfo.DocCount); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingPostingsFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingPostingsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingPostingsFormat.cs new file mode 100644 index 0000000..c039ceb --- /dev/null +++ b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingPostingsFormat.cs @@ -0,0 +1,318 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; + +namespace Lucene.Net.Codecs.Asserting +{ + using AssertingAtomicReader = Lucene.Net.Index.AssertingAtomicReader; + using BytesRef = Lucene.Net.Util.BytesRef; + using FieldInfo = Lucene.Net.Index.FieldInfo; + using IndexOptions = Lucene.Net.Index.IndexOptions; + + /* + * 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 Lucene41PostingsFormat = Lucene.Net.Codecs.Lucene41.Lucene41PostingsFormat; + using OpenBitSet = Lucene.Net.Util.OpenBitSet; + using SegmentReadState = Lucene.Net.Index.SegmentReadState; + using SegmentWriteState = Lucene.Net.Index.SegmentWriteState; + using Terms = Lucene.Net.Index.Terms; + + /// <summary> + /// Just like <seealso cref="Lucene41PostingsFormat"/> but with additional asserts. + /// </summary> + [PostingsFormatName("Asserting")] // LUCENENET specific - using PostingsFormatName attribute to ensure the default name passed from subclasses is the same as this class name + public sealed class AssertingPostingsFormat : PostingsFormat + { + private readonly PostingsFormat @in = new Lucene41PostingsFormat(); + + public AssertingPostingsFormat() + : base() + { + } + + public override FieldsConsumer FieldsConsumer(SegmentWriteState state) + { + return new AssertingFieldsConsumer(@in.FieldsConsumer(state)); + } + + public override FieldsProducer FieldsProducer(SegmentReadState state) + { + return new AssertingFieldsProducer(@in.FieldsProducer(state)); + } + + internal class AssertingFieldsProducer : FieldsProducer + { + internal readonly FieldsProducer @in; + + internal AssertingFieldsProducer(FieldsProducer @in) + { + this.@in = @in; + } + + public override void Dispose() + { + Dispose(true); + } + + protected void Dispose(bool disposing) + { + if (disposing) + @in.Dispose(); + } + + public override IEnumerator<string> GetEnumerator() + { + IEnumerator<string> iterator = @in.GetEnumerator(); + Debug.Assert(iterator != null); + return iterator; + } + + public override Terms GetTerms(string field) + { + Terms terms = @in.GetTerms(field); + return terms == null ? null : new AssertingAtomicReader.AssertingTerms(terms); + } + + public override int Count + { + get { return @in.Count; } + } + + [Obsolete("iterate fields and add their Count instead.")] + public override long UniqueTermCount + { + get + { + return @in.UniqueTermCount; + } + } + + public override long RamBytesUsed() + { + return @in.RamBytesUsed(); + } + + public override void CheckIntegrity() + { + @in.CheckIntegrity(); + } + } + + internal class AssertingFieldsConsumer : FieldsConsumer + { + internal readonly FieldsConsumer @in; + + internal AssertingFieldsConsumer(FieldsConsumer @in) + { + this.@in = @in; + } + + public override TermsConsumer AddField(FieldInfo field) + { + TermsConsumer consumer = @in.AddField(field); + Debug.Assert(consumer != null); + return new AssertingTermsConsumer(consumer, field); + } + + public override void Dispose() + { + Dispose(true); + } + + protected void Dispose(bool disposing) + { + if (disposing) + @in.Dispose(); + } + } + + internal enum TermsConsumerState + { + INITIAL, + START, + FINISHED + } + + internal class AssertingTermsConsumer : TermsConsumer + { + internal readonly TermsConsumer @in; + private readonly FieldInfo fieldInfo; + internal BytesRef LastTerm = null; + internal TermsConsumerState State = TermsConsumerState.INITIAL; + internal AssertingPostingsConsumer LastPostingsConsumer = null; + internal long SumTotalTermFreq = 0; + internal long SumDocFreq = 0; + internal OpenBitSet VisitedDocs = new OpenBitSet(); + + internal AssertingTermsConsumer(TermsConsumer @in, FieldInfo fieldInfo) + { + this.@in = @in; + this.fieldInfo = fieldInfo; + } + + public override PostingsConsumer StartTerm(BytesRef text) + { + Debug.Assert(State == TermsConsumerState.INITIAL || State == TermsConsumerState.START && LastPostingsConsumer.DocFreq == 0); + State = TermsConsumerState.START; + Debug.Assert(LastTerm == null || @in.Comparer.Compare(text, LastTerm) > 0); + LastTerm = BytesRef.DeepCopyOf(text); + return LastPostingsConsumer = new AssertingPostingsConsumer(@in.StartTerm(text), fieldInfo, VisitedDocs); + } + + public override void FinishTerm(BytesRef text, TermStats stats) + { + Debug.Assert(State == TermsConsumerState.START); + State = TermsConsumerState.INITIAL; + Debug.Assert(text.Equals(LastTerm)); + Debug.Assert(stats.DocFreq > 0); // otherwise, this method should not be called. + Debug.Assert(stats.DocFreq == LastPostingsConsumer.DocFreq); + SumDocFreq += stats.DocFreq; + if (fieldInfo.IndexOptions == IndexOptions.DOCS_ONLY) + { + Debug.Assert(stats.TotalTermFreq == -1); + } + else + { + Debug.Assert(stats.TotalTermFreq == LastPostingsConsumer.TotalTermFreq); + SumTotalTermFreq += stats.TotalTermFreq; + } + @in.FinishTerm(text, stats); + } + + public override void Finish(long sumTotalTermFreq, long sumDocFreq, int docCount) + { + Debug.Assert(State == TermsConsumerState.INITIAL || State == TermsConsumerState.START && LastPostingsConsumer.DocFreq == 0); + State = TermsConsumerState.FINISHED; + Debug.Assert(docCount >= 0); + Debug.Assert(docCount == VisitedDocs.Cardinality()); + Debug.Assert(sumDocFreq >= docCount); + Debug.Assert(sumDocFreq == this.SumDocFreq); + if (fieldInfo.IndexOptions == IndexOptions.DOCS_ONLY) + { + Debug.Assert(sumTotalTermFreq == -1); + } + else + { + Debug.Assert(sumTotalTermFreq >= sumDocFreq); + Debug.Assert(sumTotalTermFreq == this.SumTotalTermFreq); + } + @in.Finish(sumTotalTermFreq, sumDocFreq, docCount); + } + + public override IComparer<BytesRef> Comparer + { + get + { + return @in.Comparer; + } + } + } + + internal enum PostingsConsumerState + { + INITIAL, + START + } + + internal class AssertingPostingsConsumer : PostingsConsumer + { + internal readonly PostingsConsumer @in; + private readonly FieldInfo fieldInfo; + internal readonly OpenBitSet VisitedDocs; + internal PostingsConsumerState State = PostingsConsumerState.INITIAL; + internal int Freq; + internal int PositionCount; + internal int LastPosition = 0; + internal int LastStartOffset = 0; + internal int DocFreq = 0; + internal long TotalTermFreq = 0; + + internal AssertingPostingsConsumer(PostingsConsumer @in, FieldInfo fieldInfo, OpenBitSet visitedDocs) + { + this.@in = @in; + this.fieldInfo = fieldInfo; + this.VisitedDocs = visitedDocs; + } + + public override void StartDoc(int docID, int freq) + { + Debug.Assert(State == PostingsConsumerState.INITIAL); + State = PostingsConsumerState.START; + Debug.Assert(docID >= 0); + if (fieldInfo.IndexOptions == IndexOptions.DOCS_ONLY) + { + Debug.Assert(freq == -1); + this.Freq = 0; // we don't expect any positions here + } + else + { + Debug.Assert(freq > 0); + this.Freq = freq; + TotalTermFreq += freq; + } + this.PositionCount = 0; + this.LastPosition = 0; + this.LastStartOffset = 0; + DocFreq++; + VisitedDocs.Set(docID); + @in.StartDoc(docID, freq); + } + + public override void AddPosition(int position, BytesRef payload, int startOffset, int endOffset) + { + Debug.Assert(State == PostingsConsumerState.START); + Debug.Assert(PositionCount < Freq); + PositionCount++; + Debug.Assert(position >= LastPosition || position == -1); // we still allow -1 from old 3.x indexes + LastPosition = position; + if (fieldInfo.IndexOptions == IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) + { + Debug.Assert(startOffset >= 0); + Debug.Assert(startOffset >= LastStartOffset); + LastStartOffset = startOffset; + Debug.Assert(endOffset >= startOffset); + } + else + { + Debug.Assert(startOffset == -1); + Debug.Assert(endOffset == -1); + } + if (payload != null) + { + Debug.Assert(fieldInfo.HasPayloads); + } + @in.AddPosition(position, payload, startOffset, endOffset); + } + + public override void FinishDoc() + { + Debug.Assert(State == PostingsConsumerState.START); + State = PostingsConsumerState.INITIAL; + if (fieldInfo.IndexOptions < IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) + { + Debug.Assert(PositionCount == 0); // we should not have fed any positions! + } + else + { + Debug.Assert(PositionCount == Freq); + } + @in.FinishDoc(); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingStoredFieldsFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingStoredFieldsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingStoredFieldsFormat.cs new file mode 100644 index 0000000..1ad05ef --- /dev/null +++ b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingStoredFieldsFormat.cs @@ -0,0 +1,154 @@ +using System.Diagnostics; + +namespace Lucene.Net.Codecs.Asserting +{ + using Directory = Lucene.Net.Store.Directory; + using FieldInfo = Lucene.Net.Index.FieldInfo; + using FieldInfos = Lucene.Net.Index.FieldInfos; + using IIndexableField = Lucene.Net.Index.IIndexableField; + using IOContext = Lucene.Net.Store.IOContext; + + /* + * 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 Lucene41StoredFieldsFormat = Lucene.Net.Codecs.Lucene41.Lucene41StoredFieldsFormat; + using SegmentInfo = Lucene.Net.Index.SegmentInfo; + using StoredFieldVisitor = Lucene.Net.Index.StoredFieldVisitor; + + /// <summary> + /// Just like <seealso cref="Lucene41StoredFieldsFormat"/> but with additional asserts. + /// </summary> + public class AssertingStoredFieldsFormat : StoredFieldsFormat + { + private readonly StoredFieldsFormat @in = new Lucene41StoredFieldsFormat(); + + public override StoredFieldsReader FieldsReader(Directory directory, SegmentInfo si, FieldInfos fn, IOContext context) + { + return new AssertingStoredFieldsReader(@in.FieldsReader(directory, si, fn, context), si.DocCount); + } + + public override StoredFieldsWriter FieldsWriter(Directory directory, SegmentInfo si, IOContext context) + { + return new AssertingStoredFieldsWriter(@in.FieldsWriter(directory, si, context)); + } + + internal class AssertingStoredFieldsReader : StoredFieldsReader + { + internal readonly StoredFieldsReader @in; + internal readonly int MaxDoc; + + internal AssertingStoredFieldsReader(StoredFieldsReader @in, int maxDoc) + { + this.@in = @in; + this.MaxDoc = maxDoc; + } + + protected override void Dispose(bool disposing) + { + if (disposing) + @in.Dispose(); + } + + public override void VisitDocument(int n, StoredFieldVisitor visitor) + { + Debug.Assert(n >= 0 && n < MaxDoc); + @in.VisitDocument(n, visitor); + } + + public override object Clone() + { + return new AssertingStoredFieldsReader((StoredFieldsReader)@in.Clone(), MaxDoc); + } + + public override long RamBytesUsed() + { + return @in.RamBytesUsed(); + } + + public override void CheckIntegrity() + { + @in.CheckIntegrity(); + } + } + + internal enum Status + { + UNDEFINED, + STARTED, + FINISHED + } + + internal class AssertingStoredFieldsWriter : StoredFieldsWriter + { + internal readonly StoredFieldsWriter @in; + internal int NumWritten; + internal int FieldCount; + internal Status DocStatus; + + internal AssertingStoredFieldsWriter(StoredFieldsWriter @in) + { + this.@in = @in; + this.DocStatus = Status.UNDEFINED; + } + + public override void StartDocument(int numStoredFields) + { + Debug.Assert(DocStatus != Status.STARTED); + @in.StartDocument(numStoredFields); + Debug.Assert(FieldCount == 0); + FieldCount = numStoredFields; + NumWritten++; + DocStatus = Status.STARTED; + } + + public override void FinishDocument() + { + Debug.Assert(DocStatus == Status.STARTED); + Debug.Assert(FieldCount == 0); + @in.FinishDocument(); + DocStatus = Status.FINISHED; + } + + public override void WriteField(FieldInfo info, IIndexableField field) + { + Debug.Assert(DocStatus == Status.STARTED); + @in.WriteField(info, field); + Debug.Assert(FieldCount > 0); + FieldCount--; + } + + public override void Abort() + { + @in.Abort(); + } + + public override void Finish(FieldInfos fis, int numDocs) + { + Debug.Assert(DocStatus == (numDocs > 0 ? Status.FINISHED : Status.UNDEFINED)); + @in.Finish(fis, numDocs); + Debug.Assert(FieldCount == 0); + Debug.Assert(numDocs == NumWritten); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + @in.Dispose(); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingTermVectorsFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingTermVectorsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingTermVectorsFormat.cs new file mode 100644 index 0000000..cb90f52 --- /dev/null +++ b/src/Lucene.Net.TestFramework/Codecs/Asserting/AssertingTermVectorsFormat.cs @@ -0,0 +1,208 @@ +using System.Collections.Generic; +using System.Diagnostics; + +namespace Lucene.Net.Codecs.Asserting +{ + using AssertingAtomicReader = Lucene.Net.Index.AssertingAtomicReader; + using BytesRef = Lucene.Net.Util.BytesRef; + using Directory = Lucene.Net.Store.Directory; + using FieldInfo = Lucene.Net.Index.FieldInfo; + using FieldInfos = Lucene.Net.Index.FieldInfos; + using Fields = Lucene.Net.Index.Fields; + using IOContext = Lucene.Net.Store.IOContext; + + /* + * 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 Lucene40TermVectorsFormat = Lucene.Net.Codecs.Lucene40.Lucene40TermVectorsFormat; + using SegmentInfo = Lucene.Net.Index.SegmentInfo; + + /// <summary> + /// Just like <seealso cref="Lucene40TermVectorsFormat"/> but with additional asserts. + /// </summary> + public class AssertingTermVectorsFormat : TermVectorsFormat + { + private readonly TermVectorsFormat @in = new Lucene40TermVectorsFormat(); + + public override TermVectorsReader VectorsReader(Directory directory, SegmentInfo segmentInfo, FieldInfos fieldInfos, IOContext context) + { + return new AssertingTermVectorsReader(@in.VectorsReader(directory, segmentInfo, fieldInfos, context)); + } + + public override TermVectorsWriter VectorsWriter(Directory directory, SegmentInfo segmentInfo, IOContext context) + { + return new AssertingTermVectorsWriter(@in.VectorsWriter(directory, segmentInfo, context)); + } + + internal class AssertingTermVectorsReader : TermVectorsReader + { + internal readonly TermVectorsReader @in; + + internal AssertingTermVectorsReader(TermVectorsReader @in) + { + this.@in = @in; + } + + protected override void Dispose(bool disposing) + { + if (disposing) + @in.Dispose(); + } + + public override Fields Get(int doc) + { + Fields fields = @in.Get(doc); + return fields == null ? null : new AssertingAtomicReader.AssertingFields(fields); + } + + public override object Clone() + { + return new AssertingTermVectorsReader((TermVectorsReader)@in.Clone()); + } + + public override long RamBytesUsed() + { + return @in.RamBytesUsed(); + } + + public override void CheckIntegrity() + { + @in.CheckIntegrity(); + } + } + + internal enum Status + { + UNDEFINED, + STARTED, + FINISHED + } + + internal class AssertingTermVectorsWriter : TermVectorsWriter + { + internal readonly TermVectorsWriter @in; + internal Status DocStatus, FieldStatus, TermStatus; + internal int DocCount, FieldCount, TermCount, PositionCount; + internal bool HasPositions; + + internal AssertingTermVectorsWriter(TermVectorsWriter @in) + { + this.@in = @in; + DocStatus = Status.UNDEFINED; + FieldStatus = Status.UNDEFINED; + TermStatus = Status.UNDEFINED; + FieldCount = TermCount = PositionCount = 0; + } + + public override void StartDocument(int numVectorFields) + { + Debug.Assert(FieldCount == 0); + Debug.Assert(DocStatus != Status.STARTED); + @in.StartDocument(numVectorFields); + DocStatus = Status.STARTED; + FieldCount = numVectorFields; + DocCount++; + } + + public override void FinishDocument() + { + Debug.Assert(FieldCount == 0); + Debug.Assert(DocStatus == Status.STARTED); + @in.FinishDocument(); + DocStatus = Status.FINISHED; + } + + public override void StartField(FieldInfo info, int numTerms, bool positions, bool offsets, bool payloads) + { + Debug.Assert(TermCount == 0); + Debug.Assert(DocStatus == Status.STARTED); + Debug.Assert(FieldStatus != Status.STARTED); + @in.StartField(info, numTerms, positions, offsets, payloads); + FieldStatus = Status.STARTED; + TermCount = numTerms; + HasPositions = positions || offsets || payloads; + } + + public override void FinishField() + { + Debug.Assert(TermCount == 0); + Debug.Assert(FieldStatus == Status.STARTED); + @in.FinishField(); + FieldStatus = Status.FINISHED; + --FieldCount; + } + + public override void StartTerm(BytesRef term, int freq) + { + Debug.Assert(DocStatus == Status.STARTED); + Debug.Assert(FieldStatus == Status.STARTED); + Debug.Assert(TermStatus != Status.STARTED); + @in.StartTerm(term, freq); + TermStatus = Status.STARTED; + PositionCount = HasPositions ? freq : 0; + } + + public override void FinishTerm() + { + Debug.Assert(PositionCount == 0); + Debug.Assert(DocStatus == Status.STARTED); + Debug.Assert(FieldStatus == Status.STARTED); + Debug.Assert(TermStatus == Status.STARTED); + @in.FinishTerm(); + TermStatus = Status.FINISHED; + --TermCount; + } + + public override void AddPosition(int position, int startOffset, int endOffset, BytesRef payload) + { + Debug.Assert(DocStatus == Status.STARTED); + Debug.Assert(FieldStatus == Status.STARTED); + Debug.Assert(TermStatus == Status.STARTED); + @in.AddPosition(position, startOffset, endOffset, payload); + --PositionCount; + } + + public override void Abort() + { + @in.Abort(); + } + + public override void Finish(FieldInfos fis, int numDocs) + { + Debug.Assert(DocCount == numDocs); + Debug.Assert(DocStatus == (numDocs > 0 ? Status.FINISHED : Status.UNDEFINED)); + Debug.Assert(FieldStatus != Status.STARTED); + Debug.Assert(TermStatus != Status.STARTED); + @in.Finish(fis, numDocs); + } + + public override IComparer<BytesRef> Comparer + { + get + { + return @in.Comparer; + } + } + + protected override void Dispose(bool disposing) + { + if (disposing) + @in.Dispose(); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingCodec.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingCodec.cs b/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingCodec.cs deleted file mode 100644 index 3917c46..0000000 --- a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingCodec.cs +++ /dev/null @@ -1,64 +0,0 @@ -namespace Lucene.Net.Codecs.asserting -{ - /* - * 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 Lucene46Codec = Lucene.Net.Codecs.Lucene46.Lucene46Codec; - - /// <summary> - /// Acts like <seealso cref="Lucene46Codec"/> but with additional asserts. - /// </summary> - [CodecName("Asserting")] - public sealed class AssertingCodec : FilterCodec - { - private readonly PostingsFormat Postings = new AssertingPostingsFormat(); - private readonly TermVectorsFormat Vectors = new AssertingTermVectorsFormat(); - private readonly StoredFieldsFormat StoredFields = new AssertingStoredFieldsFormat(); - private readonly DocValuesFormat DocValues = new AssertingDocValuesFormat(); - private readonly NormsFormat Norms = new AssertingNormsFormat(); - - public AssertingCodec() - : base(new Lucene46Codec()) - { - } - - public override PostingsFormat PostingsFormat - { - get { return Postings; } - } - - public override TermVectorsFormat TermVectorsFormat - { - get { return Vectors; } - } - - public override StoredFieldsFormat StoredFieldsFormat - { - get { return StoredFields; } - } - - public override DocValuesFormat DocValuesFormat - { - get { return DocValues; } - } - - public override NormsFormat NormsFormat - { - get { return Norms; } - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingDocValuesFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingDocValuesFormat.cs b/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingDocValuesFormat.cs deleted file mode 100644 index 7a46b5c..0000000 --- a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingDocValuesFormat.cs +++ /dev/null @@ -1,346 +0,0 @@ -using System.Collections.Generic; -using System.Diagnostics; - -namespace Lucene.Net.Codecs.asserting -{ - using System; - using AssertingAtomicReader = Lucene.Net.Index.AssertingAtomicReader; - using BinaryDocValues = Lucene.Net.Index.BinaryDocValues; - using IBits = Lucene.Net.Util.IBits; - using BytesRef = Lucene.Net.Util.BytesRef; - using DocValuesType = Lucene.Net.Index.DocValuesType; - using FieldInfo = Lucene.Net.Index.FieldInfo; - using FixedBitSet = Lucene.Net.Util.FixedBitSet; - using Int64BitSet = Lucene.Net.Util.Int64BitSet; - - /* - * 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 Lucene45DocValuesFormat = Lucene.Net.Codecs.Lucene45.Lucene45DocValuesFormat; - using NumericDocValues = Lucene.Net.Index.NumericDocValues; - using SegmentReadState = Lucene.Net.Index.SegmentReadState; - using SegmentWriteState = Lucene.Net.Index.SegmentWriteState; - using SortedDocValues = Lucene.Net.Index.SortedDocValues; - using SortedSetDocValues = Lucene.Net.Index.SortedSetDocValues; - - /// <summary> - /// Just like <seealso cref="Lucene45DocValuesFormat"/> but with additional asserts. - /// </summary> - [DocValuesFormatName("Asserting")] - public class AssertingDocValuesFormat : DocValuesFormat - { - private readonly DocValuesFormat @in = new Lucene45DocValuesFormat(); - - public AssertingDocValuesFormat() - : base() - { - } - - public override DocValuesConsumer FieldsConsumer(SegmentWriteState state) - { - DocValuesConsumer consumer = @in.FieldsConsumer(state); - Debug.Assert(consumer != null); - return new AssertingDocValuesConsumer(consumer, state.SegmentInfo.DocCount); - } - - public override DocValuesProducer FieldsProducer(SegmentReadState state) - { - Debug.Assert(state.FieldInfos.HasDocValues); - DocValuesProducer producer = @in.FieldsProducer(state); - Debug.Assert(producer != null); - return new AssertingDocValuesProducer(producer, state.SegmentInfo.DocCount); - } - - internal class AssertingDocValuesConsumer : DocValuesConsumer - { - internal readonly DocValuesConsumer @in; - internal readonly int MaxDoc; - - internal AssertingDocValuesConsumer(DocValuesConsumer @in, int maxDoc) - { - this.@in = @in; - this.MaxDoc = maxDoc; - } - - public override void AddNumericField(FieldInfo field, IEnumerable<long?> values) - { - int count = 0; - foreach (var v in values) - { - count++; - } - Debug.Assert(count == MaxDoc); - CheckIterator(values.GetEnumerator(), MaxDoc, true); - @in.AddNumericField(field, values); - } - - public override void AddBinaryField(FieldInfo field, IEnumerable<BytesRef> values) - { - int count = 0; - foreach (BytesRef b in values) - { - Debug.Assert(b == null || b.IsValid()); - count++; - } - Debug.Assert(count == MaxDoc); - CheckIterator(values.GetEnumerator(), MaxDoc, true); - @in.AddBinaryField(field, values); - } - - public override void AddSortedField(FieldInfo field, IEnumerable<BytesRef> values, IEnumerable<long?> docToOrd) - { - int valueCount = 0; - BytesRef lastValue = null; - foreach (BytesRef b in values) - { - Debug.Assert(b != null); - Debug.Assert(b.IsValid()); - if (valueCount > 0) - { - Debug.Assert(b.CompareTo(lastValue) > 0); - } - lastValue = BytesRef.DeepCopyOf(b); - valueCount++; - } - Debug.Assert(valueCount <= MaxDoc); - - FixedBitSet seenOrds = new FixedBitSet(valueCount); - - int count = 0; - foreach (long? v in docToOrd) - { - Debug.Assert(v != null); - int ord = (int)v.Value; - Debug.Assert(ord >= -1 && ord < valueCount); - if (ord >= 0) - { - seenOrds.Set(ord); - } - count++; - } - - Debug.Assert(count == MaxDoc); - Debug.Assert(seenOrds.Cardinality() == valueCount); - CheckIterator(values.GetEnumerator(), valueCount, false); - CheckIterator(docToOrd.GetEnumerator(), MaxDoc, false); - @in.AddSortedField(field, values, docToOrd); - } - - public override void AddSortedSetField(FieldInfo field, IEnumerable<BytesRef> values, IEnumerable<long?> docToOrdCount, IEnumerable<long?> ords) - { - long valueCount = 0; - BytesRef lastValue = null; - foreach (BytesRef b in values) - { - Debug.Assert(b != null); - Debug.Assert(b.IsValid()); - if (valueCount > 0) - { - Debug.Assert(b.CompareTo(lastValue) > 0); - } - lastValue = BytesRef.DeepCopyOf(b); - valueCount++; - } - - int docCount = 0; - long ordCount = 0; - Int64BitSet seenOrds = new Int64BitSet(valueCount); - IEnumerator<long?> ordIterator = ords.GetEnumerator(); - foreach (long? v in docToOrdCount) - { - Debug.Assert(v != null); - int count = (int)v.Value; - Debug.Assert(count >= 0); - docCount++; - ordCount += count; - - long lastOrd = -1; - for (int i = 0; i < count; i++) - { - ordIterator.MoveNext(); - long? o = ordIterator.Current; - Debug.Assert(o != null); - long ord = o.Value; - Debug.Assert(ord >= 0 && ord < valueCount); - Debug.Assert(ord > lastOrd, "ord=" + ord + ",lastOrd=" + lastOrd); - seenOrds.Set(ord); - lastOrd = ord; - } - } - Debug.Assert(ordIterator.MoveNext() == false); - - Debug.Assert(docCount == MaxDoc); - Debug.Assert(seenOrds.Cardinality() == valueCount); - CheckIterator(values.GetEnumerator(), valueCount, false); - CheckIterator(docToOrdCount.GetEnumerator(), MaxDoc, false); - CheckIterator(ords.GetEnumerator(), ordCount, false); - @in.AddSortedSetField(field, values, docToOrdCount, ords); - } - - protected override void Dispose(bool disposing) - { - if (disposing) - @in.Dispose(); - } - } - - internal class AssertingNormsConsumer : DocValuesConsumer - { - internal readonly DocValuesConsumer @in; - internal readonly int MaxDoc; - - internal AssertingNormsConsumer(DocValuesConsumer @in, int maxDoc) - { - this.@in = @in; - this.MaxDoc = maxDoc; - } - - public override void AddNumericField(FieldInfo field, IEnumerable<long?> values) - { - int count = 0; - foreach (long? v in values) - { - Debug.Assert(v != null); - count++; - } - Debug.Assert(count == MaxDoc); - CheckIterator(values.GetEnumerator(), MaxDoc, false); - @in.AddNumericField(field, values); - } - - protected override void Dispose(bool disposing) - { - if (disposing) - @in.Dispose(); - } - - public override void AddBinaryField(FieldInfo field, IEnumerable<BytesRef> values) - { - throw new InvalidOperationException(); - } - - public override void AddSortedField(FieldInfo field, IEnumerable<BytesRef> values, IEnumerable<long?> docToOrd) - { - throw new InvalidOperationException(); - } - - public override void AddSortedSetField(FieldInfo field, IEnumerable<BytesRef> values, IEnumerable<long?> docToOrdCount, IEnumerable<long?> ords) - { - throw new InvalidOperationException(); - } - } - - private static void CheckIterator<T>(IEnumerator<T> iterator, long expectedSize, bool allowNull) - { - for (long i = 0; i < expectedSize; i++) - { - bool hasNext = iterator.MoveNext(); - Debug.Assert(hasNext); - T v = iterator.Current; - Debug.Assert(allowNull || v != null); - try - { - iterator.Reset(); - throw new InvalidOperationException("broken iterator (supports remove): " + iterator); - } - catch (System.NotSupportedException) - { - // ok - } - } - Debug.Assert(!iterator.MoveNext()); - /*try - { - //iterator.next(); - throw new InvalidOperationException("broken iterator (allows next() when hasNext==false) " + iterator); - } - catch (Exception) - { - // ok - }*/ - } - - internal class AssertingDocValuesProducer : DocValuesProducer - { - internal readonly DocValuesProducer @in; - internal readonly int MaxDoc; - - internal AssertingDocValuesProducer(DocValuesProducer @in, int maxDoc) - { - this.@in = @in; - this.MaxDoc = maxDoc; - } - - public override NumericDocValues GetNumeric(FieldInfo field) - { - Debug.Assert(field.DocValuesType == DocValuesType.NUMERIC || field.NormType == DocValuesType.NUMERIC); - NumericDocValues values = @in.GetNumeric(field); - Debug.Assert(values != null); - return new AssertingAtomicReader.AssertingNumericDocValues(values, MaxDoc); - } - - public override BinaryDocValues GetBinary(FieldInfo field) - { - Debug.Assert(field.DocValuesType == DocValuesType.BINARY); - BinaryDocValues values = @in.GetBinary(field); - Debug.Assert(values != null); - return new AssertingAtomicReader.AssertingBinaryDocValues(values, MaxDoc); - } - - public override SortedDocValues GetSorted(FieldInfo field) - { - Debug.Assert(field.DocValuesType == DocValuesType.SORTED); - SortedDocValues values = @in.GetSorted(field); - Debug.Assert(values != null); - return new AssertingAtomicReader.AssertingSortedDocValues(values, MaxDoc); - } - - public override SortedSetDocValues GetSortedSet(FieldInfo field) - { - Debug.Assert(field.DocValuesType == DocValuesType.SORTED_SET); - SortedSetDocValues values = @in.GetSortedSet(field); - Debug.Assert(values != null); - return new AssertingAtomicReader.AssertingSortedSetDocValues(values, MaxDoc); - } - - public override IBits GetDocsWithField(FieldInfo field) - { - Debug.Assert(field.DocValuesType != null); - IBits bits = @in.GetDocsWithField(field); - Debug.Assert(bits != null); - Debug.Assert(bits.Length == MaxDoc); - return new AssertingAtomicReader.AssertingBits(bits); - } - - protected override void Dispose(bool disposing) - { - if (disposing) - @in.Dispose(); - } - - public override long RamBytesUsed() - { - return @in.RamBytesUsed(); - } - - public override void CheckIntegrity() - { - @in.CheckIntegrity(); - } - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingNormsFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingNormsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingNormsFormat.cs deleted file mode 100644 index 0137dce..0000000 --- a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingNormsFormat.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System.Diagnostics; - -namespace Lucene.Net.Codecs.asserting -{ - using AssertingDocValuesProducer = Lucene.Net.Codecs.asserting.AssertingDocValuesFormat.AssertingDocValuesProducer; - - /* - * 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 AssertingNormsConsumer = Lucene.Net.Codecs.asserting.AssertingDocValuesFormat.AssertingNormsConsumer; - using Lucene42NormsFormat = Lucene.Net.Codecs.Lucene42.Lucene42NormsFormat; - using SegmentReadState = Lucene.Net.Index.SegmentReadState; - using SegmentWriteState = Lucene.Net.Index.SegmentWriteState; - - /// <summary> - /// Just like <seealso cref="Lucene42NormsFormat"/> but with additional asserts. - /// </summary> - public class AssertingNormsFormat : NormsFormat - { - private readonly NormsFormat @in = new Lucene42NormsFormat(); - - public override DocValuesConsumer NormsConsumer(SegmentWriteState state) - { - DocValuesConsumer consumer = @in.NormsConsumer(state); - Debug.Assert(consumer != null); - return new AssertingNormsConsumer(consumer, state.SegmentInfo.DocCount); - } - - public override DocValuesProducer NormsProducer(SegmentReadState state) - { - Debug.Assert(state.FieldInfos.HasNorms); - DocValuesProducer producer = @in.NormsProducer(state); - Debug.Assert(producer != null); - return new AssertingDocValuesProducer(producer, state.SegmentInfo.DocCount); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingPostingsFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingPostingsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingPostingsFormat.cs deleted file mode 100644 index ef582b7..0000000 --- a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingPostingsFormat.cs +++ /dev/null @@ -1,318 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; - -namespace Lucene.Net.Codecs.asserting -{ - using AssertingAtomicReader = Lucene.Net.Index.AssertingAtomicReader; - using BytesRef = Lucene.Net.Util.BytesRef; - using FieldInfo = Lucene.Net.Index.FieldInfo; - using IndexOptions = Lucene.Net.Index.IndexOptions; - - /* - * 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 Lucene41PostingsFormat = Lucene.Net.Codecs.Lucene41.Lucene41PostingsFormat; - using OpenBitSet = Lucene.Net.Util.OpenBitSet; - using SegmentReadState = Lucene.Net.Index.SegmentReadState; - using SegmentWriteState = Lucene.Net.Index.SegmentWriteState; - using Terms = Lucene.Net.Index.Terms; - - /// <summary> - /// Just like <seealso cref="Lucene41PostingsFormat"/> but with additional asserts. - /// </summary> - [PostingsFormatName("Asserting")] // LUCENENET specific - using PostingsFormatName attribute to ensure the default name passed from subclasses is the same as this class name - public sealed class AssertingPostingsFormat : PostingsFormat - { - private readonly PostingsFormat @in = new Lucene41PostingsFormat(); - - public AssertingPostingsFormat() - : base() - { - } - - public override FieldsConsumer FieldsConsumer(SegmentWriteState state) - { - return new AssertingFieldsConsumer(@in.FieldsConsumer(state)); - } - - public override FieldsProducer FieldsProducer(SegmentReadState state) - { - return new AssertingFieldsProducer(@in.FieldsProducer(state)); - } - - internal class AssertingFieldsProducer : FieldsProducer - { - internal readonly FieldsProducer @in; - - internal AssertingFieldsProducer(FieldsProducer @in) - { - this.@in = @in; - } - - public override void Dispose() - { - Dispose(true); - } - - protected void Dispose(bool disposing) - { - if (disposing) - @in.Dispose(); - } - - public override IEnumerator<string> GetEnumerator() - { - IEnumerator<string> iterator = @in.GetEnumerator(); - Debug.Assert(iterator != null); - return iterator; - } - - public override Terms GetTerms(string field) - { - Terms terms = @in.GetTerms(field); - return terms == null ? null : new AssertingAtomicReader.AssertingTerms(terms); - } - - public override int Count - { - get { return @in.Count; } - } - - [Obsolete("iterate fields and add their Count instead.")] - public override long UniqueTermCount - { - get - { - return @in.UniqueTermCount; - } - } - - public override long RamBytesUsed() - { - return @in.RamBytesUsed(); - } - - public override void CheckIntegrity() - { - @in.CheckIntegrity(); - } - } - - internal class AssertingFieldsConsumer : FieldsConsumer - { - internal readonly FieldsConsumer @in; - - internal AssertingFieldsConsumer(FieldsConsumer @in) - { - this.@in = @in; - } - - public override TermsConsumer AddField(FieldInfo field) - { - TermsConsumer consumer = @in.AddField(field); - Debug.Assert(consumer != null); - return new AssertingTermsConsumer(consumer, field); - } - - public override void Dispose() - { - Dispose(true); - } - - protected void Dispose(bool disposing) - { - if (disposing) - @in.Dispose(); - } - } - - internal enum TermsConsumerState - { - INITIAL, - START, - FINISHED - } - - internal class AssertingTermsConsumer : TermsConsumer - { - internal readonly TermsConsumer @in; - private readonly FieldInfo fieldInfo; - internal BytesRef LastTerm = null; - internal TermsConsumerState State = TermsConsumerState.INITIAL; - internal AssertingPostingsConsumer LastPostingsConsumer = null; - internal long SumTotalTermFreq = 0; - internal long SumDocFreq = 0; - internal OpenBitSet VisitedDocs = new OpenBitSet(); - - internal AssertingTermsConsumer(TermsConsumer @in, FieldInfo fieldInfo) - { - this.@in = @in; - this.fieldInfo = fieldInfo; - } - - public override PostingsConsumer StartTerm(BytesRef text) - { - Debug.Assert(State == TermsConsumerState.INITIAL || State == TermsConsumerState.START && LastPostingsConsumer.DocFreq == 0); - State = TermsConsumerState.START; - Debug.Assert(LastTerm == null || @in.Comparer.Compare(text, LastTerm) > 0); - LastTerm = BytesRef.DeepCopyOf(text); - return LastPostingsConsumer = new AssertingPostingsConsumer(@in.StartTerm(text), fieldInfo, VisitedDocs); - } - - public override void FinishTerm(BytesRef text, TermStats stats) - { - Debug.Assert(State == TermsConsumerState.START); - State = TermsConsumerState.INITIAL; - Debug.Assert(text.Equals(LastTerm)); - Debug.Assert(stats.DocFreq > 0); // otherwise, this method should not be called. - Debug.Assert(stats.DocFreq == LastPostingsConsumer.DocFreq); - SumDocFreq += stats.DocFreq; - if (fieldInfo.IndexOptions == IndexOptions.DOCS_ONLY) - { - Debug.Assert(stats.TotalTermFreq == -1); - } - else - { - Debug.Assert(stats.TotalTermFreq == LastPostingsConsumer.TotalTermFreq); - SumTotalTermFreq += stats.TotalTermFreq; - } - @in.FinishTerm(text, stats); - } - - public override void Finish(long sumTotalTermFreq, long sumDocFreq, int docCount) - { - Debug.Assert(State == TermsConsumerState.INITIAL || State == TermsConsumerState.START && LastPostingsConsumer.DocFreq == 0); - State = TermsConsumerState.FINISHED; - Debug.Assert(docCount >= 0); - Debug.Assert(docCount == VisitedDocs.Cardinality()); - Debug.Assert(sumDocFreq >= docCount); - Debug.Assert(sumDocFreq == this.SumDocFreq); - if (fieldInfo.IndexOptions == IndexOptions.DOCS_ONLY) - { - Debug.Assert(sumTotalTermFreq == -1); - } - else - { - Debug.Assert(sumTotalTermFreq >= sumDocFreq); - Debug.Assert(sumTotalTermFreq == this.SumTotalTermFreq); - } - @in.Finish(sumTotalTermFreq, sumDocFreq, docCount); - } - - public override IComparer<BytesRef> Comparer - { - get - { - return @in.Comparer; - } - } - } - - internal enum PostingsConsumerState - { - INITIAL, - START - } - - internal class AssertingPostingsConsumer : PostingsConsumer - { - internal readonly PostingsConsumer @in; - private readonly FieldInfo fieldInfo; - internal readonly OpenBitSet VisitedDocs; - internal PostingsConsumerState State = PostingsConsumerState.INITIAL; - internal int Freq; - internal int PositionCount; - internal int LastPosition = 0; - internal int LastStartOffset = 0; - internal int DocFreq = 0; - internal long TotalTermFreq = 0; - - internal AssertingPostingsConsumer(PostingsConsumer @in, FieldInfo fieldInfo, OpenBitSet visitedDocs) - { - this.@in = @in; - this.fieldInfo = fieldInfo; - this.VisitedDocs = visitedDocs; - } - - public override void StartDoc(int docID, int freq) - { - Debug.Assert(State == PostingsConsumerState.INITIAL); - State = PostingsConsumerState.START; - Debug.Assert(docID >= 0); - if (fieldInfo.IndexOptions == IndexOptions.DOCS_ONLY) - { - Debug.Assert(freq == -1); - this.Freq = 0; // we don't expect any positions here - } - else - { - Debug.Assert(freq > 0); - this.Freq = freq; - TotalTermFreq += freq; - } - this.PositionCount = 0; - this.LastPosition = 0; - this.LastStartOffset = 0; - DocFreq++; - VisitedDocs.Set(docID); - @in.StartDoc(docID, freq); - } - - public override void AddPosition(int position, BytesRef payload, int startOffset, int endOffset) - { - Debug.Assert(State == PostingsConsumerState.START); - Debug.Assert(PositionCount < Freq); - PositionCount++; - Debug.Assert(position >= LastPosition || position == -1); // we still allow -1 from old 3.x indexes - LastPosition = position; - if (fieldInfo.IndexOptions == IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS) - { - Debug.Assert(startOffset >= 0); - Debug.Assert(startOffset >= LastStartOffset); - LastStartOffset = startOffset; - Debug.Assert(endOffset >= startOffset); - } - else - { - Debug.Assert(startOffset == -1); - Debug.Assert(endOffset == -1); - } - if (payload != null) - { - Debug.Assert(fieldInfo.HasPayloads); - } - @in.AddPosition(position, payload, startOffset, endOffset); - } - - public override void FinishDoc() - { - Debug.Assert(State == PostingsConsumerState.START); - State = PostingsConsumerState.INITIAL; - if (fieldInfo.IndexOptions < IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) - { - Debug.Assert(PositionCount == 0); // we should not have fed any positions! - } - else - { - Debug.Assert(PositionCount == Freq); - } - @in.FinishDoc(); - } - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/77e95ccc/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingStoredFieldsFormat.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingStoredFieldsFormat.cs b/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingStoredFieldsFormat.cs deleted file mode 100644 index 808ed9a..0000000 --- a/src/Lucene.Net.TestFramework/Codecs/asserting/AssertingStoredFieldsFormat.cs +++ /dev/null @@ -1,154 +0,0 @@ -using System.Diagnostics; - -namespace Lucene.Net.Codecs.asserting -{ - using Directory = Lucene.Net.Store.Directory; - using FieldInfo = Lucene.Net.Index.FieldInfo; - using FieldInfos = Lucene.Net.Index.FieldInfos; - using IIndexableField = Lucene.Net.Index.IIndexableField; - using IOContext = Lucene.Net.Store.IOContext; - - /* - * 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 Lucene41StoredFieldsFormat = Lucene.Net.Codecs.Lucene41.Lucene41StoredFieldsFormat; - using SegmentInfo = Lucene.Net.Index.SegmentInfo; - using StoredFieldVisitor = Lucene.Net.Index.StoredFieldVisitor; - - /// <summary> - /// Just like <seealso cref="Lucene41StoredFieldsFormat"/> but with additional asserts. - /// </summary> - public class AssertingStoredFieldsFormat : StoredFieldsFormat - { - private readonly StoredFieldsFormat @in = new Lucene41StoredFieldsFormat(); - - public override StoredFieldsReader FieldsReader(Directory directory, SegmentInfo si, FieldInfos fn, IOContext context) - { - return new AssertingStoredFieldsReader(@in.FieldsReader(directory, si, fn, context), si.DocCount); - } - - public override StoredFieldsWriter FieldsWriter(Directory directory, SegmentInfo si, IOContext context) - { - return new AssertingStoredFieldsWriter(@in.FieldsWriter(directory, si, context)); - } - - internal class AssertingStoredFieldsReader : StoredFieldsReader - { - internal readonly StoredFieldsReader @in; - internal readonly int MaxDoc; - - internal AssertingStoredFieldsReader(StoredFieldsReader @in, int maxDoc) - { - this.@in = @in; - this.MaxDoc = maxDoc; - } - - protected override void Dispose(bool disposing) - { - if (disposing) - @in.Dispose(); - } - - public override void VisitDocument(int n, StoredFieldVisitor visitor) - { - Debug.Assert(n >= 0 && n < MaxDoc); - @in.VisitDocument(n, visitor); - } - - public override object Clone() - { - return new AssertingStoredFieldsReader((StoredFieldsReader)@in.Clone(), MaxDoc); - } - - public override long RamBytesUsed() - { - return @in.RamBytesUsed(); - } - - public override void CheckIntegrity() - { - @in.CheckIntegrity(); - } - } - - internal enum Status - { - UNDEFINED, - STARTED, - FINISHED - } - - internal class AssertingStoredFieldsWriter : StoredFieldsWriter - { - internal readonly StoredFieldsWriter @in; - internal int NumWritten; - internal int FieldCount; - internal Status DocStatus; - - internal AssertingStoredFieldsWriter(StoredFieldsWriter @in) - { - this.@in = @in; - this.DocStatus = Status.UNDEFINED; - } - - public override void StartDocument(int numStoredFields) - { - Debug.Assert(DocStatus != Status.STARTED); - @in.StartDocument(numStoredFields); - Debug.Assert(FieldCount == 0); - FieldCount = numStoredFields; - NumWritten++; - DocStatus = Status.STARTED; - } - - public override void FinishDocument() - { - Debug.Assert(DocStatus == Status.STARTED); - Debug.Assert(FieldCount == 0); - @in.FinishDocument(); - DocStatus = Status.FINISHED; - } - - public override void WriteField(FieldInfo info, IIndexableField field) - { - Debug.Assert(DocStatus == Status.STARTED); - @in.WriteField(info, field); - Debug.Assert(FieldCount > 0); - FieldCount--; - } - - public override void Abort() - { - @in.Abort(); - } - - public override void Finish(FieldInfos fis, int numDocs) - { - Debug.Assert(DocStatus == (numDocs > 0 ? Status.FINISHED : Status.UNDEFINED)); - @in.Finish(fis, numDocs); - Debug.Assert(FieldCount == 0); - Debug.Assert(numDocs == NumWritten); - } - - protected override void Dispose(bool disposing) - { - if (disposing) - @in.Dispose(); - } - } - } -} \ No newline at end of file
