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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2011, 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.storage.dfs;
  44. import java.util.HashMap;
  45. import java.util.Map;
  46. import java.util.Set;
  47. import org.eclipse.jgit.lib.ObjectId;
  48. import org.eclipse.jgit.storage.dfs.DfsObjDatabase.PackSource;
  49. import org.eclipse.jgit.storage.pack.PackConstants;
  50. import org.eclipse.jgit.storage.pack.PackWriter;
  51. /**
  52. * Description of a DFS stored pack/index file.
  53. * <p>
  54. * Implementors may extend this class and add additional data members.
  55. * <p>
  56. * Instances of this class are cached with the DfsPackFile, and should not be
  57. * modified once initialized and presented to the JGit DFS library.
  58. */
  59. public class DfsPackDescription implements Comparable<DfsPackDescription> {
  60. private final DfsRepositoryDescription repoDesc;
  61. private final String packName;
  62. private PackSource packSource;
  63. private long lastModified;
  64. private Map<String, Long> sizeMap;
  65. private long objectCount;
  66. private long deltaCount;
  67. private Set<ObjectId> tips;
  68. private PackWriter.Statistics stats;
  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(String)}.
  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<String, Long>(5);
  89. }
  90. /** @return description of the repository. */
  91. public DfsRepositoryDescription getRepositoryDescription() {
  92. return repoDesc;
  93. }
  94. /**
  95. * @param ext
  96. * the file extension
  97. * @return name of the file.
  98. * */
  99. public String getFileName(String ext) {
  100. return packName + '.' + ext;
  101. }
  102. /** @return the source of the pack. */
  103. public PackSource getPackSource() {
  104. return packSource;
  105. }
  106. /**
  107. * @param source
  108. * the source of the pack.
  109. * @return {@code this}
  110. */
  111. public DfsPackDescription setPackSource(PackSource source) {
  112. packSource = source;
  113. return this;
  114. }
  115. /** @return time the pack was created, in milliseconds. */
  116. public long getLastModified() {
  117. return lastModified;
  118. }
  119. /**
  120. * @param timeMillis
  121. * time the pack was created, in milliseconds. 0 if not known.
  122. * @return {@code this}
  123. */
  124. public DfsPackDescription setLastModified(long timeMillis) {
  125. lastModified = timeMillis;
  126. return this;
  127. }
  128. /**
  129. * @param ext
  130. * the file extension.
  131. * @param bytes
  132. * size of the file in bytes. If 0 the file is not known and will
  133. * be determined on first read.
  134. * @return {@code this}
  135. */
  136. public DfsPackDescription setFileSize(String ext, long bytes) {
  137. sizeMap.put(ext, Long.valueOf(Math.max(0, bytes)));
  138. return this;
  139. }
  140. /**
  141. * @param ext
  142. * the file extension.
  143. * @return size of the file, in bytes. If 0 the file size is not yet known.
  144. */
  145. public long getFileSize(String ext) {
  146. Long size = sizeMap.get(ext);
  147. return size == null ? 0 : size.longValue();
  148. }
  149. /** @return number of objects in the pack. */
  150. public long getObjectCount() {
  151. return objectCount;
  152. }
  153. /**
  154. * @param cnt
  155. * number of objects in the pack.
  156. * @return {@code this}
  157. */
  158. public DfsPackDescription setObjectCount(long cnt) {
  159. objectCount = Math.max(0, cnt);
  160. return this;
  161. }
  162. /** @return number of delta compressed objects in the pack. */
  163. public long getDeltaCount() {
  164. return deltaCount;
  165. }
  166. /**
  167. * @param cnt
  168. * number of delta compressed objects in the pack.
  169. * @return {@code this}
  170. */
  171. public DfsPackDescription setDeltaCount(long cnt) {
  172. deltaCount = Math.max(0, cnt);
  173. return this;
  174. }
  175. /** @return the tips that created this pack, if known. */
  176. public Set<ObjectId> getTips() {
  177. return tips;
  178. }
  179. /**
  180. * @param tips
  181. * the tips of the pack, null if it has no known tips.
  182. * @return {@code this}
  183. */
  184. public DfsPackDescription setTips(Set<ObjectId> tips) {
  185. this.tips = tips;
  186. return this;
  187. }
  188. /**
  189. * @return statistics from PackWriter, if the pack was built with it.
  190. * Generally this is only available for packs created by
  191. * DfsGarbageCollector or DfsPackCompactor, and only when the pack
  192. * is being committed to the repository.
  193. */
  194. public PackWriter.Statistics getPackStats() {
  195. return stats;
  196. }
  197. DfsPackDescription setPackStats(PackWriter.Statistics stats) {
  198. this.stats = stats;
  199. return this;
  200. }
  201. /**
  202. * Discard the pack statistics, if it was populated.
  203. *
  204. * @return {@code this}
  205. */
  206. public DfsPackDescription clearPackStats() {
  207. stats = null;
  208. return this;
  209. }
  210. @Override
  211. public int hashCode() {
  212. return packName.hashCode();
  213. }
  214. @Override
  215. public boolean equals(Object b) {
  216. if (b instanceof DfsPackDescription) {
  217. DfsPackDescription desc = (DfsPackDescription) b;
  218. return packName.equals(desc.packName) &&
  219. getRepositoryDescription().equals(desc.getRepositoryDescription());
  220. }
  221. return false;
  222. }
  223. /**
  224. * Sort packs according to the optimal lookup ordering.
  225. * <p>
  226. * This method tries to position packs in the order readers should examine
  227. * them when looking for objects by SHA-1. The default tries to sort packs
  228. * with more recent modification dates before older packs, and packs with
  229. * fewer objects before packs with more objects.
  230. *
  231. * @param b
  232. * the other pack.
  233. */
  234. public int compareTo(DfsPackDescription b) {
  235. // Newer packs should sort first.
  236. int cmp = Long.signum(b.getLastModified() - getLastModified());
  237. if (cmp != 0)
  238. return cmp;
  239. // Break ties on smaller index. Readers may get lucky and find
  240. // the object they care about in the smaller index. This also pushes
  241. // big historical packs to the end of the list, due to more objects.
  242. return Long.signum(getObjectCount() - b.getObjectCount());
  243. }
  244. @Override
  245. public String toString() {
  246. return getFileName(PackConstants.PACK_EXT);
  247. }
  248. }