public static void Main(string[] args) { // Initialize GObject type system g_type_init(); Beagrep.Util.Log.Level = LogLevel.Always; // shhhh... silence if (args.Length == 0 || Array.IndexOf(args, "--help") > -1 || Array.IndexOf(args, "--usage") > -1) { PrintUsageAndExit(); } if (Array.IndexOf(args, "--version") > -1) { VersionFu.PrintVersion(); Environment.Exit(0); } StringBuilder query_str = new StringBuilder(); query = new Query(); // Parse args int i = 0; string next_arg; while (i < args.Length) { switch (args [i]) { case "--verbose": verbose = true; break; case "--cache": display_cached_text = true; break; case "--stats-only": verbose = true; display_hits = false; break; case "--max-hits": if (++i >= args.Length) { PrintUsageAndExit(); } query.MaxHits = Int32.Parse(args[i]); break; case "--list-backends": Console.WriteLine("Current available backends:"); Console.Write(QueryDriver.ListBackends()); Environment.Exit(0); break; case "--backend": if (++i >= args.Length) { PrintUsageAndExit(); } next_arg = args [i]; if (next_arg.StartsWith("--")) { Console.WriteLine("--backend requires a backend name. Invalid name '{0}'", next_arg); Environment.Exit(1); break; } if (next_arg [0] != '+' && next_arg [0] != '-') { QueryDriver.OnlyAllow(next_arg); } else { if (next_arg [0] == '+') { QueryDriver.Allow(next_arg.Substring(1)); } else { QueryDriver.Deny(next_arg.Substring(1)); } } break; case "--add-static-backend": if (++i >= args.Length) { PrintUsageAndExit(); } next_arg = args [i]; if (!next_arg.StartsWith("--")) { QueryDriver.AddStaticQueryable(next_arg); } break; case "--keywords": PropertyKeywordFu.ReadKeywordMappings(); Console.WriteLine("Supported query keywords are:"); foreach (string key in PropertyKeywordFu.Keys) { foreach (QueryKeywordMapping mapping in PropertyKeywordFu.Properties(key)) { // Dont print properties without description; they confuse people if (string.IsNullOrEmpty(mapping.Description)) { continue; } Console.WriteLine(" {0,-20} for {1}", key, mapping.Description); } } System.Environment.Exit(0); break; default: if (args [i].StartsWith("--")) { PrintUsageAndExit(); } if (query_str.Length > 0) { query_str.Append(' '); } query_str.Append(args [i]); break; } ++i; } if (verbose) { Beagrep.Util.Log.Level = LogLevel.Debug; } if (query_str.Length > 0) { query.AddText(query_str.ToString()); } Stopwatch watch = new Stopwatch(); watch.Start(); StartQueryDriver(); watch.Stop(); if (verbose) { Console.WriteLine("QueryDriver started in {0}", watch); } QueryResult result = new QueryResult(); result.HitsAddedEvent += OnHitsAdded; result.FinishedEvent += OnFinished; queryStartTime = DateTime.Now; QueryDriver.DoQueryLocal(query, result); }
//Full beagledQuery public SearchResult doQuery(SearchRequest sreq, bool isLocalReq) { SearchResult sr = null; if (sreq.text == null || sreq.text.Length == 0 || (sreq.text.Length == 1 && sreq.text[0].Trim() == "")) { sr = new SearchResult(); sr.statusCode = SC_INVALID_QUERY; sr.statusMsg = "Error: No search terms specified"; return(sr); } Query query = new Query(); string searchString = ""; foreach (string text in sreq.text) { query.AddText(text); searchString += (searchString.Length == 0) ? text:" " + text; } Logger.Log.Info("WebServiceBackEnd: Received {0} WebService Query with search term: {1}", isLocalReq ? "Local":"External", searchString.Trim()); if (sreq.mimeType != null && sreq.mimeType[0] != null) { foreach (string mtype in sreq.mimeType) { query.AddMimeType(mtype); } } if (sreq.searchSources != null && sreq.searchSources[0] != null) { foreach (string src in sreq.searchSources) { query.AddSource(src); } } //If needed, check to restrict queries to System or Neighborhood domain, can be added here if (sreq.qdomain > 0) { query.AddDomain(sreq.qdomain); } if (!isLocalReq) //External Request, check if this Node is already processing it { lock (this) { if ((sreq.searchId != 0) && NetworkedBeagle.IsCachedRequest(sreq.searchId)) { sr = new SearchResult(); sr.numResults = sr.totalResults = sr.firstResultIndex = 0; sr.hitResults = new HitResult[sr.numResults]; sr.searchToken = ""; sr.statusCode = SC_DUPLICATE_QUERY; sr.statusMsg = "Error: Duplicate Query loopback"; Logger.Log.Warn("WebServiceBackEnd: Received duplicate Query for a query already in process!"); Logger.Log.Warn("WebServiceBackEnd: Check NetBeagle configuration on all nodes to remove possible loops"); } if (sreq.hopCount >= 5) { //If request has traversed 5 nodes in reaching here, stop cascading. //Make it a Local Query. query.RemoveDomain(sreq.qdomain); query.AddDomain(QueryDomain.System); } if ((sr == null) && (sreq.searchId != 0)) { NetworkedBeagle.CacheRequest(query, sreq.searchId, sreq.hopCount + 1); } } if (sr != null) { return(sr); } //Logger.Log.Info("New external Query: searchId = {0}", sreq.searchId); } ArrayList results = ArrayList.Synchronized(new ArrayList()); QueryResult qres = new QueryResult(); string searchToken = TokenGenerator(); SessionData sdata = new SessionData(query, results, isLocalReq); AttachQueryResult(qres, sdata); /* Include this code, if sessionID passed from front-end: * if (sessionTable.Contains(searchToken)) * sessionTable[searchToken] = sdata; * else */ sessionTable.Add(searchToken, sdata); QueryDriver.DoQueryLocal(query, qres); while (resultTable.Contains(qres) && (results.Count < MAX_RESULTS_PER_CALL)) { Thread.Sleep(100); } //Console.WriteLine("WebServiceBackEnd: Got {0} results from beagled", results.Count); sr = new SearchResult(); if (results.Count > 0) { lock (results.SyncRoot) { //Lock results ArrayList to prevent more Hits added till we've processed doQuery sr.numResults = results.Count < MAX_RESULTS_PER_CALL ? results.Count: MAX_RESULTS_PER_CALL; sr.hitResults = new HitResult[sr.numResults]; string hitUri; for (int i = 0; i < sr.numResults; i++) { Hit h = (Hit)results[i]; string snippet; //Queryable queryable = h.SourceObject as Queryable; Queryable queryable = QueryDriver.GetQueryable(h.SourceObjectName); if (queryable == null) { snippet = "ERROR: hit.SourceObject is null, uri=" + h.Uri; } else { snippet = queryable.GetSnippet(ICollection2StringList(query.StemmedText), h); } sr.hitResults[i] = new HitResult(); hitUri = h.UriAsString; if (isLocalReq || hitUri.StartsWith(NetworkedBeagle.BeagleNetPrefix)) { sr.hitResults[i].uri = hitUri; } else { sr.hitResults[i].uri = AccessFilter.TranslateHit(h); } sr.hitResults[i].resourceType = h.Type; sr.hitResults[i].mimeType = h.MimeType; sr.hitResults[i].source = h.Source; sr.hitResults[i].score = h.Score; int plen = h.Properties.Count; sr.hitResults[i].properties = new HitProperty[plen]; for (int j = 0; j < plen; j++) { Property p = (Property)h.Properties[j]; sr.hitResults[i].properties[j] = new HitProperty(); sr.hitResults[i].properties[j].PKey = p.Key; sr.hitResults[i].properties[j].PVal = p.Value; sr.hitResults[i].properties[j].IsMutable = p.IsMutable; sr.hitResults[i].properties[j].IsSearched = p.IsSearched; } sr.hitResults[i].hashCode = h.GetHashCode(); if (snippet != null) { sr.hitResults[i].snippet = snippet.Trim(); } } } //end lock } // end if else { sr.numResults = 0; sr.hitResults = new HitResult[sr.numResults]; } sr.totalResults = results.Count; sr.firstResultIndex = 0; sr.searchToken = ""; if (sr.totalResults > 0) { sr.searchToken = searchToken; } sr.statusCode = SC_QUERY_SUCCESS; sr.statusMsg = "Success"; Logger.Log.Info("WebServiceBackEnd: Total Results = " + sr.totalResults); return(sr); }
public string doQuery(webArgs wargs) { if (wargs.sessId == null || wargs.searchString == null || wargs.searchString == "") { return(NO_RESULTS); } log.Debug("WebBackEnd: Got Search String: " + wargs.searchString); Query query = new Query(); query.AddText(wargs.searchString); if (wargs.searchSource != null && wargs.searchSource != "") { query.AddSource(wargs.searchSource); query.AddDomain(QueryDomain.System); } else { query.AddDomain(wargs.globalSearch ? QueryDomain.Global:QueryDomain.System); } QueryResult qres = new QueryResult(); //Note: QueryDriver.DoQuery() local invocation is used. //The root tile is used only for adding hits and generating html. BT.SimpleRootTile root = new BT.SimpleRootTile(); root.Query = query; //root.SetSource (searchSource); Do not SetSource on root! ResultPair rp = new ResultPair(root); bufferRenderContext bctx = new bufferRenderContext(rp); Resp resp = new Resp(rp, bctx, wargs.isLocalReq); AttachQueryResult(qres, resp); //Add sessionId-Resp mapping if (sessionResp.Contains(wargs.sessId)) { sessionResp[wargs.sessId] = resp; } else { sessionResp.Add(wargs.sessId, resp); } log.Info("WebBackEnd: Starting Query for string \"{0}\"", wargs.searchString); QueryDriver.DoQueryLocal(query, qres); //Wait only till we have enough results to display while ((result.Contains(qres)) && (root.HitCollection.NumResults < 10)) { Thread.Sleep(100); } if (root.HitCollection.IsEmpty) { return(NO_RESULTS); } lock (root) { root.Render(bctx); return(getResultsLabel(root) + (wargs.isLocalReq ? bctx.buffer:bctx.bufferForExternalQuery)); } }