BasePackBitmapIndex.java

  1. /*
  2.  * Copyright (C) 2012, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.internal.storage.file;

  11. import org.eclipse.jgit.lib.AnyObjectId;
  12. import org.eclipse.jgit.lib.ObjectIdOwnerMap;

  13. import com.googlecode.javaewah.EWAHCompressedBitmap;

  14. /**
  15.  * Base implementation of the PackBitmapIndex.
  16.  */
  17. abstract class BasePackBitmapIndex extends PackBitmapIndex {
  18.     private final ObjectIdOwnerMap<StoredBitmap> bitmaps;

  19.     BasePackBitmapIndex(ObjectIdOwnerMap<StoredBitmap> bitmaps) {
  20.         this.bitmaps = bitmaps;
  21.     }

  22.     /** {@inheritDoc} */
  23.     @Override
  24.     public EWAHCompressedBitmap getBitmap(AnyObjectId objectId) {
  25.         StoredBitmap sb = bitmaps.get(objectId);
  26.         return sb != null ? sb.getBitmap() : null;
  27.     }

  28.     ObjectIdOwnerMap<StoredBitmap> getBitmaps() {
  29.         return bitmaps;
  30.     }

  31.     /**
  32.      * Data representation of the bitmap entry restored from a pack index. The
  33.      * commit of the bitmap is the map key.
  34.      */
  35.     static final class StoredBitmap extends ObjectIdOwnerMap.Entry {
  36.         private volatile Object bitmapContainer;
  37.         private final int flags;

  38.         StoredBitmap(AnyObjectId objectId, EWAHCompressedBitmap bitmap,
  39.                 StoredBitmap xorBitmap, int flags) {
  40.             super(objectId);
  41.             this.bitmapContainer = xorBitmap == null
  42.                     ? bitmap
  43.                     : new XorCompressedBitmap(bitmap, xorBitmap);
  44.             this.flags = flags;
  45.         }

  46.         /**
  47.          * Computes and returns the full bitmap.
  48.          *
  49.          * @return the full bitmap
  50.          */
  51.         EWAHCompressedBitmap getBitmap() {
  52.             EWAHCompressedBitmap bitmap = getBitmapWithoutCaching();
  53.             // Cache the result.
  54.             bitmapContainer = bitmap;
  55.             return bitmap;
  56.         }

  57.         /**
  58.          * Compute and return the full bitmap, do NOT cache the expanded bitmap,
  59.          * which saves memory and should only be used during bitmap creation in
  60.          * garbage collection.
  61.          *
  62.          * @return the full bitmap
  63.          */
  64.         EWAHCompressedBitmap getBitmapWithoutCaching() {
  65.             // Fast path to immediately return the expanded result.
  66.             Object r = bitmapContainer;
  67.             if (r instanceof EWAHCompressedBitmap)
  68.                 return (EWAHCompressedBitmap) r;

  69.             // Expand the bitmap but not cache the result.
  70.             XorCompressedBitmap xb = (XorCompressedBitmap) r;
  71.             EWAHCompressedBitmap out = xb.bitmap;
  72.             for (;;) {
  73.                 r = xb.xorBitmap.bitmapContainer;
  74.                 if (r instanceof EWAHCompressedBitmap) {
  75.                     out = out.xor((EWAHCompressedBitmap) r);
  76.                     out.trim();
  77.                     return out;
  78.                 }
  79.                 xb = (XorCompressedBitmap) r;
  80.                 out = out.xor(xb.bitmap);
  81.             }
  82.         }

  83.         /** @return the flags associated with the bitmap */
  84.         int getFlags() {
  85.             return flags;
  86.         }
  87.     }

  88.     private static final class XorCompressedBitmap {
  89.         final EWAHCompressedBitmap bitmap;
  90.         final StoredBitmap xorBitmap;

  91.         XorCompressedBitmap(EWAHCompressedBitmap b, StoredBitmap xb) {
  92.             bitmap = b;
  93.             xorBitmap = xb;
  94.         }
  95.     }
  96. }