* tools/Info.cs: Add --list-backends, --list-static-indexes to
[beagle.git] / beagled / RenamingLuceneDriver.cs
blob004f32884dfe959295b77def06efd900a0930607
1 //
2 // RenamingLuceneDriver.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all 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
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
31 using Beagle.Util;
33 namespace Beagle.Daemon {
35 public class RenamingLuceneDriver : IIndexer {
37 private LuceneDriver driver;
38 private NameIndex name_index;
39 private ArrayList renamed_uris = new ArrayList ();
41 public RenamingLuceneDriver (string index_name, int minor_version)
43 this.driver = new LuceneDriver (index_name, minor_version);
44 this.name_index = new NameIndex (this.driver.IndexDirectory,
45 this.driver.Fingerprint);
47 this.driver.ChangedEvent += OnLuceneDriverChanged;
48 this.driver.ChildIndexableEvent += OnLuceneChildIndexableEvent;
49 this.driver.UrisFilteredEvent += OnLuceneUrisFilteredEvent;
52 // We assume that
53 // (a) indexable.Uri is a uid: URI
54 // (b) indexable.ContentUri is a file: URI
55 // (c) indexable.ContentUri's filename is meaningful (i.e. isn't a temporary file)
56 public void Add (Indexable indexable)
58 driver.Add (indexable);
60 Guid uid = GuidFu.FromUri (indexable.Uri);
61 string name = FileSystem.GetFileName (indexable.ContentUri.LocalPath);
62 name_index.Add (uid, name);
65 // We assume that uri is uid: URI
66 public void Remove (Uri uri)
68 driver.Remove (uri);
70 Guid uid = GuidFu.FromUri (uri);
71 name_index.Remove (uid);
74 // We assume that
75 // (a) old_uri is a uid: URI
76 // (b) new_uri is a file: URI
77 // (c) new_uri's filename is meaningful
78 public void Rename (Uri old_uri, Uri new_uri)
80 Guid uid = GuidFu.FromUri (old_uri);
81 string name = FileSystem.GetFileName (new_uri.LocalPath);
82 name_index.Add (uid, name);
83 renamed_uris.Add (old_uri);
84 renamed_uris.Add (new_uri);
87 static object [] empty_collection = new object [0];
89 public void Flush ()
91 // FIXME: We should add some paranoid checking here.
92 // If one flush succeeds and the other fails, our two
93 // indexes will be out of sync!
94 name_index.Flush ();
95 driver.Flush ();
97 // FIXME: If necessary, fire the ChangedEvent to give
98 // notification of any renames.
99 if (renamed_uris.Count > 0) {
100 if (ChangedEvent != null) {
101 ChangedEvent (this,
102 empty_collection,
103 empty_collection,
104 renamed_uris);
106 renamed_uris.Clear ();
110 public int GetItemCount ()
112 return driver.GetItemCount ();
115 public event IIndexerChangedHandler ChangedEvent;
116 public event IIndexerChildIndexableHandler ChildIndexableEvent;
117 public event IIndexerUrisFilteredHandler UrisFilteredEvent;
119 public void OnLuceneDriverChanged (IIndexer source,
120 ICollection list_of_added_uris,
121 ICollection list_of_removed_uris,
122 ICollection list_of_renamed_uris)
124 // Since we are proxying events from the LuceneDriver, there
125 // will never been any renamed uris. Thus it is safe to
126 // substitute our own internal list of renamed uris.
127 if (ChangedEvent != null) {
128 ChangedEvent (this,
129 list_of_added_uris,
130 list_of_removed_uris,
131 renamed_uris);
133 renamed_uris.Clear ();
136 public void OnLuceneChildIndexableEvent (Indexable[] child_indexables)
138 if (ChildIndexableEvent != null)
139 ChildIndexableEvent (child_indexables);
142 public void OnLuceneUrisFilteredEvent (FilteredStatus[] list_of_uris_filtered)
144 if (UrisFilteredEvent != null)
145 UrisFilteredEvent (list_of_uris_filtered);