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.

BasePackBitmapIndex.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Fast path to immediately return the expanded result.
  53. Object r = bitmapContainer;
  54. if (r instanceof EWAHCompressedBitmap)
  55. return (EWAHCompressedBitmap) r;
  56. // Expand the bitmap and cache the result.
  57. XorCompressedBitmap xb = (XorCompressedBitmap) r;
  58. EWAHCompressedBitmap out = xb.bitmap;
  59. for (;;) {
  60. r = xb.xorBitmap.bitmapContainer;
  61. if (r instanceof EWAHCompressedBitmap) {
  62. out = out.xor((EWAHCompressedBitmap) r);
  63. out.trim();
  64. bitmapContainer = out;
  65. return out;
  66. }
  67. xb = (XorCompressedBitmap) r;
  68. out = out.xor(xb.bitmap);
  69. }
  70. }
  71. /** @return the flags associated with the bitmap */
  72. int getFlags() {
  73. return flags;
  74. }
  75. }
  76. private static final class XorCompressedBitmap {
  77. final EWAHCompressedBitmap bitmap;
  78. final StoredBitmap xorBitmap;
  79. XorCompressedBitmap(EWAHCompressedBitmap b, StoredBitmap xb) {
  80. bitmap = b;
  81. xorBitmap = xb;
  82. }
  83. }
  84. }