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
32 #define SORT_MAX_MULTI 4096
34 #define SORT_IDX_BLOCKSIZE 64
39 char term[SORT_MAX_MULTI];
43 static void sort_term_log_item(int level, const void *b, const char *txt)
47 memcpy(&a1, b, sizeof(a1));
49 yaz_log(level, "%s " ZINT_FORMAT " %.*s", txt, a1.sysno,
50 (int) a1.length-1, a1.term);
53 static int sort_term_compare(const void *a, const void *b)
55 struct sort_term a1, b1;
57 memcpy(&a1, a, sizeof(a1));
58 memcpy(&b1, b, sizeof(b1));
60 if (a1.sysno > b1.sysno)
62 else if (a1.sysno < b1.sysno)
67 static void *sort_term_code_start(void)
72 static void sort_term_encode1(void *p, char **dst, const char **src)
76 memcpy(&a1, *src, sizeof(a1));
79 zebra_zint_encode(dst, a1.sysno); /* encode record id */
80 strcpy(*dst, a1.term); /* then sort term, 0 terminated */
81 *dst += strlen(a1.term) + 1;
84 static void sort_term_encode2(void *p, char **dst, const char **src)
88 memcpy(&a1, *src, sizeof(a1));
91 zebra_zint_encode(dst, a1.sysno); /* encode record id */
92 zebra_zint_encode(dst, a1.length); /* encode length */
93 memcpy(*dst, a1.term, a1.length);
97 static void sort_term_decode1(void *p, char **dst, const char **src)
102 zebra_zint_decode(src, &a1.sysno);
104 strcpy(a1.term, *src);
105 slen = 1 + strlen(a1.term);
109 memcpy(*dst, &a1, sizeof(a1));
113 static void sort_term_decode2(void *p, char **dst, const char **src)
117 zebra_zint_decode(src, &a1.sysno);
118 zebra_zint_decode(src, &a1.length);
120 memcpy(a1.term, *src, a1.length);
123 memcpy(*dst, &a1, sizeof(a1));
127 static void sort_term_code_reset(void *p)
131 static void sort_term_code_stop(void *p)
135 struct sort_term_stream {
141 static int sort_term_code_read(void *vp, char **dst, int *insertMode)
143 struct sort_term_stream *s = (struct sort_term_stream *) vp;
150 *insertMode = s->insert_flag;
151 memcpy(*dst, &s->st, sizeof(s->st));
152 *dst += sizeof(s->st);
156 struct sortFileHead {
168 struct sortFile *next;
169 struct sortFileHead head;
174 struct zebra_sort_index {
180 struct sortFile *current_file;
181 struct sortFile *files;
184 zebra_sort_index_t zebra_sort_open(BFiles bfs, int write_flag, int type)
186 zebra_sort_index_t si = (zebra_sort_index_t) xmalloc(sizeof(*si));
188 si->write_flag = write_flag;
189 si->current_file = NULL;
192 si->entry_buf = (char *) xmalloc(SORT_IDX_ENTRYSIZE);
196 void zebra_sort_close(zebra_sort_index_t si)
198 struct sortFile *sf = si->files;
201 struct sortFile *sf_next = sf->next;
204 case ZEBRA_SORT_TYPE_FLAT:
207 case ZEBRA_SORT_TYPE_ISAMB:
208 case ZEBRA_SORT_TYPE_MULTI:
210 isamb_pp_close(sf->isam_pp);
211 isamb_set_root_ptr(sf->u.isamb, sf->isam_p);
212 isamb_close(sf->u.isamb);
218 xfree(si->entry_buf);
222 int zebra_sort_type(zebra_sort_index_t si, int id)
224 int isam_block_size = 4096;
230 method.compare_item = sort_term_compare;
231 method.log_item = sort_term_log_item;
232 method.codec.reset = sort_term_code_reset;
233 method.codec.start = sort_term_code_start;
234 method.codec.stop = sort_term_code_stop;
236 if (si->current_file && si->current_file->id == id)
238 for (sf = si->files; sf; sf = sf->next)
241 si->current_file = sf;
244 sf = (struct sortFile *) xmalloc(sizeof(*sf));
249 case ZEBRA_SORT_TYPE_FLAT:
251 sprintf(fname, "sort%d", id);
252 yaz_log(YLOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
253 sf->u.bf = bf_open(si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
259 if (!bf_read(sf->u.bf, 0, 0, sizeof(sf->head), &sf->head))
261 sf->head.sysno_max = 0;
270 case ZEBRA_SORT_TYPE_ISAMB:
271 method.codec.encode = sort_term_encode1;
272 method.codec.decode = sort_term_decode1;
274 sprintf(fname, "sortb%d", id);
275 sf->u.isamb = isamb_open2(si->bfs, fname, si->write_flag, &method,
277 /* no_cat */ 1, &isam_block_size,
278 /* use_root_ptr */ 1);
286 sf->isam_p = isamb_get_root_ptr(sf->u.isamb);
289 case ZEBRA_SORT_TYPE_MULTI:
290 isam_block_size = 32768;
291 method.codec.encode = sort_term_encode2;
292 method.codec.decode = sort_term_decode2;
294 sprintf(fname, "sortm%d", id);
295 sf->u.isamb = isamb_open2(si->bfs, fname, si->write_flag, &method,
297 /* no_cat */ 1, &isam_block_size,
298 /* use_root_ptr */ 1);
306 sf->isam_p = isamb_get_root_ptr(sf->u.isamb);
313 sf->next = si->files;
314 si->current_file = si->files = sf;
318 static void zebra_sortf_rewind(struct sortFile *sf)
321 isamb_pp_close(sf->isam_pp);
327 void zebra_sort_sysno(zebra_sort_index_t si, zint sysno)
329 zint new_sysno = rec_sysno_to_int(sysno);
332 for (sf = si->files; sf; sf = sf->next)
334 if (sf->no_inserted || sf->no_deleted)
335 zebra_sortf_rewind(sf);
336 else if (sf->isam_pp && new_sysno <= si->sysno)
337 zebra_sortf_rewind(sf);
339 si->sysno = new_sysno;
343 void zebra_sort_delete(zebra_sort_index_t si)
345 struct sortFile *sf = si->current_file;
347 if (!sf || !sf->u.bf)
351 case ZEBRA_SORT_TYPE_FLAT:
352 memset(si->entry_buf, 0, SORT_IDX_ENTRYSIZE);
353 bf_write(sf->u.bf, si->sysno+1, 0, 0, si->entry_buf);
355 case ZEBRA_SORT_TYPE_ISAMB:
356 case ZEBRA_SORT_TYPE_MULTI:
358 if (sf->no_deleted == 0)
360 struct sort_term_stream s;
363 s.st.sysno = si->sysno;
369 isamc_i.clientData = &s;
370 isamc_i.read_item = sort_term_code_read;
372 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
379 void zebra_sort_add(zebra_sort_index_t si, WRBUF wrbuf)
381 struct sortFile *sf = si->current_file;
384 if (!sf || !sf->u.bf)
388 case ZEBRA_SORT_TYPE_FLAT:
389 /* take first entry from wrbuf - itself is 0-terminated */
390 len = strlen(wrbuf_buf(wrbuf));
391 if (len > SORT_IDX_ENTRYSIZE)
392 len = SORT_IDX_ENTRYSIZE;
394 memcpy(si->entry_buf, wrbuf_buf(wrbuf), len);
395 if (len < SORT_IDX_ENTRYSIZE-len)
396 memset(si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
397 bf_write(sf->u.bf, si->sysno+1, 0, 0, si->entry_buf);
399 case ZEBRA_SORT_TYPE_ISAMB:
402 if (sf->no_inserted == 0)
404 struct sort_term_stream s;
406 /* take first entry from wrbuf - itself is 0-terminated */
408 len = wrbuf_len(wrbuf);
409 if (len > SORT_MAX_TERM)
412 wrbuf_buf(wrbuf)[len-1] = '\0';
414 memcpy(s.st.term, wrbuf_buf(wrbuf), len);
416 s.st.sysno = si->sysno;
419 isamc_i.clientData = &s;
420 isamc_i.read_item = sort_term_code_read;
422 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
426 case ZEBRA_SORT_TYPE_MULTI:
428 if (sf->no_inserted == 0)
430 struct sort_term_stream s;
432 len = wrbuf_len(wrbuf);
433 if (len > SORT_MAX_MULTI)
435 len = SORT_MAX_MULTI;
436 wrbuf_buf(wrbuf)[len-1] = '\0';
438 memcpy(s.st.term, wrbuf_buf(wrbuf), len);
440 s.st.sysno = si->sysno;
443 isamc_i.clientData = &s;
444 isamc_i.read_item = sort_term_code_read;
446 isamb_merge(sf->u.isamb, &sf->isam_p, &isamc_i);
454 int zebra_sort_read(zebra_sort_index_t si, WRBUF w)
457 struct sortFile *sf = si->current_file;
458 char tbuf[SORT_IDX_ENTRYSIZE];
465 case ZEBRA_SORT_TYPE_FLAT:
466 r = bf_read(sf->u.bf, si->sysno+1, 0, 0, tbuf);
474 case ZEBRA_SORT_TYPE_ISAMB:
475 case ZEBRA_SORT_TYPE_MULTI:
480 sf->isam_pp = isamb_pp_open(sf->u.isamb, sf->isam_p, 1);
483 struct sort_term st, st_untilbuf;
485 st_untilbuf.sysno = si->sysno;
486 st_untilbuf.length = 0;
487 st_untilbuf.term[0] = '\0';
488 r = isamb_pp_forward(sf->isam_pp, &st, &st_untilbuf);
489 if (r && st.sysno == si->sysno)
491 wrbuf_write(w, st.term, st.length);
503 * indent-tabs-mode: nil
505 * vim: shiftwidth=4 tabstop=8 expandtab