a937ad7dafbfd39f5b3edaf7741afbdd1ce97e8f
[idzebra-moved-to-github.git] / index / rankstatic.c
1 /* $Id: rankstatic.c,v 1.5 2006-05-03 09:31:26 marc Exp $
2    Copyright (C) 1995-2005
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 #include <stdio.h>
24 #include <assert.h>
25 #include <limits.h>
26 #ifdef WIN32
27 #include <io.h>
28 #endif
29 #if HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #include "index.h"
34 #include "rank.h"
35
36 static int log_level = 0;
37 static int log_initialized = 0;
38
39 struct rank_set_info {
40     int no_rank_entries;
41 };
42
43 /*
44  * create: Creates/Initialises this rank handler. This routine is 
45  *  called exactly once. The routine returns the class_handle.
46  */
47 static void *create (ZebraHandle zh)
48 {
49     if (!log_initialized)
50     {
51         log_level = yaz_log_module_level("rankstatic");
52         log_initialized = 1;
53     }
54     yaz_log(log_level, "rank-static create");
55     return 0;
56 }
57
58 /*
59  * destroy: Destroys this rank handler. This routine is called
60  *  when the handler is no longer needed - i.e. when the server
61  *  dies. The class_handle was previously returned by create.
62  */
63 static void destroy (struct zebra_register *reg, void *class_handle)
64 {
65     yaz_log(log_level, "rank-static destroy");
66 }
67
68
69 /**
70  * begin: Prepares beginning of "real" ranking. Called once for
71  *  each result set. The returned handle is a "set handle" and
72  *  will be used in each of the handlers below.
73  */
74 static void *begin (struct zebra_register *reg, 
75                     void *class_handle, RSET rset, NMEM nmem,
76                     TERMID *terms, int numterms)
77 {
78     struct rank_set_info *si = 
79         (struct rank_set_info *) nmem_malloc (nmem, sizeof(*si));
80     int i;
81
82     yaz_log(log_level, "rank-static begin");
83     /* count how many terms are ranked (2=102 or similar) */
84     si->no_rank_entries = 0;
85     for (i = 0; i < numterms; i++)
86     {
87         struct ord_list *ol = terms[i]->ol;
88
89         yaz_log(log_level, "i=%d flags=%s '%s'", i, 
90                 terms[i]->flags, terms[i]->name );
91
92         for (; ol; ol = ol->next)
93         {
94             int index_type = 0;
95             const char *db = 0;
96             const char *string_index = 0;
97             int set = -1;
98             int use = -1;
99
100             zebraExplain_lookup_ord(reg->zei,
101                                     ol->ord, &index_type, &db, &set, &use,
102                                     &string_index);
103
104             if (string_index)
105                 yaz_log(log_level, " ord=%d index_type=%c db=%s str-index=%s",
106                     ol->ord, index_type, db, string_index);
107             else
108                 yaz_log(log_level, " ord=%d index_type=%c db=%s set=%d use=%d",
109                     ol->ord, index_type, db, set, use);
110         }
111         if (!strncmp (terms[i]->flags, "rank,", 5)) 
112             (si->no_rank_entries)++;
113     }
114     return si;
115 }
116
117 /*
118  * end: Terminates ranking process. Called after a result set
119  *  has been ranked.
120  */
121 static void end (struct zebra_register *reg, void *set_handle)
122 {
123     yaz_log(log_level, "rank-static end");
124 }
125
126
127 /**
128  * add: Called for each word occurence in a result set. This routine
129  *  should be as fast as possible. This routine should "incrementally"
130  *  update the score.
131  */
132 static void add (void *set_handle, int seqno, TERMID term)
133 {
134 }
135
136 /*
137  * calc: Called for each document in a result. This handler should 
138  *  produce a score based on previous call(s) to the add handler. The
139  *  score should be between 0 and 1000. If score cannot be obtained
140  *  -1 should be returned.
141  */
142 static int calc (void *set_handle, zint sysno, zint staticrank,
143                  int *stop_flag)
144 {
145     struct rank_set_info *si = (struct rank_set_info *) set_handle;
146
147     if (!si->no_rank_entries)
148         return -1;   /* ranking not enabled for any terms */
149
150     /* if we set *stop_flag = 1, we stop processing (of result set list) */
151     /* staticrank = 0 is highest, MAXINT lowest */
152     return INT_MAX - staticrank;  /* but score is reverse (logical) */
153 }
154
155 /*
156  * Pseudo-meta code with sequence of calls as they occur in a
157  * server. Handlers are prefixed by --:
158  *
159  *     server init
160  *     -- create
161  *     foreach search
162  *        rank result set
163  *        -- begin
164  *        foreach record
165  *           foreach word
166  *              -- add
167  *           -- calc
168  *        -- end
169  *     -- destroy
170  *     server close
171  */
172
173 static struct rank_control rank_control = {
174     "rank-static",
175     create,
176     destroy,
177     begin,
178     end,
179     calc,
180     add,
181 };
182  
183 struct rank_control *rank_static_class = &rank_control;