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
;
37 using Lucene
.Net
.Index
;
38 using Lucene
.Net
.Search
;
39 using Lucene
.Net
.Documents
;
43 public class HitByUriComparer
: IComparer
{
45 public int Compare (object a
, object b
)
47 // All of this mapping to and from strings is dreadful.
48 return String
.Compare (((Hit
) a
).Uri
.ToString (), ((Hit
) b
).Uri
.ToString ());
52 static string RemapUriToPath (Hashtable all_hits_by_uri
, Hit hit
)
54 string exact_name
= hit
.GetFirstProperty ("beagle:ExactFilename");
55 string parent_uri_str
= hit
.GetFirstProperty ("_private:ParentDirUri");
57 if (parent_uri_str
== null)
60 return Path
.Combine (RemapUriToPath (all_hits_by_uri
, (Hit
) all_hits_by_uri
[parent_uri_str
]),
64 static int DumpOneIndex_Metadata (string index_name
, bool only_dump_the_urls
)
66 Console
.WriteLine (); // a visual cue that something has changed
67 LuceneQueryingDriver driver
;
68 driver
= new LuceneQueryingDriver (index_name
, -1, true);
70 Hashtable all_hits_by_uri
;
71 all_hits_by_uri
= driver
.GetAllHitsByUri ();
74 all_hits
= new ArrayList (all_hits_by_uri
.Values
);
76 if (index_name
== "FileSystemIndex") // A hard-wired hack
77 foreach (Hit hit
in all_hits
)
78 hit
.Uri
= UriFu
.PathToFileUri (RemapUriToPath (all_hits_by_uri
, hit
));
80 all_hits
.Sort (new HitByUriComparer ());
82 foreach (Hit hit
in all_hits
) {
84 if (only_dump_the_urls
) {
85 Console
.WriteLine ("{0}: {1}", index_name
, hit
.Uri
);
89 Console
.WriteLine (" Index: {0}", index_name
);
90 Console
.WriteLine (" Uri: {0}", hit
.Uri
);
91 if (hit
.ParentUri
!= null)
92 Console
.WriteLine ("Parent: {0}", hit
.ParentUri
);
93 Console
.WriteLine (" MimeT: {0}", hit
.MimeType
);
94 Console
.WriteLine (" Type: {0}", hit
.Type
);
97 props
= new ArrayList (hit
.Properties
);
99 foreach (Property prop
in props
)
100 if (! prop
.Key
.StartsWith ("_private:"))
101 Console
.WriteLine (" Prop: {0} = '{1}'", prop
.Key
, prop
.Value
);
103 Console
.WriteLine ();
106 return all_hits
.Count
;
109 static Term initial_enum_term
;
110 // Dump the term frequencies: we do this via direct Lucene access.
111 static void DumpOneIndex_TermFrequencies (string index_name
)
113 LuceneQueryingDriver driver
;
114 driver
= new LuceneQueryingDriver (index_name
, -1, true);
117 reader
= IndexReader
.Open (driver
.PrimaryStore
);
120 term_enum
= reader
.Terms (initial_enum_term
);
122 int distinct_term_count
= 0;
126 // Terms are sorted first by field, then by text
127 // so all terms with a given field are adjacent in enumerations.
128 if (term_enum
.Term () != null) {
129 while (term_enum
.Term().Field() == "Text") {
131 freq
= term_enum
.DocFreq ();
133 Console
.WriteLine ("{0} {1} {2}", index_name
, term_enum
.Term ().Text (), freq
);
135 // FIXME: spew these as a count
136 ++distinct_term_count
;
139 if (!term_enum
.Next ())
147 Console
.WriteLine ();
150 public class IndexInfo
: IComparable
{
154 public IndexInfo (string name
)
159 public int CompareTo (object obj
)
161 IndexInfo other
= (IndexInfo
) obj
;
162 return String
.Compare (this.Name
, other
.Name
);
166 static void DumpIndexInformation (Mode mode
, bool show_counts
)
168 ArrayList index_info_list
;
169 index_info_list
= new ArrayList ();
172 dir
= new DirectoryInfo (PathFinder
.IndexDir
);
173 foreach (DirectoryInfo subdir
in dir
.GetDirectories ())
174 index_info_list
.Add (new IndexInfo (subdir
.Name
));
176 index_info_list
.Sort ();
178 bool set_counts
= false;
180 if (mode
== Mode
.TermFrequencies
)
181 initial_enum_term
= new Term ("Text", "");
183 foreach (IndexInfo info
in index_info_list
) {
184 if (mode
== Mode
.Uris
|| mode
== Mode
.Properties
) {
185 info
.Count
= DumpOneIndex_Metadata (info
.Name
, mode
== Mode
.Uris
);
188 DumpOneIndex_TermFrequencies (info
.Name
);
192 if (show_counts
&& set_counts
) {
193 Console
.WriteLine ();
194 Console
.WriteLine ("FINAL COUNTS");
196 foreach (IndexInfo info
in index_info_list
)
197 Console
.WriteLine ("{0} {1}", info
.Count
.ToString ().PadLeft (7), info
.Name
);
201 class DummyQueryResult
: IQueryResult
{
202 public void Add (ICollection hits
)
206 public void Subtract (ICollection hits
)
211 static void DumpFileIndexInformation (string path
, string indexdir
)
213 //Uri uri = UriFu.PathToFileUri (path);
214 //Console.WriteLine ("Dumping information about:" + uri.AbsolutePath);
215 //path = uri.AbsolutePath;
216 if ((! File
.Exists (path
)) && (! Directory
.Exists (path
))) {
217 Console
.WriteLine ("No such file or directory:" + path
);
221 if (indexdir
== null)
222 // default is ~/.beagle/Indexes/FileSystemIndex
223 indexdir
= Path
.Combine (PathFinder
.IndexDir
, "FileSystemIndex");
224 if (! Directory
.Exists (indexdir
)) {
225 Console
.WriteLine ("Index:{0} doesnt exist.", indexdir
);
231 reader
= new StreamReader (Path
.Combine (indexdir
, "fingerprint"));
232 string fingerprint
= reader
.ReadLine ();
234 //Console.WriteLine ("Read fingerprint:" + fingerprint);
237 FileAttributesStore fa_store
= new FileAttributesStore (new FileAttributesStore_Mixed (indexdir
, fingerprint
));
238 Beagle
.Daemon
.FileAttributes attr
= fa_store
.Read (path
);
240 Console
.WriteLine ("No information about this file in index. Ignoring.");
243 string uri_string
= "uid:" + GuidFu
.ToShortString (attr
.UniqueId
);
244 Console
.WriteLine ("Uri = " + uri_string
);
245 //Console.WriteLine ("FilterName:" + attr.FilterName);
246 Console
.WriteLine ("LastAttrTime:" + attr
.LastAttrTime
);
247 Console
.WriteLine ("LastWriteTime:" + attr
.LastWriteTime
);
249 LuceneQueryingDriver driver
;
250 driver
= new LuceneQueryingDriver (indexdir
, -1, true);
253 // first try for the Uri:"uid:xxxxxxxxxxxxxxx"
254 Lucene
.Net
.Search
.Query query
= new TermQuery(new Term("Uri", uri_string
));
255 if (DoQuery (driver
, query
))
258 // else query by path - this is for static indexes
259 path
= StringFu
.PathToQuotedFileUri (path
);
260 Console
.WriteLine ("Querying by:[" + path
+ "]");
261 query
= new TermQuery(new Term("Uri", path
));
262 DoQuery (driver
, query
);
266 static bool DoQuery (LuceneQueryingDriver driver
, Lucene
.Net
.Search
.Query query
)
268 IndexSearcher primary_searcher
= LuceneCommon
.GetSearcher (driver
.PrimaryStore
);
269 IndexSearcher secondary_searcher
= LuceneCommon
.GetSearcher (driver
.SecondaryStore
);
271 Hits primary_hits
= primary_searcher
.Search(query
);
272 Hits secondary_hits
= secondary_searcher
.Search (query
);
273 Console
.WriteLine ("{0} hits from primary store; {1} hits from secondary store", primary_hits
.Length (), secondary_hits
.Length ());
275 Document primary_doc
, secondary_doc
;
276 // there should be exactly one primary hit and 0/1 secondary hit
277 if (primary_hits
.Length () == 1) {
278 primary_doc
= primary_hits
.Doc (0);
280 "-----------------------------------------[ Immutable data ]--------------------------------------");
281 foreach (Field f
in primary_doc
.Fields ()) {
283 String name
= f
.Name ();
284 String val
= f
.StringValue ();
285 bool stored
= f
.IsStored ();
286 bool searchable
= (val
[0] == 's');
287 bool tokenized
= f
.IsTokenized();
288 if (name
.Length
>= 7 && name
.StartsWith ("prop:"))
289 tokenized
= (name
[5] != 't');
290 float boost
= f
.GetBoost();
292 Console
.WriteLine ("{0,-30} = [{1}] (stored? {2}, searchable? {3}, tokenized? {4}, boost={5})",
293 name
, val
, stored
, searchable
, tokenized
, boost
);
297 if (secondary_hits
.Length () == 1) {
298 secondary_doc
= secondary_hits
.Doc (0);
300 "------------------------------------------[ Mutable data ]---------------------------------------");
301 foreach (Field f
in secondary_doc
.Fields ()) {
303 String name
= f
.Name ();
304 String val
= f
.StringValue ();
305 bool stored
= f
.IsStored ();
306 bool searchable
= (val
[0] == 's');
307 bool tokenized
= f
.IsTokenized();
308 if (name
.Length
>= 7 && name
.StartsWith ("prop:"))
309 tokenized
= (name
[5] != 't');
310 float boost
= f
.GetBoost();
312 Console
.WriteLine ("{0,-30} = [{1}] (stored? {2}, searchable? {3}, tokenized? {4}, boost={5})",
313 name
, val
, stored
, searchable
, tokenized
, boost
);
317 LuceneCommon
.ReleaseSearcher (primary_searcher
);
318 LuceneCommon
.ReleaseSearcher (secondary_searcher
);
320 if (primary_hits
.Length () != 0 || secondary_hits
.Length () != 0)
333 static void Main (string [] args
)
335 Mode mode
= Mode
.Uris
;
336 bool show_counts
= true;
338 string indexdir
= null;
340 foreach (string arg
in args
) {
345 Console
.WriteLine (@"
346 beagle-dump-index [options] [ [--indexdir=dir] file]
348 --uris Dump all Uris (default)
349 --properties Dump all properties
350 --term-frequencies Dump term frequencies
352 --show-counts Show index count totals (default)
353 --hide-counts Hide index count totals
355 --indexdir=<index directory>
356 Absolute path of the directory storing the index
357 e.g. /home/user/.beagle/Indexes/FileSystemIndex
358 file Get information in index about this file or directory
360 --help What you just did");
361 Environment
.Exit (0);
369 mode
= Mode
.Properties
;
372 case "--term-frequencies":
373 mode
= Mode
.TermFrequencies
;
376 case "--hide-counts":
380 case "--show-counts":
385 if (arg
.StartsWith ("--indexdir="))
386 indexdir
= arg
.Remove (0, 11);
394 DumpIndexInformation (mode
, show_counts
);
396 DumpFileIndexInformation (file
, indexdir
);