aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/venti/fmtindex.c
blob: 6934e3d5da2d90f2fa24861ae959b5d6b1aa311d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "stdinc.h"
#include "dat.h"
#include "fns.h"

void
usage(void)
{
	fprint(2, "usage: fmtindex config\n");
	threadexitsall(0);
}

void
threadmain(int argc, char *argv[])
{
	Config conf;
	Index *ix;
	ArenaPart *ap;
	Arena **arenas;
	AMap *amap;
	u64int addr;
	char *file;
	u32int i, j, n, narenas;
	int add;

	fmtinstall('V', vtscorefmt);
	statsinit();

	add = 0;
	ARGBEGIN{
	case 'a':
		add = 1;
		break;
	default:
		usage();
		break;
	}ARGEND

	if(argc != 1)
		usage();

	file = argv[0];

	if(runconfig(file, &conf) < 0)
		sysfatal("can't initialize config %s: %r", file);
	if(conf.index == nil)
		sysfatal("no index specified in %s", file);
	if(nameok(conf.index) < 0)
		sysfatal("illegal index name %s", conf.index);

	narenas = 0;
	for(i = 0; i < conf.naparts; i++){
		ap = conf.aparts[i];
		narenas += ap->narenas;
	}

	if(add){
		ix = initindex(conf.index, conf.sects, conf.nsects);
		if(ix == nil)
			sysfatal("can't initialize index %s: %r", conf.index);
	}else{
		ix = newindex(conf.index, conf.sects, conf.nsects);
		if(ix == nil)
			sysfatal("can't create new index %s: %r", conf.index);

		n = 0;
		for(i = 0; i < ix->nsects; i++)
			n += ix->sects[i]->blocks;

		fprint(2, "using %ud buckets of %ud; div=%d\n", ix->buckets, n, ix->div);
	}
	amap = MKNZ(AMap, narenas);
	arenas = MKNZ(Arena*, narenas);

	addr = IndexBase;
	n = 0;
	for(i = 0; i < conf.naparts; i++){
		ap = conf.aparts[i];
		for(j = 0; j < ap->narenas; j++){
			if(n >= narenas)
				sysfatal("too few slots in index's arena set");

			arenas[n] = ap->arenas[j];
			if(n < ix->narenas){
				if(arenas[n] != ix->arenas[n])
					sysfatal("mismatched arenas %s and %s at slot %d\n",
						arenas[n]->name, ix->arenas[n]->name, n);
				amap[n] = ix->amap[n];
				if(amap[n].start != addr)
					sysfatal("mis-located arena %s in index %s\n", arenas[n]->name, ix->name);
				addr = amap[n].stop;
			}else{
				amap[n].start = addr;
				addr += ap->arenas[j]->size;
				amap[n].stop = addr;
				namecp(amap[n].name, ap->arenas[j]->name);
				fprint(2, "add arena %s at [%lld,%lld)\n",
					amap[n].name, amap[n].start, amap[n].stop);
			}

			n++;
		}
	}
	fprint(2, "configured index=%s with arenas=%d and storage=%lld\n",
		ix->name, n, addr - IndexBase);
	fprint(2, "\tbitblocks=%d maxdepth=%d buckets=%d\n",
		ix->bitblocks, ix->maxdepth, ix->buckets);

	ix->amap = amap;
	ix->arenas = arenas;
	ix->narenas = narenas;

	if(wbindex(ix) < 0)
		fprint(2, "can't write back arena partition header for %s: %r\n", file);

	threadexitsall(0);
}