4 // Copyright (C) 2005 Novell, Inc.
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.
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
;
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 ();
75 public LuceneBitArray (LNS
.IndexSearcher searcher
,
76 LNS
.Query query
) : this (searcher
)
81 private void UseScratch ()
84 scratch
= new BetterBitArray (searcher
.MaxDoc ());
86 scratch
.SetAll (false);
87 collector
.Array
= scratch
;
90 public LuceneBitArray
Search (LNS
.Query query
)
97 public LuceneBitArray
And (LNS
.Query query
)
100 searcher
.Search (query
, null, collector
);
107 public LuceneBitArray
AndNot (LNS
.Query query
)
110 searcher
.Search (query
, null, collector
);
113 this.AndNot (scratch
);
117 public LuceneBitArray
Or (LNS
.Query query
)
119 collector
.Array
= this;
120 searcher
.Search (query
, null, collector
);
126 public LuceneBitArray
Xor (LNS
.Query query
)
129 searcher
.Search (query
, null, collector
);
136 private void Explain (LNS
.Query query
)
139 while (j
< collector
.Array
.Count
) {
141 i
= collector
.Array
.GetNextTrueIndex (j
);
142 if (i
>= collector
.Array
.Count
)
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
)
165 term
= new Term ("Uri", str
);
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
)
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
)
193 while (j
< this.Count
) {
195 i
= this.GetNextTrueIndex (j
);
201 doc
= searcher
.Doc (i
);
203 other
.AddUri (doc
.Get ("Uri"));
209 public void Join (LuceneBitArray other
)
211 LuceneBitArray image
;
212 image
= new LuceneBitArray (other
.searcher
);
213 this.ProjectOnto (image
);
217 // We only need to project back items in the other
218 // bit array that are not in the image of the
222 image
.ProjectOnto (this);