THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.8KB

  1. #include "cache.h"
  2. #include "pack-revindex.h"
  3. #include "object-store.h"
  4. #include "packfile.h"
  5. /*
  6. * Pack index for existing packs give us easy access to the offsets into
  7. * corresponding pack file where each object's data starts, but the entries
  8. * do not store the size of the compressed representation (uncompressed
  9. * size is easily available by examining the pack entry header). It is
  10. * also rather expensive to find the sha1 for an object given its offset.
  11. *
  12. * The pack index file is sorted by object name mapping to offset;
  13. * this revindex array is a list of offset/index_nr pairs
  14. * ordered by offset, so if you know the offset of an object, next offset
  15. * is where its packed representation ends and the index_nr can be used to
  16. * get the object sha1 from the main index.
  17. */
  18. /*
  19. * This is a least-significant-digit radix sort.
  20. *
  21. * It sorts each of the "n" items in "entries" by its offset field. The "max"
  22. * parameter must be at least as large as the largest offset in the array,
  23. * and lets us quit the sort early.
  24. */
  25. static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
  26. {
  27. /*
  28. * We use a "digit" size of 16 bits. That keeps our memory
  29. * usage reasonable, and we can generally (for a 4G or smaller
  30. * packfile) quit after two rounds of radix-sorting.
  31. */
  32. #define DIGIT_SIZE (16)
  33. #define BUCKETS (1 << DIGIT_SIZE)
  34. /*
  35. * We want to know the bucket that a[i] will go into when we are using
  36. * the digit that is N bits from the (least significant) end.
  37. */
  38. #define BUCKET_FOR(a, i, bits) (((a)[(i)].offset >> (bits)) & (BUCKETS-1))
  39. /*
  40. * We need O(n) temporary storage. Rather than do an extra copy of the
  41. * partial results into "entries", we sort back and forth between the
  42. * real array and temporary storage. In each iteration of the loop, we
  43. * keep track of them with alias pointers, always sorting from "from"
  44. * to "to".
  45. */
  46. struct revindex_entry *tmp, *from, *to;
  47. int bits;
  48. unsigned *pos;
  49. ALLOC_ARRAY(pos, BUCKETS);
  50. ALLOC_ARRAY(tmp, n);
  51. from = entries;
  52. to = tmp;
  53. /*
  54. * If (max >> bits) is zero, then we know that the radix digit we are
  55. * on (and any higher) will be zero for all entries, and our loop will
  56. * be a no-op, as everybody lands in the same zero-th bucket.
  57. */
  58. for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
  59. unsigned i;
  60. memset(pos, 0, BUCKETS * sizeof(*pos));
  61. /*
  62. * We want pos[i] to store the index of the last element that
  63. * will go in bucket "i" (actually one past the last element).
  64. * To do this, we first count the items that will go in each
  65. * bucket, which gives us a relative offset from the last
  66. * bucket. We can then cumulatively add the index from the
  67. * previous bucket to get the true index.
  68. */
  69. for (i = 0; i < n; i++)
  70. pos[BUCKET_FOR(from, i, bits)]++;
  71. for (i = 1; i < BUCKETS; i++)
  72. pos[i] += pos[i-1];
  73. /*
  74. * Now we can drop the elements into their correct buckets (in
  75. * our temporary array). We iterate the pos counter backwards
  76. * to avoid using an extra index to count up. And since we are
  77. * going backwards there, we must also go backwards through the
  78. * array itself, to keep the sort stable.
  79. *
  80. * Note that we use an unsigned iterator to make sure we can
  81. * handle 2^32-1 objects, even on a 32-bit system. But this
  82. * means we cannot use the more obvious "i >= 0" loop condition
  83. * for counting backwards, and must instead check for
  84. * wrap-around with UINT_MAX.
  85. */
  86. for (i = n - 1; i != UINT_MAX; i--)
  87. to[--pos[BUCKET_FOR(from, i, bits)]] = from[i];
  88. /*
  89. * Now "to" contains the most sorted list, so we swap "from" and
  90. * "to" for the next iteration.
  91. */
  92. SWAP(from, to);
  93. }
  94. /*
  95. * If we ended with our data in the original array, great. If not,
  96. * we have to move it back from the temporary storage.
  97. */
  98. if (from != entries)
  99. COPY_ARRAY(entries, tmp, n);
  100. free(tmp);
  101. free(pos);
  102. #undef BUCKET_FOR
  103. #undef BUCKETS
  104. #undef DIGIT_SIZE
  105. }
  106. /*
  107. * Ordered list of offsets of objects in the pack.
  108. */
  109. static void create_pack_revindex(struct packed_git *p)
  110. {
  111. const unsigned num_ent = p->num_objects;
  112. unsigned i;
  113. const char *index = p->index_data;
  114. const unsigned hashsz = the_hash_algo->rawsz;
  115. ALLOC_ARRAY(p->revindex, num_ent + 1);
  116. index += 4 * 256;
  117. if (p->index_version > 1) {
  118. const uint32_t *off_32 =
  119. (uint32_t *)(index + 8 + p->num_objects * (hashsz + 4));
  120. const uint32_t *off_64 = off_32 + p->num_objects;
  121. for (i = 0; i < num_ent; i++) {
  122. const uint32_t off = ntohl(*off_32++);
  123. if (!(off & 0x80000000)) {
  124. p->revindex[i].offset = off;
  125. } else {
  126. p->revindex[i].offset = get_be64(off_64);
  127. off_64 += 2;
  128. }
  129. p->revindex[i].nr = i;
  130. }
  131. } else {
  132. for (i = 0; i < num_ent; i++) {
  133. const uint32_t hl = *((uint32_t *)(index + (hashsz + 4) * i));
  134. p->revindex[i].offset = ntohl(hl);
  135. p->revindex[i].nr = i;
  136. }
  137. }
  138. /*
  139. * This knows the pack format -- the hash trailer
  140. * follows immediately after the last object data.
  141. */
  142. p->revindex[num_ent].offset = p->pack_size - hashsz;
  143. p->revindex[num_ent].nr = -1;
  144. sort_revindex(p->revindex, num_ent, p->pack_size);
  145. }
  146. int load_pack_revindex(struct packed_git *p)
  147. {
  148. if (!p->revindex) {
  149. if (open_pack_index(p))
  150. return -1;
  151. create_pack_revindex(p);
  152. }
  153. return 0;
  154. }
  155. int find_revindex_position(struct packed_git *p, off_t ofs)
  156. {
  157. int lo = 0;
  158. int hi = p->num_objects + 1;
  159. const struct revindex_entry *revindex = p->revindex;
  160. do {
  161. const unsigned mi = lo + (hi - lo) / 2;
  162. if (revindex[mi].offset == ofs) {
  163. return mi;
  164. } else if (ofs < revindex[mi].offset)
  165. hi = mi;
  166. else
  167. lo = mi + 1;
  168. } while (lo < hi);
  169. error("bad offset for revindex");
  170. return -1;
  171. }
  172. struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
  173. {
  174. int pos;
  175. if (load_pack_revindex(p))
  176. return NULL;
  177. pos = find_revindex_position(p, ofs);
  178. if (pos < 0)
  179. return NULL;
  180. return p->revindex + pos;
  181. }