4 // Copyright (C) 2005 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
28 using System
.Collections
;
36 using Lucene
.Net
.Index
;
37 using Lucene
.Net
.Search
;
38 using Lucene
.Net
.Documents
;
40 namespace Beagle
.Daemon
44 static private LuceneIndexingDriver driver
;
46 // Files and directories that are allowed to be in the target
47 // directory before we blow it away. If we encounter any file
48 // or dir not in this list, we'll bail out.
49 static string [] allowed_files
= {
50 "FileAttributesStore.db",
55 static string [] allowed_dirs
= {
63 static void Main (string [] args
)
68 string index_dir
= (Path
.IsPathRooted (args
[0])) ? args
[0] : Path
.GetFullPath (args
[0]);
70 if (!Directory
.Exists (index_dir
)) {
71 Console
.WriteLine ("Could not find index: {0}", index_dir
);
75 // Be *EXTRA PARANOID* about the contents of the target
76 // directory, because creating an indexing driver will
78 if (Directory
.Exists (index_dir
)) {
80 foreach (FileInfo info
in DirectoryWalker
.GetFileInfos (index_dir
)) {
81 if (Array
.IndexOf (allowed_files
, info
.Name
) == -1) {
82 Logger
.Log
.Error ("{0} doesn't look safe to delete: non-Beagle file {1} was found", index_dir
, info
.FullName
);
87 foreach (DirectoryInfo info
in DirectoryWalker
.GetDirectoryInfos (index_dir
)) {
88 if (Array
.IndexOf (allowed_dirs
, info
.Name
) == -1) {
89 Logger
.Log
.Error ("{0} doesn't look safe to delete: non-Beagle directory {1} was found", index_dir
, info
.FullName
);
95 driver
= new LuceneIndexingDriver (index_dir
, false);
103 ExecuteRemove (args
[2]);
111 ExecuteMerge (args
[2]);
118 Console
.WriteLine ("Unknown command: {0}", args
[1]);
124 /////////////////////////////////////////////////////////
126 static void PrintUsage ()
129 "beagle-manage-index: Low-level Lucene index management\n" +
130 "Web page: http://www.gnome.org/projects/beagle\n" +
131 "Copyright (C) 2004-2005 Novell, Inc.\n\n";
134 "Usage: beagle-manage-index <index_path> <command> [OPTIONS]\n\n" +
137 " list\t\t\t\tList all entries in the index.\n" +
138 " remove <uri|tag>\t\tRemove entries corresponding to the criterias specified.\n" +
140 " merge <index to merge>\tMerge another Lucene index into the target.\n" +
141 " info\t\t\t\tPrint basic index information.\n" +
142 " optimize\t\t\tOptimize index.\n";
145 Console
.WriteLine (usage
);
146 Environment
.Exit (0);
149 /////////////////////////////////////////////////////////
152 static void ExecuteList ()
154 { LuceneDriver driver
= new LuceneDriver (index_dir
, true);
156 IndexReader reader
= IndexReader
.Open (driver
.Store
);
158 for (int i
= 0; i
< reader
.NumDocs (); i
++) {
159 if (reader
.IsDeleted (i
))
161 Console
.WriteLine (reader
.Document (i
));
167 /////////////////////////////////////////////////////////
169 static void ExecuteRemove (string arg
)
171 LuceneDriver driver
= new LuceneDriver (index_dir
);
173 if (arg
.IndexOf ("://") != -1) {
174 Uri uri
= new Uri (arg
);
175 ICollection hits
= driver
.DoQueryByUri (uri
);
177 if (hits
== null || hits
.Count
== 0) {
178 Console
.WriteLine ("Uri not found in the index: {0}", uri
);
179 Environment
.Exit (1);
185 Console
.WriteLine ("Successfully removed Uri: {0}", uri
);
187 IndexSearcher searcher
= new IndexSearcher (driver
.Store
);
188 BooleanQuery query
= new BooleanQuery ();
190 Term term
= new Term ("prop:k:Tag", arg
); // Argh
191 TermQuery term_query
= new TermQuery (term
);
192 query
.Add (term_query
, false, false);
194 Hits hits
= searcher
.Search (query
);
195 int n_hits
= hits
.Length ();
199 for (int i
= 0; i
< n_hits
; ++i
) {
200 Document doc
= hits
.Doc (i
);
202 uri
= doc
.Get ("Uri");
207 driver
.Remove (UriFu
.UriStringToUri (uri
));
212 Console
.WriteLine ("Successfully removed {0} items with tag: {1}", n_hits
, arg
);
216 /////////////////////////////////////////////////////////
218 // Merge an external Beagle index to the current index. Merging will
219 // join the primary- and secondary lucene indexes and if available, the
220 // file attributes store.
222 static void ExecuteMerge (string index_to_merge
)
224 if (!Path
.IsPathRooted (index_to_merge
))
225 index_to_merge
= Path
.GetFullPath (index_to_merge
);
227 if (!Directory
.Exists (index_to_merge
)) {
228 Console
.WriteLine ("Could not find index to merge: {0}", index_to_merge
);
229 Environment
.Exit (1);
232 // Set the IO priority so we don't slow down the system
233 IoPriority
.ReduceIoPriority ();
235 LuceneQueryingDriver driver_to_merge
= new LuceneQueryingDriver (index_to_merge
, -1, false);
237 Stopwatch watch
= new Stopwatch ();
240 // Merge the lucene index
243 driver
.Merge (driver_to_merge
);
244 } catch (Exception e
) {
245 Console
.WriteLine ("Index merging (lucene) failed: {0}", e
);
246 Environment
.Exit (1);
249 // Merge file attributes stores
251 FileAttributesStore_Sqlite store
;
254 store
= new FileAttributesStore_Sqlite (driver
.TopDirectory
, driver
.Fingerprint
);
255 store
.Merge (new FileAttributesStore_Sqlite (driver_to_merge
.TopDirectory
, driver_to_merge
.Fingerprint
));
256 } catch (Exception e
) {
257 Console
.WriteLine ("Index merging (attributes store) failed: {0}", e
);
258 Environment
.Exit (1);
263 Console
.WriteLine ("Successfully merged index {0} into {1} in {2}", index_to_merge
, driver
.TopDirectory
, watch
);
266 /////////////////////////////////////////////////////////
268 // Get the total number of entries from the index.
270 static void ExecuteInfo ()
272 Console
.WriteLine ("Total number of entries in index: {0}", driver
.GetItemCount());
275 /////////////////////////////////////////////////////////
277 // Execute a lucene optimize-task on the index.
279 static void ExecuteOptimize ()
281 // Set the IO priority so we don't slow down the system
282 IoPriority
.ReduceIoPriority ();
284 Stopwatch watch
= new Stopwatch ();
287 driver
.OptimizeNow ();
291 Console
.WriteLine ("Optimized index {0} in {1}", driver
.TopDirectory
, watch
);