Work on truncation.
[idzebra-moved-to-github.git] / index / zsets.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zsets.c,v $
7  * Revision 1.11  1996-12-23 15:30:46  adam
8  * Work on truncation.
9  * Bug fix: result sets weren't deleted after server shut down.
10  *
11  * Revision 1.10  1995/10/30 15:08:08  adam
12  * Bug fixes.
13  *
14  * Revision 1.9  1995/10/17  18:02:14  adam
15  * New feature: databases. Implemented as prefix to words in dictionary.
16  *
17  * Revision 1.8  1995/10/10  13:59:25  adam
18  * Function rset_open changed its wflag parameter to general flags.
19  *
20  * Revision 1.7  1995/10/06  14:38:01  adam
21  * New result set method: r_score.
22  * Local no (sysno) and score is transferred to retrieveCtrl.
23  *
24  * Revision 1.6  1995/09/28  09:19:49  adam
25  * xfree/xmalloc used everywhere.
26  * Extract/retrieve method seems to work for text records.
27  *
28  * Revision 1.5  1995/09/27  16:17:32  adam
29  * More work on retrieve.
30  *
31  * Revision 1.4  1995/09/07  13:58:36  adam
32  * New parameter: result-set file descriptor (RSFD) to support multiple
33  * positions within the same result-set.
34  * Boolean operators: and, or, not implemented.
35  * Result-set references.
36  *
37  * Revision 1.3  1995/09/06  16:11:19  adam
38  * Option: only one word key per file.
39  *
40  * Revision 1.2  1995/09/06  10:33:04  adam
41  * More work on present. Some log messages removed.
42  *
43  * Revision 1.1  1995/09/05  15:28:40  adam
44  * More work on search engine.
45  *
46  */
47 #include <stdio.h>
48 #include <assert.h>
49 #include <unistd.h>
50
51 #include "zserver.h"
52 #include <rstemp.h>
53
54 ZServerSet *resultSetAdd (ZServerInfo *zi, const char *name, int ov, RSET rset)
55 {
56     ZServerSet *s;
57
58     for (s = zi->sets; s; s = s->next)
59         if (!strcmp (s->name, name))
60         {
61             if (!ov)
62                 return NULL;
63             rset_delete (s->rset);
64             s->rset = rset;
65             return s;
66         }
67     s = xmalloc (sizeof(*s));
68     s->next = zi->sets;
69     zi->sets = s;
70     s->name = xmalloc (strlen(name)+1);
71     strcpy (s->name, name);
72     s->rset = rset;
73     return s;
74 }
75
76 ZServerSet *resultSetGet (ZServerInfo *zi, const char *name)
77 {
78     ZServerSet *s;
79
80     for (s = zi->sets; s; s = s->next)
81         if (!strcmp (s->name, name))
82             return s;
83     return NULL;
84 }
85
86 void resultSetDestroy (ZServerInfo *zi)
87 {
88     ZServerSet *s, *s1;
89
90     for (s = zi->sets; s; s = s1)
91     {
92         s1 = s->next;
93         rset_delete (s->rset);
94         xfree (s->name);
95         xfree (s);
96     }
97     zi->sets = NULL;
98 }
99
100 ZServerSetSysno *resultSetSysnoGet (ZServerInfo *zi, const char *name, 
101                                     int num, int *positions)
102 {
103     ZServerSet *sset;
104     ZServerSetSysno *sr;
105     RSET rset;
106     int num_i = 0;
107     int position = 0;
108     int psysno = 0;
109     struct it_key key;
110     RSFD rfd;
111
112     if (!(sset = resultSetGet (zi, name)))
113         return NULL;
114     if (!(rset = sset->rset))
115         return NULL;
116     logf (LOG_DEBUG, "resultSetRecordGet");
117     sr = xmalloc (sizeof(*sr) * num);
118     rfd = rset_open (rset, RSETF_READ|RSETF_SORT_RANK);
119     while (rset_read (rset, rfd, &key))
120     {
121         if (key.sysno != psysno)
122         {
123             psysno = key.sysno;
124             position++;
125             assert (num_i < num);
126             if (position == positions[num_i])
127             {
128                 sr[num_i].sysno = psysno;
129                 rset_score (rset, rfd, &sr[num_i].score);
130                 if (++num_i == num)
131                     break;
132             }
133         }
134     }
135     rset_close (rset, rfd);
136     while (num_i < num)
137     {
138         sr[num_i].sysno = 0;
139         num_i++;
140     }
141     return sr;
142 }
143
144 void resultSetSysnoDel (ZServerInfo *zi, ZServerSetSysno *records, int num)
145 {
146     xfree (records);
147 }