split-index.con commit t7509: cleanup description and filename (4625540)
   1#include "cache.h"
   2#include "split-index.h"
   3#include "ewah/ewok.h"
   4
   5struct split_index *init_split_index(struct index_state *istate)
   6{
   7        if (!istate->split_index) {
   8                istate->split_index = xcalloc(1, sizeof(*istate->split_index));
   9                istate->split_index->refcount = 1;
  10        }
  11        return istate->split_index;
  12}
  13
  14int read_link_extension(struct index_state *istate,
  15                         const void *data_, unsigned long sz)
  16{
  17        const unsigned char *data = data_;
  18        struct split_index *si;
  19        int ret;
  20
  21        if (sz < the_hash_algo->rawsz)
  22                return error("corrupt link extension (too short)");
  23        si = init_split_index(istate);
  24        hashcpy(si->base_oid.hash, data);
  25        data += the_hash_algo->rawsz;
  26        sz -= the_hash_algo->rawsz;
  27        if (!sz)
  28                return 0;
  29        si->delete_bitmap = ewah_new();
  30        ret = ewah_read_mmap(si->delete_bitmap, data, sz);
  31        if (ret < 0)
  32                return error("corrupt delete bitmap in link extension");
  33        data += ret;
  34        sz -= ret;
  35        si->replace_bitmap = ewah_new();
  36        ret = ewah_read_mmap(si->replace_bitmap, data, sz);
  37        if (ret < 0)
  38                return error("corrupt replace bitmap in link extension");
  39        if (ret != sz)
  40                return error("garbage at the end of link extension");
  41        return 0;
  42}
  43
  44int write_link_extension(struct strbuf *sb,
  45                         struct index_state *istate)
  46{
  47        struct split_index *si = istate->split_index;
  48        strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);
  49        if (!si->delete_bitmap && !si->replace_bitmap)
  50                return 0;
  51        ewah_serialize_strbuf(si->delete_bitmap, sb);
  52        ewah_serialize_strbuf(si->replace_bitmap, sb);
  53        return 0;
  54}
  55
  56static void mark_base_index_entries(struct index_state *base)
  57{
  58        int i;
  59        /*
  60         * To keep track of the shared entries between
  61         * istate->base->cache[] and istate->cache[], base entry
  62         * position is stored in each base entry. All positions start
  63         * from 1 instead of 0, which is reserved to say "this is a new
  64         * entry".
  65         */
  66        for (i = 0; i < base->cache_nr; i++)
  67                base->cache[i]->index = i + 1;
  68}
  69
  70void move_cache_to_base_index(struct index_state *istate)
  71{
  72        struct split_index *si = istate->split_index;
  73        int i;
  74
  75        /*
  76         * If there was a previous base index, then transfer ownership of allocated
  77         * entries to the parent index.
  78         */
  79        if (si->base &&
  80                si->base->ce_mem_pool) {
  81
  82                if (!istate->ce_mem_pool)
  83                        mem_pool_init(&istate->ce_mem_pool, 0);
  84
  85                mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
  86        }
  87
  88        si->base = xcalloc(1, sizeof(*si->base));
  89        si->base->version = istate->version;
  90        /* zero timestamp disables racy test in ce_write_index() */
  91        si->base->timestamp = istate->timestamp;
  92        ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
  93        si->base->cache_nr = istate->cache_nr;
  94
  95        /*
  96         * The mem_pool needs to move with the allocated entries.
  97         */
  98        si->base->ce_mem_pool = istate->ce_mem_pool;
  99        istate->ce_mem_pool = NULL;
 100
 101        COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
 102        mark_base_index_entries(si->base);
 103        for (i = 0; i < si->base->cache_nr; i++)
 104                si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
 105}
 106
 107static void mark_entry_for_delete(size_t pos, void *data)
 108{
 109        struct index_state *istate = data;
 110        if (pos >= istate->cache_nr)
 111                die("position for delete %d exceeds base index size %d",
 112                    (int)pos, istate->cache_nr);
 113        istate->cache[pos]->ce_flags |= CE_REMOVE;
 114        istate->split_index->nr_deletions = 1;
 115}
 116
 117static void replace_entry(size_t pos, void *data)
 118{
 119        struct index_state *istate = data;
 120        struct split_index *si = istate->split_index;
 121        struct cache_entry *dst, *src;
 122
 123        if (pos >= istate->cache_nr)
 124                die("position for replacement %d exceeds base index size %d",
 125                    (int)pos, istate->cache_nr);
 126        if (si->nr_replacements >= si->saved_cache_nr)
 127                die("too many replacements (%d vs %d)",
 128                    si->nr_replacements, si->saved_cache_nr);
 129        dst = istate->cache[pos];
 130        if (dst->ce_flags & CE_REMOVE)
 131                die("entry %d is marked as both replaced and deleted",
 132                    (int)pos);
 133        src = si->saved_cache[si->nr_replacements];
 134        if (ce_namelen(src))
 135                die("corrupt link extension, entry %d should have "
 136                    "zero length name", (int)pos);
 137        src->index = pos + 1;
 138        src->ce_flags |= CE_UPDATE_IN_BASE;
 139        src->ce_namelen = dst->ce_namelen;
 140        copy_cache_entry(dst, src);
 141        discard_cache_entry(src);
 142        si->nr_replacements++;
 143}
 144
 145void merge_base_index(struct index_state *istate)
 146{
 147        struct split_index *si = istate->split_index;
 148        unsigned int i;
 149
 150        mark_base_index_entries(si->base);
 151
 152        si->saved_cache     = istate->cache;
 153        si->saved_cache_nr  = istate->cache_nr;
 154        istate->cache_nr    = si->base->cache_nr;
 155        istate->cache       = NULL;
 156        istate->cache_alloc = 0;
 157        ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
 158        COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
 159
 160        si->nr_deletions = 0;
 161        si->nr_replacements = 0;
 162        ewah_each_bit(si->replace_bitmap, replace_entry, istate);
 163        ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
 164        if (si->nr_deletions)
 165                remove_marked_cache_entries(istate);
 166
 167        for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
 168                if (!ce_namelen(si->saved_cache[i]))
 169                        die("corrupt link extension, entry %d should "
 170                            "have non-zero length name", i);
 171                add_index_entry(istate, si->saved_cache[i],
 172                                ADD_CACHE_OK_TO_ADD |
 173                                ADD_CACHE_KEEP_CACHE_TREE |
 174                                /*
 175                                 * we may have to replay what
 176                                 * merge-recursive.c:update_stages()
 177                                 * does, which has this flag on
 178                                 */
 179                                ADD_CACHE_SKIP_DFCHECK);
 180                si->saved_cache[i] = NULL;
 181        }
 182
 183        ewah_free(si->delete_bitmap);
 184        ewah_free(si->replace_bitmap);
 185        FREE_AND_NULL(si->saved_cache);
 186        si->delete_bitmap  = NULL;
 187        si->replace_bitmap = NULL;
 188        si->saved_cache_nr = 0;
 189}
 190
 191void prepare_to_write_split_index(struct index_state *istate)
 192{
 193        struct split_index *si = init_split_index(istate);
 194        struct cache_entry **entries = NULL, *ce;
 195        int i, nr_entries = 0, nr_alloc = 0;
 196
 197        si->delete_bitmap = ewah_new();
 198        si->replace_bitmap = ewah_new();
 199
 200        if (si->base) {
 201                /* Go through istate->cache[] and mark CE_MATCHED to
 202                 * entry with positive index. We'll go through
 203                 * base->cache[] later to delete all entries in base
 204                 * that are not marked with either CE_MATCHED or
 205                 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
 206                 * duplicate, deduplicate it.
 207                 */
 208                for (i = 0; i < istate->cache_nr; i++) {
 209                        struct cache_entry *base;
 210                        /* namelen is checked separately */
 211                        const unsigned int ondisk_flags =
 212                                CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
 213                        unsigned int ce_flags, base_flags, ret;
 214                        ce = istate->cache[i];
 215                        if (!ce->index)
 216                                continue;
 217                        if (ce->index > si->base->cache_nr) {
 218                                ce->index = 0;
 219                                continue;
 220                        }
 221                        ce->ce_flags |= CE_MATCHED; /* or "shared" */
 222                        base = si->base->cache[ce->index - 1];
 223                        if (ce == base)
 224                                continue;
 225                        if (ce->ce_namelen != base->ce_namelen ||
 226                            strcmp(ce->name, base->name)) {
 227                                ce->index = 0;
 228                                continue;
 229                        }
 230                        ce_flags = ce->ce_flags;
 231                        base_flags = base->ce_flags;
 232                        /* only on-disk flags matter */
 233                        ce->ce_flags   &= ondisk_flags;
 234                        base->ce_flags &= ondisk_flags;
 235                        ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
 236                                     offsetof(struct cache_entry, name) -
 237                                     offsetof(struct cache_entry, ce_stat_data));
 238                        ce->ce_flags = ce_flags;
 239                        base->ce_flags = base_flags;
 240                        if (ret)
 241                                ce->ce_flags |= CE_UPDATE_IN_BASE;
 242                        discard_cache_entry(base);
 243                        si->base->cache[ce->index - 1] = ce;
 244                }
 245                for (i = 0; i < si->base->cache_nr; i++) {
 246                        ce = si->base->cache[i];
 247                        if ((ce->ce_flags & CE_REMOVE) ||
 248                            !(ce->ce_flags & CE_MATCHED))
 249                                ewah_set(si->delete_bitmap, i);
 250                        else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
 251                                ewah_set(si->replace_bitmap, i);
 252                                ce->ce_flags |= CE_STRIP_NAME;
 253                                ALLOC_GROW(entries, nr_entries+1, nr_alloc);
 254                                entries[nr_entries++] = ce;
 255                        }
 256                        if (is_null_oid(&ce->oid))
 257                                istate->drop_cache_tree = 1;
 258                }
 259        }
 260
 261        for (i = 0; i < istate->cache_nr; i++) {
 262                ce = istate->cache[i];
 263                if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
 264                        assert(!(ce->ce_flags & CE_STRIP_NAME));
 265                        ALLOC_GROW(entries, nr_entries+1, nr_alloc);
 266                        entries[nr_entries++] = ce;
 267                }
 268                ce->ce_flags &= ~CE_MATCHED;
 269        }
 270
 271        /*
 272         * take cache[] out temporarily, put entries[] in its place
 273         * for writing
 274         */
 275        si->saved_cache = istate->cache;
 276        si->saved_cache_nr = istate->cache_nr;
 277        istate->cache = entries;
 278        istate->cache_nr = nr_entries;
 279}
 280
 281void finish_writing_split_index(struct index_state *istate)
 282{
 283        struct split_index *si = init_split_index(istate);
 284
 285        ewah_free(si->delete_bitmap);
 286        ewah_free(si->replace_bitmap);
 287        si->delete_bitmap = NULL;
 288        si->replace_bitmap = NULL;
 289        free(istate->cache);
 290        istate->cache = si->saved_cache;
 291        istate->cache_nr = si->saved_cache_nr;
 292}
 293
 294void discard_split_index(struct index_state *istate)
 295{
 296        struct split_index *si = istate->split_index;
 297        if (!si)
 298                return;
 299        istate->split_index = NULL;
 300        si->refcount--;
 301        if (si->refcount)
 302                return;
 303        if (si->base) {
 304                discard_index(si->base);
 305                free(si->base);
 306        }
 307        free(si);
 308}
 309
 310void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
 311{
 312        if (ce->index &&
 313            istate->split_index &&
 314            istate->split_index->base &&
 315            ce->index <= istate->split_index->base->cache_nr &&
 316            ce == istate->split_index->base->cache[ce->index - 1])
 317                ce->ce_flags |= CE_REMOVE;
 318        else
 319                discard_cache_entry(ce);
 320}
 321
 322void replace_index_entry_in_base(struct index_state *istate,
 323                                 struct cache_entry *old_entry,
 324                                 struct cache_entry *new_entry)
 325{
 326        if (old_entry->index &&
 327            istate->split_index &&
 328            istate->split_index->base &&
 329            old_entry->index <= istate->split_index->base->cache_nr) {
 330                new_entry->index = old_entry->index;
 331                if (old_entry != istate->split_index->base->cache[new_entry->index - 1])
 332                        discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);
 333                istate->split_index->base->cache[new_entry->index - 1] = new_entry;
 334        }
 335}
 336
 337void add_split_index(struct index_state *istate)
 338{
 339        if (!istate->split_index) {
 340                init_split_index(istate);
 341                istate->cache_changed |= SPLIT_INDEX_ORDERED;
 342        }
 343}
 344
 345void remove_split_index(struct index_state *istate)
 346{
 347        if (istate->split_index) {
 348                /*
 349                 * When removing the split index, we need to move
 350                 * ownership of the mem_pool associated with the
 351                 * base index to the main index. There may be cache entries
 352                 * allocated from the base's memory pool that are shared with
 353                 * the_index.cache[].
 354                 */
 355                mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
 356
 357                /*
 358                 * The split index no longer owns the mem_pool backing
 359                 * its cache array. As we are discarding this index,
 360                 * mark the index as having no cache entries, so it
 361                 * will not attempt to clean up the cache entries or
 362                 * validate them.
 363                 */
 364                if (istate->split_index->base)
 365                        istate->split_index->base->cache_nr = 0;
 366
 367                /*
 368                 * We can discard the split index because its
 369                 * memory pool has been incorporated into the
 370                 * memory pool associated with the the_index.
 371                 */
 372                discard_split_index(istate);
 373
 374                istate->cache_changed |= SOMETHING_CHANGED;
 375        }
 376}