Beispiel #1
0
        public BeaglePlugin()
        {
            server = Daemon.Server;
            db = Daemon.DefaultDatabase;
            log = Daemon.Log;

            query = new Query ();
            query.AddDomain (QueryDomain.Neighborhood);
            query.MaxHits = 10000;

            QueryPart_Property filePart = new QueryPart_Property ();
            filePart.Type = PropertyType.Keyword;
            filePart.Key = "beagle:HitType";
            filePart.Value = "File";
            query.AddPart (filePart);

            QueryPart_Or queryUnion = new QueryPart_Or ();

            foreach (string mt in supportedMimeTypes) {
                QueryPart_Property part = new QueryPart_Property ();
                part.Type = PropertyType.Keyword;
                part.Key = "beagle:MimeType";
                part.Value = mt;
                queryUnion.Add (part);
            }

            query.AddPart (queryUnion);
            query.HitsAddedEvent += OnHitsAdded;
            query.HitsSubtractedEvent += OnHitsSubtracted;
            query.FinishedEvent += OnFinished;

            int attempts = 0;
            while (true) {
                try {
                    query.SendAsync ();
                    break;
                } catch (Exception e) {
                    if (attempts++ >= 5) {
                        log.Warn ("Failed to initialize beagle plugin");
                        query = null;
                        break;
                    }

                    // something bad happened, wait a sec and try again
                    log.Debug ("Sending query failed: " + e.Message);
                    log.Debug ("Waiting 3 seconds...");
                    Thread.Sleep (3000);
                }
            }
        }
Beispiel #2
0
		static private Query NewRandomQuery (int length,
						     bool allow_inexpensive,
						     bool inside_an_or)
		{
			Query query;
			query = new Query ();

			// One in four queries will contain some OR terms.
			if (! inside_an_or && random.Next (4) == 0) {
				int N = random.Next (3) + 1;
				for (int i = 0; i < N; ++i) {
					QueryPart_Or part;
					part = new QueryPart_Or ();

					int sub_length;
					sub_length = random.Next (length) + 1;
					if (sub_length < 2)
						sub_length = 2;
					
					// We generate a new query at random, and stuff its QueryParts
					// into our Or QueryPart.
					Query or_query;
					or_query = NewRandomQuery (sub_length, allow_inexpensive, true);
					foreach (QueryPart sub_part in or_query.Parts)
						part.Add (sub_part);
					
					query.AddPart (part);
				}
			}

			if (allow_inexpensive && ! inside_an_or) {
				int mime_type;
				mime_type = random.Next (3);

				QueryPart_Or mime_type_part = new QueryPart_Or ();
				QueryPart_Property part;
				part = new QueryPart_Property ();
				part.Type = PropertyType.Keyword;
				part.Key = "beagle:MimeType";

				if (mime_type == 0) {
					part.Value = "inode/directory";
					mime_type_part.Add (part);
					query.AddPart (mime_type_part);
				} else if (mime_type == 1) {
					part.Value = "text/plain";
					mime_type_part.Add (part);
					query.AddPart (mime_type_part);
				}
			}

			// Every query must contain at least
			// one required part.
			bool contains_required;
			contains_required = false;

			for (int i = 0; i < length; ++i) {
				QueryPart_Text part;
				part = new QueryPart_Text ();
				part.Text = Token.GetRandom ();

				// Prohibited parts are not allowed inside an or
				if (contains_required && ! inside_an_or) {
					if (random.Next (2) == 0)
						part.Logic = QueryPartLogic.Prohibited;
				} else {
					// This part will be required.
					contains_required = true;
				}
				
				if (random.Next (2) == 0)
					part.SearchTextProperties = false;
				else if (allow_inexpensive && random.Next (2) == 0)
					part.SearchFullText = false;
				
				query.AddPart (part);
			}

			// Note the ! inside_an_or; date range queries don't
			// work right inside OR queries when being searched
			// within the resolution of one day.  See the FIXME
			// about hit filters in LuceneCommon.cs
			if (allow_inexpensive && ! inside_an_or && random.Next (3) == 0) {
				DateTime a, b;
				FileSystemObject.PickTimestampRange (out a, out b);

				QueryPart_DateRange part;
				part = new QueryPart_DateRange ();
				part.StartDate = a;
				part.EndDate = b;
				query.AddPart (part);
			}

			return query;
		}