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.

PackBitmapIndexBuilder.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 java.text.MessageFormat;
  12. import java.util.Collections;
  13. import java.util.Iterator;
  14. import java.util.List;
  15. import java.util.NoSuchElementException;
  16. import org.eclipse.jgit.internal.JGitText;
  17. import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
  18. import org.eclipse.jgit.lib.AnyObjectId;
  19. import org.eclipse.jgit.lib.BitmapIndex.Bitmap;
  20. import org.eclipse.jgit.lib.Constants;
  21. import org.eclipse.jgit.lib.ObjectId;
  22. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  23. import org.eclipse.jgit.util.BlockList;
  24. import com.googlecode.javaewah.EWAHCompressedBitmap;
  25. /**
  26. * Helper for constructing
  27. * {@link org.eclipse.jgit.internal.storage.file.PackBitmapIndex}es.
  28. */
  29. public class PackBitmapIndexBuilder extends BasePackBitmapIndex {
  30. private static final int MAX_XOR_OFFSET_SEARCH = 10;
  31. private final EWAHCompressedBitmap commits;
  32. private final EWAHCompressedBitmap trees;
  33. private final EWAHCompressedBitmap blobs;
  34. private final EWAHCompressedBitmap tags;
  35. private final BlockList<PositionEntry> byOffset;
  36. final BlockList<StoredBitmap>
  37. byAddOrder = new BlockList<>();
  38. final ObjectIdOwnerMap<PositionEntry>
  39. positionEntries = new ObjectIdOwnerMap<>();
  40. /**
  41. * Creates a PackBitmapIndex used for building the contents of an index
  42. * file.
  43. *
  44. * @param objects
  45. * objects sorted by name. The list must be initially sorted by
  46. * ObjectId (name); it will be resorted in place.
  47. */
  48. public PackBitmapIndexBuilder(List<ObjectToPack> objects) {
  49. super(new ObjectIdOwnerMap<StoredBitmap>());
  50. byOffset = new BlockList<>(objects.size());
  51. sortByOffsetAndIndex(byOffset, positionEntries, objects);
  52. // 64 objects fit in a single long word (64 bits).
  53. // On average a repository is 30% commits, 30% trees, 30% blobs.
  54. // Initialize bitmap capacity for worst case to minimize growing.
  55. int sizeInWords = Math.max(4, byOffset.size() / 64 / 3);
  56. commits = new EWAHCompressedBitmap(sizeInWords);
  57. trees = new EWAHCompressedBitmap(sizeInWords);
  58. blobs = new EWAHCompressedBitmap(sizeInWords);
  59. tags = new EWAHCompressedBitmap(sizeInWords);
  60. for (int i = 0; i < objects.size(); i++) {
  61. int type = objects.get(i).getType();
  62. switch (type) {
  63. case Constants.OBJ_COMMIT:
  64. commits.set(i);
  65. break;
  66. case Constants.OBJ_TREE:
  67. trees.set(i);
  68. break;
  69. case Constants.OBJ_BLOB:
  70. blobs.set(i);
  71. break;
  72. case Constants.OBJ_TAG:
  73. tags.set(i);
  74. break;
  75. default:
  76. throw new IllegalArgumentException(MessageFormat.format(
  77. JGitText.get().badObjectType, String.valueOf(type)));
  78. }
  79. }
  80. commits.trim();
  81. trees.trim();
  82. blobs.trim();
  83. tags.trim();
  84. }
  85. private static void sortByOffsetAndIndex(BlockList<PositionEntry> byOffset,
  86. ObjectIdOwnerMap<PositionEntry> positionEntries,
  87. List<ObjectToPack> entries) {
  88. for (int i = 0; i < entries.size(); i++) {
  89. positionEntries.add(new PositionEntry(entries.get(i), i));
  90. }
  91. Collections.sort(entries, (ObjectToPack a, ObjectToPack b) -> Long
  92. .signum(a.getOffset() - b.getOffset()));
  93. for (int i = 0; i < entries.size(); i++) {
  94. PositionEntry e = positionEntries.get(entries.get(i));
  95. e.offsetPosition = i;
  96. byOffset.add(e);
  97. }
  98. }
  99. /**
  100. * Get set of objects included in the pack.
  101. *
  102. * @return set of objects included in the pack.
  103. */
  104. public ObjectIdOwnerMap<ObjectIdOwnerMap.Entry> getObjectSet() {
  105. ObjectIdOwnerMap<ObjectIdOwnerMap.Entry> r = new ObjectIdOwnerMap<>();
  106. for (PositionEntry e : byOffset) {
  107. r.add(new ObjectIdOwnerMap.Entry(e) {
  108. // A new entry that copies the ObjectId
  109. });
  110. }
  111. return r;
  112. }
  113. /**
  114. * Stores the bitmap for the objectId.
  115. *
  116. * @param objectId
  117. * the object id key for the bitmap.
  118. * @param bitmap
  119. * the bitmap
  120. * @param flags
  121. * the flags to be stored with the bitmap
  122. */
  123. public void addBitmap(AnyObjectId objectId, Bitmap bitmap, int flags) {
  124. addBitmap(objectId, bitmap.retrieveCompressed(), flags);
  125. }
  126. /**
  127. * Stores the bitmap for the objectId.
  128. *
  129. * @param objectId
  130. * the object id key for the bitmap.
  131. * @param bitmap
  132. * the bitmap
  133. * @param flags
  134. * the flags to be stored with the bitmap
  135. */
  136. public void addBitmap(
  137. AnyObjectId objectId, EWAHCompressedBitmap bitmap, int flags) {
  138. bitmap.trim();
  139. StoredBitmap result = new StoredBitmap(objectId, bitmap, null, flags);
  140. getBitmaps().add(result);
  141. byAddOrder.add(result);
  142. }
  143. /** {@inheritDoc} */
  144. @Override
  145. public EWAHCompressedBitmap ofObjectType(
  146. EWAHCompressedBitmap bitmap, int type) {
  147. switch (type) {
  148. case Constants.OBJ_BLOB:
  149. return getBlobs().and(bitmap);
  150. case Constants.OBJ_TREE:
  151. return getTrees().and(bitmap);
  152. case Constants.OBJ_COMMIT:
  153. return getCommits().and(bitmap);
  154. case Constants.OBJ_TAG:
  155. return getTags().and(bitmap);
  156. }
  157. throw new IllegalArgumentException();
  158. }
  159. /** {@inheritDoc} */
  160. @Override
  161. public int findPosition(AnyObjectId objectId) {
  162. PositionEntry entry = positionEntries.get(objectId);
  163. if (entry == null)
  164. return -1;
  165. return entry.offsetPosition;
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. public ObjectId getObject(int position) throws IllegalArgumentException {
  170. ObjectId objectId = byOffset.get(position);
  171. if (objectId == null)
  172. throw new IllegalArgumentException();
  173. return objectId;
  174. }
  175. /**
  176. * Get the commit object bitmap.
  177. *
  178. * @return the commit object bitmap.
  179. */
  180. public EWAHCompressedBitmap getCommits() {
  181. return commits;
  182. }
  183. /**
  184. * Get the tree object bitmap.
  185. *
  186. * @return the tree object bitmap.
  187. */
  188. public EWAHCompressedBitmap getTrees() {
  189. return trees;
  190. }
  191. /**
  192. * Get the blob object bitmap.
  193. *
  194. * @return the blob object bitmap.
  195. */
  196. public EWAHCompressedBitmap getBlobs() {
  197. return blobs;
  198. }
  199. /**
  200. * Get the tag object bitmap.
  201. *
  202. * @return the tag object bitmap.
  203. */
  204. public EWAHCompressedBitmap getTags() {
  205. return tags;
  206. }
  207. /**
  208. * Get the index storage options.
  209. *
  210. * @return the index storage options.
  211. */
  212. public int getOptions() {
  213. return PackBitmapIndexV1.OPT_FULL;
  214. }
  215. /** {@inheritDoc} */
  216. @Override
  217. public int getBitmapCount() {
  218. return getBitmaps().size();
  219. }
  220. /**
  221. * Remove all the bitmaps entries added.
  222. */
  223. public void clearBitmaps() {
  224. byAddOrder.clear();
  225. getBitmaps().clear();
  226. }
  227. /** {@inheritDoc} */
  228. @Override
  229. public int getObjectCount() {
  230. return byOffset.size();
  231. }
  232. /**
  233. * Get an iterator over the xor compressed entries.
  234. *
  235. * @return an iterator over the xor compressed entries.
  236. */
  237. public Iterable<StoredEntry> getCompressedBitmaps() {
  238. // Add order is from oldest to newest. The reverse add order is the
  239. // output order.
  240. return () -> new Iterator<StoredEntry>() {
  241. private int index = byAddOrder.size() - 1;
  242. @Override
  243. public boolean hasNext() {
  244. return index >= 0;
  245. }
  246. @Override
  247. public StoredEntry next() {
  248. if (!hasNext()) {
  249. throw new NoSuchElementException();
  250. }
  251. StoredBitmap item = byAddOrder.get(index);
  252. int bestXorOffset = 0;
  253. EWAHCompressedBitmap bestBitmap = item.getBitmap();
  254. // Attempt to compress the bitmap with an XOR of the
  255. // previously written entries.
  256. for (int i = 1; i <= MAX_XOR_OFFSET_SEARCH; i++) {
  257. int curr = i + index;
  258. if (curr >= byAddOrder.size()) {
  259. break;
  260. }
  261. StoredBitmap other = byAddOrder.get(curr);
  262. EWAHCompressedBitmap bitmap = other.getBitmap()
  263. .xor(item.getBitmap());
  264. if (bitmap.sizeInBytes() < bestBitmap.sizeInBytes()) {
  265. bestBitmap = bitmap;
  266. bestXorOffset = i;
  267. }
  268. }
  269. index--;
  270. PositionEntry entry = positionEntries.get(item);
  271. if (entry == null) {
  272. throw new IllegalStateException();
  273. }
  274. bestBitmap.trim();
  275. return new StoredEntry(entry.namePosition, bestBitmap,
  276. bestXorOffset, item.getFlags());
  277. }
  278. @Override
  279. public void remove() {
  280. throw new UnsupportedOperationException();
  281. }
  282. };
  283. }
  284. /** Data object for the on disk representation of a bitmap entry. */
  285. public static final class StoredEntry {
  286. private final long objectId;
  287. private final EWAHCompressedBitmap bitmap;
  288. private final int xorOffset;
  289. private final int flags;
  290. StoredEntry(long objectId, EWAHCompressedBitmap bitmap,
  291. int xorOffset, int flags) {
  292. this.objectId = objectId;
  293. this.bitmap = bitmap;
  294. this.xorOffset = xorOffset;
  295. this.flags = flags;
  296. }
  297. /** @return the bitmap */
  298. public EWAHCompressedBitmap getBitmap() {
  299. return bitmap;
  300. }
  301. /** @return the xorOffset */
  302. public int getXorOffset() {
  303. return xorOffset;
  304. }
  305. /** @return the flags */
  306. public int getFlags() {
  307. return flags;
  308. }
  309. /** @return the ObjectId */
  310. public long getObjectId() {
  311. return objectId;
  312. }
  313. }
  314. private static final class PositionEntry extends ObjectIdOwnerMap.Entry {
  315. final int namePosition;
  316. int offsetPosition;
  317. PositionEntry(AnyObjectId objectId, int namePosition) {
  318. super(objectId);
  319. this.namePosition = namePosition;
  320. }
  321. }
  322. }