4 // Copyright (C) 2005 Novell, Inc.
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
28 using System
.Collections
;
32 namespace Beagle
.Daemon
{
34 public class HitRegulator
: IQueryResult
{
36 private int max_n_hits
= 100;
38 get { return max_n_hits; }
39 set { max_n_hits = (value > 100) ? 100 : value; }
44 ArrayList hit_array
= new ArrayList ();
45 Hashtable by_uri
=UriFu
.NewHashtable ();
47 const double epsilon
= 1e-8;
48 double cutoff_score
= epsilon
;
50 public Hashtable added_hits
= UriFu
.NewHashtable ();
51 public Hashtable subtracted_uris
= UriFu
.NewHashtable ();
53 public HitRegulator (Queryable queryable
)
55 this.queryable
= queryable
;
58 public bool WillReject (double score
)
60 return score
< cutoff_score
;
63 public bool Add (Hit hit
)
65 if (WillReject (hit
.Score
))
68 int i
= hit_array
.BinarySearch (hit
);
71 hit_array
.Insert (i
, hit
);
73 if (hit_array
.Count
> max_n_hits
) {
75 for (int j
= max_n_hits
; j
< hit_array
.Count
; ++j
) {
76 low_hit
= hit_array
[j
] as Hit
;
77 by_uri
.Remove (low_hit
.Uri
);
78 added_hits
.Remove (low_hit
.Uri
);
80 hit_array
.RemoveRange (max_n_hits
, hit_array
.Count
- max_n_hits
);
82 low_hit
= hit_array
[hit_array
.Count
- 1] as Hit
;
83 cutoff_score
= low_hit
.Score
;
84 //Logger.Log.Debug ("cutoff score is {0:0.00000}", cutoff_score);
87 by_uri
[hit
.Uri
] = hit
;
88 added_hits
[hit
.Uri
] = hit
;
93 public void Subtract (Uri uri
)
98 Hit hit
= by_uri
[uri
] as Hit
;
100 int i
= hit_array
.BinarySearch (hit
);
102 hit_array
.RemoveAt (i
);
103 added_hits
.Remove (uri
);
104 subtracted_uris
[uri
] = uri
;
109 public void Flush (QueryResult result
)
111 ICollection added
, subtracted
;
113 added
= added_hits
.Values
;
114 subtracted
= subtracted_uris
.Values
;
115 added_hits
= new Hashtable ();
116 subtracted_uris
= new Hashtable ();
119 foreach (Hit hit
in added
) {
120 hit
.SourceObject
= queryable
;
121 hit
.SourceObjectName
= queryable
.Name
;
124 result
.Subtract (subtracted
);