static void Main() { int trial = 0; Random rng = new Random(); for (int j = 0; j < 1000; ++j) { ++trial; Console.WriteLine("Trial #{0}", trial); TopScores top; top = new TopScores(6 + rng.Next(100)); ArrayList all = new ArrayList(); int i; int N = 100 + rng.Next(100); for (i = 0; i < N; ++i) { int n = rng.Next(200); all.Add(n); top.Add(n, n); } all.Sort(); i = all.Count - 1; foreach (object obj in top.TopScoringObjects) { if (!obj.Equals(all [i])) { Console.WriteLine("Mismatch!"); i = all.Count - 1; foreach (object x in top.TopScoringObjects) { Console.WriteLine("{0} {1} {2}", x, all [i], x.Equals(all [i]) ? "" : "***"); --i; } return; } --i; } } }
static void Main () { int trial = 0; Random rng = new Random (); for (int j = 0; j < 1000; ++j) { ++trial; Console.WriteLine ("Trial #{0}", trial); TopScores top; top = new TopScores (6 + rng.Next (100)); ArrayList all = new ArrayList (); int i; int N = 100 + rng.Next (100); for (i = 0; i < N; ++i) { int n = rng.Next (200); all.Add (n); top.Add (n, n); } all.Sort (); i = all.Count - 1; foreach (object obj in top.TopScoringObjects) { if (! obj.Equals (all [i])) { Console.WriteLine ("Mismatch!"); i = all.Count - 1; foreach (object x in top.TopScoringObjects) { Console.WriteLine ("{0} {1} {2}", x, all [i], x.Equals (all [i]) ? "" : "***"); --i; } return; } --i; } } }
private static ArrayList FindRecentResults (IndexReader primary_reader, IndexReader secondary_reader, BetterBitArray primary_matches, Dictionary<int, Hit> hits_by_id, int max_results, ref int total_number_of_matches, HitFilter hit_filter, string index_name) { Stopwatch b = new Stopwatch (); b.Start (); int count = 0; Document doc; ArrayList all_docs = null; TopScores top_docs = null; TermDocs term_docs = null; if (primary_matches.TrueCount > max_results) top_docs = new TopScores (max_results); else all_docs = new ArrayList (primary_matches.TrueCount); if (secondary_reader != null) term_docs = secondary_reader.TermDocs (); for (int match_index = primary_matches.Count; ; match_index --) { // Walk across the matches backwards, since newer // documents are more likely to be at the end of // the index. match_index = primary_matches.GetPreviousTrueIndex (match_index); if (match_index < 0) break; count++; doc = primary_reader.Document (match_index, fields_timestamp_uri); // Check the timestamp --- if we have already reached our // limit, we might be able to reject it immediately. string timestamp_str; long timestamp_num = 0; timestamp_str = doc.Get ("Timestamp"); if (timestamp_str == null) { Logger.Log.Warn ("No timestamp on {0}!", GetUriFromDocument (doc)); } else { timestamp_num = Int64.Parse (doc.Get ("Timestamp")); if (top_docs != null && ! top_docs.WillAccept (timestamp_num)) continue; } // Get the actual hit now // doc was created with only 2 fields, so first get the complete lucene document for primary document. // Also run our hit_filter now, if we have one. Since we insist of returning max_results // most recent hits, any hits that would be filtered out should happen now and not later. Hit hit = CreateHit (primary_reader.Document (match_index), secondary_reader, term_docs); if (hit_filter != null && ! hit_filter (hit)) { if (Debug) Log.Debug ("Filtered out {0}", hit.Uri); total_number_of_matches --; continue; } hits_by_id [match_index] = hit; // Add the document to the appropriate data structure. // We use the timestamp_num as the score, so high // scores correspond to more-recent timestamps. if (all_docs != null) all_docs.Add (hit); else top_docs.Add (timestamp_num, hit); } if (term_docs != null) term_docs.Close (); b.Stop (); if (Debug) Log.Debug (">>> {0}: Instantiated and scanned {1} documents in {2}", index_name, count, b); if (all_docs != null) { // Sort results before sending all_docs.Sort (); return all_docs; } else { return top_docs.TopScoringObjects; } }