4 // Copyright (C) 2004 Novell, Inc.
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // 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 FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 using System
.Collections
;
36 using Hit
= Beagle
.Hit
;
38 using Lucene
.Net
.Index
;
39 using Lucene
.Net
.Search
;
40 using Lucene
.Net
.Documents
;
44 public class HitByUriComparer
: IComparer
{
46 public int Compare (object a
, object b
)
48 // All of this mapping to and from strings is dreadful.
49 return String
.Compare (((Hit
) a
).Uri
.ToString (), ((Hit
) b
).Uri
.ToString ());
53 static string RemapUriToPath (Hashtable all_hits_by_uri
, Hit hit
)
55 string exact_name
= hit
.GetFirstProperty ("beagle:ExactFilename");
56 string parent_uri_str
= hit
.GetFirstProperty ("_private:ParentDirUri");
58 if (parent_uri_str
== null)
61 return Path
.Combine (RemapUriToPath (all_hits_by_uri
, (Hit
) all_hits_by_uri
[parent_uri_str
]),
65 static int DumpOneIndex_Metadata (string index_name
, bool only_dump_the_urls
)
67 Console
.WriteLine (); // a visual cue that something has changed
68 LuceneQueryingDriver driver
;
69 driver
= new LuceneQueryingDriver (index_name
, -1, true);
71 Hashtable all_hits_by_uri
;
72 all_hits_by_uri
= driver
.GetAllHitsByUri ();
75 all_hits
= new ArrayList (all_hits_by_uri
.Values
);
77 if (index_name
== "FileSystemIndex") // A hard-wired hack
78 foreach (Hit hit
in all_hits
)
79 hit
.Uri
= UriFu
.PathToFileUri (RemapUriToPath (all_hits_by_uri
, hit
));
81 all_hits
.Sort (new HitByUriComparer ());
83 foreach (Hit hit
in all_hits
) {
85 if (only_dump_the_urls
) {
86 Console
.WriteLine ("{0}: {1}", index_name
, hit
.Uri
);
90 Console
.WriteLine (" Index: {0}", index_name
);
91 Console
.WriteLine (" Uri: {0}", hit
.Uri
);
92 if (hit
.ParentUri
!= null)
93 Console
.WriteLine ("Parent: {0}", hit
.ParentUri
);
94 Console
.WriteLine (" MimeT: {0}", hit
.MimeType
);
95 Console
.WriteLine (" Type: {0}", hit
.Type
);
98 props
= new ArrayList (hit
.Properties
);
100 foreach (Property prop
in props
)
101 if (! prop
.Key
.StartsWith ("_private:"))
102 Console
.WriteLine (" Prop: {0} = '{1}'", prop
.Key
, prop
.Value
);
104 Console
.WriteLine ();
107 return all_hits
.Count
;
110 static Term initial_enum_term
;
111 // Dump the term frequencies: we do this via direct Lucene access.
112 static void DumpOneIndex_TermFrequencies (string index_name
)
114 LuceneQueryingDriver driver
;
115 driver
= new LuceneQueryingDriver (index_name
, -1, true);
118 reader
= IndexReader
.Open (driver
.PrimaryStore
);
121 term_enum
= reader
.Terms (initial_enum_term
);
123 int distinct_term_count
= 0;
127 // Terms are sorted first by field, then by text
128 // so all terms with a given field are adjacent in enumerations.
129 if (term_enum
.Term () != null) {
130 while (term_enum
.Term().Field() == "Text") {
132 freq
= term_enum
.DocFreq ();
134 Console
.WriteLine ("{0} {1} {2}", index_name
, term_enum
.Term ().Text (), freq
);
136 // FIXME: spew these as a count
137 ++distinct_term_count
;
140 if (!term_enum
.Next ())
148 Console
.WriteLine ();
151 public class IndexInfo
: IComparable
{
155 public IndexInfo (string name
)
160 public int CompareTo (object obj
)
162 IndexInfo other
= (IndexInfo
) obj
;
163 return String
.Compare (this.Name
, other
.Name
);
167 static void DumpIndexInformation (Mode mode
, bool show_counts
)
169 ArrayList index_info_list
;
170 index_info_list
= new ArrayList ();
173 dir
= new DirectoryInfo (PathFinder
.IndexDir
);
174 foreach (DirectoryInfo subdir
in dir
.GetDirectories ())
175 index_info_list
.Add (new IndexInfo (subdir
.Name
));
177 index_info_list
.Sort ();
179 bool set_counts
= false;
181 if (mode
== Mode
.TermFrequencies
)
182 initial_enum_term
= new Term ("Text", "");
184 foreach (IndexInfo info
in index_info_list
) {
185 if (mode
== Mode
.Uris
|| mode
== Mode
.Properties
) {
186 info
.Count
= DumpOneIndex_Metadata (info
.Name
, mode
== Mode
.Uris
);
189 DumpOneIndex_TermFrequencies (info
.Name
);
193 if (show_counts
&& set_counts
) {
194 Console
.WriteLine ();
195 Console
.WriteLine ("FINAL COUNTS");
197 foreach (IndexInfo info
in index_info_list
)
198 Console
.WriteLine ("{0} {1}", info
.Count
.ToString ().PadLeft (7), info
.Name
);
202 class DummyQueryResult
: IQueryResult
{
203 public void Add (ICollection hits
)
207 public void Subtract (ICollection hits
)
212 static void DumpFileIndexInformation (string path
, string indexdir
)
214 //Uri uri = UriFu.PathToFileUri (path);
215 //Console.WriteLine ("Dumping information about:" + uri.AbsolutePath);
216 //path = uri.AbsolutePath;
217 if ((! File
.Exists (path
)) && (! Directory
.Exists (path
))) {
218 Console
.WriteLine ("No such file or directory:" + path
);
222 if (indexdir
== null)
223 // default is ~/.beagle/Indexes/FileSystemIndex
224 indexdir
= Path
.Combine (PathFinder
.IndexDir
, "FileSystemIndex");
225 if (! Directory
.Exists (indexdir
)) {
226 Console
.WriteLine ("Index:{0} doesnt exist.", indexdir
);
232 reader
= new StreamReader (Path
.Combine (indexdir
, "fingerprint"));
233 string fingerprint
= reader
.ReadLine ();
235 //Console.WriteLine ("Read fingerprint:" + fingerprint);
238 FileAttributesStore fa_store
= new FileAttributesStore (new FileAttributesStore_Mixed (indexdir
, fingerprint
));
239 Beagle
.Daemon
.FileAttributes attr
= fa_store
.Read (path
);
241 Console
.WriteLine ("No information about this file in index. Ignoring.");
244 string uri_string
= "uid:" + GuidFu
.ToShortString (attr
.UniqueId
);
245 Console
.WriteLine ("Uri = " + uri_string
);
246 //Console.WriteLine ("FilterName:" + attr.FilterName);
247 Console
.WriteLine ("LastAttrTime:" + attr
.LastAttrTime
);
248 Console
.WriteLine ("LastWriteTime:" + attr
.LastWriteTime
);
250 LuceneQueryingDriver driver
;
251 driver
= new LuceneQueryingDriver (indexdir
, -1, true);
254 // first try for the Uri:"uid:xxxxxxxxxxxxxxx"
255 Lucene
.Net
.Search
.Query query
= new TermQuery(new Term("Uri", uri_string
));
256 if (DoQuery (driver
, query
))
259 // else query by path - this is for static indexes
260 path
= UriFu
.PathToFileUriString (path
);
261 Console
.WriteLine ("Querying by:[" + path
+ "]");
262 query
= new TermQuery(new Term("Uri", path
));
263 DoQuery (driver
, query
);
267 static bool DoQuery (LuceneQueryingDriver driver
, Lucene
.Net
.Search
.Query query
)
269 IndexSearcher primary_searcher
= LuceneCommon
.GetSearcher (driver
.PrimaryStore
);
270 IndexSearcher secondary_searcher
= LuceneCommon
.GetSearcher (driver
.SecondaryStore
);
272 Hits primary_hits
= primary_searcher
.Search(query
);
273 Hits secondary_hits
= secondary_searcher
.Search (query
);
274 Console
.WriteLine ("{0} hits from primary store; {1} hits from secondary store", primary_hits
.Length (), secondary_hits
.Length ());
276 Document primary_doc
, secondary_doc
;
277 // there should be exactly one primary hit and 0/1 secondary hit
278 if (primary_hits
.Length () == 1) {
279 primary_doc
= primary_hits
.Doc (0);
281 "-----------------------------------------[ Immutable data ]--------------------------------------");
282 foreach (Field f
in primary_doc
.Fields ()) {
284 String name
= f
.Name ();
285 String val
= f
.StringValue ();
286 bool stored
= f
.IsStored ();
287 bool searchable
= (val
[0] == 's');
288 bool tokenized
= f
.IsTokenized();
289 if (name
.Length
>= 7 && name
.StartsWith ("prop:"))
290 tokenized
= (name
[5] != 't');
291 float boost
= f
.GetBoost();
293 Console
.WriteLine ("{0,-30} = [{1}] (stored? {2}, searchable? {3}, tokenized? {4}, boost={5})",
294 name
, val
, stored
, searchable
, tokenized
, boost
);
298 if (secondary_hits
.Length () == 1) {
299 secondary_doc
= secondary_hits
.Doc (0);
301 "------------------------------------------[ Mutable data ]---------------------------------------");
302 foreach (Field f
in secondary_doc
.Fields ()) {
304 String name
= f
.Name ();
305 String val
= f
.StringValue ();
306 bool stored
= f
.IsStored ();
307 bool searchable
= (val
[0] == 's');
308 bool tokenized
= f
.IsTokenized();
309 if (name
.Length
>= 7 && name
.StartsWith ("prop:"))
310 tokenized
= (name
[5] != 't');
311 float boost
= f
.GetBoost();
313 Console
.WriteLine ("{0,-30} = [{1}] (stored? {2}, searchable? {3}, tokenized? {4}, boost={5})",
314 name
, val
, stored
, searchable
, tokenized
, boost
);
318 LuceneCommon
.ReleaseSearcher (primary_searcher
);
319 LuceneCommon
.ReleaseSearcher (secondary_searcher
);
321 if (primary_hits
.Length () != 0 || secondary_hits
.Length () != 0)
334 static void Main (string [] args
)
336 Mode mode
= Mode
.Uris
;
337 bool show_counts
= true;
339 string indexdir
= null;
341 foreach (string arg
in args
) {
346 Console
.WriteLine (@"
347 beagle-dump-index [options] [ [--indexdir=dir] file]
349 --uris Dump all Uris (default)
350 --properties Dump all properties
351 --term-frequencies Dump term frequencies
353 --show-counts Show index count totals (default)
354 --hide-counts Hide index count totals
356 --indexdir=<index directory>
357 Absolute path of the directory storing the index
358 e.g. /home/user/.beagle/Indexes/FileSystemIndex
359 file Get information in index about this file or directory
361 --help What you just did");
362 Environment
.Exit (0);
370 mode
= Mode
.Properties
;
373 case "--term-frequencies":
374 mode
= Mode
.TermFrequencies
;
377 case "--hide-counts":
381 case "--show-counts":
386 if (arg
.StartsWith ("--indexdir="))
387 indexdir
= arg
.Remove (0, 11);
395 DumpIndexInformation (mode
, show_counts
);
397 DumpFileIndexInformation (file
, indexdir
);