Improved installation. Updated for inclusion of YAZ header files.
[idzebra-moved-to-github.git] / index / sortidx.c
1 /*
2  * Copyright (C) 1998, Index Data ApS
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: sortidx.c,v $
7  * Revision 1.4  1999-11-30 13:48:03  adam
8  * Improved installation. Updated for inclusion of YAZ header files.
9  *
10  * Revision 1.3  1999/05/26 07:49:13  adam
11  * C++ compilation.
12  *
13  * Revision 1.2  1998/06/25 09:55:50  adam
14  * Minor changes - fixex headers.
15  *
16  */
17  
18 #include <string.h>
19
20 #include <yaz/log.h>
21 #include <bfile.h>
22 #include <sortidx.h>
23
24 #define SORT_IDX_BLOCKSIZE 64
25
26 struct sortFileHead {
27     int sysno_max;
28 };
29
30 struct sortFile {
31     int type;
32     BFile bf;
33     struct sortFile *next;
34     struct sortFileHead head;
35 };
36
37 struct sortIdx {
38     BFiles bfs;
39     int write_flag;
40     int sysno;
41     char *entry_buf;
42     struct sortFile *current_file;
43     struct sortFile *files;
44 };
45
46 SortIdx sortIdx_open (BFiles bfs, int write_flag)
47 {
48     SortIdx si = (SortIdx) xmalloc (sizeof(*si));
49     si->bfs = bfs;
50     si->write_flag = write_flag;
51     si->current_file = NULL;
52     si->files = NULL;
53     si->entry_buf = (char *) xmalloc (SORT_IDX_ENTRYSIZE);
54     return si;
55 }
56
57 void sortIdx_close (SortIdx si)
58 {
59     struct sortFile *sf = si->files;
60     while (sf)
61     {
62         struct sortFile *sf_next = sf->next;
63         if (sf->bf)
64             bf_close (sf->bf);
65         xfree (sf);
66         sf = sf_next;
67     }
68     xfree (si->entry_buf);
69     xfree (si);
70 }
71
72 int sortIdx_type (SortIdx si, int type)
73 {
74     char fname[80];
75     struct sortFile *sf;
76     if (si->current_file && si->current_file->type == type)
77         return 0;
78     for (sf = si->files; sf; sf = sf->next)
79         if (sf->type == type)
80         {
81             si->current_file = sf;
82             return 0;
83         }
84     sf = (struct sortFile *) xmalloc (sizeof(*sf));
85     sf->type = type;
86     sf->bf = NULL;
87     sf->next = si->files;
88     si->current_file = si->files = sf;
89     sprintf (fname, "sort%d", type);
90     logf (LOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
91     sf->bf = bf_open (si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
92     if (!sf->bf)
93         return -1;
94     if (!bf_read (sf->bf, 0, 0, sizeof(sf->head), &sf->head))
95     {
96         sf->head.sysno_max = 0;
97         if (!si->write_flag)
98             return -1;
99     }
100     return 0;
101 }
102
103 void sortIdx_sysno (SortIdx si, int sysno)
104 {
105     si->sysno = sysno;
106 }
107
108 void sortIdx_add (SortIdx si, const char *buf, int len)
109 {
110     if (!si->current_file || !si->current_file->bf)
111         return;
112     if (len > SORT_IDX_ENTRYSIZE)
113     {
114         len = SORT_IDX_ENTRYSIZE;
115         memcpy (si->entry_buf, buf, len);
116     }
117     else
118     {
119         memcpy (si->entry_buf, buf, len);
120         memset (si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
121     }
122     bf_write (si->current_file->bf, si->sysno+1, 0, 0, si->entry_buf);
123 }
124
125 void sortIdx_read (SortIdx si, char *buf)
126 {
127     bf_read (si->current_file->bf, si->sysno+1, 0, 0, buf);
128 }