cvsimport
[beagle.git] / beagled / IndexerRequest.cs
blob39b5f52fd5493c12ff0aed15c1e5bc48419bf0a6
1 //
2 // IndexerRequest.cs
3 //
4 // Copyright (C) 2005-2006 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.Xml.Serialization;
31 using Beagle.Util;
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;
46 public void Clear ()
48 OptimizeIndex = false;
49 indexables_by_uri = null;
50 indexables = 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)
59 return;
61 if (indexables_by_uri == null)
62 indexables_by_uri = UriFu.NewHashtable ();
64 Indexable prior;
65 prior = indexables_by_uri [indexable.Uri] as Indexable;
67 if (prior != null) {
69 switch (indexable.Type) {
71 case IndexableType.Add:
72 case IndexableType.Remove:
73 // Do nothing, and just clobber the prior indexable.
74 break;
76 case IndexableType.PropertyChange:
77 // Merge with the prior indexable.
78 prior.Merge (indexable);
79 indexable = prior;
80 break;
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))
96 return indexable;
98 return null;
101 [XmlIgnore]
102 public ICollection Indexables {
103 get {
104 if (indexables != null)
105 return indexables;
106 else if (indexables_by_uri != null)
107 return indexables_by_uri.Values;
108 else
109 return new Indexable [0];
113 [XmlArray (ElementName="Indexables")]
114 [XmlArrayItem (ElementName="Indexable", Type=typeof (Indexable))]
115 public ArrayList IndexablesForSerialization {
116 get {
117 if (indexables == null) {
118 indexables = new ArrayList (Indexables);
119 indexables.Sort (); // sort into ascending timestamp order
121 return indexables;
125 [XmlIgnore]
126 public int Count {
127 get { return Indexables.Count; }
130 [XmlIgnore]
131 public bool IsEmpty {
132 get { return Count == 0 && ! OptimizeIndex; }
135 public void Cleanup ()
137 foreach (Indexable i in Indexables)
138 i.Cleanup ();