8 #define SORT_IDX_BLOCKSIZE 64
17 struct sortFile *next;
18 struct sortFileHead head;
26 struct sortFile *current_file;
27 struct sortFile *files;
30 SortIdx sortIdx_open (BFiles bfs, int write_flag)
32 SortIdx si = xmalloc (sizeof(*si));
34 si->write_flag = write_flag;
35 si->current_file = NULL;
37 si->entry_buf = xmalloc (SORT_IDX_ENTRYSIZE);
41 void sortIdx_close (SortIdx si)
43 struct sortFile *sf = si->files;
46 struct sortFile *sf_next = sf->next;
52 xfree (si->entry_buf);
56 int sortIdx_type (SortIdx si, int type)
60 if (si->current_file && si->current_file->type == type)
62 for (sf = si->files; sf; sf = sf->next)
65 si->current_file = sf;
68 sf = xmalloc (sizeof(*sf));
72 si->current_file = si->files = sf;
73 sprintf (fname, "sort%d", type);
74 logf (LOG_DEBUG, "sort idx %s wr=%d", fname, si->write_flag);
75 sf->bf = bf_open (si->bfs, fname, SORT_IDX_BLOCKSIZE, si->write_flag);
78 if (!bf_read (sf->bf, 0, 0, sizeof(sf->head), &sf->head))
80 sf->head.sysno_max = 0;
87 void sortIdx_sysno (SortIdx si, int sysno)
92 void sortIdx_add (SortIdx si, const char *buf, int len)
94 if (!si->current_file || !si->current_file->bf)
96 if (len > SORT_IDX_ENTRYSIZE)
98 len = SORT_IDX_ENTRYSIZE;
99 memcpy (si->entry_buf, buf, len);
103 memcpy (si->entry_buf, buf, len);
104 memset (si->entry_buf+len, 0, SORT_IDX_ENTRYSIZE-len);
106 bf_write (si->current_file->bf, si->sysno+1, 0, 0, si->entry_buf);
109 void sortIdx_read (SortIdx si, char *buf)
111 bf_read (si->current_file->bf, si->sysno+1, 0, 0, buf);