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