Fixed bug #165: Use multi-and and multi-or always. WS cleanups.
[idzebra-moved-to-github.git] / include / rset.h
1 /* $Id: rset.h,v 1.45 2005-01-15 20:47:15 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 #ifndef RSET_H
24 #define RSET_H
25
26 #include <stddef.h>
27
28 /* unfortunately we need the isam includes here, for the arguments for */
29 /* rsisamX_create */
30 #include <idzebra/isamb.h> 
31 #include <idzebra/isamc.h> 
32 #include <idzebra/isams.h> 
33
34 YAZ_BEGIN_CDECL
35
36 typedef struct rsfd *RSFD; /* Rset "file descriptor" */
37 typedef struct rset *RSET; /* Result set */
38
39
40 /** 
41  * rset_term is all we need to know of a term to do ranking etc. 
42  * As far as the rsets are concerned, it is just a dummy pointer to
43  * be passed around.
44  */
45
46 struct rset_term {
47     /** the term itself */
48     char *name;
49     char *flags;
50     int  type;
51     /** the rset corresponding to this term */
52     RSET rset;
53     /** private stuff for the ranking algorithm */
54     void *rankpriv;
55 };
56
57 typedef struct rset_term *TERMID; 
58 TERMID rset_term_create (const char *name, int length, const char *flags,
59                                     int type, NMEM nmem);
60
61
62
63 /** rsfd is a "file descriptor" for reading from a rset */
64 struct rsfd {  /* the stuff common to all rsfd's. */
65     RSET rset;  /* ptr to the rset this FD is opened to */
66     void *priv; /* private parameters for this type */
67     RSFD next;  /* to keep lists of used/free rsfd's */
68 };
69
70
71 /** 
72  * rset_control has function pointers to all the important functions
73  * of a rset. Each type of rset will have its own control block, pointing
74  * to the functions for that type. They all have their own create function
75  * which is not part of the control block, as it takes different args for
76  * each type.
77  */
78 struct rset_control
79 {
80     /** text description of set type (for debugging) */
81     char *desc; 
82 /* RSET rs_something_create(const struct rset_control *sel, ...); */
83     void (*f_delete)(RSET ct);
84
85     /** recursively fills the terms array with terms. call with curterm=0 */
86     /* always counts them all into cur, but of course won't touch the term */
87     /* array past max. You can use this to count, set max=0 */
88     void (*f_getterms)(RSET ct, TERMID *terms, int maxterms, int *curterm);
89
90     RSFD (*f_open)(RSET ct, int wflag);
91     void (*f_close)(RSFD rfd);
92     /** forward behaves like a read, but it skips some items first */
93     int (*f_forward)(RSFD rfd, void *buf, TERMID *term, const void *untilbuf);
94     void (*f_pos)(RSFD rfd, double *current, double *total);
95        /* returns -1,-1 if pos function not implemented for this type */
96     int (*f_read)(RSFD rfd, void *buf, TERMID *term);
97     int (*f_write)(RSFD rfd, const void *buf);
98 };
99
100 /** rset_default_forward implements a generic forward with a read-loop */
101 int rset_default_forward(RSFD rfd, void *buf, TERMID *term,
102                      const void *untilbuf);
103
104 /** rset_get_no_terms is a getterms function for those that don't have any */
105 void rset_get_no_terms(RSET ct, TERMID *terms, int maxterms, int *curterm);
106
107 /** 
108  * rset_get_one_term is a getterms function for those rsets that have
109  * exactly one term, like all rsisamX types. 
110  */
111 void rset_get_one_term(RSET ct,TERMID *terms,int maxterms,int *curterm);
112
113 /**
114  * key_control contains all there is to know about the keys stored in 
115  * an isam, and therefore operated by the rsets. Other than this info,
116  * all we assume is that all keys are the same size, and they can be
117  * memcpy'd around
118  */
119 struct key_control {
120     int key_size;
121     int scope;  /* default for what level we operate (book/chapter/verse) on*/
122                 /* usual sysno/seqno is 2 */
123     int (*cmp) (const void *p1, const void *p2);
124     void (*key_logdump_txt) (int logmask, const void *p, const char *txt);
125     zint (*getseq)(const void *p);
126       /* FIXME - Should not need a getseq, it won't make much sense with */
127       /* higher-order keys. Use a (generalized) cmp instead, or something */
128     /* FIXME - decode and encode, and lots of other stuff */
129 };
130
131 /**
132  * A rset is an ordered sequence of keys, either directly from an underlaying
133  * isam, or from one of the higher-level operator rsets (and, or, ...).
134  * Actually, it is "virtual base class", no pure rsets exist in the system,
135  * they all are of some derived type.
136  */
137 typedef struct rset
138 {
139     const struct rset_control *control;
140     const struct key_control *keycontrol;
141     int  count;  /* reference count */
142     void *priv;  /* stuff private to the given type of rset */
143     NMEM nmem;    /* nibble memory for various allocs */
144     char my_nmem; /* Should the nmem be destroyed with the rset?  */
145                   /* 1 if created with it, 0 if passed from above */
146     RSFD free_list; /* all rfd's allocated but not currently in use */
147     int scope;    /* On what level do we count hits and compare them? */
148     TERMID term; /* the term thing for ranking etc */
149 } rset;
150 /* rset is a "virtual base class", which will never exist on its own 
151  * all instances are rsets of some specific type, like rsisamb, or rsbool
152  * They keep their own stuff behind the priv pointer.  */
153
154 /* On the old sysno-seqno type isams, the scope was hard-coded to be 2.
155  * This means that we count hits on the sysno level, and when matching an
156  * 'and', we consider it a match if both term occur within the same sysno.
157  * In more complex isams we can specify on what level we wish to do the
158  * matching and counting of hits. For example, we can have book / chapter /
159  * verse, and a seqno. Scope 2 means then "give me all verses that match",
160  * 3 would be chapters, 4 books. 
161  * The resolution tells how much of the occurences we need to return. If we 
162  * are doing some sort of proximity, we need to get the seqnos of all
163  * occurences, whereas if we are only counting hits, we do not need anything
164  * below the scope. Again 1 is seqnos, 2 sysnos (or verses), 3 books, etc.
165  */
166
167 RSFD rfd_create_base(RSET rs);
168 void rfd_delete_base(RSFD rfd);
169
170 RSET rset_create_base(const struct rset_control *sel, 
171                       NMEM nmem,
172                       const struct key_control *kcontrol,
173                       int scope,
174                       TERMID term);
175
176 void rset_delete(RSET rs);
177 RSET rset_dup (RSET rs);
178
179
180 #define RSETF_READ       0
181 #define RSETF_WRITE      1
182 /* RSFD rset_open(RSET rs, int wflag); */
183 #define rset_open(rs, wflag) (*(rs)->control->f_open)((rs), (wflag))
184
185 /* void rset_close(RSFD rfd); */
186 #define rset_close(rfd) (*(rfd)->rset->control->f_close)(rfd)
187
188 /* int rset_forward(RSFD rfd, void *buf, TERMID term, void *untilbuf); */
189 #define rset_forward(rfd, buf, term, untilbuf) \
190     (*(rfd)->rset->control->f_forward)((rfd),(buf),(term),(untilbuf))
191
192 /* void rset_getterms(RSET ct, TERMID *terms, int maxterms, int *curterm); */
193 #define rset_getterms(ct, terms, maxterms, curterm) \
194     (*(ct)->control->f_getterms)((ct),(terms),(maxterms),(curterm))
195
196 /* int rset_pos(RSFD fd, double *current, double *total); */
197 #define rset_pos(rfd,cur,tot) \
198     (*(rfd)->rset->control->f_pos)((rfd),(cur),(tot))
199
200 /* int rset_read(RSFD rfd, void *buf, TERMID term); */
201 #define rset_read(rfd, buf, term) \
202     (*(rfd)->rset->control->f_read)((rfd), (buf), (term))
203
204 /* int rset_write(RSFD rfd, const void *buf); */
205 #define rset_write(rfd, buf) (*(rfd)->rset->control->f_write)((rfd), (buf))
206
207 /* int rset_type (RSET) */
208 #define rset_type(rs) ((rs)->control->desc)
209
210 /** rset_count counts or estimates the keys in it*/
211 zint rset_count(RSET rs);
212
213 RSET rstemp_create(NMEM nmem, const struct key_control *kcontrol,
214                     int scope, 
215                     const char *temp_path, TERMID term);
216
217 RSET rsnull_create(NMEM nmem, const struct key_control *kcontrol);
218
219 RSET rsbool_create_and(NMEM nmem, const struct key_control *kcontrol,
220                        int scope, RSET rset_l, RSET rset_r);
221
222 RSET rsbool_create_or (NMEM nmem, const struct key_control *kcontrol,
223                        int scope, RSET rset_l, RSET rset_r);
224
225 RSET rsbool_create_not(NMEM nmem, const struct key_control *kcontrol,
226                        int scope, RSET rset_l, RSET rset_r);
227
228 RSET rsbetween_create(NMEM nmem, const struct key_control *kcontrol,
229                       int scope, RSET rset_l, RSET rset_m, RSET rset_r, 
230                       RSET rset_attr);
231
232 RSET rsmulti_or_create(NMEM nmem, const struct key_control *kcontrol,
233                        int scope, int no_rsets, RSET* rsets);
234
235 RSET rsmulti_and_create(NMEM nmem, const struct key_control *kcontrol,
236                         int scope, int no_rsets, RSET* rsets);
237
238 RSET rsprox_create(NMEM nmem, const struct key_control *kcontrol,
239                    int scope, int rset_no, RSET *rset,
240                    int ordered, int exclusion, int relation, int distance);
241
242 RSET rsisamb_create(NMEM nmem, const struct key_control *kcontrol,
243                     int scope, ISAMB is, ISAMB_P pos, TERMID term);
244
245 RSET rsisamc_create(NMEM nmem, const struct key_control *kcontrol,
246                     int scope, ISAMC is, ISAMC_P pos, TERMID term);
247
248 RSET rsisams_create(NMEM nmem, const struct key_control *kcontrol,
249                     int scope, ISAMS is, ISAMS_P pos, TERMID term);
250
251
252 YAZ_END_CDECL
253
254 #endif