1 /* $Id: sortidx.c,v 1.16 2006-05-10 08:13:22 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>
33 #define SORT_IDX_BLOCKSIZE 64
42 struct sortFile *next;
43 struct sortFileHead head;
51 struct sortFile *current_file;
52 struct sortFile *files;
55 SortIdx sortIdx_open (BFiles bfs, int write_flag)
57 SortIdx si = (SortIdx) xmalloc (sizeof(*si));
59 si->write_flag = write_flag;
60 si->current_file = NULL;
62 si->entry_buf = (char *) xmalloc (SORT_IDX_ENTRYSIZE);
66 void sortIdx_close (SortIdx si)
68 struct sortFile *sf = si->files;
71 struct sortFile *sf_next = sf->next;
77 xfree (si->entry_buf);
81 int sortIdx_type (SortIdx si, int type)
85 if (si->current_file && si->current_file->type == type)
87 for (sf = si->files; sf; sf = sf->next)
90 si->current_file = sf;
93 sf = (struct sortFile *) xmalloc (sizeof(*sf));
96 sprintf (fname, "sort%d", type);
97 yaz_log (YLOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
98 sf->bf = bf_open (si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
104 if (!bf_read (sf->bf, 0, 0, sizeof(sf->head), &sf->head))
106 sf->head.sysno_max = 0;
114 sf->next = si->files;
115 si->current_file = si->files = sf;
119 void sortIdx_sysno (SortIdx si, SYSNO sysno)
121 si->sysno = rec_sysno_to_int(sysno);
124 void sortIdx_add (SortIdx si, const char *buf, int len)
126 if (!si->current_file || !si->current_file->bf)
128 if (len > SORT_IDX_ENTRYSIZE)
130 len = SORT_IDX_ENTRYSIZE;
131 memcpy (si->entry_buf, buf, len);
135 memcpy (si->entry_buf, buf, len);
136 memset (si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
138 bf_write (si->current_file->bf, si->sysno+1, 0, 0, si->entry_buf);
141 void sortIdx_read (SortIdx si, char *buf)
143 int r = bf_read (si->current_file->bf, si->sysno+1, 0, 0, buf);
145 memset (buf, 0, SORT_IDX_ENTRYSIZE);
150 * indent-tabs-mode: nil
152 * vim: shiftwidth=4 tabstop=8 expandtab