1 /* $Id: sortidx.c,v 1.11 2004-12-08 12:23:09 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 <idzebra/bfile.h>
31 #define SORT_IDX_BLOCKSIZE 64
40 struct sortFile *next;
41 struct sortFileHead head;
49 struct sortFile *current_file;
50 struct sortFile *files;
53 SortIdx sortIdx_open (BFiles bfs, int write_flag)
55 SortIdx si = (SortIdx) xmalloc (sizeof(*si));
57 si->write_flag = write_flag;
58 si->current_file = NULL;
60 si->entry_buf = (char *) xmalloc (SORT_IDX_ENTRYSIZE);
64 void sortIdx_close (SortIdx si)
66 struct sortFile *sf = si->files;
69 struct sortFile *sf_next = sf->next;
75 xfree (si->entry_buf);
79 int sortIdx_type (SortIdx si, int type)
83 if (si->current_file && si->current_file->type == type)
85 for (sf = si->files; sf; sf = sf->next)
88 si->current_file = sf;
91 sf = (struct sortFile *) xmalloc (sizeof(*sf));
94 sprintf (fname, "sort%d", type);
95 yaz_log (YLOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
96 sf->bf = bf_open (si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
102 if (!bf_read (sf->bf, 0, 0, sizeof(sf->head), &sf->head))
104 sf->head.sysno_max = 0;
112 sf->next = si->files;
113 si->current_file = si->files = sf;
117 void sortIdx_sysno (SortIdx si, SYSNO sysno)
122 void sortIdx_add (SortIdx si, const char *buf, int len)
124 if (!si->current_file || !si->current_file->bf)
126 if (len > SORT_IDX_ENTRYSIZE)
128 len = SORT_IDX_ENTRYSIZE;
129 memcpy (si->entry_buf, buf, len);
133 memcpy (si->entry_buf, buf, len);
134 memset (si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
136 bf_write (si->current_file->bf, si->sysno+1, 0, 0, si->entry_buf);
139 void sortIdx_read (SortIdx si, char *buf)
141 int r = bf_read (si->current_file->bf, si->sysno+1, 0, 0, buf);
143 memset (buf, 0, SORT_IDX_ENTRYSIZE);