lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject [41/50] [abbrv] lucenenet git commit: Ported Lucene.Net.Memory + tests
Date Sun, 02 Oct 2016 14:35:59 GMT
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 <shad@shadstorhaug.com>
Authored: Mon Sep 19 11:46:41 2016 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
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<KeyValuePair<TKey, TValue>> SetOfKeyValuePairs<TKey,
TValue>(this IDictionary<TKey, TValue> dictionary)
-	{
-		var entries = new HashSet<KeyValuePair<TKey, TValue>>();
-		foreach (KeyValuePair<TKey, TValue> keyValuePair in dictionary)
-		{
-			entries.Add(keyValuePair);
-		}
-		return entries;
-	}
-
-	internal static TValue GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue>
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 @@
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>Lucene.Net.Memory</RootNamespace>
     <AssemblyName>Lucene.Net.Memory</AssemblyName>
-    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
+    <TargetFrameworkProfile />
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
     <OutputPath>bin\Debug\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <DefineConstants>TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <Prefer32Bit>false</Prefer32Bit>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
@@ -28,6 +30,7 @@
     <DefineConstants>TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <Prefer32Bit>false</Prefer32Bit>
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
@@ -39,8 +42,10 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="HashMapHelperClass.cs" />
     <Compile Include="MemoryIndex.cs" />
+    <Compile Include="MemoryIndex.Info.cs" />
+    <Compile Include="MemoryIndex.MemoryIndexReader.cs" />
+    <Compile Include="MemoryIndex.TermComparer.cs" />
     <Compile Include="MemoryIndexNormDocValues.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>

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
+    {
+        /// <summary>
+        /// Index data structure for a field; Contains the tokenized term texts and
+        /// their positions.
+        /// </summary>
+        private sealed class Info
+        {
+            /// <summary>
+            /// Term strings and their positions for this field: <see cref="System.Collections.Generic.IDictionary{String,
ArrayIntList}"/>
+            /// </summary>
+            internal readonly BytesRefHash terms;
+
+            internal readonly SliceByteStartArray sliceArray;
+
+            /// <summary>
+            /// Terms sorted ascending by term text; computed on demand 
+            /// </summary>
+            [NonSerialized]
+            internal int[] sortedTerms;
+
+            /// <summary>
+            /// Number of added tokens for this field 
+            /// </summary>
+            internal readonly int numTokens;
+
+            /// <summary>
+            /// Number of overlapping tokens for this field 
+            /// </summary>
+            internal readonly int numOverlapTokens;
+
+            /// <summary>
+            /// Boost factor for hits for this field 
+            /// </summary>
+            internal readonly float boost;
+
+            internal readonly long sumTotalTermFreq;
+
+            /// <summary>
+            /// the last position encountered in this field for multi field support 
+            /// </summary>
+            internal int lastPosition;
+
+            /// <summary>
+            /// the last offset encountered in this field for multi field support 
+            /// </summary>
+            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;
+                }
+            }
+
+            /// <summary>
+            /// 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).
+            /// </summary>
+            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:
+        ///////////////////////////////////////////////////////////////////////////////
+
+        /// <summary>
+        /// Search support for Lucene framework integration; implements all methods
+        /// required by the Lucene IndexReader contracts.
+        /// </summary>
+        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<string> GetEnumerator()
+                {
+                    return new IteratorAnonymousInnerClassHelper(this);
+                }
+
+                private class IteratorAnonymousInnerClassHelper : IEnumerator<string>
+                {
+                    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<string, Info>(field, null);
+                    int i = Array.BinarySearch(outerInstance.outerInstance.sortedFields,
searchField, new TermComparer<string, Info>());
+                    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<BytesRef> 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<BytesRef> 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<BytesRef> 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
+            }
+
+            /// <summary>
+            /// performance hack: cache norms to avoid repeated expensive calculations </summary>
+            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
+        {
+            /// <summary>
+            /// Sorts term entries into ascending order; also works for
+            /// <see cref="Array.BinarySearch{T}(T[], T, IComparer{T})"/> and 
+            /// <see cref="Array.Sort{T}(T[], IComparer{T})"/>.
+            /// </summary>
+            public static int KeyComparer<TKey, TValue>(KeyValuePair<TKey, TValue>
x, KeyValuePair<TKey, TValue> y)
+                where TKey : class, IComparable<TKey>
+            {
+                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<TKey, TValue> : TermComparer, IComparer<KeyValuePair<TKey,
TValue>>
+            where TKey : class, IComparable<TKey>
+        {
+            public int Compare(KeyValuePair<TKey, TValue> x, KeyValuePair<TKey,
TValue> y)
+            {
+                return KeyComparer(x, y);
+            }
+        }
+    }
+}


Mime
View raw message