2 // LuceneNameResolver.cs
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
;
31 using Lucene
.Net
.Documents
;
32 using Lucene
.Net
.Index
;
33 using LNS
= Lucene
.Net
.Search
;
38 namespace Beagle
.Daemon
.FileSystemQueryable
{
41 // This is just a LuceneQueryingDriver with the ability to do the
42 // special secondary-index-only queries we need to map internal uris
46 public class LuceneNameResolver
: LuceneQueryingDriver
{
48 public class NameInfo
{
52 public bool IsDirectory
;
55 public LuceneNameResolver (string index_name
, int minor_version
, bool read_only
)
56 : base (index_name
, minor_version
, read_only
)
61 ////////////////////////////////////////////////////////////////
64 private NameInfo
DocumentToNameInfo (Document doc
)
67 info
= new NameInfo ();
70 str
= doc
.Get ("Uri");
71 info
.Id
= GuidFu
.FromUriString (str
);
73 bool have_name
= false;
74 bool have_parent_id
= false;
75 bool have_is_dir
= false;
77 foreach (Field f
in doc
.Fields ()) {
79 prop
= GetPropertyFromDocument (f
, doc
, false);
85 case FileSystemQueryable
.ExactFilenamePropKey
:
86 info
.Name
= prop
.Value
;
90 case FileSystemQueryable
.ParentDirUriPropKey
:
91 info
.ParentId
= GuidFu
.FromUriString (prop
.Value
);
92 have_parent_id
= true;
95 case FileSystemQueryable
.IsDirectoryPropKey
:
96 info
.IsDirectory
= (prop
.Value
== "true");
101 if (have_name
&& have_parent_id
&& have_is_dir
)
108 ////////////////////////////////////////////////////////////////
110 // Pull a single record out of the index
112 private class SingletonCollector
: LNS
.HitCollector
{
114 public int MatchId
= -1;
116 public override void Collect (int id
, float score
)
119 throw new Exception ("What you talkin' about Willis?");
125 public NameInfo
GetNameInfoById (Guid id
)
128 uri
= GuidFu
.ToUri (id
);
131 query
= UriQuery ("Uri", uri
);
133 SingletonCollector collector
;
134 collector
= new SingletonCollector ();
136 LNS
.IndexSearcher searcher
;
137 searcher
= new LNS
.IndexSearcher (SecondaryStore
);
138 searcher
.Search (query
, null, collector
);
140 NameInfo info
= null;
142 if (collector
.MatchId
!= -1) {
144 doc
= searcher
.Doc (collector
.MatchId
);
145 info
= DocumentToNameInfo (doc
);
153 ////////////////////////////////////////////////////////////////
155 public Guid
GetIdByNameAndParentId (string name
, Guid parent_id
)
157 string parent_uri_str
;
158 parent_uri_str
= GuidFu
.ToUriString (parent_id
);
161 key1
= PropertyToFieldName (PropertyType
.Keyword
, FileSystemQueryable
.ParentDirUriPropKey
);
164 key2
= PropertyToFieldName (PropertyType
.Keyword
, FileSystemQueryable
.ExactFilenamePropKey
);
167 q1
= new LNS
.TermQuery (new Term (key1
, parent_uri_str
));
170 q2
= new LNS
.TermQuery (new Term (key2
, name
));
172 LNS
.BooleanQuery query
;
173 query
= new LNS
.BooleanQuery ();
174 query
.Add (q1
, true, false);
175 query
.Add (q2
, true, false);
177 SingletonCollector collector
;
178 collector
= new SingletonCollector ();
180 LNS
.IndexSearcher searcher
;
181 searcher
= new LNS
.IndexSearcher (SecondaryStore
);
182 searcher
.Search (query
, null, collector
);
185 if (collector
.MatchId
!= -1) {
187 doc
= searcher
.Doc (collector
.MatchId
);
188 id
= GuidFu
.FromUriString (doc
.Get ("Uri"));
197 ////////////////////////////////////////////////////////////////
199 // Pull all of the directories out of the index and cache them
201 // Not to be confused with LuceneQueryingDriver.BitArrayHitCollector
202 private class BitArrayHitCollector
: LNS
.HitCollector
{
204 private BetterBitArray matches
;
206 public BitArrayHitCollector (BetterBitArray matches
)
208 this.matches
= matches
;
211 public override void Collect (int id
, float score
)
217 public ICollection
GetAllDirectoryNameInfo ()
219 // First we assemble a query to find all of the directories.
221 field_name
= PropertyToFieldName (PropertyType
.Keyword
,
222 FileSystemQueryable
.IsDirectoryPropKey
);
225 query
= new LNS
.TermQuery (new Term (field_name
, "true"));
227 // Then we actually run the query
228 LNS
.IndexSearcher searcher
;
229 searcher
= new LNS
.IndexSearcher (SecondaryStore
);
231 BetterBitArray matches
;
232 matches
= new BetterBitArray (searcher
.MaxDoc ());
234 BitArrayHitCollector collector
;
235 collector
= new BitArrayHitCollector (matches
);
237 searcher
.Search (query
, null, collector
);
239 // Finally we pull all of the matching documents,
240 // convert them to NameInfo, and store them in a list.
242 ArrayList match_list
= new ArrayList ();
244 while (i
< matches
.Count
) {
246 i
= matches
.GetNextTrueIndex (i
);
247 if (i
>= matches
.Count
)
251 doc
= searcher
.Doc (i
);
254 info
= DocumentToNameInfo (doc
);
256 match_list
.Add (info
);