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