Pass a stop_flag to ranking method calc. When set to 1, it signals
[idzebra-moved-to-github.git] / index / rankstatic.c
1 /* $Id: rankstatic.c,v 1.3 2005-08-19 11:04:23 adam 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
35 static int log_level = 0;
36 static int log_initialized = 0;
37
38 struct rank_set_info {
39     int no_rank_entries;
40 };
41
42 /*
43  * create: Creates/Initialises this rank handler. This routine is 
44  *  called exactly once. The routine returns the class_handle.
45  */
46 static void *create (ZebraHandle zh)
47 {
48     if (!log_initialized)
49     {
50         log_level = yaz_log_module_level("rankstatic");
51         log_initialized = 1;
52     }
53     yaz_log(log_level, "rank-static create");
54     return 0;
55 }
56
57 /*
58  * destroy: Destroys this rank handler. This routine is called
59  *  when the handler is no longer needed - i.e. when the server
60  *  dies. The class_handle was previously returned by create.
61  */
62 static void destroy (struct zebra_register *reg, void *class_handle)
63 {
64     yaz_log(log_level, "rank-static destroy");
65 }
66
67
68 /**
69  * begin: Prepares beginning of "real" ranking. Called once for
70  *  each result set. The returned handle is a "set handle" and
71  *  will be used in each of the handlers below.
72  */
73 static void *begin (struct zebra_register *reg, 
74                     void *class_handle, RSET rset, NMEM nmem,
75                     TERMID *terms, int numterms)
76 {
77     struct rank_set_info *si = 
78         (struct rank_set_info *) nmem_malloc (nmem, sizeof(*si));
79     int i;
80
81     yaz_log(log_level, "rank-static begin");
82     /* count how many terms are ranked (2=102 or similar) */
83     si->no_rank_entries = 0;
84     for (i = 0; i < numterms; i++)
85     {
86         yaz_log(log_level, "i=%d flags=%s '%s'", i, 
87                 terms[i]->flags, terms[i]->name );
88         if (!strncmp (terms[i]->flags, "rank,", 5)) 
89             (si->no_rank_entries)++;
90     }
91     return si;
92 }
93
94 /*
95  * end: Terminates ranking process. Called after a result set
96  *  has been ranked.
97  */
98 static void end (struct zebra_register *reg, void *set_handle)
99 {
100     yaz_log(log_level, "rank-static end");
101 }
102
103
104 /**
105  * add: Called for each word occurence in a result set. This routine
106  *  should be as fast as possible. This routine should "incrementally"
107  *  update the score.
108  */
109 static void add (void *set_handle, int seqno, TERMID term)
110 {
111 }
112
113 /*
114  * calc: Called for each document in a result. This handler should 
115  *  produce a score based on previous call(s) to the add handler. The
116  *  score should be between 0 and 1000. If score cannot be obtained
117  *  -1 should be returned.
118  */
119 static int calc (void *set_handle, zint sysno, zint staticrank,
120                  int *stop_flag)
121 {
122     struct rank_set_info *si = (struct rank_set_info *) set_handle;
123
124     if (!si->no_rank_entries)
125         return -1;   /* ranking not enabled for any terms */
126
127     /* if we set *stop_flag = 1, we stop processing (of result set list) */
128     /* staticrank = 0 is highest, MAXINT lowest */
129     return INT_MAX - staticrank;  /* but score is reverse (logical) */
130 }
131
132 /*
133  * Pseudo-meta code with sequence of calls as they occur in a
134  * server. Handlers are prefixed by --:
135  *
136  *     server init
137  *     -- create
138  *     foreach search
139  *        rank result set
140  *        -- begin
141  *        foreach record
142  *           foreach word
143  *              -- add
144  *           -- calc
145  *        -- end
146  *     -- destroy
147  *     server close
148  */
149
150 static struct rank_control rank_control = {
151     "rank-static",
152     create,
153     destroy,
154     begin,
155     end,
156     calc,
157     add,
158 };
159  
160 struct rank_control *rank_static_class = &rank_control;