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