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.

DfsPackDescription.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (C) 2011, 2013 Google Inc., and others.
  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.dfs;
  44. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  45. import java.util.HashMap;
  46. import java.util.Map;
  47. import org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource;
  48. import org.eclipse.jgit.internal.storage.pack.PackExt;
  49. import org.eclipse.jgit.storage.pack.PackStatistics;
  50. /**
  51. * Description of a DFS stored pack/index file.
  52. * <p>
  53. * Implementors may extend this class and add additional data members.
  54. * <p>
  55. * Instances of this class are cached with the DfsPackFile, and should not be
  56. * modified once initialized and presented to the JGit DFS library.
  57. */
  58. public class DfsPackDescription implements Comparable<DfsPackDescription> {
  59. private final DfsRepositoryDescription repoDesc;
  60. private final String packName;
  61. private PackSource packSource;
  62. private long lastModified;
  63. private final Map<PackExt, Long> sizeMap;
  64. private long objectCount;
  65. private long deltaCount;
  66. private PackStatistics stats;
  67. private int extensions;
  68. private int indexVersion;
  69. /**
  70. * Initialize a description by pack name and repository.
  71. * <p>
  72. * The corresponding index file is assumed to exist. If this is not true
  73. * implementors must extend the class and override
  74. * {@link #getFileName(PackExt)}.
  75. * <p>
  76. * Callers should also try to fill in other fields if they are reasonably
  77. * free to access at the time this instance is being initialized.
  78. *
  79. * @param name
  80. * name of the pack file. Must end with ".pack".
  81. * @param repoDesc
  82. * description of the repo containing the pack file.
  83. */
  84. public DfsPackDescription(DfsRepositoryDescription repoDesc, String name) {
  85. this.repoDesc = repoDesc;
  86. int dot = name.lastIndexOf('.');
  87. this.packName = (dot < 0) ? name : name.substring(0, dot);
  88. this.sizeMap = new HashMap<PackExt, Long>(PackExt.values().length * 2);
  89. }
  90. /** @return description of the repository. */
  91. public DfsRepositoryDescription getRepositoryDescription() {
  92. return repoDesc;
  93. }
  94. /**
  95. * Adds the pack file extension to the known list.
  96. *
  97. * @param ext
  98. * the file extension
  99. */
  100. public void addFileExt(PackExt ext) {
  101. extensions |= ext.getBit();
  102. }
  103. /**
  104. * @param ext
  105. * the file extension
  106. * @return whether the pack file extensions is known to exist.
  107. */
  108. public boolean hasFileExt(PackExt ext) {
  109. return (extensions & ext.getBit()) != 0;
  110. }
  111. /**
  112. * @param ext
  113. * the file extension
  114. * @return name of the file.
  115. */
  116. public String getFileName(PackExt ext) {
  117. return packName + '.' + ext.getExtension();
  118. }
  119. /** @return the source of the pack. */
  120. public PackSource getPackSource() {
  121. return packSource;
  122. }
  123. /**
  124. * @param source
  125. * the source of the pack.
  126. * @return {@code this}
  127. */
  128. public DfsPackDescription setPackSource(PackSource source) {
  129. packSource = source;
  130. return this;
  131. }
  132. /** @return time the pack was created, in milliseconds. */
  133. public long getLastModified() {
  134. return lastModified;
  135. }
  136. /**
  137. * @param timeMillis
  138. * time the pack was created, in milliseconds. 0 if not known.
  139. * @return {@code this}
  140. */
  141. public DfsPackDescription setLastModified(long timeMillis) {
  142. lastModified = timeMillis;
  143. return this;
  144. }
  145. /**
  146. * @param ext
  147. * the file extension.
  148. * @param bytes
  149. * size of the file in bytes. If 0 the file is not known and will
  150. * be determined on first read.
  151. * @return {@code this}
  152. */
  153. public DfsPackDescription setFileSize(PackExt ext, long bytes) {
  154. sizeMap.put(ext, Long.valueOf(Math.max(0, bytes)));
  155. return this;
  156. }
  157. /**
  158. * @param ext
  159. * the file extension.
  160. * @return size of the file, in bytes. If 0 the file size is not yet known.
  161. */
  162. public long getFileSize(PackExt ext) {
  163. Long size = sizeMap.get(ext);
  164. return size == null ? 0 : size.longValue();
  165. }
  166. /** @return number of objects in the pack. */
  167. public long getObjectCount() {
  168. return objectCount;
  169. }
  170. /**
  171. * @param cnt
  172. * number of objects in the pack.
  173. * @return {@code this}
  174. */
  175. public DfsPackDescription setObjectCount(long cnt) {
  176. objectCount = Math.max(0, cnt);
  177. return this;
  178. }
  179. /** @return number of delta compressed objects in the pack. */
  180. public long getDeltaCount() {
  181. return deltaCount;
  182. }
  183. /**
  184. * @param cnt
  185. * number of delta compressed objects in the pack.
  186. * @return {@code this}
  187. */
  188. public DfsPackDescription setDeltaCount(long cnt) {
  189. deltaCount = Math.max(0, cnt);
  190. return this;
  191. }
  192. /**
  193. * @return statistics from PackWriter, if the pack was built with it.
  194. * Generally this is only available for packs created by
  195. * DfsGarbageCollector or DfsPackCompactor, and only when the pack
  196. * is being committed to the repository.
  197. */
  198. public PackStatistics getPackStats() {
  199. return stats;
  200. }
  201. DfsPackDescription setPackStats(PackStatistics stats) {
  202. this.stats = stats;
  203. setFileSize(PACK, stats.getTotalBytes());
  204. setObjectCount(stats.getTotalObjects());
  205. setDeltaCount(stats.getTotalDeltas());
  206. return this;
  207. }
  208. /**
  209. * Discard the pack statistics, if it was populated.
  210. *
  211. * @return {@code this}
  212. */
  213. public DfsPackDescription clearPackStats() {
  214. stats = null;
  215. return this;
  216. }
  217. /** @return the version of the index file written. */
  218. public int getIndexVersion() {
  219. return indexVersion;
  220. }
  221. /**
  222. * @param version
  223. * the version of the index file written.
  224. * @return {@code this}
  225. */
  226. public DfsPackDescription setIndexVersion(int version) {
  227. indexVersion = version;
  228. return this;
  229. }
  230. @Override
  231. public int hashCode() {
  232. return packName.hashCode();
  233. }
  234. @Override
  235. public boolean equals(Object b) {
  236. if (b instanceof DfsPackDescription) {
  237. DfsPackDescription desc = (DfsPackDescription) b;
  238. return packName.equals(desc.packName) &&
  239. getRepositoryDescription().equals(desc.getRepositoryDescription());
  240. }
  241. return false;
  242. }
  243. /**
  244. * Sort packs according to the optimal lookup ordering.
  245. * <p>
  246. * This method tries to position packs in the order readers should examine
  247. * them when looking for objects by SHA-1. The default tries to sort packs
  248. * with more recent modification dates before older packs, and packs with
  249. * fewer objects before packs with more objects.
  250. *
  251. * @param b
  252. * the other pack.
  253. */
  254. public int compareTo(DfsPackDescription b) {
  255. // Cluster by PackSource, pushing UNREACHABLE_GARBAGE to the end.
  256. PackSource as = getPackSource();
  257. PackSource bs = b.getPackSource();
  258. if (as != null && bs != null) {
  259. int cmp = as.category - bs.category;
  260. if (cmp != 0)
  261. return cmp;
  262. }
  263. // Newer packs should sort first.
  264. int cmp = Long.signum(b.getLastModified() - getLastModified());
  265. if (cmp != 0)
  266. return cmp;
  267. // Break ties on smaller index. Readers may get lucky and find
  268. // the object they care about in the smaller index. This also pushes
  269. // big historical packs to the end of the list, due to more objects.
  270. return Long.signum(getObjectCount() - b.getObjectCount());
  271. }
  272. @Override
  273. public String toString() {
  274. return getFileName(PackExt.PACK);
  275. }
  276. }