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 11KB

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