Extended the result set system. Added support for filtering/limits.
[idzebra-moved-to-github.git] / rset / rsisamc.c
1 /* $Id: rsisamc.c,v 1.39 2005-05-03 09:11:36 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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     rset_default_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);
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     rfd_delete_base(rfd);
117 }
118
119
120 static int r_read (RSFD rfd, void *buf, TERMID *term)
121 {
122     struct rset_pp_info *p = (struct rset_pp_info *)(rfd->priv);
123     int r;
124     r = isamc_pp_read(p->pt, buf);
125     if (term)
126         *term = rfd->rset->term;
127     yaz_log(log_level, "isamc.r_read");
128     return r;
129 }
130
131 static int r_write (RSFD rfd, const void *buf)
132 {
133     yaz_log(YLOG_FATAL, "ISAMC set type is read-only");
134     return -1;
135 }
136
137 static void r_pos (RSFD rfd, double *current, double *total)
138 {
139     *current = -1;  /* sorry, not implemented yet */
140     *total = -1;
141 }
142
143
144