1 /* This file is part of the Zebra server.
2 Copyright (C) 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
28 #include <yaz/xmalloc.h>
29 #include <idzebra/isamb.h>
30 #include <idzebra/bfile.h>
34 #define SORT_MAX_TERM 110
35 #define SORT_MAX_MULTI 4096
37 #define SORT_IDX_BLOCKSIZE 64
43 char term[SORT_MAX_MULTI];
47 static void sort_term_log_item(int level, const void *b, const char *txt)
51 memcpy(&a1, b, sizeof(a1));
53 yaz_log(level, "%s " ZINT_FORMAT " " ZINT_FORMAT " %.*s", txt, a1.sysno,
54 a1.section_id, (int) a1.length-1, a1.term);
57 static int sort_term_compare(const void *a, const void *b)
59 struct sort_term a1, b1;
61 memcpy(&a1, a, sizeof(a1));
62 memcpy(&b1, b, sizeof(b1));
64 if (a1.sysno > b1.sysno)
66 else if (a1.sysno < b1.sysno)
68 if (a1.section_id > b1.section_id)
70 else if (a1.section_id < b1.section_id)
76 static void *sort_term_code_start(void)
81 static void sort_term_encode1(void *p, char **dst, const char **src)
85 memcpy(&a1, *src, sizeof(a1));
88 zebra_zint_encode(dst, a1.sysno); /* encode record id */
89 strcpy(*dst, a1.term); /* then sort term, 0 terminated */
90 *dst += strlen(a1.term) + 1;
93 static void sort_term_encode2(void *p, char **dst, const char **src)
97 memcpy(&a1, *src, sizeof(a1));
100 zebra_zint_encode(dst, a1.sysno);
101 zebra_zint_encode(dst, a1.section_id);
102 zebra_zint_encode(dst, a1.length); /* encode length */
103 memcpy(*dst, a1.term, a1.length);
107 static void sort_term_decode1(void *p, char **dst, const char **src)
112 zebra_zint_decode(src, &a1.sysno);
115 strcpy(a1.term, *src);
116 slen = 1 + strlen(a1.term);
120 memcpy(*dst, &a1, sizeof(a1));
124 static void sort_term_decode2(void *p, char **dst, const char **src)
128 zebra_zint_decode(src, &a1.sysno);
129 zebra_zint_decode(src, &a1.section_id);
130 zebra_zint_decode(src, &a1.length);
132 memcpy(a1.term, *src, a1.length);
135 memcpy(*dst, &a1, sizeof(a1));
139 static void sort_term_code_reset(void *p)
143 static void sort_term_code_stop(void *p)
147 struct sort_term_stream {
153 static int sort_term_code_read(void *vp, char **dst, int *insertMode)
155 struct sort_term_stream *s = (struct sort_term_stream *) vp;
162 *insertMode = s->insert_flag;
163 memcpy(*dst, &s->st, sizeof(s->st));
164 *dst += sizeof(s->st);
168 struct sortFileHead {
180 struct sortFile *next;
181 struct sortFileHead head;
186 struct zebra_sort_index {
192 struct sortFile *current_file;
193 struct sortFile *files;
196 zebra_sort_index_t zebra_sort_open(BFiles bfs, int write_flag, int type)
198 zebra_sort_index_t si = (zebra_sort_index_t) xmalloc(sizeof(*si));
200 si->write_flag = write_flag;
201 si->current_file = NULL;
204 si->entry_buf = (char *) xmalloc(SORT_IDX_ENTRYSIZE);
208 void zebra_sort_close(zebra_sort_index_t si)
210 struct sortFile *sf = si->files;
213 struct sortFile *sf_next = sf->next;
216 case ZEBRA_SORT_TYPE_FLAT:
219 case ZEBRA_SORT_TYPE_ISAMB:
220 case ZEBRA_SORT_TYPE_MULTI:
222 isamb_pp_close(sf->isam_pp);
223 isamb_set_root_ptr(sf->u.isamb, sf->isam_p);
224 isamb_close(sf->u.isamb);
230 xfree(si->entry_buf);
234 int zebra_sort_type(zebra_sort_index_t si, int id)
236 int isam_block_size = 4096;
242 method.compare_item = sort_term_compare;
243 method.log_item = sort_term_log_item;
244 method.codec.reset = sort_term_code_reset;
245 method.codec.start = sort_term_code_start;
246 method.codec.stop = sort_term_code_stop;
248 if (si->current_file && si->current_file->id == id)
250 for (sf = si->files; sf; sf = sf->next)
253 si->current_file = sf;
256 sf = (struct sortFile *) xmalloc(sizeof(*sf));
261 case ZEBRA_SORT_TYPE_FLAT:
263 sprintf(fname, "sort%d", id);
264 yaz_log(YLOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
265 sf->u.bf = bf_open(si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
271 if (!bf_read(sf->u.bf, 0, 0, sizeof(sf->head), &sf->head))
273 sf->head.sysno_max = 0;
282 case ZEBRA_SORT_TYPE_ISAMB:
283 method.codec.encode = sort_term_encode1;
284 method.codec.decode = sort_term_decode1;
286 sprintf(fname, "sortb%d", id);
287 sf->u.isamb = isamb_open2(si->bfs, fname, si->write_flag, &method,
289 /* no_cat */ 1, &isam_block_size,
290 /* use_root_ptr */ 1);
298 sf->isam_p = isamb_get_root_ptr(sf->u.isamb);
301 case ZEBRA_SORT_TYPE_MULTI:
302 isam_block_size = 32768;
303 method.codec.encode = sort_term_encode2;
304 method.codec.decode = sort_term_decode2;
306 sprintf(fname, "sortm%d", id);
307 sf->u.isamb = isamb_open2(si->bfs, fname, si->write_flag, &method,
309 /* no_cat */ 1, &isam_block_size,
310 /* use_root_ptr */ 1);
318 sf->isam_p = isamb_get_root_ptr(sf->u.isamb);
325 sf->next = si->files;
326 si->current_file = si->files = sf;
330 static void zebra_sortf_rewind(struct sortFile *sf)
333 isamb_pp_close(sf->isam_pp);
339 void zebra_sort_sysno(zebra_sort_index_t si, zint sysno)
341 zint new_sysno = rec_sysno_to_int(sysno);
344 for (sf = si->files; sf; sf = sf->next)
346 if (sf->no_inserted || sf->no_deleted)
347 zebra_sortf_rewind(sf);
348 else if (sf->isam_pp && new_sysno <= si->sysno)
349 zebra_sortf_rewind(sf);
351 si->sysno = new_sysno;
355 void zebra_sort_delete(zebra_sort_index_t si, zint section_id)
357 struct sortFile *sf = si->current_file;
359 if (!sf || !sf->u.bf)
363 case ZEBRA_SORT_TYPE_FLAT:
364 memset(si->entry_buf, 0, SORT_IDX_ENTRYSIZE);
365 bf_write(sf->u.bf, si->sysno+1, 0, 0, si->entry_buf);
367 case ZEBRA_SORT_TYPE_ISAMB:
368 case ZEBRA_SORT_TYPE_MULTI:
370 if (sf->no_deleted == 0)
372 struct sort_term_stream s;
375 s.st.sysno = si->sysno;
376 s.st.section_id = section_id;
382 isamc_i.clientData = &s;
383 isamc_i.read_item = sort_term_code_read;
385 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
392 void zebra_sort_add(zebra_sort_index_t si, zint section_id, WRBUF wrbuf)
394 struct sortFile *sf = si->current_file;
397 if (!sf || !sf->u.bf)
401 case ZEBRA_SORT_TYPE_FLAT:
402 /* take first entry from wrbuf - itself is 0-terminated */
403 len = strlen(wrbuf_buf(wrbuf));
404 if (len > SORT_IDX_ENTRYSIZE)
405 len = SORT_IDX_ENTRYSIZE;
407 memcpy(si->entry_buf, wrbuf_buf(wrbuf), len);
408 if (len < SORT_IDX_ENTRYSIZE-len)
409 memset(si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
410 bf_write(sf->u.bf, si->sysno+1, 0, 0, si->entry_buf);
412 case ZEBRA_SORT_TYPE_ISAMB:
415 if (sf->no_inserted == 0)
417 struct sort_term_stream s;
419 /* take first entry from wrbuf - itself is 0-terminated */
421 len = wrbuf_len(wrbuf);
422 if (len > SORT_MAX_TERM)
425 wrbuf_buf(wrbuf)[len-1] = '\0';
427 memcpy(s.st.term, wrbuf_buf(wrbuf), len);
429 s.st.sysno = si->sysno;
433 isamc_i.clientData = &s;
434 isamc_i.read_item = sort_term_code_read;
436 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
440 case ZEBRA_SORT_TYPE_MULTI:
442 if (sf->no_inserted == 0)
444 struct sort_term_stream s;
446 len = wrbuf_len(wrbuf);
447 if (len > SORT_MAX_MULTI)
449 len = SORT_MAX_MULTI;
450 wrbuf_buf(wrbuf)[len-1] = '\0';
452 memcpy(s.st.term, wrbuf_buf(wrbuf), len);
454 s.st.sysno = si->sysno;
455 s.st.section_id = section_id;
458 isamc_i.clientData = &s;
459 isamc_i.read_item = sort_term_code_read;
461 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
469 int zebra_sort_read(zebra_sort_index_t si, zint *section_id, WRBUF w)
472 struct sortFile *sf = si->current_file;
473 char tbuf[SORT_IDX_ENTRYSIZE];
480 case ZEBRA_SORT_TYPE_FLAT:
481 r = bf_read(sf->u.bf, si->sysno+1, 0, 0, tbuf);
489 case ZEBRA_SORT_TYPE_ISAMB:
490 case ZEBRA_SORT_TYPE_MULTI:
495 sf->isam_pp = isamb_pp_open(sf->u.isamb, sf->isam_p, 1);
498 struct sort_term st, st_untilbuf;
500 st_untilbuf.sysno = si->sysno;
501 st_untilbuf.section_id = 0;
502 st_untilbuf.length = 0;
503 st_untilbuf.term[0] = '\0';
504 r = isamb_pp_forward(sf->isam_pp, &st, &st_untilbuf);
505 if (r && st.sysno == si->sysno)
507 wrbuf_write(w, st.term, st.length);
509 *section_id = st.section_id;
521 * c-file-style: "Stroustrup"
522 * indent-tabs-mode: nil
524 * vim: shiftwidth=4 tabstop=8 expandtab