bbb3859be9299c0ab5b7cd712ee7d4b5a6d47afb
[idzebra-moved-to-github.git] / rset / rsisam.c
1 /*
2  * Copyright (C) 1994-2002, Index Data
3  * All rights reserved.
4  *
5  * $Id: rsisam.c,v 1.22 2002-04-05 08:46:26 adam Exp $
6  */
7 #include <stdio.h>
8 #include <assert.h>
9 #include <zebrautl.h>
10 #include <rsisam.h>
11
12 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
13 static RSFD r_open (RSET ct, int flag);
14 static void r_close (RSFD rfd);
15 static void r_delete (RSET ct);
16 static void r_rewind (RSFD rfd);
17 static int r_count (RSET ct);
18 static int r_read (RSFD rfd, void *buf, int *term_index);
19 static int r_write (RSFD rfd, const void *buf);
20
21 static const struct rset_control control = 
22 {
23     "isam",
24     r_create,
25     r_open,
26     r_close,
27     r_delete,
28     r_rewind,
29     r_count,
30     r_read,
31     r_write,
32 };
33
34 const struct rset_control *rset_kind_isam = &control;
35
36 struct rset_ispt_info {
37     ISPT   pt;
38     struct rset_ispt_info *next;
39     struct rset_isam_info *info;
40 };
41
42 struct rset_isam_info {
43     ISAM   is;
44     ISAM_P pos;
45     struct rset_ispt_info *ispt_list;
46 };
47
48 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
49 {
50     rset_isam_parms *pt = (rset_isam_parms *) parms;
51     struct rset_isam_info *info;
52
53     ct->flags |= RSET_FLAG_VOLATILE;
54     info = (struct rset_isam_info *) xmalloc (sizeof(struct rset_isam_info));
55     info->is = pt->is;
56     info->pos = pt->pos;
57     info->ispt_list = NULL;
58
59     ct->no_rset_terms = 1;
60     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
61     ct->rset_terms[0] = pt->rset_term;
62     return info;
63 }
64
65 RSFD r_open (RSET ct, int flag)
66 {
67     struct rset_isam_info *info = (struct rset_isam_info *) ct->buf;
68     struct rset_ispt_info *ptinfo;
69
70     logf (LOG_DEBUG, "risam_open");
71     if (flag & RSETF_WRITE)
72     {
73         logf (LOG_FATAL, "ISAM set type is read-only");
74         return NULL;
75     }
76     ptinfo = (struct rset_ispt_info *) xmalloc (sizeof(*ptinfo));
77     ptinfo->next = info->ispt_list;
78     info->ispt_list = ptinfo;
79     ptinfo->pt = is_position (info->is, info->pos);
80     ptinfo->info = info;
81
82     if (ct->rset_terms[0]->nn < 0)
83         ct->rset_terms[0]->nn = is_numkeys (ptinfo->pt);
84     return ptinfo;
85 }
86
87 static void r_close (RSFD rfd)
88 {
89     struct rset_isam_info *info = ((struct rset_ispt_info*) rfd)->info;
90     struct rset_ispt_info **ptinfop;
91
92     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
93         if (*ptinfop == rfd)
94         {
95             is_pt_free ((*ptinfop)->pt);
96             *ptinfop = (*ptinfop)->next;
97             xfree (rfd);
98             return;
99         }
100     logf (LOG_FATAL, "r_close but no rfd match!");
101     assert (0);
102 }
103
104 static void r_delete (RSET ct)
105 {
106     struct rset_isam_info *info = (struct rset_isam_info *) ct->buf;
107
108     logf (LOG_DEBUG, "rsisam_delete");
109     assert (info->ispt_list == NULL);
110     rset_term_destroy (ct->rset_terms[0]);
111     xfree (ct->rset_terms);
112     xfree (info);
113 }
114
115 static void r_rewind (RSFD rfd)
116 {   
117     logf (LOG_DEBUG, "rsisam_rewind");
118     is_rewind( ((struct rset_ispt_info*) rfd)->pt);
119 }
120
121 static int r_count (RSET ct)
122 {
123     return 0;
124 }
125
126 static int r_read (RSFD rfd, void *buf, int *term_index)
127 {
128     *term_index = 0;
129     return is_readkey( ((struct rset_ispt_info*) rfd)->pt, buf);
130 }
131
132 static int r_write (RSFD rfd, const void *buf)
133 {
134     logf (LOG_FATAL, "ISAM set type is read-only");
135     return -1;
136 }