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