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 static void Main (string [] args
)
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
);
58 driver
= new LuceneIndexingDriver (index_dir
, -1);
66 ExecuteRemove (args
[2]);
74 ExecuteMerge (args
[2]);
81 Console
.WriteLine ("Unknown command: {0}", args
[1]);
87 /////////////////////////////////////////////////////////
89 static void PrintUsage ()
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";
97 "Usage: beagle-manage-index <index_path> <command> [OPTIONS]\n\n" +
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" +
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 /////////////////////////////////////////////////////////
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
))
124 Console
.WriteLine (reader
.Document (i
));
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);
148 Console
.WriteLine ("Successfully removed Uri: {0}", uri
);
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 ();
162 for (int i
= 0; i
< n_hits
; ++i
) {
163 Document doc
= hits
.Doc (i
);
165 uri
= doc
.Get ("Uri");
170 driver
.Remove (UriFu
.UriStringToUri (uri
));
175 Console
.WriteLine ("Successfully removed {0} items with tag: {1}", n_hits
, arg
);
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 ();
200 // Merge the lucene index
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
;
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);
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 ();
244 driver
.OptimizeNow ();
248 Console
.WriteLine ("Optimized index {0} in {1}", driver
.TopDirectory
, watch
);