3aceccb40f3ec7873ac258d4a151773ee07e2cf3
[idzebra-moved-to-github.git] / rset / rsisamd.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsisamd.c,v $
7  * Revision 1.1  2001-01-16 19:17:18  heikki
8  * Added rsisamd.c
9  *
10  *
11  */
12
13
14 #include <stdio.h>
15 #include <assert.h>
16 #include <zebrautl.h>
17 #if ZMBOL
18 #include <rsisamd.h>
19
20 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
21 static RSFD r_open (RSET ct, int flag);
22 static void r_close (RSFD rfd);
23 static void r_delete (RSET ct);
24 static void r_rewind (RSFD rfd);
25 static int r_count (RSET ct);
26 static int r_read (RSFD rfd, void *buf, int *term_index);
27 static int r_write (RSFD rfd, const void *buf);
28
29 static const struct rset_control control = 
30 {
31     "isamd",
32     r_create,
33     r_open,
34     r_close,
35     r_delete,
36     r_rewind,
37     r_count,
38     r_read,
39     r_write,
40 };
41
42 const struct rset_control *rset_kind_isamd = &control;
43
44 struct rset_pp_info {
45     ISAMD_PP pt;
46     struct rset_pp_info *next;
47     struct rset_isamd_info *info;
48 };
49
50 struct rset_isamd_info {
51     ISAMD   is;
52     ISAMD_P pos;
53     struct rset_pp_info *ispt_list;
54 };
55
56 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
57 {
58     rset_isamd_parms *pt = (rset_isamd_parms *) parms;
59     struct rset_isamd_info *info;
60
61     ct->flags |= RSET_FLAG_VOLATILE;
62     info = (struct rset_isamd_info *) xmalloc (sizeof(*info));
63     info->is = pt->is;
64     info->pos = pt->pos;
65     info->ispt_list = NULL;
66     ct->no_rset_terms = 1;
67     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
68     ct->rset_terms[0] = pt->rset_term;
69     return info;
70 }
71
72 RSFD r_open (RSET ct, int flag)
73 {
74     struct rset_isamd_info *info = (struct rset_isamd_info *) ct->buf;
75     struct rset_pp_info *ptinfo;
76
77     logf (LOG_DEBUG, "risamd_open");
78     if (flag & RSETF_WRITE)
79     {
80         logf (LOG_FATAL, "ISAMD set type is read-only");
81         return NULL;
82     }
83     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
84     ptinfo->next = info->ispt_list;
85     info->ispt_list = ptinfo;
86     ptinfo->pt = isamd_pp_open (info->is, info->pos);
87     ptinfo->info = info;
88     if (ct->rset_terms[0]->nn < 0)
89         ct->rset_terms[0]->nn = isamd_pp_num (ptinfo->pt);
90     return ptinfo;
91 }
92
93 static void r_close (RSFD rfd)
94 {
95     struct rset_isamd_info *info = ((struct rset_pp_info*) rfd)->info;
96     struct rset_pp_info **ptinfop;
97
98     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
99         if (*ptinfop == rfd)
100         {
101             isamd_pp_close ((*ptinfop)->pt);
102             *ptinfop = (*ptinfop)->next;
103             xfree (rfd);
104             return;
105         }
106     logf (LOG_FATAL, "r_close but no rfd match!");
107     assert (0);
108 }
109
110 static void r_delete (RSET ct)
111 {
112     struct rset_isamd_info *info = (struct rset_isamd_info *) ct->buf;
113
114     logf (LOG_DEBUG, "rsisamd_delete");
115     assert (info->ispt_list == NULL);
116     rset_term_destroy (ct->rset_terms[0]);
117     xfree (ct->rset_terms);
118     xfree (info);
119 }
120
121 static void r_rewind (RSFD rfd)
122 {   
123     logf (LOG_DEBUG, "rsisamd_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, int *term_index)
133 {
134     *term_index = 0;
135     return isamd_pp_read( ((struct rset_pp_info*) rfd)->pt, buf);
136 }
137
138 static int r_write (RSFD rfd, const void *buf)
139 {
140     logf (LOG_FATAL, "ISAMD set type is read-only");
141     return -1;
142 }
143 #endif