Do not use readdir_r
[idzebra-moved-to-github.git] / index / rank1.c
1 /* $Id: rank1.c,v 1.11 2003-01-13 22:37:12 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24
25 #include <stdio.h>
26 #include <assert.h>
27 #ifdef WIN32
28 #include <io.h>
29 #else
30 #include <unistd.h>
31 #endif
32
33 #include "index.h"
34
35 struct rank_class_info {
36     int dummy;
37 };
38
39 struct rank_term_info {
40     int local_occur;
41     int global_occur;
42     int global_inv;
43     int rank_flag;
44     int rank_weight;
45 };
46
47 struct rank_set_info {
48     int last_pos;
49     int no_entries;
50     int no_rank_entries;
51     struct rank_term_info *entries;
52 };
53
54 static int log2_int (unsigned g)
55 {
56     int n = 0;
57     while ((g = g>>1))
58         n++;
59     return n;
60 }
61
62 /*
63  * create: Creates/Initialises this rank handler. This routine is 
64  *  called exactly once. The routine returns the class_handle.
65  */
66 static void *create (struct zebra_register *reg)
67 {
68     struct rank_class_info *ci = (struct rank_class_info *)
69         xmalloc (sizeof(*ci));
70
71     logf (LOG_DEBUG, "rank-1 create");
72     return ci;
73 }
74
75 /*
76  * destroy: Destroys this rank handler. This routine is called
77  *  when the handler is no longer needed - i.e. when the server
78  *  dies. The class_handle was previously returned by create.
79  */
80 static void destroy (struct zebra_register *reg, void *class_handle)
81 {
82     struct rank_class_info *ci = (struct rank_class_info *) class_handle;
83
84     logf (LOG_DEBUG, "rank-1 destroy");
85     xfree (ci);
86 }
87
88
89 /*
90  * begin: Prepares beginning of "real" ranking. Called once for
91  *  each result set. The returned handle is a "set handle" and
92  *  will be used in each of the handlers below.
93  */
94 static void *begin (struct zebra_register *reg, void *class_handle, RSET rset)
95 {
96     struct rank_set_info *si = (struct rank_set_info *) xmalloc (sizeof(*si));
97     int i;
98
99     logf (LOG_LOG, "rank-1 begin");
100     si->no_entries = rset->no_rset_terms;
101     si->no_rank_entries = 0;
102     si->entries = (struct rank_term_info *)
103         xmalloc (sizeof(*si->entries)*si->no_entries);
104     for (i = 0; i < si->no_entries; i++)
105     {
106         int g = rset->rset_terms[i]->nn;
107         yaz_log(LOG_LOG, "i=%d flags=%s", i, rset->rset_terms[i]->flags);
108         if (!strncmp (rset->rset_terms[i]->flags, "rank,", 5))
109         {
110             si->entries[i].rank_flag = 1;
111             si->entries[i].rank_weight = atoi (rset->rset_terms[i]->flags+5);
112             yaz_log (LOG_LOG, " weight=%d", i, si->entries[i].rank_weight);
113             (si->no_rank_entries)++;
114         }
115         else
116             si->entries[i].rank_flag = 0;
117         si->entries[i].local_occur = 0;
118         si->entries[i].global_occur = g;
119         si->entries[i].global_inv = 32 - log2_int (g);
120         logf (LOG_DEBUG, "-------- %d ------", 32 - log2_int (g));
121     }
122     return si;
123 }
124
125 /*
126  * end: Terminates ranking process. Called after a result set
127  *  has been ranked.
128  */
129 static void end (struct zebra_register *reg, void *set_handle)
130 {
131     struct rank_set_info *si = (struct rank_set_info *) set_handle;
132     logf (LOG_DEBUG, "rank-1 end");
133     xfree (si->entries);
134     xfree (si);
135 }
136
137 /*
138  * add: Called for each word occurence in a result set. This routine
139  *  should be as fast as possible. This routine should "incrementally"
140  *  update the score.
141  */
142 static void add (void *set_handle, int seqno, int term_index)
143 {
144     struct rank_set_info *si = (struct rank_set_info *) set_handle;
145     yaz_log (LOG_LOG, "rank-1 add seqno=%d term_index=%d", seqno, term_index);
146     si->last_pos = seqno;
147     si->entries[term_index].local_occur++;
148 }
149
150 /*
151  * calc: Called for each document in a result. This handler should 
152  *  produce a score based on previous call(s) to the add handler. The
153  *  score should be between 0 and 1000. If score cannot be obtained
154  *  -1 should be returned.
155  */
156 static int calc (void *set_handle, int sysno)
157 {
158     int i, lo, divisor, score = 0;
159     struct rank_set_info *si = (struct rank_set_info *) set_handle;
160
161     if (!si->no_rank_entries)
162         return -1;
163
164     yaz_log(LOG_LOG, "calc");
165     for (i = 0; i < si->no_entries; i++)
166     {
167         yaz_log(LOG_LOG, "i=%d rank_flag=%d lo=%d",
168                 i, si->entries[i].rank_flag, si->entries[i].local_occur);
169         if (si->entries[i].rank_flag && (lo = si->entries[i].local_occur))
170             score += (8+log2_int (lo)) * si->entries[i].global_inv *
171                 si->entries[i].rank_weight;
172     }
173     divisor = si->no_rank_entries * (8+log2_int (si->last_pos/si->no_entries));
174     score = score / divisor;
175     yaz_log (LOG_LOG, "sysno=%d score=%d", sysno, score);
176     if (score > 1000)
177         score = 1000;
178     for (i = 0; i < si->no_entries; i++)
179         si->entries[i].local_occur = 0;
180     return score;
181 }
182
183 /*
184  * Pseudo-meta code with sequence of calls as they occur in a
185  * server. Handlers are prefixed by --:
186  *
187  *     server init
188  *     -- create
189  *     foreach search
190  *        rank result set
191  *        -- begin
192  *        foreach record
193  *           foreach word
194  *              -- add
195  *           -- calc
196  *        -- end
197  *     -- destroy
198  *     server close
199  */
200
201 static struct rank_control rank_control = {
202     "rank-1",
203     create,
204     destroy,
205     begin,
206     end,
207     calc,
208     add,
209 };
210  
211 struct rank_control *rank1_class = &rank_control;