6 typedef struct IEBuck IEBuck;
7 typedef struct IEBucks IEBucks;
16 u32int head; /* head of chain of chunks on the disk */
17 u32int used; /* usage of the last chunk */
18 u64int total; /* total number of bytes in this bucket */
19 u8int *buf; /* chunk of entries for this bucket */
25 u64int off; /* offset for writing data in the partition */
26 u32int chunks; /* total chunks written to fd */
27 u64int max; /* max bytes entered in any one bucket */
28 int bits; /* number of bits in initial bucket sort */
29 int nbucks; /* 1 << bits, the number of buckets */
30 u32int size; /* bytes in each of the buckets chunks */
31 u32int usable; /* amount usable for IEntry data */
32 u8int *buf; /* buffer for all chunks */
37 #define U32GET(p) (((p)[0]<<24)|((p)[1]<<16)|((p)[2]<<8)|(p)[3])
38 #define U32PUT(p,v) (p)[0]=(v)>>24;(p)[1]=(v)>>16;(p)[2]=(v)>>8;(p)[3]=(v)
40 static IEBucks *initiebucks(Part *part, int bits, u32int size);
41 static int flushiebuck(IEBucks *ib, int b, int reset);
42 static int flushiebucks(IEBucks *ib);
43 static u32int sortiebuck(IEBucks *ib, int b);
44 static u64int sortiebucks(IEBucks *ib);
45 static int sprayientry(IEBucks *ib, IEntry *ie);
46 static u32int readarenainfo(IEBucks *ib, Arena *arena, u64int a, Bloom *b);
47 static u32int readiebuck(IEBucks *ib, int b);
48 static void freeiebucks(IEBucks *ib);
51 * build a sorted file with all IEntries which should be in ix.
52 * assumes the arenas' directories are up to date.
53 * reads each, converts the entries to index entries,
57 sortrawientries(Index *ix, Part *tmp, u64int *base, Bloom *bloom)
60 u64int clumps, sorted;
64 /* ZZZ should allow configuration of bits, bucket size */
65 ib = initiebucks(tmp, 8, 64*1024);
67 seterr(EOk, "can't create sorting buckets: %r");
72 fprint(2, "constructing entry list\n");
73 for(i = 0; i < ix->narenas; i++){
74 n = readarenainfo(ib, ix->arenas[i], ix->amap[i].start, bloom);
81 fprint(2, "sorting %lld entries\n", clumps);
83 sorted = sortiebucks(ib);
84 *base = (u64int)ib->chunks * ib->size;
86 fprint(2, "sorting messed up: clumps=%lld sorted=%lld\n", clumps, sorted);
96 #define CHECK(cis) if(((ulong*)cis)[-4] != 0xA110C09) xabort();
108 * read in all of the arena's clump directory,
109 * convert to IEntry format, and bucket sort based
110 * on the first few bits.
113 readarenainfo(IEBucks *ib, Arena *arena, u64int a, Bloom *b)
120 if(arena->memstats.clumps)
121 fprint(2, "\tarena %s: %d entries\n", arena->name, arena->memstats.clumps);
123 fprint(2, "[%s] ", arena->name);
125 cis = MKN(ClumpInfo, ClumpChunks);
128 memset(&ie, 0, sizeof(IEntry));
129 for(clump = 0; clump < arena->memstats.clumps; clump += n){
131 if(n > arena->memstats.clumps - clump)
132 n = arena->memstats.clumps - clump;
133 if(readclumpinfos(arena, clump, cis, n) != n){
134 seterr(EOk, "arena directory read failed: %r");
139 for(i = 0; i < n; i++){
141 ie.ia.type = ci->type;
142 ie.ia.size = ci->uncsize;
144 a += ci->size + ClumpSize;
145 ie.ia.blocks = (ci->size + ClumpSize + (1 << ABlockLog) - 1) >> ABlockLog;
146 scorecp(ie.score, ci->score);
147 if(ci->type == VtCorruptType){
148 if(0) print("! %V %22lld %3d %5d %3d\n",
149 ie.score, ie.ia.addr, ie.ia.type, ie.ia.size, ie.ia.blocks);
152 sprayientry(ib, &ie);
153 markbloomfilter(b, ie.score);
159 return clump - nskip;
163 * initialize the external bucket sorting data structures
166 initiebucks(Part *part, int bits, u32int size)
173 seterr(EOk, "out of memory");
177 ib->nbucks = 1 << bits;
179 ib->usable = (size - U32Size) / IEntrySize * IEntrySize;
180 ib->bucks = MKNZ(IEBuck, ib->nbucks);
181 if(ib->bucks == nil){
182 seterr(EOk, "out of memory allocation sorting buckets");
186 ib->xbuf = MKN(u8int, size * ((1 << bits)+1));
187 ib->buf = (u8int*)(((uintptr)ib->xbuf+size-1)&~(uintptr)(size-1));
189 seterr(EOk, "out of memory allocating sorting buckets' buffers");
193 for(i = 0; i < ib->nbucks; i++){
194 ib->bucks[i].head = TWID32;
195 ib->bucks[i].buf = &ib->buf[i * size];
202 freeiebucks(IEBucks *ib)
212 * initial sort: put the entry into the correct bucket
215 sprayientry(IEBucks *ib, IEntry *ie)
220 b = hashbits(ie->score, ib->bits);
221 n = ib->bucks[b].used;
222 if(n + IEntrySize > ib->usable){
223 /* should be flushed below, but if flush fails, this can happen */
224 seterr(EOk, "out of space in bucket");
227 packientry(ie, &ib->bucks[b].buf[n]);
229 ib->bucks[b].used = n;
230 if(n + IEntrySize <= ib->usable)
232 return flushiebuck(ib, b, 1);
237 * for each bucket, read it in and sort it
238 * write out the the final file
241 sortiebucks(IEBucks *ib)
247 if(flushiebucks(ib) < 0)
249 for(i = 0; i < ib->nbucks; i++)
250 ib->bucks[i].buf = nil;
251 ib->off = (u64int)ib->chunks * ib->size;
254 ib->buf = MKN(u8int, ib->max + U32Size);
256 seterr(EOk, "out of memory allocating final sorting buffer; try more buckets");
260 for(i = 0; i < ib->nbucks; i++){
261 n = sortiebuck(ib, i);
264 if(n != ib->bucks[i].total/IEntrySize)
265 fprint(2, "bucket %d changed count %d => %d\n",
266 i, (int)(ib->bucks[i].total/IEntrySize), n);
273 * sort from bucket b of ib into the output file to
276 sortiebuck(IEBucks *ib, int b)
280 n = readiebuck(ib, b);
283 qsort(ib->buf, n, IEntrySize, ientrycmp);
284 if(writepart(ib->part, ib->off, ib->buf, n*IEntrySize) < 0){
285 seterr(EOk, "can't write sorted bucket: %r");
288 ib->off += n * IEntrySize;
293 * write out a single bucket
296 flushiebuck(IEBucks *ib, int b, int reset)
300 if(ib->bucks[b].used == 0)
302 n = ib->bucks[b].used;
303 U32PUT(&ib->bucks[b].buf[n], ib->bucks[b].head);
306 if(writepart(ib->part, (u64int)ib->chunks * ib->size, ib->bucks[b].buf, ib->size) < 0){
307 seterr(EOk, "can't write sorting bucket to file: %r");
311 ib->bucks[b].head = ib->chunks++;
312 ib->bucks[b].total += ib->bucks[b].used;
314 ib->bucks[b].used = 0;
319 * write out all of the buckets, and compute
320 * the maximum size of any bucket
323 flushiebucks(IEBucks *ib)
327 for(i = 0; i < ib->nbucks; i++){
328 if(flushiebuck(ib, i, 0) < 0)
330 if(ib->bucks[i].total > ib->max)
331 ib->max = ib->bucks[i].total;
337 * read in the chained buffers for bucket b,
338 * and return it's total number of IEntries
341 readiebuck(IEBucks *ib, int b)
345 head = ib->bucks[b].head;
347 m = ib->bucks[b].used;
350 if(0) if(ib->bucks[b].total)
351 fprint(2, "\tbucket %d: %lld entries\n", b, ib->bucks[b].total/IEntrySize);
352 while(head != TWID32){
353 if(readpart(ib->part, (u64int)head * ib->size, &ib->buf[n], m+U32Size) < 0){
354 seterr(EOk, "can't read index sort bucket: %r");
358 head = U32GET(&ib->buf[n]);
361 if(n != ib->bucks[b].total)
362 fprint(2, "\tbucket %d: expected %d entries, got %d\n",
363 b, (int)ib->bucks[b].total/IEntrySize, n/IEntrySize);
364 return n / IEntrySize;