buildindex.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. return 0;
  40. }
  41. ok = 1;
  42. next = 0;
  43. ib.data = b->data + IBucketSize;
  44. zib.data = z->data + IBucketSize;
  45. zib.n = 0;
  46. zib.next = 0;
  47. for(;;){
  48. buck = buildBucket(ix, ies, &ib);
  49. found += ib.n;
  50. if(zero){
  51. for(; next != buck; next++){
  52. if(next == ix->buckets){
  53. if(buck != TWID32){
  54. fprint(2, "bucket out of range\n");
  55. ok = 0;
  56. }
  57. goto breakout;
  58. }
  59. if(!writeBucket(ix, next, &zib, z)){
  60. fprint(2, "can't write zero bucket to buck=%d: %R", next);
  61. ok = 0;
  62. }
  63. }
  64. }
  65. if(buck >= ix->buckets){
  66. if(buck == TWID32)
  67. break;
  68. fprint(2, "bucket out of range\n");
  69. ok = 0;
  70. goto breakout;
  71. }
  72. if(!writeBucket(ix, buck, &ib, b)){
  73. fprint(2, "bad bucket found=%lld: %R\n", found);
  74. ok = 0;
  75. }
  76. next = buck + 1;
  77. }
  78. breakout:;
  79. fprint(2, "constructed index with %lld entries\n", found);
  80. freeIEStream(ies);
  81. freeZBlock(z);
  82. freeZBlock(b);
  83. return ok;
  84. }
  85. void
  86. usage(void)
  87. {
  88. fprint(2, "usage: buildindex [-Z] [-B blockcachesize] config tmppart\n");
  89. exits(0);
  90. }
  91. int
  92. main(int argc, char *argv[])
  93. {
  94. Part *part;
  95. u64int clumps, base;
  96. u32int bcmem;
  97. int zero;
  98. vtAttach();
  99. zero = 1;
  100. bcmem = 0;
  101. ARGBEGIN{
  102. case 'B':
  103. bcmem = unittoull(ARGF());
  104. break;
  105. case 'Z':
  106. zero = 0;
  107. break;
  108. default:
  109. usage();
  110. break;
  111. }ARGEND
  112. if(argc != 2)
  113. usage();
  114. if(!initVenti(argv[0]))
  115. fatal("can't init venti: %R");
  116. if(bcmem < maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16))
  117. bcmem = maxBlockSize * (mainIndex->narenas + mainIndex->nsects * 4 + 16);
  118. fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
  119. initDCache(bcmem);
  120. fprint(2, "building a new index %s using %s for temporary storage\n", mainIndex->name, argv[1]);
  121. part = initPart(argv[1], 1);
  122. if(part == nil)
  123. fatal("can't initialize temporary parition: %R");
  124. clumps = sortRawIEntries(mainIndex, part, &base);
  125. if(clumps == TWID64)
  126. fatal("can't build sorted index: %R");
  127. fprint(2, "found and sorted index entries for clumps=%lld at %lld\n", clumps, base);
  128. if(!buildIndex(mainIndex, part, base, clumps, zero))
  129. fatal("can't build new index: %R");
  130. exits(0);
  131. return 0; /* shut up stupid compiler */
  132. }