cvsimport
[beagle.git] / beagled / QueryExecutor.cs
blobd8410db79c6a3107a517e52c91864dbbf64c1cbe
1 //
2 // QueryExecutor.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.Xml.Serialization;
31 using Beagle.Util;
33 namespace Beagle.Daemon {
35 [RequestMessage (typeof (Beagle.Query))]
36 public class QueryExecutor : RequestMessageExecutor {
38 private Query query;
39 private QueryResult result;
41 private void DisconnectResult ()
43 this.result.HitsAddedEvent -= OnResultHitsAdded;
44 this.result.HitsSubtractedEvent -= OnResultHitsSubtracted;
45 this.result.FinishedEvent -= OnResultFinished;
47 this.result.Cancel ();
48 this.result.Dispose ();
51 private void AttachResult ()
53 this.result.HitsAddedEvent += OnResultHitsAdded;
54 this.result.HitsSubtractedEvent += OnResultHitsSubtracted;
55 this.result.FinishedEvent += OnResultFinished;
58 public void OnResultHitsAdded (QueryResult result, ICollection some_hits, int total_results)
60 HitsAddedResponse response = new HitsAddedResponse (some_hits, total_results);
62 this.SendAsyncResponse (response);
65 public void OnResultHitsSubtracted (QueryResult result, ICollection some_uris)
67 HitsSubtractedResponse response = new HitsSubtractedResponse (some_uris);
69 this.SendAsyncResponse (response);
72 public void OnResultFinished (QueryResult result)
74 this.SendAsyncResponse (new FinishedResponse ());
77 private void OnQueryDriverChanged (Queryable queryable, IQueryableChangeData change_data)
79 if (this.result != null)
80 QueryDriver.DoOneQuery (queryable, this.query, this.result, change_data);
83 public override ResponseMessage Execute (RequestMessage req)
85 this.query = (Query) req;
87 this.result = new QueryResult ();
88 this.result.IsIndexListener = this.query.IsIndexListener;
89 AttachResult ();
91 QueryDriver.ChangedEvent += OnQueryDriverChanged;
92 QueryDriver.DoQuery (query,
93 this.result,
94 new RequestMessageExecutor.AsyncResponse (this.SendAsyncResponse));
96 // Don't send a response; we'll be sending them async
97 return null;
100 public override void Cleanup ()
102 QueryDriver.ChangedEvent -= OnQueryDriverChanged;
103 DisconnectResult ();