Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

PackBitmapIndexBuilder.java 10KB

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