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