1 /* $Id: sortidx.c,v 1.13 2005-01-15 19:38:27 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #include <yaz/xmalloc.h>
29 #include <idzebra/bfile.h>
32 #define SORT_IDX_BLOCKSIZE 64
41 struct sortFile *next;
42 struct sortFileHead head;
50 struct sortFile *current_file;
51 struct sortFile *files;
54 SortIdx sortIdx_open (BFiles bfs, int write_flag)
56 SortIdx si = (SortIdx) xmalloc (sizeof(*si));
58 si->write_flag = write_flag;
59 si->current_file = NULL;
61 si->entry_buf = (char *) xmalloc (SORT_IDX_ENTRYSIZE);
65 void sortIdx_close (SortIdx si)
67 struct sortFile *sf = si->files;
70 struct sortFile *sf_next = sf->next;
76 xfree (si->entry_buf);
80 int sortIdx_type (SortIdx si, int type)
84 if (si->current_file && si->current_file->type == type)
86 for (sf = si->files; sf; sf = sf->next)
89 si->current_file = sf;
92 sf = (struct sortFile *) xmalloc (sizeof(*sf));
95 sprintf (fname, "sort%d", type);
96 yaz_log (YLOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
97 sf->bf = bf_open (si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
103 if (!bf_read (sf->bf, 0, 0, sizeof(sf->head), &sf->head))
105 sf->head.sysno_max = 0;
113 sf->next = si->files;
114 si->current_file = si->files = sf;
118 void sortIdx_sysno (SortIdx si, SYSNO sysno)
123 void sortIdx_add (SortIdx si, const char *buf, int len)
125 if (!si->current_file || !si->current_file->bf)
127 if (len > SORT_IDX_ENTRYSIZE)
129 len = SORT_IDX_ENTRYSIZE;
130 memcpy (si->entry_buf, buf, len);
134 memcpy (si->entry_buf, buf, len);
135 memset (si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
137 bf_write (si->current_file->bf, si->sysno+1, 0, 0, si->entry_buf);
140 void sortIdx_read (SortIdx si, char *buf)
142 int r = bf_read (si->current_file->bf, si->sysno+1, 0, 0, buf);
144 memset (buf, 0, SORT_IDX_ENTRYSIZE);