Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / FileSystemQueryable / LuceneNameResolver.cs
blob6f8f22e24ed015ca706a8675cfa612e33bc2164d
1 //
2 // LuceneNameResolver.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;
29 using System.IO;
31 using Lucene.Net.Documents;
32 using Lucene.Net.Index;
33 using LNS = Lucene.Net.Search;
35 using Beagle.Daemon;
36 using Beagle.Util;
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
43 // back to filenames.
46 public class LuceneNameResolver : LuceneQueryingDriver {
48 public class NameInfo {
49 public Guid Id;
50 public Guid ParentId;
51 public string Name;
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)
66 NameInfo info;
67 info = new NameInfo ();
69 string str;
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 ()) {
78 Property prop;
79 prop = GetPropertyFromDocument (f, doc, false);
80 if (prop == null)
81 continue;
83 switch (prop.Key) {
85 case FileSystemQueryable.ExactFilenamePropKey:
86 info.Name = prop.Value;
87 have_name = true;
88 break;
90 case FileSystemQueryable.ParentDirUriPropKey:
91 info.ParentId = GuidFu.FromUriString (prop.Value);
92 have_parent_id = true;
93 break;
95 case FileSystemQueryable.IsDirectoryPropKey:
96 info.IsDirectory = (prop.Value == "true");
97 have_is_dir = true;
98 break;
101 if (have_name && have_parent_id && have_is_dir)
102 break;
105 return info;
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)
118 if (MatchId != -1)
119 throw new Exception ("What you talkin' about Willis?");
121 MatchId = id;
125 public NameInfo GetNameInfoById (Guid id)
127 Uri uri;
128 uri = GuidFu.ToUri (id);
130 LNS.Query query;
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) {
143 Document doc;
144 doc = searcher.Doc (collector.MatchId);
145 info = DocumentToNameInfo (doc);
148 searcher.Close ();
150 return info;
153 ////////////////////////////////////////////////////////////////
155 public Guid GetIdByNameAndParentId (string name, Guid parent_id)
157 string parent_uri_str;
158 parent_uri_str = GuidFu.ToUriString (parent_id);
160 string key1;
161 key1 = PropertyToFieldName (PropertyType.Keyword, FileSystemQueryable.ParentDirUriPropKey);
163 string key2;
164 key2 = PropertyToFieldName (PropertyType.Keyword, FileSystemQueryable.ExactFilenamePropKey);
166 LNS.Query q1;
167 q1 = new LNS.TermQuery (new Term (key1, parent_uri_str));
169 LNS.Query q2;
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);
184 Guid id;
185 if (collector.MatchId != -1) {
186 Document doc;
187 doc = searcher.Doc (collector.MatchId);
188 id = GuidFu.FromUriString (doc.Get ("Uri"));
189 } else
190 id = Guid.Empty;
192 searcher.Close ();
194 return id;
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)
213 matches [id] = true;
217 public ICollection GetAllDirectoryNameInfo ()
219 // First we assemble a query to find all of the directories.
220 string field_name;
221 field_name = PropertyToFieldName (PropertyType.Keyword,
222 FileSystemQueryable.IsDirectoryPropKey);
224 LNS.Query query;
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 ();
243 int i = 0;
244 while (i < matches.Count) {
246 i = matches.GetNextTrueIndex (i);
247 if (i >= matches.Count)
248 break;
250 Document doc;
251 doc = searcher.Doc (i);
253 NameInfo info;
254 info = DocumentToNameInfo (doc);
256 match_list.Add (info);
258 ++i;
261 return match_list;