* tools/Info.cs: Add --list-backends, --list-static-indexes to
[beagle.git] / beagled / LuceneBitArray.cs
blobd0fa3957d82e2e81fc31266ad6e7bebde5ad238c
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 LNS.IndexSearcher searcher;
54 BitArrayHitCollector collector;
55 BetterBitArray scratch;
57 LNS.BooleanQuery pending_uri_query = null;
58 int pending_clause_count = 0;
59 static int max_clause_count;
61 static LuceneBitArray ()
63 max_clause_count = LNS.BooleanQuery.GetMaxClauseCount ();
66 public LuceneBitArray (LNS.IndexSearcher searcher) : base (searcher.MaxDoc ())
68 this.searcher = searcher;
69 this.collector = new BitArrayHitCollector ();
70 this.scratch = null;
73 public LuceneBitArray (LNS.IndexSearcher searcher,
74 LNS.Query query) : this (searcher)
76 this.Or (query);
79 private void UseScratch ()
81 if (scratch == null)
82 scratch = new BetterBitArray (searcher.MaxDoc ());
83 else
84 scratch.SetAll (false);
85 collector.Array = scratch;
88 public LuceneBitArray Search (LNS.Query query)
90 this.SetAll (false);
91 this.Or (query);
92 return this;
95 public LuceneBitArray And (LNS.Query query)
97 UseScratch ();
98 searcher.Search (query, null, collector);
99 this.And (scratch);
100 return this;
103 public LuceneBitArray AndNot (LNS.Query query)
105 UseScratch ();
106 searcher.Search (query, null, collector);
107 this.AndNot (scratch);
108 return this;
111 public LuceneBitArray Or (LNS.Query query)
113 collector.Array = this;
114 searcher.Search (query, null, collector);
115 return this;
118 public LuceneBitArray Xor (LNS.Query query)
120 UseScratch ();
121 searcher.Search (query, null, collector);
122 this.Xor (scratch);
123 return this;
126 ////////////////////////////////////////////////////////////
128 public void AddUri (Uri uri)
130 AddUri (UriFu.UriToSerializableString (uri));
133 public void AddUri (string str)
135 Term term;
136 term = new Term ("Uri", str);
138 LNS.TermQuery q;
139 q = new LNS.TermQuery (term);
141 if (pending_uri_query == null)
142 pending_uri_query = new LNS.BooleanQuery ();
143 pending_uri_query.Add (q, false, false);
144 ++pending_clause_count;
146 if (pending_clause_count == max_clause_count)
147 FlushUris ();
150 public void FlushUris ()
152 if (pending_uri_query != null) {
153 this.Or (pending_uri_query);
154 pending_uri_query = null;
155 pending_clause_count = 0;
159 ////////////////////////////////////////////////////////////
161 public void ProjectOnto (LuceneBitArray other)
163 int j = 0;
164 while (j < this.Count) {
165 int i;
166 i = this.GetNextTrueIndex (j);
167 if (i >= this.Count)
168 break;
169 j = i+1;
171 Document doc;
172 doc = searcher.Doc (i);
174 other.AddUri (doc.Get ("Uri"));
177 other.FlushUris ();
180 public void Join (LuceneBitArray other)
182 LuceneBitArray image;
183 image = new LuceneBitArray (other.searcher);
184 this.ProjectOnto (image);
186 other.Or (image);
188 // We only need to project back items in the other
189 // bit array that are not in the image of the
190 // first projection.
191 image.Not ();
192 image.And (other);
193 image.ProjectOnto (this);