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