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