7fad179428a7e8f28e7e1f52a17cf1ab7787ec04
[idzebra-moved-to-github.git] / rset / rsisamc.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsisamc.c,v $
7  * Revision 1.5  1998-03-05 08:36:28  adam
8  * New result set model.
9  *
10  * Revision 1.4  1997/12/18 10:54:25  adam
11  * New method result set method rs_hits that returns the number of
12  * hits in result-set (if known). The ranked result set returns real
13  * number of hits but only when not combined with other operands.
14  *
15  * Revision 1.3  1997/10/31 12:37:01  adam
16  * Code calls xfree() instead of free().
17  *
18  * Revision 1.2  1996/11/08 11:15:57  adam
19  * Compressed isam fully supported.
20  *
21  * Revision 1.1  1996/10/29 13:41:48  adam
22  * First use of isamc.
23  *
24  */
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <rsisamc.h>
29 #include <zebrautl.h>
30
31 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
32 static RSFD r_open (RSET ct, int flag);
33 static void r_close (RSFD rfd);
34 static void r_delete (RSET ct);
35 static void r_rewind (RSFD rfd);
36 static int r_count (RSET ct);
37 static int r_read (RSFD rfd, void *buf, int *term_index);
38 static int r_write (RSFD rfd, const void *buf);
39
40 static const struct 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_read,
50     r_write,
51 };
52
53 const struct rset_control *rset_kind_isamc = &control;
54
55 struct rset_pp_info {
56     ISAMC_PP pt;
57     struct rset_pp_info *next;
58     struct rset_isamc_info *info;
59 };
60
61 struct rset_isamc_info {
62     ISAMC   is;
63     ISAMC_P pos;
64     struct rset_pp_info *ispt_list;
65 };
66
67 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
68 {
69     rset_isamc_parms *pt = parms;
70     struct rset_isamc_info *info;
71
72     ct->flags |= RSET_FLAG_VOLATILE;
73     info = xmalloc (sizeof(*info));
74     info->is = pt->is;
75     info->pos = pt->pos;
76     info->ispt_list = NULL;
77     ct->no_rset_terms = 1;
78     ct->rset_terms = xmalloc (sizeof(*ct->rset_terms));
79     ct->rset_terms[0] = pt->rset_term;
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     if (ct->rset_terms[0]->nn < 0)
100         ct->rset_terms[0]->nn = isc_pp_num (ptinfo->pt);
101     return ptinfo;
102 }
103
104 static void r_close (RSFD rfd)
105 {
106     struct rset_isamc_info *info = ((struct rset_pp_info*) rfd)->info;
107     struct rset_pp_info **ptinfop;
108
109     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
110         if (*ptinfop == rfd)
111         {
112             isc_pp_close ((*ptinfop)->pt);
113             *ptinfop = (*ptinfop)->next;
114             xfree (rfd);
115             return;
116         }
117     logf (LOG_FATAL, "r_close but no rfd match!");
118     assert (0);
119 }
120
121 static void r_delete (RSET ct)
122 {
123     struct rset_isamc_info *info = ct->buf;
124
125     logf (LOG_DEBUG, "rsisamc_delete");
126     assert (info->ispt_list == NULL);
127     rset_term_destroy (ct->rset_terms[0]);
128     xfree (ct->rset_terms);
129     xfree (info);
130 }
131
132 static void r_rewind (RSFD rfd)
133 {   
134     logf (LOG_DEBUG, "rsisamc_rewind");
135     abort ();
136 }
137
138 static int r_count (RSET ct)
139 {
140     return 0;
141 }
142
143 static int r_read (RSFD rfd, void *buf, int *term_index)
144 {
145     *term_index = 0;
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 }