Isam-D now stores small entries directly in the dictionary.
[idzebra-moved-to-github.git] / rset / rsisamd.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: rsisamd.c,v $
7  * Revision 1.3  2002-07-12 18:12:22  heikki
8  * Isam-D now stores small entries directly in the dictionary.
9  * Needs more tuning and cleaning...
10  *
11  * Revision 1.2  2002/04/05 08:46:26  adam
12  * Zebra with full functionality
13  *
14  * Revision 1.1  2001/01/16 19:17:18  heikki
15  * Added rsisamd.c
16  *
17  *
18  */
19
20
21 #include <stdio.h>
22 #include <assert.h>
23 #include <zebrautl.h>
24 #include <rsisamd.h>
25
26 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
27 static RSFD r_open (RSET ct, int flag);
28 static void r_close (RSFD rfd);
29 static void r_delete (RSET ct);
30 static void r_rewind (RSFD rfd);
31 static int r_count (RSET ct);
32 static int r_read (RSFD rfd, void *buf, int *term_index);
33 static int r_write (RSFD rfd, const void *buf);
34
35 static const struct rset_control control = 
36 {
37     "isamd",
38     r_create,
39     r_open,
40     r_close,
41     r_delete,
42     r_rewind,
43     r_count,
44     r_read,
45     r_write,
46 };
47
48 const struct rset_control *rset_kind_isamd = &control;
49
50 struct rset_pp_info {
51     ISAMD_PP pt;
52     struct rset_pp_info *next;
53     struct rset_isamd_info *info;
54 };
55
56 struct rset_isamd_info {
57     ISAMD   is; 
58     /* ISAMD_P pos; */
59     char dictentry[ISAMD_MAX_DICT_LEN];
60     int dictlen;
61     struct rset_pp_info *ispt_list;
62 };
63
64 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
65 {
66     rset_isamd_parms *pt = (rset_isamd_parms *) parms;
67     struct rset_isamd_info *info;
68
69     ct->flags |= RSET_FLAG_VOLATILE;
70     info = (struct rset_isamd_info *) xmalloc (sizeof(*info));
71     info->is = pt->is;
72     /*info->pos = pt->pos;*/
73     info->dictlen = pt->dictlen;
74     memcpy(info->dictentry, pt->dictentry, pt->dictlen);
75     info->ispt_list = NULL;
76     ct->no_rset_terms = 1;
77     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
78     ct->rset_terms[0] = pt->rset_term;
79     return info;
80 }
81
82 RSFD r_open (RSET ct, int flag)
83 {
84     struct rset_isamd_info *info = (struct rset_isamd_info *) ct->buf;
85     struct rset_pp_info *ptinfo;
86
87     logf (LOG_DEBUG, "risamd_open");
88     if (flag & RSETF_WRITE)
89     {
90         logf (LOG_FATAL, "ISAMD set type is read-only");
91         return NULL;
92     }
93     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
94     ptinfo->next = info->ispt_list;
95     info->ispt_list = ptinfo;
96     ptinfo->pt = isamd_pp_open (info->is, info->dictentry, info->dictlen);
97     ptinfo->info = info;
98     if (ct->rset_terms[0]->nn < 0)
99         ct->rset_terms[0]->nn = isamd_pp_num (ptinfo->pt);
100     return ptinfo;
101 }
102
103 static void r_close (RSFD rfd)
104 {
105     struct rset_isamd_info *info = ((struct rset_pp_info*) rfd)->info;
106     struct rset_pp_info **ptinfop;
107
108     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
109         if (*ptinfop == rfd)
110         {
111             isamd_pp_close ((*ptinfop)->pt);
112             *ptinfop = (*ptinfop)->next;
113             xfree (rfd);
114             return;
115         }
116     logf (LOG_FATAL, "r_close but no rfd match!");
117     assert (0);
118 }
119
120 static void r_delete (RSET ct)
121 {
122     struct rset_isamd_info *info = (struct rset_isamd_info *) ct->buf;
123
124     logf (LOG_DEBUG, "rsisamd_delete");
125     assert (info->ispt_list == NULL);
126     rset_term_destroy (ct->rset_terms[0]);
127     xfree (ct->rset_terms);
128     xfree (info);
129 }
130
131 static void r_rewind (RSFD rfd)
132 {   
133     logf (LOG_DEBUG, "rsisamd_rewind");
134     abort ();
135 }
136
137 static int r_count (RSET ct)
138 {
139     return 0;
140 }
141
142 static int r_read (RSFD rfd, void *buf, int *term_index)
143 {
144     *term_index = 0;
145     return isamd_pp_read( ((struct rset_pp_info*) rfd)->pt, buf);
146 }
147
148 static int r_write (RSFD rfd, const void *buf)
149 {
150     logf (LOG_FATAL, "ISAMD set type is read-only");
151     return -1;
152 }