lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From synhers...@apache.org
Subject svn commit: r1375240 - in /incubator/lucene.net/trunk/src/contrib/Spatial: ./ Prefix/ Util/
Date Mon, 20 Aug 2012 21:41:26 GMT
Author: synhershko
Date: Mon Aug 20 21:41:25 2012
New Revision: 1375240

URL: http://svn.apache.org/viewvc?rev=1375240&view=rev
Log:
Adding TermsFilter (untested)

Added:
    incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsFilter.cs
Modified:
    incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj
    incubator/lucene.net/trunk/src/contrib/Spatial/Prefix/TermQueryPrefixTreeStrategy.cs
    incubator/lucene.net/trunk/src/contrib/Spatial/Util/Bits.cs
    incubator/lucene.net/trunk/src/contrib/Spatial/Util/CompatibilityExtensions.cs
    incubator/lucene.net/trunk/src/contrib/Spatial/Util/FixedBitSet.cs
    incubator/lucene.net/trunk/src/contrib/Spatial/Util/OpenBitSetIterator.cs
    incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsEnumCompatibility.cs

Modified: incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj?rev=1375240&r1=1375239&r2=1375240&view=diff
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj (original)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/Contrib.Spatial.csproj Mon Aug 20 21:41:25
2012
@@ -155,6 +155,7 @@
     <Compile Include="Util\ShapeFieldCacheProvider.cs" />
     <Compile Include="Util\StringListTokenizer.cs" />
     <Compile Include="Util\TermsEnumCompatibility.cs" />
+    <Compile Include="Util\TermsFilter.cs" />
     <Compile Include="Util\TruncateFilter.cs" />
     <Compile Include="Util\ValueSourceFilter.cs" />
     <Compile Include="Vector\DistanceValueSource.cs" />

Modified: incubator/lucene.net/trunk/src/contrib/Spatial/Prefix/TermQueryPrefixTreeStrategy.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/Prefix/TermQueryPrefixTreeStrategy.cs?rev=1375240&r1=1375239&r2=1375240&view=diff
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/Prefix/TermQueryPrefixTreeStrategy.cs (original)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/Prefix/TermQueryPrefixTreeStrategy.cs Mon
Aug 20 21:41:25 2012
@@ -18,6 +18,7 @@
 using Lucene.Net.Index;
 using Lucene.Net.Search;
 using Lucene.Net.Spatial.Prefix.Tree;
+using Lucene.Net.Spatial.Util;
 using Spatial4n.Core.Exceptions;
 using Spatial4n.Core.Query;
 using Spatial4n.Core.Shapes;

Modified: incubator/lucene.net/trunk/src/contrib/Spatial/Util/Bits.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/Util/Bits.cs?rev=1375240&r1=1375239&r2=1375240&view=diff
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/Util/Bits.cs (original)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/Util/Bits.cs Mon Aug 20 21:41:25 2012
@@ -20,57 +20,73 @@ namespace Lucene.Net.Spatial.Util
 	/// <summary>
 	/// Interface for Bitset-like structures.
 	/// </summary>
-	public abstract class Bits
+	public interface IBits
 	{
-		public abstract bool Get(int index);
-		public abstract int Length();
+		bool Get(int index);
+		int Length();
+	}
 
+	/// <summary>
+	/// Empty implementation, basically just so we can provide EMPTY_ARRAY
+	/// </summary>
+	public abstract class Bits : IBits
+	{
 		public static readonly Bits[] EMPTY_ARRAY = new Bits[0];
 
-		/// <summary>
-		/// Bits impl of the specified length with all bits set.
-		/// </summary>
-		public class MatchAllBits : Bits
-		{
-			readonly int len;
-
-			public MatchAllBits(int len)
-			{
-				this.len = len;
-			}
-
-			public override bool Get(int index)
-			{
-				return true;
-			}
-
-			public override int Length()
-			{
-				return len;
-			}
-		}
-
-		/// <summary>
-		/// Bits impl of the specified length with no bits set. 
-		/// </summary>
-		public class MatchNoBits : Bits
-		{
-			readonly int len;
-
-			public MatchNoBits(int len)
-			{
-				this.len = len;
-			}
-
-			public override bool Get(int index)
-			{
-				return false;
-			}
-
-			public override int Length()
-			{
-				return len;
-			}
+		public virtual bool Get(int index)
+		{
+			throw new System.NotImplementedException();
+		}
+
+		public virtual int Length()
+		{
+			throw new System.NotImplementedException();
+		}
+	}
+
+	/// <summary>
+	/// Bits impl of the specified length with all bits set.
+	/// </summary>
+	public class MatchAllBits : Bits
+	{
+		private readonly int len;
+
+		public MatchAllBits(int len)
+		{
+			this.len = len;
+		}
+
+		public override bool Get(int index)
+		{
+			return true;
+		}
+
+		public override int Length()
+		{
+			return len;
+		}
+	}
+
+	/// <summary>
+	/// Bits impl of the specified length with no bits set. 
+	/// </summary>
+	public class MatchNoBits : Bits
+	{
+		private readonly int len;
+
+		public MatchNoBits(int len)
+		{
+			this.len = len;
+		}
+
+		public override bool Get(int index)
+		{
+			return false;
+		}
+
+		public override int Length()
+		{
+			return len;
 		}
 	}
 }
\ No newline at end of file

Modified: incubator/lucene.net/trunk/src/contrib/Spatial/Util/CompatibilityExtensions.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/Util/CompatibilityExtensions.cs?rev=1375240&r1=1375239&r2=1375240&view=diff
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/Util/CompatibilityExtensions.cs (original)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/Util/CompatibilityExtensions.cs Mon Aug
20 21:41:25 2012
@@ -40,14 +40,14 @@ namespace Lucene.Net.Spatial.Util
 			termAtt.SetTermBuffer(termAtt.Term + new string(new[] { ch })); // TODO: Not optimal,
but works
 		}
 
-		private static readonly ConcurrentDictionary<string, Bits> _docsWithFieldCache =
new ConcurrentDictionary<string, Bits>();
+		private static readonly ConcurrentDictionary<string, IBits> _docsWithFieldCache =
new ConcurrentDictionary<string, IBits>();
 
-		internal static Bits GetDocsWithField(this FieldCache fc, IndexReader reader, String field)
+		internal static IBits GetDocsWithField(this FieldCache fc, IndexReader reader, String field)
 		{
 			return _docsWithFieldCache.GetOrAdd(field, f => DocsWithFieldCacheEntry_CreateValue(reader,
new Entry(field, null), false));
 		}
 
-		private static Bits DocsWithFieldCacheEntry_CreateValue(IndexReader reader, Entry entryKey,
bool setDocsWithField /* ignored */)
+		private static IBits DocsWithFieldCacheEntry_CreateValue(IndexReader reader, Entry entryKey,
bool setDocsWithField /* ignored */)
 		{
 			var field = entryKey.field;
 			FixedBitSet res = null;
@@ -62,7 +62,7 @@ namespace Lucene.Net.Spatial.Util
 				if (termsDocCount == maxDoc)
 				{
 					// Fast case: all docs have this field:
-					return new Bits.MatchAllBits(maxDoc);
+					return new MatchAllBits(maxDoc);
 				}
 
 				while (true)
@@ -88,14 +88,14 @@ namespace Lucene.Net.Spatial.Util
 			}
 			if (res == null)
 			{
-				return new Bits.MatchNoBits(maxDoc);
+				return new MatchNoBits(maxDoc);
 			}
 			int numSet = res.Cardinality();
 			if (numSet >= maxDoc)
 			{
 				// The cardinality of the BitSet is maxDoc if all documents have a value.
 				Debug.Assert(numSet == maxDoc);
-				return new Bits.MatchAllBits(maxDoc);
+				return new MatchAllBits(maxDoc);
 			}
 			return res;
 		}
@@ -112,7 +112,7 @@ namespace Lucene.Net.Spatial.Util
 		{
 			int n = 0;
 			// do the first step as a long
-			int y = (int)((ulong)x >> 32);
+			var y = (int)((ulong)x >> 32);
 			if (y == 0) { n += 32; y = (int)(x); }
 			if ((y & 0xFFFF0000) == 0) { n += 16; y <<= 16; }
 			if ((y & 0xFF000000) == 0) { n += 8; y <<= 8; }

Modified: incubator/lucene.net/trunk/src/contrib/Spatial/Util/FixedBitSet.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/Util/FixedBitSet.cs?rev=1375240&r1=1375239&r2=1375240&view=diff
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/Util/FixedBitSet.cs (original)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/Util/FixedBitSet.cs Mon Aug 20 21:41:25
2012
@@ -17,8 +17,6 @@
 
 using System;
 using System.Collections;
-using System.Diagnostics;
-using System.Linq;
 using Lucene.Net.Search;
 using Lucene.Net.Util;
 
@@ -33,7 +31,7 @@ namespace Lucene.Net.Spatial.Util
  *
  * @lucene.internal
  **/
-	public class FixedBitSet : Bits
+	public class FixedBitSet : DocIdSet, IBits
 	{
 		private readonly BitArray bits;
 
@@ -66,19 +64,19 @@ namespace Lucene.Net.Spatial.Util
 			bits = new BitArray(other.bits);
 		}
 
-		public Bits Bits()
+		public IBits Bits()
 		{
 			return this;
 		}
 
-		public override int Length()
+		public int Length()
 		{
 			return bits.Length;
 		}
 
-		public bool IsCacheable()
+		public override bool IsCacheable
 		{
-			return true;
+			get { return true; }
 		}
 
 		/// <summary>
@@ -97,7 +95,7 @@ namespace Lucene.Net.Spatial.Util
 			return ret;
 		}
 
-		public override bool Get(int index)
+		public bool Get(int index)
 		{
 			return bits[index];
 		}
@@ -410,5 +408,12 @@ namespace Lucene.Net.Spatial.Util
 			return bits.GetHashCode();
 		}
 
+		public override DocIdSetIterator Iterator()
+		{
+			// TODO: avoid copying, create a FixedBitSetIterator instead
+			var arr = new long[bits.Count];
+			bits.CopyTo(arr, 0);
+			return new OpenBitSetIterator(arr, bits.Length);
+		}
 	}
 }

Modified: incubator/lucene.net/trunk/src/contrib/Spatial/Util/OpenBitSetIterator.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/Util/OpenBitSetIterator.cs?rev=1375240&r1=1375239&r2=1375240&view=diff
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/Util/OpenBitSetIterator.cs (original)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/Util/OpenBitSetIterator.cs Mon Aug 20 21:41:25
2012
@@ -15,7 +15,6 @@
  * limitations under the License.
  */
 
-using System;
 using Lucene.Net.Search;
 using Lucene.Net.Util;
 

Modified: incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsEnumCompatibility.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsEnumCompatibility.cs?rev=1375240&r1=1375239&r2=1375240&view=diff
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsEnumCompatibility.cs (original)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsEnumCompatibility.cs Mon Aug
20 21:41:25 2012
@@ -127,5 +127,14 @@ namespace Lucene.Net.Spatial.Util
 				bits.FastSet(termDocs.Doc);
 			}
 		}
+
+		public void Docs(FixedBitSet bits)
+		{
+			var termDocs = reader.TermDocs(new Term(fieldName, Term().Text));
+			while (termDocs.Next())
+			{
+				bits.Set(termDocs.Doc);
+			}
+		}
 	}
 }

Added: incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsFilter.cs
URL: http://svn.apache.org/viewvc/incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsFilter.cs?rev=1375240&view=auto
==============================================================================
--- incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsFilter.cs (added)
+++ incubator/lucene.net/trunk/src/contrib/Spatial/Util/TermsFilter.cs Mon Aug 20 21:41:25
2012
@@ -0,0 +1,84 @@
+/*
+ * 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 System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using Lucene.Net.Index;
+using Lucene.Net.Search;
+
+namespace Lucene.Net.Spatial.Util
+{
+	/// <summary>
+	/// Constructs a filter for docs matching any of the terms added to this class.
+	/// Unlike a RangeFilter this can be used for filtering on multiple terms that are not necessarily
in
+	/// a sequence. An example might be a collection of primary keys from a database query result
or perhaps
+	/// a choice of "category" labels picked by the end user. As a filter, this is much faster
than the
+	/// equivalent query (a BooleanQuery with many "should" TermQueries)
+	/// </summary>
+	public class TermsFilter : Filter
+	{
+		private readonly SortedSet<Term> terms = new SortedSet<Term>();
+
+		/// <summary>
+		/// Adds a term to the list of acceptable terms
+		/// </summary>
+		/// <param name="term"></param>
+		public void AddTerm(Term term)
+		{
+			terms.Add(term);
+		}
+
+		public override DocIdSet GetDocIdSet(IndexReader reader)
+		{
+			var result = new FixedBitSet(reader.MaxDoc);
+			var fields = reader.GetFieldNames(IndexReader.FieldOption.ALL);
+
+			if (fields == null || fields.Count == 0)
+			{
+				return result;
+			}
+
+			String lastField = null;
+			TermsEnumCompatibility termsEnum = null;
+			foreach (Term term in terms)
+			{
+				if (!term.Field.Equals(lastField))
+				{
+					var termsC = new TermsEnumCompatibility(reader, term.Field);
+					if (termsC.Term() == null)
+					{
+						return result;
+					}
+					termsEnum = termsC;
+					lastField = term.Field;
+				}
+
+				if (terms != null)
+				{
+					// TODO this check doesn't make sense, decide which variable its supposed to be for
+					Debug.Assert(termsEnum != null);
+					if (termsEnum.SeekCeil(term.Text) == TermsEnumCompatibility.SeekStatus.FOUND)
+					{
+						termsEnum.Docs(result);
+					}
+				}
+			}
+			return result;
+		}
+	}
+}



Mime
View raw message