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