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