* KNotesQueryable.cs: Dont re-index all the notes when the notes file changes. Since...
[beagle.git] / beagled / LuceneBitArray.cs
blob377a9f78e985c8f4d2b30cdf679bb9884aafcab8
1 //
2 // LuceneBitArray.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Collections;
30 using Lucene.Net.Analysis;
31 using Lucene.Net.Analysis.Standard;
32 using Lucene.Net.Documents;
33 using Lucene.Net.Index;
34 using Lucene.Net.QueryParsers;
35 using LNS = Lucene.Net.Search;
37 using Beagle.Util;
39 namespace Beagle.Daemon {
41 public class LuceneBitArray : BetterBitArray {
43 private class BitArrayHitCollector : LNS.HitCollector {
45 public BetterBitArray Array;
47 public override void Collect (int id, float score)
49 this.Array.Set (id, true);
53 private static bool Debug = false;
55 LNS.IndexSearcher searcher;
56 BitArrayHitCollector collector;
57 BetterBitArray scratch;
59 LNS.BooleanQuery pending_uri_query = null;
60 int pending_clause_count = 0;
61 static int max_clause_count;
63 static LuceneBitArray ()
65 max_clause_count = LNS.BooleanQuery.GetMaxClauseCount ();
68 public LuceneBitArray (LNS.IndexSearcher searcher) : base (searcher.MaxDoc ())
70 this.searcher = searcher;
71 this.collector = new BitArrayHitCollector ();
72 this.scratch = null;
75 public LuceneBitArray (LNS.IndexSearcher searcher,
76 LNS.Query query) : this (searcher)
78 this.Or (query);
81 private void UseScratch ()
83 if (scratch == null)
84 scratch = new BetterBitArray (searcher.MaxDoc ());
85 else
86 scratch.SetAll (false);
87 collector.Array = scratch;
90 public LuceneBitArray Search (LNS.Query query)
92 this.SetAll (false);
93 this.Or (query);
94 return this;
97 public LuceneBitArray And (LNS.Query query)
99 UseScratch ();
100 searcher.Search (query, null, collector);
101 if (Debug)
102 Explain (query);
103 this.And (scratch);
104 return this;
107 public LuceneBitArray AndNot (LNS.Query query)
109 UseScratch ();
110 searcher.Search (query, null, collector);
111 if (Debug)
112 Explain (query);
113 this.AndNot (scratch);
114 return this;
117 public LuceneBitArray Or (LNS.Query query)
119 collector.Array = this;
120 searcher.Search (query, null, collector);
121 if (Debug)
122 Explain (query);
123 return this;
126 public LuceneBitArray Xor (LNS.Query query)
128 UseScratch ();
129 searcher.Search (query, null, collector);
130 if (Debug)
131 Explain (query);
132 this.Xor (scratch);
133 return this;
136 private void Explain (LNS.Query query)
138 int j = 0;
139 while (j < collector.Array.Count) {
140 int i;
141 i = collector.Array.GetNextTrueIndex (j);
142 if (i >= collector.Array.Count)
143 break;
144 j = i + 1;
146 Document doc = searcher.Doc (i);
147 LNS.Explanation exp = searcher.Explain (query, i);
149 Log.Debug ("Query: [{0}]", query);
150 Log.Debug ("Matching URI: {0}", doc.Get ("Uri"));
151 Log.Debug ("Explanation: {0}", exp);
155 ////////////////////////////////////////////////////////////
157 public void AddUri (Uri uri)
159 AddUri (UriFu.UriToEscapedString (uri));
162 public void AddUri (string str)
164 Term term;
165 term = new Term ("Uri", str);
167 LNS.TermQuery q;
168 q = new LNS.TermQuery (term);
170 if (pending_uri_query == null)
171 pending_uri_query = new LNS.BooleanQuery ();
172 pending_uri_query.Add (q, false, false);
173 ++pending_clause_count;
175 if (pending_clause_count == max_clause_count)
176 FlushUris ();
179 public void FlushUris ()
181 if (pending_uri_query != null) {
182 this.Or (pending_uri_query);
183 pending_uri_query = null;
184 pending_clause_count = 0;
188 ////////////////////////////////////////////////////////////
190 public void ProjectOnto (LuceneBitArray other)
192 int j = 0;
193 while (j < this.Count) {
194 int i;
195 i = this.GetNextTrueIndex (j);
196 if (i >= this.Count)
197 break;
198 j = i+1;
200 Document doc;
201 doc = searcher.Doc (i);
203 other.AddUri (doc.Get ("Uri"));
206 other.FlushUris ();
209 public void Join (LuceneBitArray other)
211 LuceneBitArray image;
212 image = new LuceneBitArray (other.searcher);
213 this.ProjectOnto (image);
215 other.Or (image);
217 // We only need to project back items in the other
218 // bit array that are not in the image of the
219 // first projection.
220 image.Not ();
221 image.And (other);
222 image.ProjectOnto (this);