URI:
       tfmtindex.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tfmtindex.c (2599B)
       ---
            1 #include "stdinc.h"
            2 #include "dat.h"
            3 #include "fns.h"
            4 
            5 void
            6 usage(void)
            7 {
            8         fprint(2, "usage: fmtindex [-a] venti.conf\n");
            9         threadexitsall(0);
           10 }
           11 
           12 void
           13 threadmain(int argc, char *argv[])
           14 {
           15         Config conf;
           16         Index *ix;
           17         ArenaPart *ap;
           18         Arena **arenas;
           19         AMap *amap;
           20         u64int addr;
           21         char *file;
           22         u32int i, j, n, narenas;
           23         int add;
           24 
           25         ventifmtinstall();
           26         statsinit();
           27 
           28         add = 0;
           29         ARGBEGIN{
           30         case 'a':
           31                 add = 1;
           32                 break;
           33         default:
           34                 usage();
           35                 break;
           36         }ARGEND
           37 
           38         if(argc != 1)
           39                 usage();
           40 
           41         file = argv[0];
           42 
           43         if(runconfig(file, &conf) < 0)
           44                 sysfatal("can't initialize config %s: %r", file);
           45         if(conf.index == nil)
           46                 sysfatal("no index specified in %s", file);
           47         if(nameok(conf.index) < 0)
           48                 sysfatal("illegal index name %s", conf.index);
           49 
           50         narenas = 0;
           51         for(i = 0; i < conf.naparts; i++){
           52                 ap = conf.aparts[i];
           53                 narenas += ap->narenas;
           54         }
           55 
           56         if(add){
           57                 ix = initindex(conf.index, conf.sects, conf.nsects);
           58                 if(ix == nil)
           59                         sysfatal("can't initialize index %s: %r", conf.index);
           60         }else{
           61                 ix = newindex(conf.index, conf.sects, conf.nsects);
           62                 if(ix == nil)
           63                         sysfatal("can't create new index %s: %r", conf.index);
           64 
           65                 n = 0;
           66                 for(i = 0; i < ix->nsects; i++)
           67                         n += ix->sects[i]->blocks;
           68 
           69                 if(0) fprint(2, "using %ud buckets of %ud; div=%d\n", ix->buckets, n, ix->div);
           70         }
           71         amap = MKNZ(AMap, narenas);
           72         arenas = MKNZ(Arena*, narenas);
           73 
           74         addr = IndexBase;
           75         n = 0;
           76         for(i = 0; i < conf.naparts; i++){
           77                 ap = conf.aparts[i];
           78                 for(j = 0; j < ap->narenas; j++){
           79                         if(n >= narenas)
           80                                 sysfatal("too few slots in index's arena set");
           81 
           82                         arenas[n] = ap->arenas[j];
           83                         if(n < ix->narenas){
           84                                 if(arenas[n] != ix->arenas[n])
           85                                         sysfatal("mismatched arenas %s and %s at slot %d",
           86                                                 arenas[n]->name, ix->arenas[n]->name, n);
           87                                 amap[n] = ix->amap[n];
           88                                 if(amap[n].start != addr)
           89                                         sysfatal("mis-located arena %s in index %s", arenas[n]->name, ix->name);
           90                                 addr = amap[n].stop;
           91                         }else{
           92                                 amap[n].start = addr;
           93                                 addr += ap->arenas[j]->size;
           94                                 amap[n].stop = addr;
           95                                 namecp(amap[n].name, ap->arenas[j]->name);
           96                                 if(0) fprint(2, "add arena %s at [%lld,%lld)\n",
           97                                         amap[n].name, amap[n].start, amap[n].stop);
           98                         }
           99 
          100                         n++;
          101                 }
          102         }
          103         if(0){
          104                 fprint(2, "configured index=%s with arenas=%d and storage=%lld\n",
          105                         ix->name, n, addr - IndexBase);
          106                 fprint(2, "\tbuckets=%d\n",
          107                         ix->buckets);
          108         }
          109         fprint(2, "fmtindex: %,d arenas, %,d index buckets, %,lld bytes storage\n",
          110                 n, ix->buckets, addr-IndexBase);
          111 
          112         ix->amap = amap;
          113         ix->arenas = arenas;
          114         ix->narenas = narenas;
          115 
          116         if(wbindex(ix) < 0)
          117                 fprint(2, "can't write back arena partition header for %s: %r\n", file);
          118 
          119         threadexitsall(0);
          120 }