4 // Copyright (C) 2005 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 private Hashtable indexables_by_uri
= UriFu
.NewHashtable ();
40 private ArrayList indexables
= null;
42 // FIXME: We don't try to keep the ArrayList and the Hashtable
43 // in-sync. This is a hack to make XML serialization work easily,
44 // and it assumes that an IndexerRequest is never changed after
45 // it has been serialized.
49 OptimizeIndex
= false;
50 indexables_by_uri
.Clear ();
54 public void Add (Indexable indexable
)
56 if (indexables
!= null)
57 throw new Exception ("Attempt to add to a serialized IndexerRequest");
59 if (indexable
== null)
63 prior
= indexables_by_uri
[indexable
.Uri
] as Indexable
;
67 switch (indexable
.Type
) {
69 case IndexableType
.Add
:
70 case IndexableType
.Remove
:
71 // Do nothing, and just clobber the prior indexable.
74 case IndexableType
.PropertyChange
:
75 // Merge with the prior indexable.
76 prior
.Merge (indexable
);
82 indexables_by_uri
[indexable
.Uri
] = indexable
;
85 public Indexable
GetByUri (Uri uri
)
87 if (indexables_by_uri
!= null)
88 return indexables_by_uri
[uri
] as Indexable
;
90 // Fall back to using the list. This happens when we
91 // have been serialized. Which is sort of lame.
92 foreach (Indexable indexable
in indexables
) {
93 if (UriFu
.Equals (uri
, indexable
.Uri
))
99 [XmlArray (ElementName
="Indexables")]
100 [XmlArrayItem (ElementName
="Indexable", Type
=typeof (Indexable
))]
101 public ArrayList Indexables
{
103 if (indexables
== null) {
104 indexables
= new ArrayList ();
105 foreach (Indexable indexable
in indexables_by_uri
.Values
)
106 indexables
.Add (indexable
);
107 indexables
.Sort (); // sort into ascending timestamp order
115 get { return indexables != null ? indexables.Count : indexables_by_uri.Count; }
119 public bool IsEmpty
{
120 get { return Count == 0 && ! OptimizeIndex; }
123 public void Cleanup ()
125 if (indexables_by_uri
!= null)
126 foreach (Indexable i
in indexables_by_uri
.Values
)
129 foreach (Indexable i
in indexables
)