Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / bludgeon / QueryFu.cs
blob11b47e2814e4280b4af2be8fa43e317da4404d1a
2 using System;
3 using System.Collections;
4 using System.Threading;
6 using Beagle.Util;
7 using Beagle;
9 namespace Bludgeon {
11 public class QueryFu {
13 static public Query NewTokenQuery (string token)
15 Query query;
16 query = new Query ();
18 QueryPart_Text part;
19 part = new QueryPart_Text ();
20 part.Text = token;
21 query.AddPart (part);
23 return query;
26 static public Query NewTokenQuery (int id)
28 return NewTokenQuery (Token.IdToString (id));
31 static Random random = new Random ();
33 static public Query NewRandomQuery (int length,
34 bool allow_inexpensive)
36 Query query;
37 query = new Query ();
39 if (allow_inexpensive) {
40 int mime_type;
41 mime_type = random.Next (3);
42 if (mime_type == 0)
43 query.AddMimeType ("inode/directory");
44 else if (mime_type == 1)
45 query.AddMimeType ("text/plain");
48 // Every query must contain at least
49 // one required part.
50 bool contains_required;
51 contains_required = false;
53 for (int i = 0; i < length; ++i) {
54 QueryPart_Text part;
55 part = new QueryPart_Text ();
56 part.Text = Token.GetRandom ();
58 if (contains_required) {
59 if (random.Next (2) == 0)
60 part.Logic = QueryPartLogic.Prohibited;
61 } else {
62 // This part will be required.
63 contains_required = true;
66 if (random.Next (2) == 0)
67 part.SearchTextProperties = false;
68 else if (allow_inexpensive && random.Next (2) == 0)
69 part.SearchFullText = false;
71 query.AddPart (part);
74 return query;
77 static public Query NewRandomQuery ()
79 return NewRandomQuery (2 + random.Next (4), true);
82 /////////////////////////////////////////////////////////////
84 static public void SpewQuery (Query query)
86 int i = 0;
88 foreach (QueryPart abstract_part in query.Parts) {
90 ++i;
92 string msg;
93 msg = "????";
95 if (abstract_part is QueryPart_Text) {
96 QueryPart_Text part;
97 part = (QueryPart_Text) abstract_part;
99 msg = "";
100 if (part.Logic == QueryPartLogic.Prohibited)
101 msg = "NOT ";
102 msg += part.Text;
104 if (! (part.SearchFullText && part.SearchTextProperties)) {
105 if (part.SearchFullText)
106 msg += " IN FULLTEXT";
107 else if (part.SearchTextProperties)
108 msg += " IN TEXT PROPERTIES";
110 } else if (abstract_part is QueryPart_Property) {
111 QueryPart_Property part;
112 part = (QueryPart_Property) abstract_part;
113 msg = String.Format ("PROPERTY {0} = {1}", part.Key, part.Value);
116 Log.Spew ("{0}: {1}", i, msg);
120 /////////////////////////////////////////////////////////////
122 private class QueryClosure {
124 public ArrayList Hits = new ArrayList ();
126 private Query query;
128 public QueryClosure (Query query)
130 this.query = query;
133 public void OnHitsAdded (HitsAddedResponse response)
135 Hits.AddRange (response.Hits);
138 public void OnFinished (FinishedResponse response)
140 query.Close ();
145 static public ArrayList GetHits (Query q)
147 QueryClosure qc;
148 qc = new QueryClosure (q);
149 q.HitsAddedEvent += qc.OnHitsAdded;
150 q.FinishedEvent += qc.OnFinished;
152 q.SendAsyncBlocking ();
154 return qc.Hits;
157 static public ArrayList GetUris (Query q)
159 ArrayList array;
160 array = GetHits (q);
161 for (int i = 0; i < array.Count; ++i)
162 array [i] = ((Beagle.Hit) array [i]).Uri;
163 return array;