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