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.

BitmapIndexImpl.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Copyright (C) 2012, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.internal.storage.file;
  44. import java.text.MessageFormat;
  45. import java.util.Iterator;
  46. import java.util.NoSuchElementException;
  47. import org.eclipse.jgit.internal.JGitText;
  48. import org.eclipse.jgit.lib.AnyObjectId;
  49. import org.eclipse.jgit.lib.BitmapIndex;
  50. import org.eclipse.jgit.lib.BitmapObject;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  54. import org.eclipse.jgit.util.BlockList;
  55. import com.googlecode.javaewah.EWAHCompressedBitmap;
  56. import com.googlecode.javaewah.IntIterator;
  57. /**
  58. * A compressed bitmap representation of the entire object graph.
  59. */
  60. public class BitmapIndexImpl implements BitmapIndex {
  61. private static final int EXTRA_BITS = 10 * 1024;
  62. final PackBitmapIndex packIndex;
  63. final MutableBitmapIndex mutableIndex;
  64. final int indexObjectCount;
  65. /**
  66. * Creates a BitmapIndex that is back by Compressed bitmaps.
  67. *
  68. * @param packIndex
  69. * the bitmap index for the pack.
  70. */
  71. public BitmapIndexImpl(PackBitmapIndex packIndex) {
  72. this.packIndex = packIndex;
  73. mutableIndex = new MutableBitmapIndex();
  74. indexObjectCount = packIndex.getObjectCount();
  75. }
  76. PackBitmapIndex getPackBitmapIndex() {
  77. return packIndex;
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public CompressedBitmap getBitmap(AnyObjectId objectId) {
  82. EWAHCompressedBitmap compressed = packIndex.getBitmap(objectId);
  83. if (compressed == null)
  84. return null;
  85. return new CompressedBitmap(compressed, this);
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public CompressedBitmapBuilder newBitmapBuilder() {
  90. return new CompressedBitmapBuilder(this);
  91. }
  92. int findPosition(AnyObjectId objectId) {
  93. int position = packIndex.findPosition(objectId);
  94. if (position < 0) {
  95. position = mutableIndex.findPosition(objectId);
  96. if (position >= 0)
  97. position += indexObjectCount;
  98. }
  99. return position;
  100. }
  101. int findOrInsert(AnyObjectId objectId, int type) {
  102. int position = findPosition(objectId);
  103. if (position < 0) {
  104. position = mutableIndex.findOrInsert(objectId, type);
  105. position += indexObjectCount;
  106. }
  107. return position;
  108. }
  109. private static final class ComboBitset {
  110. private InflatingBitSet inflatingBitmap;
  111. private BitSet toAdd;
  112. private BitSet toRemove;
  113. ComboBitset() {
  114. this(new EWAHCompressedBitmap());
  115. }
  116. ComboBitset(EWAHCompressedBitmap bitmap) {
  117. this.inflatingBitmap = new InflatingBitSet(bitmap);
  118. }
  119. EWAHCompressedBitmap combine() {
  120. EWAHCompressedBitmap toAddCompressed = null;
  121. if (toAdd != null) {
  122. toAddCompressed = toAdd.toEWAHCompressedBitmap();
  123. toAdd = null;
  124. }
  125. EWAHCompressedBitmap toRemoveCompressed = null;
  126. if (toRemove != null) {
  127. toRemoveCompressed = toRemove.toEWAHCompressedBitmap();
  128. toRemove = null;
  129. }
  130. if (toAddCompressed != null)
  131. or(toAddCompressed);
  132. if (toRemoveCompressed != null)
  133. andNot(toRemoveCompressed);
  134. return inflatingBitmap.getBitmap();
  135. }
  136. void or(EWAHCompressedBitmap inbits) {
  137. if (toRemove != null)
  138. combine();
  139. inflatingBitmap = inflatingBitmap.or(inbits);
  140. }
  141. void andNot(EWAHCompressedBitmap inbits) {
  142. if (toAdd != null || toRemove != null)
  143. combine();
  144. inflatingBitmap = inflatingBitmap.andNot(inbits);
  145. }
  146. void xor(EWAHCompressedBitmap inbits) {
  147. if (toAdd != null || toRemove != null)
  148. combine();
  149. inflatingBitmap = inflatingBitmap.xor(inbits);
  150. }
  151. boolean contains(int position) {
  152. if (toRemove != null && toRemove.get(position))
  153. return false;
  154. if (toAdd != null && toAdd.get(position))
  155. return true;
  156. return inflatingBitmap.contains(position);
  157. }
  158. void remove(int position) {
  159. if (toAdd != null)
  160. toAdd.clear(position);
  161. if (inflatingBitmap.maybeContains(position)) {
  162. if (toRemove == null)
  163. toRemove = new BitSet(position + EXTRA_BITS);
  164. toRemove.set(position);
  165. }
  166. }
  167. void set(int position) {
  168. if (toRemove != null)
  169. toRemove.clear(position);
  170. if (toAdd == null)
  171. toAdd = new BitSet(position + EXTRA_BITS);
  172. toAdd.set(position);
  173. }
  174. }
  175. private static final class CompressedBitmapBuilder implements BitmapBuilder {
  176. private ComboBitset bitset;
  177. private final BitmapIndexImpl bitmapIndex;
  178. CompressedBitmapBuilder(BitmapIndexImpl bitmapIndex) {
  179. this.bitset = new ComboBitset();
  180. this.bitmapIndex = bitmapIndex;
  181. }
  182. @Override
  183. public boolean add(AnyObjectId objectId, int type) {
  184. int position = bitmapIndex.findOrInsert(objectId, type);
  185. if (bitset.contains(position))
  186. return false;
  187. Bitmap entry = bitmapIndex.getBitmap(objectId);
  188. if (entry != null) {
  189. or(entry);
  190. return false;
  191. }
  192. bitset.set(position);
  193. return true;
  194. }
  195. @Override
  196. public boolean contains(AnyObjectId objectId) {
  197. int position = bitmapIndex.findPosition(objectId);
  198. return 0 <= position && bitset.contains(position);
  199. }
  200. @Override
  201. public BitmapBuilder addObject(AnyObjectId objectId, int type) {
  202. bitset.set(bitmapIndex.findOrInsert(objectId, type));
  203. return this;
  204. }
  205. @Override
  206. public void remove(AnyObjectId objectId) {
  207. int position = bitmapIndex.findPosition(objectId);
  208. if (0 <= position)
  209. bitset.remove(position);
  210. }
  211. @Override
  212. public CompressedBitmapBuilder or(Bitmap other) {
  213. bitset.or(ewahBitmap(other));
  214. return this;
  215. }
  216. @Override
  217. public CompressedBitmapBuilder andNot(Bitmap other) {
  218. bitset.andNot(ewahBitmap(other));
  219. return this;
  220. }
  221. @Override
  222. public CompressedBitmapBuilder xor(Bitmap other) {
  223. bitset.xor(ewahBitmap(other));
  224. return this;
  225. }
  226. /** @return the fully built immutable bitmap */
  227. @Override
  228. public CompressedBitmap build() {
  229. return new CompressedBitmap(bitset.combine(), bitmapIndex);
  230. }
  231. @Override
  232. public Iterator<BitmapObject> iterator() {
  233. return build().iterator();
  234. }
  235. @Override
  236. public int cardinality() {
  237. return bitset.combine().cardinality();
  238. }
  239. @Override
  240. public boolean removeAllOrNone(PackBitmapIndex index) {
  241. if (!bitmapIndex.packIndex.equals(index))
  242. return false;
  243. EWAHCompressedBitmap curr = bitset.combine()
  244. .xor(ones(bitmapIndex.indexObjectCount));
  245. IntIterator ii = curr.intIterator();
  246. if (ii.hasNext() && ii.next() < bitmapIndex.indexObjectCount)
  247. return false;
  248. bitset = new ComboBitset(curr);
  249. return true;
  250. }
  251. @Override
  252. public BitmapIndexImpl getBitmapIndex() {
  253. return bitmapIndex;
  254. }
  255. private EWAHCompressedBitmap ewahBitmap(Bitmap other) {
  256. if (other instanceof CompressedBitmap) {
  257. CompressedBitmap b = (CompressedBitmap) other;
  258. if (b.bitmapIndex != bitmapIndex) {
  259. throw new IllegalArgumentException();
  260. }
  261. return b.bitmap;
  262. }
  263. if (other instanceof CompressedBitmapBuilder) {
  264. CompressedBitmapBuilder b = (CompressedBitmapBuilder) other;
  265. if (b.bitmapIndex != bitmapIndex) {
  266. throw new IllegalArgumentException();
  267. }
  268. return b.bitset.combine();
  269. }
  270. throw new IllegalArgumentException();
  271. }
  272. }
  273. /**
  274. * Wrapper for a {@link EWAHCompressedBitmap} and {@link PackBitmapIndex}.
  275. * <p>
  276. * For a EWAHCompressedBitmap {@code bitmap} representing a vector of
  277. * bits, {@code new CompressedBitmap(bitmap, bitmapIndex)} represents the
  278. * objects at those positions in {@code bitmapIndex.packIndex}.
  279. */
  280. public static final class CompressedBitmap implements Bitmap {
  281. final EWAHCompressedBitmap bitmap;
  282. final BitmapIndexImpl bitmapIndex;
  283. /**
  284. * Construct compressed bitmap for given bitmap and bitmap index
  285. *
  286. * @param bitmap
  287. * @param bitmapIndex
  288. */
  289. public CompressedBitmap(EWAHCompressedBitmap bitmap, BitmapIndexImpl bitmapIndex) {
  290. this.bitmap = bitmap;
  291. this.bitmapIndex = bitmapIndex;
  292. }
  293. @Override
  294. public CompressedBitmap or(Bitmap other) {
  295. return new CompressedBitmap(bitmap.or(ewahBitmap(other)), bitmapIndex);
  296. }
  297. @Override
  298. public CompressedBitmap andNot(Bitmap other) {
  299. return new CompressedBitmap(bitmap.andNot(ewahBitmap(other)), bitmapIndex);
  300. }
  301. @Override
  302. public CompressedBitmap xor(Bitmap other) {
  303. return new CompressedBitmap(bitmap.xor(ewahBitmap(other)), bitmapIndex);
  304. }
  305. private final IntIterator ofObjectType(int type) {
  306. return bitmapIndex.packIndex.ofObjectType(bitmap, type).intIterator();
  307. }
  308. @Override
  309. public Iterator<BitmapObject> iterator() {
  310. final IntIterator dynamic = bitmap.andNot(ones(bitmapIndex.indexObjectCount))
  311. .intIterator();
  312. final IntIterator commits = ofObjectType(Constants.OBJ_COMMIT);
  313. final IntIterator trees = ofObjectType(Constants.OBJ_TREE);
  314. final IntIterator blobs = ofObjectType(Constants.OBJ_BLOB);
  315. final IntIterator tags = ofObjectType(Constants.OBJ_TAG);
  316. return new Iterator<BitmapObject>() {
  317. private final BitmapObjectImpl out = new BitmapObjectImpl();
  318. private int type;
  319. private IntIterator cached = dynamic;
  320. @Override
  321. public boolean hasNext() {
  322. if (!cached.hasNext()) {
  323. if (commits.hasNext()) {
  324. type = Constants.OBJ_COMMIT;
  325. cached = commits;
  326. } else if (trees.hasNext()) {
  327. type = Constants.OBJ_TREE;
  328. cached = trees;
  329. } else if (blobs.hasNext()) {
  330. type = Constants.OBJ_BLOB;
  331. cached = blobs;
  332. } else if (tags.hasNext()) {
  333. type = Constants.OBJ_TAG;
  334. cached = tags;
  335. } else {
  336. return false;
  337. }
  338. }
  339. return true;
  340. }
  341. @Override
  342. public BitmapObject next() {
  343. if (!hasNext())
  344. throw new NoSuchElementException();
  345. int position = cached.next();
  346. if (position < bitmapIndex.indexObjectCount) {
  347. out.type = type;
  348. out.objectId = bitmapIndex.packIndex.getObject(position);
  349. } else {
  350. position -= bitmapIndex.indexObjectCount;
  351. MutableEntry entry = bitmapIndex.mutableIndex.getObject(position);
  352. out.type = entry.type;
  353. out.objectId = entry;
  354. }
  355. return out;
  356. }
  357. @Override
  358. public void remove() {
  359. throw new UnsupportedOperationException();
  360. }
  361. };
  362. }
  363. EWAHCompressedBitmap getEwahCompressedBitmap() {
  364. return bitmap;
  365. }
  366. private EWAHCompressedBitmap ewahBitmap(Bitmap other) {
  367. if (other instanceof CompressedBitmap) {
  368. CompressedBitmap b = (CompressedBitmap) other;
  369. if (b.bitmapIndex != bitmapIndex) {
  370. throw new IllegalArgumentException();
  371. }
  372. return b.bitmap;
  373. }
  374. if (other instanceof CompressedBitmapBuilder) {
  375. CompressedBitmapBuilder b = (CompressedBitmapBuilder) other;
  376. if (b.bitmapIndex != bitmapIndex) {
  377. throw new IllegalArgumentException();
  378. }
  379. return b.bitset.combine();
  380. }
  381. throw new IllegalArgumentException();
  382. }
  383. }
  384. private static final class MutableBitmapIndex {
  385. private final ObjectIdOwnerMap<MutableEntry>
  386. revMap = new ObjectIdOwnerMap<>();
  387. private final BlockList<MutableEntry>
  388. revList = new BlockList<>();
  389. int findPosition(AnyObjectId objectId) {
  390. MutableEntry entry = revMap.get(objectId);
  391. if (entry == null)
  392. return -1;
  393. return entry.position;
  394. }
  395. MutableEntry getObject(int position) {
  396. try {
  397. MutableEntry entry = revList.get(position);
  398. if (entry == null)
  399. throw new IllegalArgumentException(MessageFormat.format(
  400. JGitText.get().objectNotFound,
  401. String.valueOf(position)));
  402. return entry;
  403. } catch (IndexOutOfBoundsException ex) {
  404. throw new IllegalArgumentException(ex);
  405. }
  406. }
  407. int findOrInsert(AnyObjectId objectId, int type) {
  408. MutableEntry entry = new MutableEntry(
  409. objectId, type, revList.size());
  410. revList.add(entry);
  411. revMap.add(entry);
  412. return entry.position;
  413. }
  414. }
  415. private static final class MutableEntry extends ObjectIdOwnerMap.Entry {
  416. final int type;
  417. final int position;
  418. MutableEntry(AnyObjectId objectId, int type, int position) {
  419. super(objectId);
  420. this.type = type;
  421. this.position = position;
  422. }
  423. }
  424. private static final class BitmapObjectImpl extends BitmapObject {
  425. private ObjectId objectId;
  426. private int type;
  427. @Override
  428. public ObjectId getObjectId() {
  429. return objectId;
  430. }
  431. @Override
  432. public int getType() {
  433. return type;
  434. }
  435. }
  436. static final EWAHCompressedBitmap ones(int sizeInBits) {
  437. EWAHCompressedBitmap mask = new EWAHCompressedBitmap();
  438. mask.addStreamOfEmptyWords(
  439. true, sizeInBits / EWAHCompressedBitmap.WORD_IN_BITS);
  440. int remaining = sizeInBits % EWAHCompressedBitmap.WORD_IN_BITS;
  441. if (remaining > 0)
  442. mask.addWord((1L << remaining) - 1, remaining);
  443. return mask;
  444. }
  445. }