Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id A04C8200B94 for ; Sun, 2 Oct 2016 16:35:29 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 9F3EC160AC7; Sun, 2 Oct 2016 14:35:29 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E7ADC160AEA for ; Sun, 2 Oct 2016 16:35:27 +0200 (CEST) Received: (qmail 95930 invoked by uid 500); 2 Oct 2016 14:35:21 -0000 Mailing-List: contact commits-help@lucenenet.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: lucene-net-dev@lucenenet.apache.org Delivered-To: mailing list commits@lucenenet.apache.org Received: (qmail 95215 invoked by uid 99); 2 Oct 2016 14:35:20 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 02 Oct 2016 14:35:20 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id A704BE188F; Sun, 2 Oct 2016 14:35:20 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: synhershko@apache.org To: commits@lucenenet.apache.org Date: Sun, 02 Oct 2016 14:35:59 -0000 Message-Id: <0a27ca3669b54b78a54ee5c26237ed69@git.apache.org> In-Reply-To: <6014ffb9c2e748049fcffef430b6e351@git.apache.org> References: <6014ffb9c2e748049fcffef430b6e351@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [41/50] [abbrv] lucenenet git commit: Ported Lucene.Net.Memory + tests archived-at: Sun, 02 Oct 2016 14:35:29 -0000 Ported Lucene.Net.Memory + tests Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/d47b6088 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/d47b6088 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/d47b6088 Branch: refs/heads/master Commit: d47b60884ebf12492a30066a969d4662ed2ae6e4 Parents: c9adcae Author: Shad Storhaug Authored: Mon Sep 19 11:46:41 2016 +0700 Committer: Shad Storhaug Committed: Sun Oct 2 17:45:05 2016 +0700 ---------------------------------------------------------------------- Lucene.Net.sln | 52 + src/Lucene.Net.Memory/HashMapHelperClass.cs | 26 - src/Lucene.Net.Memory/Lucene.Net.Memory.csproj | 11 +- src/Lucene.Net.Memory/MemoryIndex.Info.cs | 99 + .../MemoryIndex.MemoryIndexReader.cs | 675 +++++ .../MemoryIndex.TermComparer.cs | 34 + src/Lucene.Net.Memory/MemoryIndex.cs | 2324 ++++++------------ .../MemoryIndexNormDocValues.cs | 5 +- .../Properties/AssemblyInfo.cs | 7 +- .../Index/Memory/MemoryIndexTest.cs | 584 +++++ .../Index/Memory/testqueries.txt | 129 + .../Index/Memory/testqueries2.txt | 5 + .../Lucene.Net.Tests.Memory.csproj | 86 + .../Properties/AssemblyInfo.cs | 35 + src/Lucene.Net.Tests.Memory/packages.config | 4 + 15 files changed, 2459 insertions(+), 1617 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d47b6088/Lucene.Net.sln ---------------------------------------------------------------------- diff --git a/Lucene.Net.sln b/Lucene.Net.sln index 1914ec2..25ab1ab 100644 --- a/Lucene.Net.sln +++ b/Lucene.Net.sln @@ -59,6 +59,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lucene.Net.Suggest", "src\L EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lucene.Net.Tests.Suggest", "src\Lucene.Net.Tests.Suggest\Lucene.Net.Tests.Suggest.csproj", "{A6511598-3008-4A3B-AE68-2D1DA792CA8A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lucene.Net.QueryParser", "src\Lucene.Net.QueryParser\Lucene.Net.QueryParser.csproj", "{949BA34B-6AE6-4CE3-B578-61E13E4D76BF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lucene.Net.Tests.QueryParser", "src\Lucene.Net.Tests.QueryParser\Lucene.Net.Tests.QueryParser.csproj", "{5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lucene.Net.Memory", "src\Lucene.Net.Memory\Lucene.Net.Memory.csproj", "{42ECF239-AFC1-427D-921E-B5A277809CF0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lucene.Net.Tests.Memory", "src\Lucene.Net.Tests.Memory\Lucene.Net.Tests.Memory.csproj", "{7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -326,6 +334,50 @@ Global {A6511598-3008-4A3B-AE68-2D1DA792CA8A}.Release|Mixed Platforms.Build.0 = Release|Any CPU {A6511598-3008-4A3B-AE68-2D1DA792CA8A}.Release|x86.ActiveCfg = Release|Any CPU {A6511598-3008-4A3B-AE68-2D1DA792CA8A}.Release|x86.Build.0 = Release|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Debug|x86.ActiveCfg = Debug|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Release|Any CPU.Build.0 = Release|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {949BA34B-6AE6-4CE3-B578-61E13E4D76BF}.Release|x86.ActiveCfg = Release|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Debug|x86.ActiveCfg = Debug|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Release|Any CPU.Build.0 = Release|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {5719FB4F-BF80-40E5-BACC-37E8E18FCA2E}.Release|x86.ActiveCfg = Release|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Debug|x86.ActiveCfg = Debug|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Debug|x86.Build.0 = Debug|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Release|Any CPU.Build.0 = Release|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Release|x86.ActiveCfg = Release|Any CPU + {42ECF239-AFC1-427D-921E-B5A277809CF0}.Release|x86.Build.0 = Release|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Debug|x86.ActiveCfg = Debug|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Debug|x86.Build.0 = Debug|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Release|Any CPU.Build.0 = Release|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Release|x86.ActiveCfg = Release|Any CPU + {7F9378BF-C88D-46FF-9AE8-5E7D8C0225D3}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d47b6088/src/Lucene.Net.Memory/HashMapHelperClass.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Memory/HashMapHelperClass.cs b/src/Lucene.Net.Memory/HashMapHelperClass.cs deleted file mode 100644 index 395d53e..0000000 --- a/src/Lucene.Net.Memory/HashMapHelperClass.cs +++ /dev/null @@ -1,26 +0,0 @@ -//--------------------------------------------------------------------------------------------------------- -// Copyright © 2007 - 2014 Tangible Software Solutions Inc. -// This class can be used by anyone provided that the copyright notice remains intact. -// -// This class is used to replace calls to some Java HashMap or Hashtable methods. -//--------------------------------------------------------------------------------------------------------- -using System.Collections.Generic; -internal static class HashMapHelperClass -{ - internal static HashSet> SetOfKeyValuePairs(this IDictionary dictionary) - { - var entries = new HashSet>(); - foreach (KeyValuePair keyValuePair in dictionary) - { - entries.Add(keyValuePair); - } - return entries; - } - - internal static TValue GetValueOrNull(this IDictionary dictionary, TKey key) - { - TValue ret; - dictionary.TryGetValue(key, out ret); - return ret; - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d47b6088/src/Lucene.Net.Memory/Lucene.Net.Memory.csproj ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Memory/Lucene.Net.Memory.csproj b/src/Lucene.Net.Memory/Lucene.Net.Memory.csproj index 3ba5780..8997f33 100644 --- a/src/Lucene.Net.Memory/Lucene.Net.Memory.csproj +++ b/src/Lucene.Net.Memory/Lucene.Net.Memory.csproj @@ -9,17 +9,19 @@ Properties Lucene.Net.Memory Lucene.Net.Memory - v4.0 + v4.5.1 512 + true full false bin\Debug\ - DEBUG;TRACE + TRACE prompt 4 + false pdbonly @@ -28,6 +30,7 @@ TRACE prompt 4 + false @@ -39,8 +42,10 @@ - + + + http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d47b6088/src/Lucene.Net.Memory/MemoryIndex.Info.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Memory/MemoryIndex.Info.cs b/src/Lucene.Net.Memory/MemoryIndex.Info.cs new file mode 100644 index 0000000..0953ec2 --- /dev/null +++ b/src/Lucene.Net.Memory/MemoryIndex.Info.cs @@ -0,0 +1,99 @@ +using Lucene.Net.Util; +using System; + +namespace Lucene.Net.Index.Memory +{ + public partial class MemoryIndex + { + /// + /// Index data structure for a field; Contains the tokenized term texts and + /// their positions. + /// + private sealed class Info + { + /// + /// Term strings and their positions for this field: + /// + internal readonly BytesRefHash terms; + + internal readonly SliceByteStartArray sliceArray; + + /// + /// Terms sorted ascending by term text; computed on demand + /// + [NonSerialized] + internal int[] sortedTerms; + + /// + /// Number of added tokens for this field + /// + internal readonly int numTokens; + + /// + /// Number of overlapping tokens for this field + /// + internal readonly int numOverlapTokens; + + /// + /// Boost factor for hits for this field + /// + internal readonly float boost; + + internal readonly long sumTotalTermFreq; + + /// + /// the last position encountered in this field for multi field support + /// + internal int lastPosition; + + /// + /// the last offset encountered in this field for multi field support + /// + internal int lastOffset; + + public Info(BytesRefHash terms, SliceByteStartArray sliceArray, int numTokens, int numOverlapTokens, float boost, int lastPosition, int lastOffset, long sumTotalTermFreq) + { + this.terms = terms; + this.sliceArray = sliceArray; + this.numTokens = numTokens; + this.numOverlapTokens = numOverlapTokens; + this.boost = boost; + this.sumTotalTermFreq = sumTotalTermFreq; + this.lastPosition = lastPosition; + this.lastOffset = lastOffset; + } + + public long SumTotalTermFreq + { + get + { + return sumTotalTermFreq; + } + } + + /// + /// Sorts hashed terms into ascending order, reusing memory along the + /// way. Note that sorting is lazily delayed until required (often it's + /// not required at all). If a sorted view is required then hashing + + /// sort + binary search is still faster and smaller than TreeMap usage + /// (which would be an alternative and somewhat more elegant approach, + /// apart from more sophisticated Tries / prefix trees). + /// + public void SortTerms() + { + if (sortedTerms == null) + { + sortedTerms = terms.Sort(BytesRef.UTF8SortedAsUnicodeComparer); + } + } + + public float Boost + { + get + { + return boost; + } + } + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d47b6088/src/Lucene.Net.Memory/MemoryIndex.MemoryIndexReader.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Memory/MemoryIndex.MemoryIndexReader.cs b/src/Lucene.Net.Memory/MemoryIndex.MemoryIndexReader.cs new file mode 100644 index 0000000..608b397 --- /dev/null +++ b/src/Lucene.Net.Memory/MemoryIndex.MemoryIndexReader.cs @@ -0,0 +1,675 @@ +using Lucene.Net.Search; +using Lucene.Net.Search.Similarities; +using Lucene.Net.Util; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +namespace Lucene.Net.Index.Memory +{ + public partial class MemoryIndex + { + /////////////////////////////////////////////////////////////////////////////// + // Nested classes: + /////////////////////////////////////////////////////////////////////////////// + + /// + /// Search support for Lucene framework integration; implements all methods + /// required by the Lucene IndexReader contracts. + /// + private sealed class MemoryIndexReader : AtomicReader + { + private readonly MemoryIndex outerInstance; + + + internal IndexSearcher searcher; // needed to find searcher.getSimilarity() + + internal MemoryIndexReader(MemoryIndex outerInstance) + : base() // avoid as much superclass baggage as possible + { + this.outerInstance = outerInstance; + } + + internal Info GetInfo(string fieldName) + { + return outerInstance.fields[fieldName]; + } + + internal Info GetInfo(int pos) + { + return outerInstance.sortedFields[pos].Value; + } + + public override Bits LiveDocs + { + get + { + return null; + } + } + + public override FieldInfos FieldInfos + { + get + { + return new FieldInfos(outerInstance.fieldInfos.Values.ToArray(/*new FieldInfo[outerInstance.fieldInfos.Count]*/)); + } + } + + public override NumericDocValues GetNumericDocValues(string field) + { + return null; + } + + public override BinaryDocValues GetBinaryDocValues(string field) + { + return null; + } + + public override SortedDocValues GetSortedDocValues(string field) + { + return null; + } + + public override SortedSetDocValues GetSortedSetDocValues(string field) + { + return null; + } + + public override Bits GetDocsWithField(string field) + { + return null; + } + + public override void CheckIntegrity() + { + // no-op + } + + private class MemoryFields : Fields + { + private readonly MemoryIndex.MemoryIndexReader outerInstance; + + public MemoryFields(MemoryIndex.MemoryIndexReader outerInstance) + { + this.outerInstance = outerInstance; + } + + public override IEnumerator GetEnumerator() + { + return new IteratorAnonymousInnerClassHelper(this); + } + + private class IteratorAnonymousInnerClassHelper : IEnumerator + { + private readonly MemoryFields outerInstance; + + public IteratorAnonymousInnerClassHelper(MemoryFields outerInstance) + { + this.outerInstance = outerInstance; + upto = -1; + } + + internal int upto; + private string current; + + public string Current + { + get + { + return this.current; + } + } + + object IEnumerator.Current + { + get + { + return Current; + } + } + + public void Dispose() + { + // Nothing to do + } + + public bool MoveNext() + { + if (upto + 1 >= outerInstance.outerInstance.outerInstance.sortedFields.Length) + { + return false; + } + upto++; + current = outerInstance.outerInstance.outerInstance.sortedFields[upto].Key; + return true; + } + + public void Reset() + { + throw new NotSupportedException(); + } + } + + public override Terms Terms(string field) + { + var searchField = new KeyValuePair(field, null); + int i = Array.BinarySearch(outerInstance.outerInstance.sortedFields, searchField, new TermComparer()); + if (i < 0) + { + return null; + } + else + { + Info info = outerInstance.GetInfo(i); + info.SortTerms(); + + return new TermsAnonymousInnerClassHelper(this, info); + } + } + + private class TermsAnonymousInnerClassHelper : Terms + { + private readonly MemoryFields outerInstance; + + private MemoryIndex.Info info; + + public TermsAnonymousInnerClassHelper(MemoryFields outerInstance, MemoryIndex.Info info) + { + this.outerInstance = outerInstance; + this.info = info; + } + + public override TermsEnum Iterator(TermsEnum reuse) + { + return new MemoryTermsEnum(outerInstance.outerInstance, info); + } + + public override IComparer Comparator + { + get + { + return BytesRef.UTF8SortedAsUnicodeComparer; + } + } + + public override long Size() + { + return info.terms.Size(); + } + + public override long SumTotalTermFreq + { + get + { + return info.SumTotalTermFreq; + } + } + + public override long SumDocFreq + { + get + { + // each term has df=1 + return info.terms.Size(); + } + } + + public override int DocCount + { + get + { + return info.terms.Size() > 0 ? 1 : 0; + } + } + + public override bool HasFreqs() + { + return true; + } + + public override bool HasOffsets() + { + return outerInstance.outerInstance.outerInstance.storeOffsets; + } + + public override bool HasPositions() + { + return true; + } + + public override bool HasPayloads() + { + return false; + } + } + + public override int Size + { + get { return outerInstance.outerInstance.sortedFields.Length; } + } + } + + public override Fields Fields + { + get + { + outerInstance.SortFields(); + return new MemoryFields(this); + } + } + + private class MemoryTermsEnum : TermsEnum + { + private readonly MemoryIndex.MemoryIndexReader outerInstance; + + internal readonly Info info; + internal readonly BytesRef br = new BytesRef(); + internal int termUpto = -1; + + public MemoryTermsEnum(MemoryIndex.MemoryIndexReader outerInstance, Info info) + { + this.outerInstance = outerInstance; + this.info = info; + info.SortTerms(); + } + + internal int BinarySearch(BytesRef b, BytesRef bytesRef, int low, int high, BytesRefHash hash, int[] ords, IComparer comparator) + { + int mid = 0; + while (low <= high) + { + mid = (int)((uint)(low + high) >> 1); + hash.Get(ords[mid], bytesRef); + int cmp = comparator.Compare(bytesRef, b); + if (cmp < 0) + { + low = mid + 1; + } + else if (cmp > 0) + { + high = mid - 1; + } + else + { + return mid; + } + } + Debug.Assert(comparator.Compare(bytesRef, b) != 0); + return -(low + 1); + } + + + public override bool SeekExact(BytesRef text) + { + termUpto = BinarySearch(text, br, 0, info.terms.Size() - 1, info.terms, info.sortedTerms, BytesRef.UTF8SortedAsUnicodeComparer); + return termUpto >= 0; + } + + public override SeekStatus SeekCeil(BytesRef text) + { + termUpto = BinarySearch(text, br, 0, info.terms.Size() - 1, info.terms, info.sortedTerms, BytesRef.UTF8SortedAsUnicodeComparer); + if (termUpto < 0) // not found; choose successor + { + termUpto = -termUpto - 1; + if (termUpto >= info.terms.Size()) + { + return SeekStatus.END; + } + else + { + info.terms.Get(info.sortedTerms[termUpto], br); + return SeekStatus.NOT_FOUND; + } + } + else + { + return SeekStatus.FOUND; + } + } + + public override void SeekExact(long ord) + { + Debug.Assert(ord < info.terms.Size()); + termUpto = (int)ord; + } + + public override BytesRef Next() + { + termUpto++; + if (termUpto >= info.terms.Size()) + { + return null; + } + else + { + info.terms.Get(info.sortedTerms[termUpto], br); + return br; + } + } + + public override BytesRef Term() + { + return br; + } + + public override long Ord() + { + return termUpto; + } + + public override int DocFreq() + { + return 1; + } + + public override long TotalTermFreq() + { + return info.sliceArray.freq[info.sortedTerms[termUpto]]; + } + + public override DocsEnum Docs(Bits liveDocs, DocsEnum reuse, int flags) + { + if (reuse == null || !(reuse is MemoryDocsEnum)) + { + reuse = new MemoryDocsEnum(outerInstance); + } + return ((MemoryDocsEnum)reuse).Reset(liveDocs, info.sliceArray.freq[info.sortedTerms[termUpto]]); + } + + public override DocsAndPositionsEnum DocsAndPositions(Bits liveDocs, DocsAndPositionsEnum reuse, int flags) + { + if (reuse == null || !(reuse is MemoryDocsAndPositionsEnum)) + { + reuse = new MemoryDocsAndPositionsEnum(outerInstance); + } + int ord = info.sortedTerms[termUpto]; + return ((MemoryDocsAndPositionsEnum)reuse).Reset(liveDocs, info.sliceArray.start[ord], info.sliceArray.end[ord], info.sliceArray.freq[ord]); + } + + public override IComparer Comparator + { + get + { + return BytesRef.UTF8SortedAsUnicodeComparer; + } + } + + public override void SeekExact(BytesRef term, TermState state) + { + Debug.Assert(state != null); + this.SeekExact(((OrdTermState)state).Ord); + } + + public override TermState TermState() + { + OrdTermState ts = new OrdTermState(); + ts.Ord = termUpto; + return ts; + } + } + + private class MemoryDocsEnum : DocsEnum + { + private readonly MemoryIndex.MemoryIndexReader outerInstance; + + public MemoryDocsEnum(MemoryIndex.MemoryIndexReader outerInstance) + { + this.outerInstance = outerInstance; + } + + internal bool hasNext; + internal Bits liveDocs; + internal int doc = -1; + internal int freq_Renamed; + + public virtual DocsEnum Reset(Bits liveDocs, int freq) + { + this.liveDocs = liveDocs; + hasNext = true; + doc = -1; + this.freq_Renamed = freq; + return this; + } + + public override int DocID() + { + return doc; + } + + public override int NextDoc() + { + if (hasNext && (liveDocs == null || liveDocs.Get(0))) + { + hasNext = false; + return doc = 0; + } + else + { + return doc = NO_MORE_DOCS; + } + } + + public override int Advance(int target) + { + return SlowAdvance(target); + } + + public override int Freq() + { + return freq_Renamed; + } + + public override long Cost() + { + return 1; + } + } + + private class MemoryDocsAndPositionsEnum : DocsAndPositionsEnum + { + private readonly MemoryIndex.MemoryIndexReader outerInstance; + + internal int posUpto; // for assert + internal bool hasNext; + internal Bits liveDocs; + internal int doc = -1; + internal IntBlockPool.SliceReader sliceReader; + internal int freq_Renamed; + internal int startOffset_Renamed; + internal int endOffset_Renamed; + + public MemoryDocsAndPositionsEnum(MemoryIndex.MemoryIndexReader outerInstance) + { + this.outerInstance = outerInstance; + this.sliceReader = new IntBlockPool.SliceReader(outerInstance.outerInstance.intBlockPool); + } + + public virtual DocsAndPositionsEnum Reset(Bits liveDocs, int start, int end, int freq) + { + this.liveDocs = liveDocs; + this.sliceReader.Reset(start, end); + posUpto = 0; // for assert + hasNext = true; + doc = -1; + this.freq_Renamed = freq; + return this; + } + + + public override int DocID() + { + return doc; + } + + public override int NextDoc() + { + if (hasNext && (liveDocs == null || liveDocs.Get(0))) + { + hasNext = false; + return doc = 0; + } + else + { + return doc = NO_MORE_DOCS; + } + } + + public override int Advance(int target) + { + return SlowAdvance(target); + } + + public override int Freq() + { + return freq_Renamed; + } + + public override int NextPosition() + { + Debug.Assert(posUpto++ < freq_Renamed); + Debug.Assert(!sliceReader.EndOfSlice(), " stores offsets : " + startOffset_Renamed); + if (outerInstance.outerInstance.storeOffsets) + { + int pos = sliceReader.ReadInt(); + startOffset_Renamed = sliceReader.ReadInt(); + endOffset_Renamed = sliceReader.ReadInt(); + return pos; + } + else + { + return sliceReader.ReadInt(); + } + } + + public override int StartOffset() + { + return startOffset_Renamed; + } + + public override int EndOffset() + { + return endOffset_Renamed; + } + + public override BytesRef Payload + { + get + { + return null; + } + } + + public override long Cost() + { + return 1; + } + } + + public override Fields GetTermVectors(int docID) + { + if (docID == 0) + { + return Fields; + } + else + { + return null; + } + } + + internal Similarity Similarity + { + get + { + if (searcher != null) + { + return searcher.Similarity; + } + return IndexSearcher.DefaultSimilarity; + } + } + + internal IndexSearcher Searcher + { + set + { + this.searcher = value; + } + } + + public override int NumDocs + { + get + { +#if DEBUG + Debug.WriteLine("MemoryIndexReader.NumDocs"); +#endif + return 1; + } + } + + public override int MaxDoc + { + get + { +#if DEBUG + Debug.WriteLine("MemoryIndexReader.MaxDoc"); +#endif + return 1; + } + } + + public override void Document(int docID, StoredFieldVisitor visitor) + { +#if DEBUG + Debug.WriteLine("MemoryIndexReader.Document"); +#endif + // no-op: there are no stored fields + } + protected override void DoClose() + { +#if DEBUG + Debug.WriteLine("MemoryIndexReader.DoClose"); +#endif + } + + /// + /// performance hack: cache norms to avoid repeated expensive calculations + internal NumericDocValues cachedNormValues; + internal string cachedFieldName; + internal Similarity cachedSimilarity; + + public override NumericDocValues GetNormValues(string field) + { + FieldInfo fieldInfo; + if (!outerInstance.fieldInfos.TryGetValue(field, out fieldInfo) || fieldInfo.OmitsNorms()) + { + return null; + } + NumericDocValues norms = cachedNormValues; + Similarity sim = Similarity; + if (!field.Equals(cachedFieldName) || sim != cachedSimilarity) // not cached? + { + Info info = GetInfo(field); + int numTokens = info != null ? info.numTokens : 0; + int numOverlapTokens = info != null ? info.numOverlapTokens : 0; + float boost = info != null ? info.Boost : 1.0f; + FieldInvertState invertState = new FieldInvertState(field, 0, numTokens, numOverlapTokens, 0, boost); + long value = sim.ComputeNorm(invertState); + norms = new MemoryIndexNormDocValues(value); + // cache it for future reuse + cachedNormValues = norms; + cachedFieldName = field; + cachedSimilarity = sim; +#if DEBUG + Debug.WriteLine("MemoryIndexReader.norms: " + field + ":" + value + ":" + numTokens); +#endif + } + return norms; + } + } + } +} http://git-wip-us.apache.org/repos/asf/lucenenet/blob/d47b6088/src/Lucene.Net.Memory/MemoryIndex.TermComparer.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Memory/MemoryIndex.TermComparer.cs b/src/Lucene.Net.Memory/MemoryIndex.TermComparer.cs new file mode 100644 index 0000000..39b9e2b --- /dev/null +++ b/src/Lucene.Net.Memory/MemoryIndex.TermComparer.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; + +namespace Lucene.Net.Index.Memory +{ + public partial class MemoryIndex + { + private class TermComparer + { + /// + /// Sorts term entries into ascending order; also works for + /// and + /// . + /// + public static int KeyComparer(KeyValuePair x, KeyValuePair y) + where TKey : class, IComparable + { + if (x.Key == y.Key) return 0; + return typeof(TKey) == typeof(string) + ? string.Compare(x.Key as string, y.Key as string, StringComparison.Ordinal) + : x.Key.CompareTo(y.Key); + } + } + + private sealed class TermComparer : TermComparer, IComparer> + where TKey : class, IComparable + { + public int Compare(KeyValuePair x, KeyValuePair y) + { + return KeyComparer(x, y); + } + } + } +}