Function rset_open changed its wflag parameter to general flags.
[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.8  1995-10-10 13:59:25  adam
8  * Function rset_open changed its wflag parameter to general flags.
9  *
10  * Revision 1.7  1995/10/06  14:38:01  adam
11  * New result set method: r_score.
12  * Local no (sysno) and score is transferred to retrieveCtrl.
13  *
14  * Revision 1.6  1995/09/28  09:19:49  adam
15  * xfree/xmalloc used everywhere.
16  * Extract/retrieve method seems to work for text records.
17  *
18  * Revision 1.5  1995/09/27  16:17:32  adam
19  * More work on retrieve.
20  *
21  * Revision 1.4  1995/09/07  13:58:36  adam
22  * New parameter: result-set file descriptor (RSFD) to support multiple
23  * positions within the same result-set.
24  * Boolean operators: and, or, not implemented.
25  * Result-set references.
26  *
27  * Revision 1.3  1995/09/06  16:11:19  adam
28  * Option: only one word key per file.
29  *
30  * Revision 1.2  1995/09/06  10:33:04  adam
31  * More work on present. Some log messages removed.
32  *
33  * Revision 1.1  1995/09/05  15:28:40  adam
34  * More work on search engine.
35  *
36  */
37 #include <stdio.h>
38 #include <assert.h>
39 #include <unistd.h>
40
41 #include "zserver.h"
42 #include <rstemp.h>
43
44 ZServerSet *resultSetAdd (ZServerInfo *zi, const char *name, int ov, RSET rset)
45 {
46     ZServerSet *s;
47
48     for (s = zi->sets; s; s = s->next)
49         if (!strcmp (s->name, name))
50         {
51             if (!ov)
52                 return NULL;
53             rset_delete (s->rset);
54             s->rset = rset;
55             return s;
56         }
57     s = xmalloc (sizeof(*s));
58     s->next = zi->sets;
59     zi->sets = s;
60     s->name = xmalloc (strlen(name)+1);
61     strcpy (s->name, name);
62     s->rset = rset;
63     return s;
64 }
65
66 ZServerSet *resultSetGet (ZServerInfo *zi, const char *name)
67 {
68     ZServerSet *s;
69
70     for (s = zi->sets; s; s = s->next)
71         if (!strcmp (s->name, name))
72             return s;
73     return NULL;
74 }
75
76 ZServerSetSysno *resultSetSysnoGet  (ZServerInfo *zi, const char *name, 
77                                      int num, int *positions)
78 {
79     ZServerSet *sset;
80     ZServerSetSysno *sr;
81     RSET rset;
82     int num_i = 0;
83     int position = 0;
84     int psysno = 0;
85     struct it_key key;
86     RSFD rfd;
87
88     if (!(sset = resultSetGet (zi, name)))
89         return NULL;
90     if (!(rset = sset->rset))
91         return NULL;
92     logf (LOG_DEBUG, "resultSetRecordGet");
93     sr = xmalloc (sizeof(*sr) * num);
94     rfd = rset_open (rset, RSETF_READ|RSETF_SORT_RANK);
95     while (rset_read (rset, rfd, &key))
96     {
97         if (key.sysno != psysno)
98         {
99             psysno = key.sysno;
100             position++;
101             if (position == positions[num_i])
102             {
103                 sr[num_i].sysno = psysno;
104                 rset_score (rset, rfd, &sr[num_i].score);
105                 num_i++;
106                 if (++num_i == num)
107                     break;
108             }
109         }
110     }
111     rset_close (rset, rfd);
112     while (num_i < num)
113     {
114         sr[num_i].sysno = 0;
115         num_i++;
116     }
117     return sr;
118 }
119
120 void resultSetRecordDel (ZServerInfo *zi, ZServerRecord *records, int num)
121 {
122     int i;
123
124     for (i = 0; i<num; i++)
125         xfree (records[i].buf);
126     xfree (records);
127 }