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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.Set;
  45. import org.eclipse.jgit.lib.ObjectId;
  46. import org.eclipse.jgit.storage.pack.PackWriter;
  47. /**
  48. * Description of a DFS stored pack/index file.
  49. * <p>
  50. * Implementors may extend this class and add additional data members.
  51. * <p>
  52. * Instances of this class are cached with the DfsPackFile, and should not be
  53. * modified once initialized and presented to the JGit DFS library.
  54. */
  55. public class DfsPackDescription implements Comparable<DfsPackDescription> {
  56. private final String packName;
  57. private long lastModified;
  58. private long packSize;
  59. private long indexSize;
  60. private long objectCount;
  61. private long deltaCount;
  62. private Set<ObjectId> tips;
  63. private PackWriter.Statistics stats;
  64. /**
  65. * Initialize a description by pack name.
  66. * <p>
  67. * The corresponding index file is assumed to exist and end with ".idx"
  68. * instead of ".pack". If this is not true implementors must extend the
  69. * class and override {@link #getIndexName()}.
  70. * <p>
  71. * Callers should also try to fill in other fields if they are reasonably
  72. * free to access at the time this instance is being initialized.
  73. *
  74. * @param name
  75. * name of the pack file. Must end with ".pack".
  76. */
  77. public DfsPackDescription(String name) {
  78. this.packName = name;
  79. }
  80. /** @return name of the pack file. */
  81. public String getPackName() {
  82. return packName;
  83. }
  84. /** @return name of the index file. */
  85. public String getIndexName() {
  86. String name = getPackName();
  87. int dot = name.lastIndexOf('.');
  88. if (dot < 0)
  89. dot = name.length();
  90. return name.substring(0, dot) + ".idx";
  91. }
  92. /** @return time the pack was created, in milliseconds. */
  93. public long getLastModified() {
  94. return lastModified;
  95. }
  96. /**
  97. * @param timeMillis
  98. * time the pack was created, in milliseconds. 0 if not known.
  99. * @return {@code this}
  100. */
  101. public DfsPackDescription setLastModified(long timeMillis) {
  102. lastModified = timeMillis;
  103. return this;
  104. }
  105. /** @return size of the pack, in bytes. If 0 the pack size is not yet known. */
  106. public long getPackSize() {
  107. return packSize;
  108. }
  109. /**
  110. * @param bytes
  111. * size of the pack in bytes. If 0 the size is not known and will
  112. * be determined on first read.
  113. * @return {@code this}
  114. */
  115. public DfsPackDescription setPackSize(long bytes) {
  116. packSize = Math.max(0, bytes);
  117. return this;
  118. }
  119. /**
  120. * @return size of the index, in bytes. If 0 the index size is not yet
  121. * known.
  122. */
  123. public long getIndexSize() {
  124. return indexSize;
  125. }
  126. /**
  127. * @param bytes
  128. * size of the index in bytes. If 0 the size is not known and
  129. * will be determined on first read.
  130. * @return {@code this}
  131. */
  132. public DfsPackDescription setIndexSize(long bytes) {
  133. indexSize = Math.max(0, bytes);
  134. return this;
  135. }
  136. /** @return number of objects in the pack. */
  137. public long getObjectCount() {
  138. return objectCount;
  139. }
  140. /**
  141. * @param cnt
  142. * number of objects in the pack.
  143. * @return {@code this}
  144. */
  145. public DfsPackDescription setObjectCount(long cnt) {
  146. objectCount = Math.max(0, cnt);
  147. return this;
  148. }
  149. /** @return number of delta compressed objects in the pack. */
  150. public long getDeltaCount() {
  151. return deltaCount;
  152. }
  153. /**
  154. * @param cnt
  155. * number of delta compressed objects in the pack.
  156. * @return {@code this}
  157. */
  158. public DfsPackDescription setDeltaCount(long cnt) {
  159. deltaCount = Math.max(0, cnt);
  160. return this;
  161. }
  162. /** @return the tips that created this pack, if known. */
  163. public Set<ObjectId> getTips() {
  164. return tips;
  165. }
  166. /**
  167. * @param tips
  168. * the tips of the pack, null if it has no known tips.
  169. * @return {@code this}
  170. */
  171. public DfsPackDescription setTips(Set<ObjectId> tips) {
  172. this.tips = tips;
  173. return this;
  174. }
  175. /**
  176. * @return statistics from PackWriter, if the pack was built with it.
  177. * Generally this is only available for packs created by
  178. * DfsGarbageCollector or DfsPackCompactor, and only when the pack
  179. * is being committed to the repository.
  180. */
  181. public PackWriter.Statistics getPackStats() {
  182. return stats;
  183. }
  184. DfsPackDescription setPackStats(PackWriter.Statistics stats) {
  185. this.stats = stats;
  186. return this;
  187. }
  188. /**
  189. * Discard the pack statistics, if it was populated.
  190. *
  191. * @return {@code this}
  192. */
  193. public DfsPackDescription clearPackStats() {
  194. stats = null;
  195. return this;
  196. }
  197. @Override
  198. public int hashCode() {
  199. return getPackName().hashCode();
  200. }
  201. @Override
  202. public boolean equals(Object b) {
  203. if (b instanceof DfsPackDescription)
  204. return getPackName().equals(((DfsPackDescription) b).getPackName());
  205. return false;
  206. }
  207. /**
  208. * Sort packs according to the optimal lookup ordering.
  209. * <p>
  210. * This method tries to position packs in the order readers should examine
  211. * them when looking for objects by SHA-1. The default tries to sort packs
  212. * with more recent modification dates before older packs, and packs with
  213. * fewer objects before packs with more objects.
  214. *
  215. * @param b
  216. * the other pack.
  217. */
  218. public int compareTo(DfsPackDescription b) {
  219. // Newer packs should sort first.
  220. int cmp = Long.signum(b.getLastModified() - getLastModified());
  221. if (cmp != 0)
  222. return cmp;
  223. // Break ties on smaller index. Readers may get lucky and find
  224. // the object they care about in the smaller index. This also pushes
  225. // big historical packs to the end of the list, due to more objects.
  226. return Long.signum(getObjectCount() - b.getObjectCount());
  227. }
  228. @Override
  229. public String toString() {
  230. return getPackName();
  231. }
  232. }