Happy new year
[idzebra-moved-to-github.git] / index / rankstatic.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stdio.h>
21 #include <assert.h>
22 #include <limits.h>
23 #ifdef WIN32
24 #include <io.h>
25 #endif
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #include "index.h"
31 #include "rank.h"
32
33 static int log_level = 0;
34 static int log_initialized = 0;
35
36 struct rank_set_info {
37     int no_rank_entries;
38 };
39
40 /*
41  * create: Creates/Initialises this rank handler. This routine is 
42  *  called exactly once. The routine returns the class_handle.
43  */
44 static void *create (ZebraHandle zh)
45 {
46     if (!log_initialized)
47     {
48         log_level = yaz_log_module_level("rankstatic");
49         log_initialized = 1;
50     }
51     yaz_log(log_level, "rank-static create");
52     return 0;
53 }
54
55 /*
56  * destroy: Destroys this rank handler. This routine is called
57  *  when the handler is no longer needed - i.e. when the server
58  *  dies. The class_handle was previously returned by create.
59  */
60 static void destroy (struct zebra_register *reg, void *class_handle)
61 {
62     yaz_log(log_level, "rank-static destroy");
63 }
64
65
66 /**
67  * begin: Prepares beginning of "real" ranking. Called once for
68  *  each result set. The returned handle is a "set handle" and
69  *  will be used in each of the handlers below.
70  */
71 static void *begin (struct zebra_register *reg, 
72                     void *class_handle, RSET rset, NMEM nmem,
73                     TERMID *terms, int numterms)
74 {
75     struct rank_set_info *si = 
76         (struct rank_set_info *) nmem_malloc (nmem, sizeof(*si));
77     int i;
78
79     yaz_log(log_level, "rank-static begin");
80     /* count how many terms are ranked (2=102 or similar) */
81     si->no_rank_entries = 0;
82     for (i = 0; i < numterms; i++)
83     {
84         struct ord_list *ol = terms[i]->ol;
85
86         yaz_log(log_level, "i=%d flags=%s '%s'", i, 
87                 terms[i]->flags, terms[i]->name );
88
89         for (; ol; ol = ol->next)
90         {
91             const char *index_type = 0;
92             const char *db = 0;
93             const char *string_index = 0;
94             int set = -1;
95             int use = -1;
96
97             zebraExplain_lookup_ord(reg->zei,
98                                     ol->ord, &index_type, &db, &string_index);
99
100             if (string_index)
101                 yaz_log(log_level, " ord=%d index_type=%s db=%s str-index=%s",
102                         ol->ord, index_type, db, string_index);
103             else
104                 yaz_log(log_level, " ord=%d index_type=%s db=%s set=%d use=%d",
105                         ol->ord, index_type, db, set, use);
106         }
107         if (!strncmp (terms[i]->flags, "rank,", 5)) 
108             (si->no_rank_entries)++;
109     }
110     return si;
111 }
112
113 /*
114  * end: Terminates ranking process. Called after a result set
115  *  has been ranked.
116  */
117 static void end (struct zebra_register *reg, void *set_handle)
118 {
119     yaz_log(log_level, "rank-static end");
120 }
121
122
123 /**
124  * add: Called for each word occurence in a result set. This routine
125  *  should be as fast as possible. This routine should "incrementally"
126  *  update the score.
127  */
128 static void add (void *set_handle, int seqno, TERMID term)
129 {
130 }
131
132 /*
133  * calc: Called for each document in a result. This handler should 
134  *  produce a score based on previous call(s) to the add handler. The
135  *  score should be between 0 and 1000. If score cannot be obtained
136  *  -1 should be returned.
137  */
138 static int calc (void *set_handle, zint sysno, zint staticrank,
139                  int *stop_flag)
140 {
141     struct rank_set_info *si = (struct rank_set_info *) set_handle;
142
143     if (!si->no_rank_entries)
144         return -1;   /* ranking not enabled for any terms */
145
146     /* if we set *stop_flag = 1, we stop processing (of result set list) */
147     /* staticrank = 0 is highest, MAXINT lowest */
148     if (staticrank >= INT_MAX)
149         return 0;
150     /* but score is reverse (logical) */
151     return INT_MAX - CAST_ZINT_TO_INT(staticrank);
152 }
153
154 /*
155  * Pseudo-meta code with sequence of calls as they occur in a
156  * server. Handlers are prefixed by --:
157  *
158  *     server init
159  *     -- create
160  *     foreach search
161  *        rank result set
162  *        -- begin
163  *        foreach record
164  *           foreach word
165  *              -- add
166  *           -- calc
167  *        -- end
168  *     -- destroy
169  *     server close
170  */
171
172 static struct rank_control rank_control = {
173     "rank-static",
174     create,
175     destroy,
176     begin,
177     end,
178     calc,
179     add,
180 };
181  
182 struct rank_control *rank_static_class = &rank_control;
183 /*
184  * Local variables:
185  * c-basic-offset: 4
186  * indent-tabs-mode: nil
187  * End:
188  * vim: shiftwidth=4 tabstop=8 expandtab
189  */
190