New method result set method rs_hits that returns the number of
[idzebra-moved-to-github.git] / rset / rsisamc.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsisamc.c,v $
7  * Revision 1.4  1997-12-18 10:54:25  adam
8  * New method result set method rs_hits that returns the number of
9  * hits in result-set (if known). The ranked result set returns real
10  * number of hits but only when not combined with other operands.
11  *
12  * Revision 1.3  1997/10/31 12:37:01  adam
13  * Code calls xfree() instead of free().
14  *
15  * Revision 1.2  1996/11/08 11:15:57  adam
16  * Compressed isam fully supported.
17  *
18  * Revision 1.1  1996/10/29 13:41:48  adam
19  * First use of isamc.
20  *
21  */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <rsisamc.h>
26 #include <zebrautl.h>
27
28 static void *r_create(const struct rset_control *sel, void *parms,
29                       int *flags);
30 static RSFD r_open (RSET ct, int flag);
31 static void r_close (RSFD rfd);
32 static void r_delete (RSET ct);
33 static void r_rewind (RSFD rfd);
34 static int r_count (RSET ct);
35 static int r_hits (RSET ct, void *oi);
36 static int r_read (RSFD rfd, void *buf);
37 static int r_write (RSFD rfd, const void *buf);
38 static int r_score (RSFD rfd, int *score);
39
40 static const rset_control control = 
41 {
42     "isamc",
43     r_create,
44     r_open,
45     r_close,
46     r_delete,
47     r_rewind,
48     r_count,
49     r_hits,
50     r_read,
51     r_write,
52     r_score
53 };
54
55 const rset_control *rset_kind_isamc = &control;
56
57 struct rset_pp_info {
58     ISAMC_PP pt;
59     struct rset_pp_info *next;
60     struct rset_isamc_info *info;
61 };
62
63 struct rset_isamc_info {
64     ISAMC   is;
65     ISAMC_P pos;
66     struct rset_pp_info *ispt_list;
67 };
68
69 static void *r_create(const struct rset_control *sel, void *parms,
70                       int *flags)
71 {
72     rset_isamc_parms *pt = parms;
73     struct rset_isamc_info *info;
74
75     *flags |= RSET_FLAG_VOLATILE;
76     info = xmalloc (sizeof(*info));
77     info->is = pt->is;
78     info->pos = pt->pos;
79     info->ispt_list = NULL;
80     return info;
81 }
82
83 RSFD r_open (RSET ct, int flag)
84 {
85     struct rset_isamc_info *info = ct->buf;
86     struct rset_pp_info *ptinfo;
87
88     logf (LOG_DEBUG, "risamc_open");
89     if (flag & RSETF_WRITE)
90     {
91         logf (LOG_FATAL, "ISAMC set type is read-only");
92         return NULL;
93     }
94     ptinfo = xmalloc (sizeof(*ptinfo));
95     ptinfo->next = info->ispt_list;
96     info->ispt_list = ptinfo;
97     ptinfo->pt = isc_pp_open (info->is, info->pos);
98     ptinfo->info = info;
99     return ptinfo;
100 }
101
102 static void r_close (RSFD rfd)
103 {
104     struct rset_isamc_info *info = ((struct rset_pp_info*) rfd)->info;
105     struct rset_pp_info **ptinfop;
106
107     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
108         if (*ptinfop == rfd)
109         {
110             isc_pp_close ((*ptinfop)->pt);
111             *ptinfop = (*ptinfop)->next;
112             xfree (rfd);
113             return;
114         }
115     logf (LOG_FATAL, "r_close but no rfd match!");
116     assert (0);
117 }
118
119 static void r_delete (RSET ct)
120 {
121     struct rset_isamc_info *info = ct->buf;
122
123     logf (LOG_DEBUG, "rsisamc_delete");
124     assert (info->ispt_list == NULL);
125     xfree (info);
126 }
127
128 static void r_rewind (RSFD rfd)
129 {   
130     logf (LOG_DEBUG, "rsisamc_rewind");
131     abort ();
132 }
133
134 static int r_count (RSET ct)
135 {
136     return 0;
137 }
138
139 static int r_hits (RSET ct, void *oi)
140 {
141     return -1;
142 }
143
144 static int r_read (RSFD rfd, void *buf)
145 {
146     return isc_pp_read( ((struct rset_pp_info*) rfd)->pt, buf);
147 }
148
149 static int r_write (RSFD rfd, const void *buf)
150 {
151     logf (LOG_FATAL, "ISAMC set type is read-only");
152     return -1;
153 }
154
155 static int r_score (RSFD rfd, int *score)
156 {
157     *score = -1;
158     return -1;
159 }