/// <summary> /// ALL processing happens here, since we are not using ASP.NET controls or events. /// Page_Load will: /// * check the Cache for a catalog to use /// * if not, check the filesystem for a serialized cache /// * and if STILL not, Server.Transfer to the Spider to build a new cache /// * check the QueryString for search arguments (and if so, do a search) /// * otherwise just show the HTML of this page - a blank search form /// </summary> public void Page_Load() { // prevent Searcharoo from indexing itself (ie. it's own results page) if ((Request.UserAgent != null) && (Request.UserAgent.ToLower().IndexOf("searcharoo") > 0)) { Response.Clear(); Response.End(); return; } bool getCatalog = false; try { // see if there is a catalog object in the cache _Catalog = (Catalog)Application["Searcharoo_Catalog"]; // if so, get the _WordCount _WordCount = _Catalog.Length; _Cache = (Searcharoo.Common.Cache)Application["Searcharoo_Cache"]; } catch (Exception ex) { // otherwise, we'll need to build the catalog log.Warn("Catalog object unavailable : building a new one!"); _Catalog = null; // in case _Cache = null; } ucSearchPanelHeader.WordCount = _WordCount; //ucSearchPanelFooter.WordCount = _WordCount; if (_Catalog == null) { getCatalog = true; } else if (_Catalog.Length == 0) { getCatalog = true; } if (getCatalog) { // Create the thread object, passing in the Alpha.Beta method // via a ThreadStart delegate. This does not start the thread. SearchCatalogInit sci = new SearchCatalogInit(); Thread t = new Thread(() => sci.GetCatalog(this)); t.Start(); if ((string)Application["CatalogLoad"] == "") { // Still no Catalog, so we have to start building a new one if (_Catalog == null) { _Catalog = (Catalog)Application["Searcharoo_Catalog"]; _Cache = (Searcharoo.Common.Cache)Application["Searcharoo_Cache"]; } } } if (this.SearchQuery == "") { ucSearchPanelHeader.IsSearchResultsPage = false; } else { //refactored into class - catalog can be build via a console application as well as the SearchSpider.aspx page Searcharoo.Engine.Search se = new Searcharoo.Engine.Search(); SortedList output = this.GetSearchResults(se); // se.GetResults(this.SearchQuery, _Catalog); _NumberOfMatches = output.Count.ToString(); if (output.Count > 0) { _PagedResults.DataSource = output.GetValueList(); _PagedResults.AllowPaging = true; _PagedResults.PageSize = MaxResultsPerPage; //;Preferences.ResultsPerPage; //10; _PagedResults.CurrentPageIndex = Request.QueryString["page"] == null ? 0 : Convert.ToInt32(Request.QueryString["page"]) - 1; _Matches = se.SearchQueryMatchHtml; _DisplayTime = se.DisplayTime; _Geocoded = se.GeocodedMatches; SearchResults.DataSource = _PagedResults; SearchResults.DataBind(); } else { lblNoSearchResults.Visible = true; } // Set the display info in the top & bottom user controls ucSearchPanelHeader.Word = this.SearchQuery; ucSearchPanelHeader.IsSearchResultsPage = true; } }
public void GetCatalog(Page pg) { if ((string)pg.Application["CatalogLoad"] == "") { pg.Application.Lock(); pg.Application["CatalogLoad"] = "Loading catalog in progress..."; pg.Application.UnLock(); log.Info("Loading catolog in progress..."); // No catalog 'in memory', so let's look for one // First, for a serialized version on disk try { _Catalog = Catalog.Load(); // returns null if not found } catch (Exception ex) { log.Error("Loading catalog error", ex); } pg.Application.Lock(); pg.Application["CatalogLoad"] = "Loading cache catalog in progress..."; pg.Application.UnLock(); log.Info("Loading cache catolog in progress..."); try { _Cache = Searcharoo.Common.Cache.Load(); _Catalog.FileCache = _Cache; } catch (Exception ex) { log.Error("Loading cache catalog error", ex); } // Still no Catalog, so we have to start building a new one if (_Catalog == null) { _Catalog = (Catalog)pg.Application["Searcharoo_Catalog"]; _Cache = (Searcharoo.Common.Cache)pg.Application["Searcharoo_Cache"]; if (_Catalog != null) { log.Info("Catalog retrieved from Cache[] " + _Catalog.Words); } } else { // Yep, there was a serialized catalog file // Don't forget to add to cache for next time (the Spider does this too) pg.Application.Lock(); pg.Application["Searcharoo_Catalog"] = _Catalog; pg.Application["Searcharoo_Cache"] = _Cache; pg.Application.UnLock(); if (_Catalog != null) { log.Info("Deserialized catalog and put in Cache[] " + _Catalog.Words); } } pg.Application.Lock(); pg.Application["CatalogLoad"] = ""; pg.Application.UnLock(); } }