4 // Copyright (C) 2005-2006 Novell, Inc.
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.
28 using System
.Collections
;
29 using System
.Xml
.Serialization
;
33 namespace Beagle
.Daemon
{
35 public class IndexerRequest
{
37 public bool OptimizeIndex
= false;
39 // These two collections are mutually exclusive. It's an ugly
40 // hack, but it's necessary to make XML serialization work
41 // easily, and assumes that an IndexerRequest is never changed
42 // after it has been serialized.
43 private Hashtable indexables_by_uri
= null;
44 private ArrayList indexables
= null;
48 OptimizeIndex
= false;
49 indexables_by_uri
= null;
53 public void Add (Indexable indexable
)
55 if (indexables
!= null)
56 throw new Exception ("Attempt to add to a serialized IndexerRequest");
58 if (indexable
== null)
61 if (indexables_by_uri
== null)
62 indexables_by_uri
= UriFu
.NewHashtable ();
65 prior
= indexables_by_uri
[indexable
.Uri
] as Indexable
;
69 switch (indexable
.Type
) {
71 case IndexableType
.Add
:
72 case IndexableType
.Remove
:
73 // Do nothing, and just clobber the prior indexable.
76 case IndexableType
.PropertyChange
:
77 // Merge with the prior indexable.
78 prior
.Merge (indexable
);
84 indexables_by_uri
[indexable
.Uri
] = indexable
;
87 public Indexable
GetByUri (Uri uri
)
89 if (indexables_by_uri
!= null)
90 return indexables_by_uri
[uri
] as Indexable
;
92 // Fall back to using the list. This happens when we
93 // have been serialized. Which is sort of lame.
94 foreach (Indexable indexable
in indexables
) {
95 if (UriFu
.Equals (uri
, indexable
.Uri
))
102 public ICollection Indexables
{
104 if (indexables
!= null)
106 else if (indexables_by_uri
!= null)
107 return indexables_by_uri
.Values
;
109 return new Indexable
[0];
113 [XmlArray (ElementName
="Indexables")]
114 [XmlArrayItem (ElementName
="Indexable", Type
=typeof (Indexable
))]
115 public ArrayList IndexablesForSerialization
{
117 if (indexables
== null) {
118 indexables
= new ArrayList (Indexables
);
119 indexables
.Sort (); // sort into ascending timestamp order
127 get { return Indexables.Count; }
131 public bool IsEmpty
{
132 get { return Count == 0 && ! OptimizeIndex; }
135 public void Cleanup ()
137 foreach (Indexable i
in Indexables
)