1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 Index Data
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
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
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
25 #include <yaz/xmalloc.h>
26 #include <idzebra/isamb.h>
27 #include <idzebra/bfile.h>
31 #define SORT_MAX_TERM 110
33 #define SORT_IDX_BLOCKSIZE 64
37 char term[SORT_MAX_TERM];
41 static void sort_term_log_item(int level, const void *b, const char *txt)
45 memcpy(&a1, b, sizeof(a1));
47 yaz_log(level, "%s " ZINT_FORMAT " %s", txt, a1.sysno, a1.term);
50 int sort_term_compare(const void *a, const void *b)
52 struct sort_term a1, b1;
54 memcpy(&a1, a, sizeof(a1));
55 memcpy(&b1, b, sizeof(b1));
57 if (a1.sysno > b1.sysno)
59 else if (a1.sysno < b1.sysno)
64 void *sort_term_code_start(void)
69 void sort_term_encode(void *p, char **dst, const char **src)
73 memcpy(&a1, *src, sizeof(a1));
76 zebra_zint_encode(dst, a1.sysno); /* encode record id */
77 strcpy(*dst, a1.term); /* then sort term, 0 terminated */
78 *dst += strlen(a1.term) + 1;
81 void sort_term_decode(void *p, char **dst, const char **src)
85 zebra_zint_decode(src, &a1.sysno);
87 strcpy(a1.term, *src);
88 *src += strlen(a1.term) + 1;
90 memcpy(*dst, &a1, sizeof(a1));
94 void sort_term_code_reset(void *p)
98 void sort_term_code_stop(void *p)
103 struct sort_term_stream {
109 int sort_term_code_read(void *vp, char **dst, int *insertMode)
111 struct sort_term_stream *s = (struct sort_term_stream *) vp;
118 *insertMode = s->insert_flag;
119 memcpy(*dst, &s->st, sizeof(s->st));
120 *dst += sizeof(s->st);
125 struct sortFileHead {
137 struct sortFile *next;
138 struct sortFileHead head;
143 struct zebra_sort_index {
149 struct sortFile *current_file;
150 struct sortFile *files;
153 zebra_sort_index_t zebra_sort_open(BFiles bfs, int write_flag, int type)
155 zebra_sort_index_t si = (zebra_sort_index_t) xmalloc(sizeof(*si));
157 si->write_flag = write_flag;
158 si->current_file = NULL;
161 si->entry_buf = (char *) xmalloc(SORT_IDX_ENTRYSIZE);
165 void zebra_sort_close(zebra_sort_index_t si)
167 struct sortFile *sf = si->files;
170 struct sortFile *sf_next = sf->next;
173 case ZEBRA_SORT_TYPE_FLAT:
176 case ZEBRA_SORT_TYPE_ISAMB:
178 isamb_pp_close(sf->isam_pp);
179 isamb_set_root_ptr(sf->u.isamb, sf->isam_p);
180 isamb_close(sf->u.isamb);
186 xfree(si->entry_buf);
190 int zebra_sort_type(zebra_sort_index_t si, int id)
192 int isam_block_size = 4096;
196 if (si->current_file && si->current_file->id == id)
198 for (sf = si->files; sf; sf = sf->next)
201 si->current_file = sf;
204 sf = (struct sortFile *) xmalloc(sizeof(*sf));
207 method.compare_item = sort_term_compare;
208 method.log_item = sort_term_log_item;
209 method.codec.start = sort_term_code_start;
210 method.codec.encode = sort_term_encode;
211 method.codec.decode = sort_term_decode;
212 method.codec.reset = sort_term_code_reset;
213 method.codec.stop = sort_term_code_stop;
217 case ZEBRA_SORT_TYPE_FLAT:
219 sprintf(fname, "sort%d", id);
220 yaz_log(YLOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
221 sf->u.bf = bf_open(si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
227 if (!bf_read(sf->u.bf, 0, 0, sizeof(sf->head), &sf->head))
229 sf->head.sysno_max = 0;
238 case ZEBRA_SORT_TYPE_ISAMB:
239 sprintf(fname, "sortb%d", id);
241 sf->u.isamb = isamb_open2(si->bfs, fname, si->write_flag, &method,
243 /* no_cat */ 1, &isam_block_size,
244 /* use_root_ptr */ 1);
252 sf->isam_p = isamb_get_root_ptr(sf->u.isamb);
259 sf->next = si->files;
260 si->current_file = si->files = sf;
264 void zebra_sort_sysno(zebra_sort_index_t si, zint sysno)
266 struct sortFile *sf = si->current_file;
267 zint new_sysno = rec_sysno_to_int(sysno);
269 for (sf = si->files; sf; sf = sf->next)
273 if (sf->isam_pp && new_sysno < si->sysno && sf->isam_pp)
275 isamb_pp_close(sf->isam_pp);
279 si->sysno = new_sysno;
283 void zebra_sort_delete(zebra_sort_index_t si)
285 struct sortFile *sf = si->current_file;
287 if (!sf || !sf->u.bf)
291 case ZEBRA_SORT_TYPE_FLAT:
292 zebra_sort_add(si, "", 0);
294 case ZEBRA_SORT_TYPE_ISAMB:
296 if (sf->no_deleted == 0)
298 struct sort_term_stream s;
301 s.st.sysno = si->sysno;
306 isamc_i.clientData = &s;
307 isamc_i.read_item = sort_term_code_read;
309 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
316 void zebra_sort_add(zebra_sort_index_t si, const char *buf, int len)
318 struct sortFile *sf = si->current_file;
320 if (!sf || !sf->u.bf)
324 case ZEBRA_SORT_TYPE_FLAT:
325 if (len > SORT_IDX_ENTRYSIZE)
327 len = SORT_IDX_ENTRYSIZE;
328 memcpy(si->entry_buf, buf, len);
332 memcpy(si->entry_buf, buf, len);
333 memset(si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
335 bf_write(sf->u.bf, si->sysno+1, 0, 0, si->entry_buf);
337 case ZEBRA_SORT_TYPE_ISAMB:
339 if (sf->no_inserted == 0)
341 struct sort_term_stream s;
344 s.st.sysno = si->sysno;
345 if (len >= SORT_MAX_TERM)
346 len = SORT_MAX_TERM-1;
347 memcpy(s.st.term, buf, len);
348 s.st.term[len] = '\0';
351 isamc_i.clientData = &s;
352 isamc_i.read_item = sort_term_code_read;
354 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
361 void zebra_sort_read(zebra_sort_index_t si, char *buf)
364 struct sortFile *sf = si->current_file;
370 case ZEBRA_SORT_TYPE_FLAT:
371 r = bf_read(sf->u.bf, si->sysno+1, 0, 0, buf);
373 memset(buf, 0, SORT_IDX_ENTRYSIZE);
375 case ZEBRA_SORT_TYPE_ISAMB:
376 memset(buf, 0, SORT_IDX_ENTRYSIZE);
380 struct sort_term st, st_untilbuf;
383 sf->isam_pp = isamb_pp_open(sf->u.isamb, sf->isam_p, 1);
390 r = isamb_pp_read(sf->isam_pp, &st);
393 if (st.sysno == si->sysno)
395 yaz_log(YLOG_LOG, "Received sysno=" ZINT_FORMAT " looking for "
396 ZINT_FORMAT, st.sysno, si->sysno);
399 st_untilbuf.sysno = si->sysno;
400 st_untilbuf.term[0] = '\0';
401 r = isamb_pp_forward(sf->isam_pp, &st, &st_untilbuf);
407 if (st.sysno != si->sysno)
409 yaz_log(YLOG_LOG, "Received sysno=" ZINT_FORMAT " looking for "
410 ZINT_FORMAT, st.sysno, si->sysno);
413 if (strlen(st.term) < SORT_IDX_ENTRYSIZE)
414 strcpy(buf, st.term);
416 memcpy(buf, st.term, SORT_IDX_ENTRYSIZE);
425 * indent-tabs-mode: nil
427 * vim: shiftwidth=4 tabstop=8 expandtab