buildindex.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. static int
  5. writeBucket(Index *ix, u32int buck, IBucket *ib, ZBlock *b)
  6. {
  7. ISect *is;
  8. is = findISect(ix, buck);
  9. if(is == nil){
  10. setErr(EAdmin, "bad math in writeBucket");
  11. return 0;
  12. }
  13. if(buck < is->start || buck >= is->stop)
  14. setErr(EAdmin, "index write out of bounds: %d not in [%d,%d)\n",
  15. buck, is->start, is->stop);
  16. buck -= is->start;
  17. vtLock(stats.lock);
  18. stats.indexWrites++;
  19. vtUnlock(stats.lock);
  20. packIBucket(ib, b->data);
  21. return writePart(is->part, is->blockBase + ((u64int)buck << is->blockLog), b->data, is->blockSize);
  22. }
  23. static int
  24. buildIndex(Index *ix, Part *part, u64int off, u64int clumps, int zero)
  25. {
  26. IEStream *ies;
  27. IBucket ib, zib;
  28. ZBlock *z, *b;
  29. u32int next, buck;
  30. int ok;
  31. u64int found = 0;
  32. //ZZZ make buffer size configurable
  33. b = allocZBlock(ix->blockSize, 0);
  34. z = allocZBlock(ix->blockSize, 1);
  35. ies = initIEStream(part, off, clumps, 64*1024);
  36. if(b == nil || z == nil || ies == nil){
  37. ok = 0;
  38. goto breakout;
  39. }
  40. ok = 1;
  41. next = 0;
  42. ib.data = b->data + IBucketSize;
  43. zib.data = z->data + IBucketSize;
  44. zib.n = 0;
  45. zib.next = 0;
  46. for(;;){
  47. buck = buildBucket(ix, ies, &ib);
  48. found += ib.n;
  49. if(zero){
  50. for(; next != buck; next++){
  51. if(next == ix->buckets){
  52. if(buck != TWID32){
  53. fprint(2, "bucket out of range\n");
  54. ok = 0;
  55. }
  56. goto breakout;
  57. }
  58. if(!writeBucket(ix, next, &zib, z)){
  59. fprint(2, "can't write zero bucket to buck=%d: %R", next);
  60. ok = 0;
  61. }
  62. }
  63. }
  64. if(buck >= ix->buckets){
  65. if(buck == TWID32)
  66. break;
  67. fprint(2, "bucket out of range\n");
  68. ok = 0;
  69. goto breakout;
  70. }
  71. if(!writeBucket(ix, buck, &ib, b)){
  72. fprint(2, "bad bucket found=%lld: %R\n", found);
  73. ok = 0;
  74. }
  75. next = buck + 1;
  76. }
  77. breakout:;
  78. fprint(2, "constructed index with %lld entries\n", found);
  79. freeIEStream(ies);
  80. freeZBlock(z);
  81. freeZBlock(b);
  82. return ok;
  83. }
  84. void
  85. usage(void)
  86. {
  87. fprint(2, "usage: buildindex [-Z] [-B blockcachesize] config tmppart\n");
  88. exits(0);
  89. }
  90. int
  91. main(int argc, char *argv[])
  92. {
  93. Part *part;
  94. u64int clumps, base;
  95. u32int bcmem;
  96. int zero;
  97. vtAttach();
  98. zero = 1;
  99. bcmem = 0;
  100. ARGBEGIN{
  101. case 'B':
  102. bcmem = unittoull(ARGF());
  103. break;
  104. case 'Z':
  105. zero = 0;
  106. break;
  107. default:
  108. usage();
  109. break;
  110. }ARGEND
  111. if(argc != 2)
  112. usage();
  113. if(!initVenti(argv[0], nil))
  114. fatal("can't init venti: %R");
  115. if(bcmem < maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16))
  116. bcmem = maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16);
  117. fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
  118. initDCache(bcmem);
  119. fprint(2, "building a new index %s using %s for temporary storage\n", mainIndex->name, argv[1]);
  120. part = initPart(argv[1], 1);
  121. if(part == nil)
  122. fatal("can't initialize temporary partition: %R");
  123. clumps = sortRawIEntries(mainIndex, part, &base);
  124. if(clumps == TWID64)
  125. fatal("can't build sorted index: %R");
  126. fprint(2, "found and sorted index entries for clumps=%lld at %lld\n", clumps, base);
  127. if(!buildIndex(mainIndex, part, base, clumps, zero))
  128. fatal("can't build new index: %R");
  129. exits(0);
  130. return 0; /* shut up stupid compiler */
  131. }