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