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 static 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 static void *sort_term_code_start(void)
69 static 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 static 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 static void sort_term_code_reset(void *p)
98 static void sort_term_code_stop(void *p)
102 struct sort_term_stream {
108 static int sort_term_code_read(void *vp, char **dst, int *insertMode)
110 struct sort_term_stream *s = (struct sort_term_stream *) vp;
117 *insertMode = s->insert_flag;
118 memcpy(*dst, &s->st, sizeof(s->st));
119 *dst += sizeof(s->st);
123 struct sortFileHead {
135 struct sortFile *next;
136 struct sortFileHead head;
141 struct zebra_sort_index {
147 struct sortFile *current_file;
148 struct sortFile *files;
151 zebra_sort_index_t zebra_sort_open(BFiles bfs, int write_flag, int type)
153 zebra_sort_index_t si = (zebra_sort_index_t) xmalloc(sizeof(*si));
155 si->write_flag = write_flag;
156 si->current_file = NULL;
159 si->entry_buf = (char *) xmalloc(SORT_IDX_ENTRYSIZE);
163 void zebra_sort_close(zebra_sort_index_t si)
165 struct sortFile *sf = si->files;
168 struct sortFile *sf_next = sf->next;
171 case ZEBRA_SORT_TYPE_FLAT:
174 case ZEBRA_SORT_TYPE_ISAMB:
176 isamb_pp_close(sf->isam_pp);
177 isamb_set_root_ptr(sf->u.isamb, sf->isam_p);
178 isamb_close(sf->u.isamb);
184 xfree(si->entry_buf);
188 int zebra_sort_type(zebra_sort_index_t si, int id)
190 int isam_block_size = 4096;
195 if (si->current_file && si->current_file->id == id)
197 for (sf = si->files; sf; sf = sf->next)
200 si->current_file = sf;
203 sf = (struct sortFile *) xmalloc(sizeof(*sf));
208 case ZEBRA_SORT_TYPE_FLAT:
210 sprintf(fname, "sort%d", id);
211 yaz_log(YLOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
212 sf->u.bf = bf_open(si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
218 if (!bf_read(sf->u.bf, 0, 0, sizeof(sf->head), &sf->head))
220 sf->head.sysno_max = 0;
229 case ZEBRA_SORT_TYPE_ISAMB:
230 method.compare_item = sort_term_compare;
231 method.log_item = sort_term_log_item;
232 method.codec.start = sort_term_code_start;
233 method.codec.encode = sort_term_encode;
234 method.codec.decode = sort_term_decode;
235 method.codec.reset = sort_term_code_reset;
236 method.codec.stop = sort_term_code_stop;
238 sprintf(fname, "sortb%d", id);
239 sf->u.isamb = isamb_open2(si->bfs, fname, si->write_flag, &method,
241 /* no_cat */ 1, &isam_block_size,
242 /* use_root_ptr */ 1);
250 sf->isam_p = isamb_get_root_ptr(sf->u.isamb);
257 sf->next = si->files;
258 si->current_file = si->files = sf;
262 void zebra_sort_sysno(zebra_sort_index_t si, zint sysno)
264 struct sortFile *sf = si->current_file;
265 zint new_sysno = rec_sysno_to_int(sysno);
267 for (sf = si->files; sf; sf = sf->next)
271 if (sf->isam_pp && new_sysno < si->sysno && sf->isam_pp)
273 isamb_pp_close(sf->isam_pp);
277 si->sysno = new_sysno;
281 void zebra_sort_delete(zebra_sort_index_t si)
283 struct sortFile *sf = si->current_file;
285 if (!sf || !sf->u.bf)
289 case ZEBRA_SORT_TYPE_FLAT:
290 zebra_sort_add(si, "", 0);
292 case ZEBRA_SORT_TYPE_ISAMB:
294 if (sf->no_deleted == 0)
296 struct sort_term_stream s;
299 s.st.sysno = si->sysno;
304 isamc_i.clientData = &s;
305 isamc_i.read_item = sort_term_code_read;
307 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
314 void zebra_sort_add(zebra_sort_index_t si, const char *buf, int len)
316 struct sortFile *sf = si->current_file;
318 if (!sf || !sf->u.bf)
322 case ZEBRA_SORT_TYPE_FLAT:
323 if (len > SORT_IDX_ENTRYSIZE)
325 len = SORT_IDX_ENTRYSIZE;
326 memcpy(si->entry_buf, buf, len);
330 memcpy(si->entry_buf, buf, len);
331 memset(si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
333 bf_write(sf->u.bf, si->sysno+1, 0, 0, si->entry_buf);
335 case ZEBRA_SORT_TYPE_ISAMB:
337 if (sf->no_inserted == 0)
339 struct sort_term_stream s;
342 s.st.sysno = si->sysno;
343 if (len >= SORT_MAX_TERM)
344 len = SORT_MAX_TERM-1;
345 memcpy(s.st.term, buf, len);
346 s.st.term[len] = '\0';
349 isamc_i.clientData = &s;
350 isamc_i.read_item = sort_term_code_read;
352 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
359 int zebra_sort_read(zebra_sort_index_t si, char *buf)
362 struct sortFile *sf = si->current_file;
369 case ZEBRA_SORT_TYPE_FLAT:
370 r = bf_read(sf->u.bf, si->sysno+1, 0, 0, buf);
372 memset(buf, 0, SORT_IDX_ENTRYSIZE);
376 case ZEBRA_SORT_TYPE_ISAMB:
377 memset(buf, 0, SORT_IDX_ENTRYSIZE);
382 struct sort_term st, st_untilbuf;
385 sf->isam_pp = isamb_pp_open(sf->u.isamb, sf->isam_p, 1);
389 st_untilbuf.sysno = si->sysno;
390 st_untilbuf.term[0] = '\0';
391 r = isamb_pp_forward(sf->isam_pp, &st, &st_untilbuf);
396 if (st.sysno != si->sysno)
398 yaz_log(YLOG_LOG, "Received sysno=" ZINT_FORMAT " looking for "
399 ZINT_FORMAT, st.sysno, si->sysno);
402 if (strlen(st.term) < SORT_IDX_ENTRYSIZE)
403 strcpy(buf, st.term);
405 memcpy(buf, st.term, SORT_IDX_ENTRYSIZE);
415 * indent-tabs-mode: nil
417 * vim: shiftwidth=4 tabstop=8 expandtab