288c71433c37297ac313024a370ea3e8fd3974d2
[idzebra-moved-to-github.git] / rset / rsisamc.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stdio.h>
21 #include <assert.h>
22 #include <string.h>
23 #include <idzebra/util.h>
24 #include <rset.h>
25
26 static RSFD r_open (RSET ct, int flag);
27 static void r_close (RSFD rfd);
28 static void r_delete (RSET ct);
29 static int r_read (RSFD rfd, void *buf, TERMID *term);
30 static int r_write (RSFD rfd, const void *buf);
31 static void r_pos (RSFD rfd, double *current, double *total);
32
33 static const struct rset_control control = 
34 {
35     "isamc",
36     r_delete,
37     rset_get_one_term,
38     r_open,
39     r_close,
40     0, /* no forward */
41     r_pos,
42     r_read,
43     r_write,
44 };
45
46 struct rset_pp_info {
47     ISAMC_PP pt;
48     void *buf;
49 };
50
51 struct rset_isamc_info {
52     ISAMC   is;
53     ISAM_P pos;
54 };
55
56 static int log_level = 0;
57 static int log_level_initialized = 0;
58
59 RSET rsisamc_create(NMEM nmem, struct rset_key_control *kcontrol,
60                     int scope,
61                     ISAMC is, ISAM_P pos, TERMID term)
62 {
63     RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, term, 0, 0);
64     struct rset_isamc_info *info;
65     if (!log_level_initialized)
66     {
67         log_level = yaz_log_module_level("rsisamc");
68         log_level_initialized = 1;
69     }
70     info = (struct rset_isamc_info *) nmem_malloc(rnew->nmem, sizeof(*info));
71     info->is = is;
72     info->pos = pos;
73     rnew->priv = info;
74     yaz_log(log_level, "create: term=%p", term);
75     return rnew;
76 }
77
78 static void r_delete (RSET ct)
79 {
80     yaz_log(log_level, "rsisamc_delete");
81 }
82
83
84 RSFD r_open (RSET ct, int flag)
85 {
86     struct rset_isamc_info *info = (struct rset_isamc_info *) ct->priv;
87     RSFD rfd;
88     struct rset_pp_info *ptinfo;
89
90     yaz_log(log_level, "risamc_open");
91     if (flag & RSETF_WRITE)
92     {
93         yaz_log(YLOG_FATAL, "ISAMC set type is read-only");
94         return NULL;
95     }
96     rfd = rfd_create_base(ct);
97     if (rfd->priv)
98         ptinfo = (struct rset_pp_info *)rfd->priv;
99     else {
100         ptinfo = (struct rset_pp_info *) nmem_malloc (ct->nmem,sizeof(*ptinfo));
101         rfd->priv = ptinfo;
102         ptinfo->buf = nmem_malloc (ct->nmem,ct->keycontrol->key_size);
103     }
104     ptinfo->pt = isamc_pp_open(info->is, info->pos);
105     return rfd;
106 }
107
108 static void r_close (RSFD rfd)
109 {
110     struct rset_pp_info *p = (struct rset_pp_info *)(rfd->priv);
111
112     isamc_pp_close(p->pt);
113 }
114
115
116 static int r_read (RSFD rfd, void *buf, TERMID *term)
117 {
118     struct rset_pp_info *p = (struct rset_pp_info *)(rfd->priv);
119     int r;
120     r = isamc_pp_read(p->pt, buf);
121     if (term)
122         *term = rfd->rset->term;
123     yaz_log(log_level, "isamc.r_read");
124     return r;
125 }
126
127 static int r_write (RSFD rfd, const void *buf)
128 {
129     yaz_log(YLOG_FATAL, "ISAMC set type is read-only");
130     return -1;
131 }
132
133 static void r_pos (RSFD rfd, double *current, double *total)
134 {
135     *current = -1;  /* sorry, not implemented yet */
136     *total = -1;
137 }
138
139
140
141 /*
142  * Local variables:
143  * c-basic-offset: 4
144  * indent-tabs-mode: nil
145  * End:
146  * vim: shiftwidth=4 tabstop=8 expandtab
147  */
148