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