3 using System
.Collections
;
4 using System
.Threading
;
11 public class QueryFu
{
13 static public Query
NewTokenQuery (string token
)
19 part
= new QueryPart_Text ();
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
)
39 if (allow_inexpensive
) {
41 mime_type
= random
.Next (3);
43 query
.AddMimeType ("inode/directory");
44 else if (mime_type
== 1)
45 query
.AddMimeType ("text/plain");
48 // Every query must contain at least
50 bool contains_required
;
51 contains_required
= false;
53 for (int i
= 0; i
< length
; ++i
) {
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
;
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;
77 static public Query
NewRandomQuery ()
79 return NewRandomQuery (2 + random
.Next (4), true);
82 /////////////////////////////////////////////////////////////
84 static public void SpewQuery (Query query
)
88 foreach (QueryPart abstract_part
in query
.Parts
) {
95 if (abstract_part
is QueryPart_Text
) {
97 part
= (QueryPart_Text
) abstract_part
;
100 if (part
.Logic
== QueryPartLogic
.Prohibited
)
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 ();
128 public QueryClosure (Query query
)
133 public void OnHitsAdded (HitsAddedResponse response
)
135 Hits
.AddRange (response
.Hits
);
138 public void OnFinished (FinishedResponse response
)
145 static public ArrayList
GetHits (Query q
)
148 qc
= new QueryClosure (q
);
149 q
.HitsAddedEvent
+= qc
.OnHitsAdded
;
150 q
.FinishedEvent
+= qc
.OnFinished
;
152 q
.SendAsyncBlocking ();
157 static public ArrayList
GetUris (Query q
)
161 for (int i
= 0; i
< array
.Count
; ++i
)
162 array
[i
] = ((Beagle
.Hit
) array
[i
]).Uri
;