Happy new year
[idzebra-moved-to-github.git] / rset / rsisamc.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 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 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
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 void r_pos (RSFD rfd, double *current, double *total);
34
35 static const struct rset_control control =
36 {
37     "isamc",
38     r_delete,
39     rset_get_one_term,
40     r_open,
41     r_close,
42     0, /* no forward */
43     r_pos,
44     r_read,
45     rset_no_write,
46 };
47
48 struct rset_pp_info {
49     ISAMC_PP pt;
50     void *buf;
51 };
52
53 struct rset_isamc_info {
54     ISAMC   is;
55     ISAM_P pos;
56 };
57
58 static int log_level = 0;
59 static int log_level_initialized = 0;
60
61 RSET rsisamc_create(NMEM nmem, struct rset_key_control *kcontrol,
62                     int scope,
63                     ISAMC is, ISAM_P pos, TERMID term)
64 {
65     RSET rnew = rset_create_base(&control, nmem, kcontrol, scope, term, 0, 0);
66     struct rset_isamc_info *info;
67     if (!log_level_initialized)
68     {
69         log_level = yaz_log_module_level("rsisamc");
70         log_level_initialized = 1;
71     }
72     info = (struct rset_isamc_info *) nmem_malloc(rnew->nmem, sizeof(*info));
73     info->is = is;
74     info->pos = pos;
75     rnew->priv = info;
76     yaz_log(log_level, "create: term=%p", term);
77     return rnew;
78 }
79
80 static void r_delete (RSET ct)
81 {
82     yaz_log(log_level, "rsisamc_delete");
83 }
84
85
86 RSFD r_open (RSET ct, int flag)
87 {
88     struct rset_isamc_info *info = (struct rset_isamc_info *) ct->priv;
89     RSFD rfd;
90     struct rset_pp_info *ptinfo;
91
92     yaz_log(log_level, "risamc_open");
93     if (flag & RSETF_WRITE)
94     {
95         yaz_log(YLOG_FATAL, "ISAMC set type is read-only");
96         return NULL;
97     }
98     rfd = rfd_create_base(ct);
99     if (rfd->priv)
100         ptinfo = (struct rset_pp_info *)rfd->priv;
101     else {
102         ptinfo = (struct rset_pp_info *) nmem_malloc (ct->nmem,sizeof(*ptinfo));
103         rfd->priv = ptinfo;
104         ptinfo->buf = nmem_malloc (ct->nmem,ct->keycontrol->key_size);
105     }
106     ptinfo->pt = isamc_pp_open(info->is, info->pos);
107     return rfd;
108 }
109
110 static void r_close (RSFD rfd)
111 {
112     struct rset_pp_info *p = (struct rset_pp_info *)(rfd->priv);
113
114     isamc_pp_close(p->pt);
115 }
116
117
118 static int r_read (RSFD rfd, void *buf, TERMID *term)
119 {
120     struct rset_pp_info *p = (struct rset_pp_info *)(rfd->priv);
121     int r;
122     r = isamc_pp_read(p->pt, buf);
123     if (term)
124         *term = rfd->rset->term;
125     yaz_log(log_level, "isamc.r_read");
126     return r;
127 }
128
129 static void r_pos (RSFD rfd, double *current, double *total)
130 {
131     *current = -1;  /* sorry, not implemented yet */
132     *total = -1;
133 }
134
135
136 /*
137  * Local variables:
138  * c-basic-offset: 4
139  * c-file-style: "Stroustrup"
140  * indent-tabs-mode: nil
141  * End:
142  * vim: shiftwidth=4 tabstop=8 expandtab
143  */
144