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