1a50c6525f59f60e17b6af37e36ad45b1fbe0196
[idzebra-moved-to-github.git] / include / rset.h
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #ifndef RSET_H
21 #define RSET_H
22
23 #include <yaz/yaz-util.h>
24 /* unfortunately we need the isam includes here, for the arguments for */
25 /* rsisamX_create */
26 #include <idzebra/isamb.h> 
27 #include <idzebra/isamc.h> 
28 #include <idzebra/isams.h> 
29
30 YAZ_BEGIN_CDECL
31
32 typedef struct rsfd *RSFD;
33 typedef struct rset *RSET;
34
35 struct ord_list {
36     int ord;
37     struct ord_list *next;
38 };
39
40 struct ord_list *ord_list_create(NMEM nmem);
41 struct ord_list *ord_list_append(NMEM nmem, struct ord_list *list, int ord);
42 struct ord_list *ord_list_dup(NMEM nmem, struct ord_list *list);
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 struct rset_term {
50     char *name;    /** the term itself in internal encoding (UTF-8/raw) */
51     char *flags;   /** flags for rank method */
52     int  type;     /** Term_type from RPN Query. Actually this
53                        is Z_Term_general, Z_Term_numeric,
54                        Z_Term_characterString, ..
55                        This info is used to return encoded term back for
56                        search-result-1 .
57                    */
58     int reg_type;  /** register type */
59     RSET rset;     /** the rset corresponding to this term */
60     void *rankpriv;/** private stuff for the ranking algorithm */
61     zint hits_limit;/** limit for hits if > 0 */
62     char *ref_id;  /** reference for this term */
63     struct ord_list *ol;
64 };
65
66 typedef struct rset_term *TERMID; 
67 TERMID rset_term_create (const char *name, int length, const char *flags,
68                          int type, NMEM nmem, struct ord_list *ol,
69                          int reg_type, zint hits_limit, const char *ref_id);
70
71 /** rsfd is a "file descriptor" for reading from a rset */
72 struct rsfd {  /* the stuff common to all rsfd's. */
73     RSET rset;  /* ptr to the rset this FD is opened to */
74     void *priv; /* private parameters for this type */
75     RSFD next;  /* to keep lists of used/free rsfd's */
76     zint counted_items;
77     char *counted_buf;
78 };
79
80
81 /** 
82  * rset_control has function pointers to all the important functions
83  * of a rset. Each type of rset will have its own control block, pointing
84  * to the functions for that type. They all have their own create function
85  * which is not part of the control block, as it takes different args for
86  * each type.
87  */
88 struct rset_control
89 {
90     /** text description of set type (for debugging) */
91     char *desc; 
92 /* RSET rs_something_create(const struct rset_control *sel, ...); */
93     void (*f_delete)(RSET ct);
94
95     /** recursively fills the terms array with terms. call with curterm=0 */
96     /* always counts them all into cur, but of course won't touch the term */
97     /* array past max. You can use this to count, set max=0 */
98     void (*f_getterms)(RSET ct, TERMID *terms, int maxterms, int *curterm);
99
100     RSFD (*f_open)(RSET ct, int wflag);
101     void (*f_close)(RSFD rfd);
102     /** forward behaves like a read, but it skips some items first */
103     int (*f_forward)(RSFD rfd, void *buf, TERMID *term, const void *untilbuf);
104     void (*f_pos)(RSFD rfd, double *current, double *total);
105        /* returns -1,-1 if pos function not implemented for this type */
106     int (*f_read)(RSFD rfd, void *buf, TERMID *term);
107     int (*f_write)(RSFD rfd, const void *buf);
108 };
109
110 /** rset_default_forward implements a generic forward with a read-loop */
111 int rset_default_forward(RSFD rfd, void *buf, TERMID *term,
112                      const void *untilbuf);
113
114 /** rset_default_read implements a generic read */
115 int rset_default_read(RSFD rfd, void *buf, TERMID *term);
116
117 void rset_get_one_term(RSET ct,TERMID *terms,int maxterms,int *curterm);
118
119 /**
120  * key_control contains all there is to know about the keys stored in 
121  * an isam, and therefore operated by the rsets. Other than this info,
122  * all we assume is that all keys are the same size, and they can be
123  * memcpy'd around
124  */
125 struct rset_key_control {
126     void *context;
127     int key_size;
128     int scope;  /* default for what level we operate (book/chapter/verse) on*/
129                 /* usual sysno/seqno is 2 */
130     int (*cmp)(const void *p1, const void *p2);
131     void (*key_logdump_txt) (int logmask, const void *p, const char *txt);
132     zint (*getseq)(const void *p);
133     zint (*get_segment)(const void *p);
134     int (*filter_func)(const void *p, void *data);
135     void *filter_data;
136     void (*inc)(struct rset_key_control *kc);
137     void (*dec)(struct rset_key_control *kc);
138     /* FIXME - Should not need a getseq, it won't make much sense with */
139     /* higher-order keys. Use a (generalized) cmp instead, or something */
140     /* FIXME - decode and encode, and lots of other stuff */
141 };
142
143 /**
144  * A rset is an ordered sequence of keys, either directly from an underlaying
145  * isam, or from one of the higher-level operator rsets (and, or, ...).
146  * Actually, it is "virtual base class", no pure rsets exist in the system,
147  * they all are of some derived type.
148  */
149 typedef struct rset
150 {
151     const struct rset_control *control;
152     struct rset_key_control *keycontrol;
153     int  refcount;   /* reference count */
154     void *priv;      /* stuff private to the given type of rset */
155     NMEM nmem;       /* nibble memory for various allocs */
156     RSFD free_list;  /* all rfd's allocated but not currently in use */
157     RSFD use_list;   /* all rfd's in use */
158     int scope;       /* On what level do we count hits and compare them? */
159     TERMID term;     /* the term thing for ranking etc */
160     int no_children;
161     RSET *children;
162     zint hits_limit;
163     zint hits_count;
164     zint hits_round;
165     int hits_approx; 
166 } rset;
167 /* rset is a "virtual base class", which will never exist on its own 
168  * all instances are rsets of some specific type, like rsisamb, or rsbool
169  * They keep their own stuff behind the priv pointer.  */
170
171 /* On the old sysno-seqno type isams, the scope was hard-coded to be 2.
172  * This means that we count hits on the sysno level, and when matching an
173  * 'and', we consider it a match if both term occur within the same sysno.
174  * In more complex isams we can specify on what level we wish to do the
175  * matching and counting of hits. For example, we can have book / chapter /
176  * verse, and a seqno. Scope 2 means then "give me all verses that match",
177  * 3 would be chapters, 4 books. 
178  * The resolution tells how much of the occurences we need to return. If we 
179  * are doing some sort of proximity, we need to get the seqnos of all
180  * occurences, whereas if we are only counting hits, we do not need anything
181  * below the scope. Again 1 is seqnos, 2 sysnos (or verses), 3 books, etc.
182  */
183
184 RSFD rfd_create_base(RSET rs);
185 int rfd_is_last(RSFD rfd);
186
187 RSET rset_create_base(const struct rset_control *sel, 
188                       NMEM nmem,
189                       struct rset_key_control *kcontrol,
190                       int scope,
191                       TERMID term,
192                       int no_children, RSET *children);
193
194 void rset_delete(RSET rs);
195 RSET rset_dup (RSET rs);
196 void rset_close(RSFD rfd);
197
198 #define RSETF_READ       0
199 #define RSETF_WRITE      1
200 /* RSFD rset_open(RSET rs, int wflag); */
201 #define rset_open(rs, wflag) (*(rs)->control->f_open)((rs), (wflag))
202
203 /* int rset_forward(RSFD rfd, void *buf, TERMID term, void *untilbuf); */
204 #define rset_forward(rfd, buf, term, untilbuf) \
205     rset_default_forward((rfd), (buf), (term), (untilbuf))
206
207 /* void rset_getterms(RSET ct, TERMID *terms, int maxterms, int *curterm); */
208 #define rset_getterms(ct, terms, maxterms, curterm) \
209     (*(ct)->control->f_getterms)((ct),(terms),(maxterms),(curterm))
210
211 /* int rset_pos(RSFD fd, double *current, double *total); */
212 #define rset_pos(rfd,cur,tot) \
213     (*(rfd)->rset->control->f_pos)((rfd),(cur),(tot))
214
215 /* int rset_read(RSFD rfd, void *buf, TERMID term); */
216 #define rset_read(rfd, buf, term) rset_default_read((rfd), (buf), (term))
217
218 /* int rset_write(RSFD rfd, const void *buf); */
219 #define rset_write(rfd, buf) (*(rfd)->rset->control->f_write)((rfd), (buf))
220
221 /* int rset_type (RSET) */
222 #define rset_type(rs) ((rs)->control->desc)
223
224 /** rset_count counts or estimates the keys in it*/
225 zint rset_count(RSET rs);
226
227 RSET rset_create_temp(NMEM nmem, struct rset_key_control *kcontrol,
228                       int scope, const char *temp_path, TERMID term);
229
230 RSET rset_create_null(NMEM nmem, struct rset_key_control *kcontrol, TERMID term);
231
232 RSET rset_create_not(NMEM nmem, struct rset_key_control *kcontrol,
233                      int scope, RSET rset_l, RSET rset_r);
234
235 RSET rset_create_between(NMEM nmem, struct rset_key_control *kcontrol,
236                          int scope, RSET rset_l, RSET rset_m, RSET rset_r, 
237                          RSET rset_attr);
238
239 RSET rset_create_or(NMEM nmem, struct rset_key_control *kcontrol,
240                           int scope, TERMID termid, int no_rsets, RSET* rsets);
241
242 RSET rset_create_and(NMEM nmem, struct rset_key_control *kcontrol,
243                      int scope, int no_rsets, RSET* rsets);
244
245 RSET rset_create_prox(NMEM nmem, struct rset_key_control *kcontrol,
246                       int scope, int rset_no, RSET *rset,
247                       int ordered, int exclusion, int relation, int distance);
248
249 RSET rsisamb_create(NMEM nmem, struct rset_key_control *kcontrol,
250                     int scope, ISAMB is, ISAM_P pos, TERMID term);
251
252 RSET rsisamc_create(NMEM nmem, struct rset_key_control *kcontrol,
253                     int scope, ISAMC is, ISAM_P pos, TERMID term);
254
255 RSET rsisams_create(NMEM nmem, struct rset_key_control *kcontrol,
256                     int scope, ISAMS is, ISAM_P pos, TERMID term);
257
258 void rset_visit(RSET rset, int level);
259
260 void rset_set_hits_limit(RSET rs, zint l);
261
262 YAZ_END_CDECL
263
264 #endif
265 /*
266  * Local variables:
267  * c-basic-offset: 4
268  * indent-tabs-mode: nil
269  * End:
270  * vim: shiftwidth=4 tabstop=8 expandtab
271  */
272