Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / ManageIndex.cs
blobada38c4ee011eb31e46edbcd7d73791fc869361d
1 //
2 // ManageIndex.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
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
24 // SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
30 using System.Net;
32 using Beagle;
33 using Beagle.Util;
34 using Beagle.Daemon;
36 using Lucene.Net.Index;
37 using Lucene.Net.Search;
38 using Lucene.Net.Documents;
40 namespace Beagle.Daemon
42 class ManageIndex
44 static private LuceneIndexingDriver driver;
46 static void Main (string [] args)
48 if (args.Length < 2)
49 PrintUsage ();
51 string index_dir = (Path.IsPathRooted (args [0])) ? args [0] : Path.GetFullPath (args [0]);
53 if (!Directory.Exists (index_dir)) {
54 Console.WriteLine ("Could not find index: {0}", index_dir);
55 Environment.Exit (1);
58 driver = new LuceneIndexingDriver (index_dir, -1);
60 switch (args [1]) {
61 #if false
62 case "list":
63 ExecuteList ();
64 break;
65 case "remove":
66 ExecuteRemove (args [2]);
67 break;
68 #endif
69 case "info":
70 ExecuteInfo ();
71 break;
73 case "merge":
74 ExecuteMerge (args [2]);
75 break;
77 case "optimize":
78 ExecuteOptimize ();
79 break;
80 default:
81 Console.WriteLine ("Unknown command: {0}", args [1]);
82 PrintUsage ();
83 break;
87 /////////////////////////////////////////////////////////
89 static void PrintUsage ()
91 string usage =
92 "beagle-manage-index: Low-level Lucene index management\n" +
93 "Web page: http://www.gnome.org/projects/beagle\n" +
94 "Copyright (C) 2004-2005 Novell, Inc.\n\n";
96 usage +=
97 "Usage: beagle-manage-index <index_path> <command> [OPTIONS]\n\n" +
98 "Commands:\n" +
99 #if false
100 " list\t\t\t\tList all entries in the index.\n" +
101 " remove <uri|tag>\t\tRemove entries corresponding to the criterias specified.\n" +
102 #endif
103 " merge <index to merge>\tMerge another Lucene index into the target.\n" +
104 " info\t\t\t\tPrint basic index information.\n" +
105 " optimize\t\t\tOptimize index.\n";
108 Console.WriteLine (usage);
109 Environment.Exit (0);
112 /////////////////////////////////////////////////////////
114 #if false
115 static void ExecuteList ()
117 { LuceneDriver driver = new LuceneDriver (index_dir, true);
119 IndexReader reader = IndexReader.Open (driver.Store);
121 for (int i = 0; i < reader.NumDocs (); i++) {
122 if (reader.IsDeleted (i))
123 continue;
124 Console.WriteLine (reader.Document (i));
127 reader.Close ();
130 /////////////////////////////////////////////////////////
132 static void ExecuteRemove (string arg)
134 LuceneDriver driver = new LuceneDriver (index_dir);
136 if (arg.IndexOf ("://") != -1) {
137 Uri uri = new Uri (arg);
138 ICollection hits = driver.DoQueryByUri (uri);
140 if (hits == null || hits.Count == 0) {
141 Console.WriteLine ("Uri not found in the index: {0}", uri);
142 Environment.Exit (1);
145 driver.Remove (uri);
146 driver.Flush ();
148 Console.WriteLine ("Successfully removed Uri: {0}", uri);
149 } else {
150 IndexSearcher searcher = new IndexSearcher (driver.Store);
151 BooleanQuery query = new BooleanQuery ();
153 Term term = new Term ("prop:k:Tag", arg); // Argh
154 TermQuery term_query = new TermQuery (term);
155 query.Add (term_query, false, false);
157 Hits hits = searcher.Search (query);
158 int n_hits = hits.Length ();
160 string uri;
162 for (int i = 0; i < n_hits; ++i) {
163 Document doc = hits.Doc (i);
165 uri = doc.Get ("Uri");
167 if (uri == null)
168 continue;
170 driver.Remove (UriFu.UriStringToUri (uri));
173 driver.Flush ();
175 Console.WriteLine ("Successfully removed {0} items with tag: {1}", n_hits, arg);
178 #endif
179 /////////////////////////////////////////////////////////
181 // Merge an external Beagle index to the current index. Merging will
182 // join the primary- and secondary lucene indexes and if available, the
183 // file attributes store.
185 static void ExecuteMerge (string index_to_merge)
187 if (!Path.IsPathRooted (index_to_merge))
188 index_to_merge = Path.GetFullPath (index_to_merge);
190 if (!Directory.Exists (index_to_merge)) {
191 Console.WriteLine ("Could not find index to merge: {0}", index_to_merge);
192 Environment.Exit (1);
195 LuceneQueryingDriver driver_to_merge = new LuceneQueryingDriver (index_to_merge, -1, false);
197 Stopwatch watch = new Stopwatch ();
198 watch.Start ();
200 // Merge the lucene index
202 try {
203 driver.Merge (driver_to_merge);
204 } catch (Exception e) {
205 Console.WriteLine ("Index merging (lucene) failed: {0}", e);
206 Environment.Exit (1);
209 // Merge file attributes stores
211 FileAttributesStore_Sqlite store;
213 try {
214 store = new FileAttributesStore_Sqlite (driver.TopDirectory, driver.Fingerprint);
215 store.Merge (new FileAttributesStore_Sqlite (driver_to_merge.TopDirectory, driver_to_merge.Fingerprint));
216 } catch (Exception e) {
217 Console.WriteLine ("Index merging (attributes store) failed: {0}", e);
218 Environment.Exit (1);
221 watch.Stop ();
223 Console.WriteLine ("Successfully merged index {0} into {1} in {2}", index_to_merge, driver.TopDirectory, watch);
226 /////////////////////////////////////////////////////////
228 // Get the total number of entries from the index.
230 static void ExecuteInfo ()
232 Console.WriteLine ("Total number of entries in index: {0}", driver.GetItemCount());
235 /////////////////////////////////////////////////////////
237 // Execute a lucene optimize-task on the index.
239 static void ExecuteOptimize ()
241 Stopwatch watch = new Stopwatch ();
242 watch.Start ();
244 driver.OptimizeNow ();
246 watch.Stop ();
248 Console.WriteLine ("Optimized index {0} in {1}", driver.TopDirectory, watch);