b67839f77212283e7fbf365a6ce7f550c7af0120
[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.3  1997-10-31 12:37:01  adam
8  * Code calls xfree() instead of free().
9  *
10  * Revision 1.2  1996/11/08 11:15:57  adam
11  * Compressed isam fully supported.
12  *
13  * Revision 1.1  1996/10/29 13:41:48  adam
14  * First use of isamc.
15  *
16  */
17
18 #include <stdio.h>
19 #include <assert.h>
20 #include <rsisamc.h>
21 #include <zebrautl.h>
22
23 static void *r_create(const struct rset_control *sel, void *parms,
24                       int *flags);
25 static RSFD r_open (RSET ct, int flag);
26 static void r_close (RSFD rfd);
27 static void r_delete (RSET ct);
28 static void r_rewind (RSFD rfd);
29 static int r_count (RSET ct);
30 static int r_read (RSFD rfd, void *buf);
31 static int r_write (RSFD rfd, const void *buf);
32 static int r_score (RSFD rfd, int *score);
33
34 static const rset_control control = 
35 {
36     "isamc",
37     r_create,
38     r_open,
39     r_close,
40     r_delete,
41     r_rewind,
42     r_count,
43     r_read,
44     r_write,
45     r_score
46 };
47
48 const rset_control *rset_kind_isamc = &control;
49
50 struct rset_pp_info {
51     ISAMC_PP pt;
52     struct rset_pp_info *next;
53     struct rset_isamc_info *info;
54 };
55
56 struct rset_isamc_info {
57     ISAMC   is;
58     ISAMC_P pos;
59     struct rset_pp_info *ispt_list;
60 };
61
62 static void *r_create(const struct rset_control *sel, void *parms,
63                       int *flags)
64 {
65     rset_isamc_parms *pt = parms;
66     struct rset_isamc_info *info;
67
68     *flags |= RSET_FLAG_VOLATILE;
69     info = xmalloc (sizeof(*info));
70     info->is = pt->is;
71     info->pos = pt->pos;
72     info->ispt_list = NULL;
73     return info;
74 }
75
76 RSFD r_open (RSET ct, int flag)
77 {
78     struct rset_isamc_info *info = ct->buf;
79     struct rset_pp_info *ptinfo;
80
81     logf (LOG_DEBUG, "risamc_open");
82     if (flag & RSETF_WRITE)
83     {
84         logf (LOG_FATAL, "ISAMC set type is read-only");
85         return NULL;
86     }
87     ptinfo = xmalloc (sizeof(*ptinfo));
88     ptinfo->next = info->ispt_list;
89     info->ispt_list = ptinfo;
90     ptinfo->pt = isc_pp_open (info->is, info->pos);
91     ptinfo->info = info;
92     return ptinfo;
93 }
94
95 static void r_close (RSFD rfd)
96 {
97     struct rset_isamc_info *info = ((struct rset_pp_info*) rfd)->info;
98     struct rset_pp_info **ptinfop;
99
100     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
101         if (*ptinfop == rfd)
102         {
103             isc_pp_close ((*ptinfop)->pt);
104             *ptinfop = (*ptinfop)->next;
105             xfree (rfd);
106             return;
107         }
108     logf (LOG_FATAL, "r_close but no rfd match!");
109     assert (0);
110 }
111
112 static void r_delete (RSET ct)
113 {
114     struct rset_isamc_info *info = ct->buf;
115
116     logf (LOG_DEBUG, "rsisamc_delete");
117     assert (info->ispt_list == NULL);
118     xfree (info);
119 }
120
121 static void r_rewind (RSFD rfd)
122 {   
123     logf (LOG_DEBUG, "rsisamc_rewind");
124     abort ();
125 }
126
127 static int r_count (RSET ct)
128 {
129     return 0;
130 }
131
132 static int r_read (RSFD rfd, void *buf)
133 {
134     return isc_pp_read( ((struct rset_pp_info*) rfd)->pt, buf);
135 }
136
137 static int r_write (RSFD rfd, const void *buf)
138 {
139     logf (LOG_FATAL, "ISAMC set type is read-only");
140     return -1;
141 }
142
143 static int r_score (RSFD rfd, int *score)
144 {
145     *score = -1;
146     return -1;
147 }