View Javadoc
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  
11  package org.eclipse.jgit.internal.storage.file;
12  
13  import org.eclipse.jgit.lib.AnyObjectId;
14  import org.eclipse.jgit.lib.ObjectIdOwnerMap;
15  
16  import com.googlecode.javaewah.EWAHCompressedBitmap;
17  
18  /**
19   * Base implementation of the PackBitmapIndex.
20   */
21  abstract class BasePackBitmapIndex extends PackBitmapIndex {
22  	private final ObjectIdOwnerMap<StoredBitmap> bitmaps;
23  
24  	BasePackBitmapIndex(ObjectIdOwnerMap<StoredBitmap> bitmaps) {
25  		this.bitmaps = bitmaps;
26  	}
27  
28  	/** {@inheritDoc} */
29  	@Override
30  	public EWAHCompressedBitmap getBitmap(AnyObjectId objectId) {
31  		StoredBitmap sb = bitmaps.get(objectId);
32  		return sb != null ? sb.getBitmap() : null;
33  	}
34  
35  	ObjectIdOwnerMap<StoredBitmap> getBitmaps() {
36  		return bitmaps;
37  	}
38  
39  	/**
40  	 * Data representation of the bitmap entry restored from a pack index. The
41  	 * commit of the bitmap is the map key.
42  	 */
43  	static final class StoredBitmap extends ObjectIdOwnerMap.Entry {
44  		private volatile Object bitmapContainer;
45  		private final int flags;
46  
47  		StoredBitmap(AnyObjectId objectId, EWAHCompressedBitmap bitmap,
48  				StoredBitmap xorBitmap, int flags) {
49  			super(objectId);
50  			this.bitmapContainer = xorBitmap == null
51  					? bitmap
52  					: new XorCompressedBitmap(bitmap, xorBitmap);
53  			this.flags = flags;
54  		}
55  
56  		/**
57  		 * Computes and returns the full bitmap.
58  		 *
59  		 * @return the full bitmap
60  		 */
61  		EWAHCompressedBitmap getBitmap() {
62  			// Fast path to immediately return the expanded result.
63  			Object r = bitmapContainer;
64  			if (r instanceof EWAHCompressedBitmap)
65  				return (EWAHCompressedBitmap) r;
66  
67  			// Expand the bitmap and cache the result.
68  			XorCompressedBitmap xb = (XorCompressedBitmap) r;
69  			EWAHCompressedBitmap out = xb.bitmap;
70  			for (;;) {
71  				r = xb.xorBitmap.bitmapContainer;
72  				if (r instanceof EWAHCompressedBitmap) {
73  					out = out.xor((EWAHCompressedBitmap) r);
74  					out.trim();
75  					bitmapContainer = out;
76  					return out;
77  				}
78  				xb = (XorCompressedBitmap) r;
79  				out = out.xor(xb.bitmap);
80  			}
81  		}
82  
83  		/** @return the flags associated with the bitmap */
84  		int getFlags() {
85  			return flags;
86  		}
87  	}
88  
89  	private static final class XorCompressedBitmap {
90  		final EWAHCompressedBitmap bitmap;
91  		final StoredBitmap xorBitmap;
92  
93  		XorCompressedBitmap(EWAHCompressedBitmap b, StoredBitmap xb) {
94  			bitmap = b;
95  			xorBitmap = xb;
96  		}
97  	}
98  }