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 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 ();
73 public LuceneBitArray (LNS
.IndexSearcher searcher
,
74 LNS
.Query query
) : this (searcher
)
79 private void UseScratch ()
82 scratch
= new BetterBitArray (searcher
.MaxDoc ());
84 scratch
.SetAll (false);
85 collector
.Array
= scratch
;
88 public LuceneBitArray
Search (LNS
.Query query
)
95 public LuceneBitArray
And (LNS
.Query query
)
98 searcher
.Search (query
, null, collector
);
103 public LuceneBitArray
AndNot (LNS
.Query query
)
106 searcher
.Search (query
, null, collector
);
107 this.AndNot (scratch
);
111 public LuceneBitArray
Or (LNS
.Query query
)
113 collector
.Array
= this;
114 searcher
.Search (query
, null, collector
);
118 public LuceneBitArray
Xor (LNS
.Query query
)
121 searcher
.Search (query
, null, collector
);
126 ////////////////////////////////////////////////////////////
128 public void AddUri (Uri uri
)
130 AddUri (UriFu
.UriToSerializableString (uri
));
133 public void AddUri (string str
)
136 term
= new Term ("Uri", str
);
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
)
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
)
164 while (j
< this.Count
) {
166 i
= this.GetNextTrueIndex (j
);
172 doc
= searcher
.Doc (i
);
174 other
.AddUri (doc
.Get ("Uri"));
180 public void Join (LuceneBitArray other
)
182 LuceneBitArray image
;
183 image
= new LuceneBitArray (other
.searcher
);
184 this.ProjectOnto (image
);
188 // We only need to project back items in the other
189 // bit array that are not in the image of the
193 image
.ProjectOnto (this);