Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
TermsFilter.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections.Generic;
20 using System.Diagnostics;
21 using Lucene.Net.Index;
22 using Lucene.Net.Search;
23 
24 namespace Lucene.Net.Spatial.Util
25 {
33  public class TermsFilter : Filter
34  {
35  private readonly SortedSet<Term> terms = new SortedSet<Term>();
36 
41  public void AddTerm(Term term)
42  {
43  terms.Add(term);
44  }
45 
46  public override DocIdSet GetDocIdSet(IndexReader reader)
47  {
48  var result = new FixedBitSet(reader.MaxDoc);
49  var fields = reader.GetFieldNames(IndexReader.FieldOption.ALL);
50 
51  if (fields == null || fields.Count == 0)
52  {
53  return result;
54  }
55 
56  String lastField = null;
57  TermsEnumCompatibility termsEnum = null;
58  foreach (Term term in terms)
59  {
60  if (!term.Field.Equals(lastField))
61  {
62  var termsC = new TermsEnumCompatibility(reader, term.Field);
63  if (termsC.Term() == null)
64  {
65  return result;
66  }
67  termsEnum = termsC;
68  lastField = term.Field;
69  }
70 
71  if (terms != null)
72  {
73  // TODO this check doesn't make sense, decide which variable its supposed to be for
74  Debug.Assert(termsEnum != null);
75  if (termsEnum.SeekCeil(term.Text) == TermsEnumCompatibility.SeekStatus.FOUND)
76  {
77  termsEnum.Docs(result);
78  }
79  }
80  }
81  return result;
82  }
83 
84  public override bool Equals(object obj)
85  {
86  if (this == obj)
87  return true;
88 
89  if ((obj == null) || (obj.GetType() != this.GetType()))
90  return false;
91 
92  var test = (TermsFilter)obj;
93  if (terms == test.terms)
94  return true;
95  if (terms == null || terms.Count != test.terms.Count)
96  return false;
97 
98  var e1 = terms.GetEnumerator();
99  var e2 = test.terms.GetEnumerator();
100  while (e1.MoveNext() && e2.MoveNext())
101  {
102  if (!e1.Current.Equals(e2.Current)) return false;
103  }
104  return true;
105  }
106 
107  public override int GetHashCode()
108  {
109  int hash = 9;
110  foreach (Term term in terms)
111  {
112  hash = 31 * hash + term.GetHashCode();
113  }
114  return hash;
115  }
116  }
117 }