Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.dht;
  44. import java.util.Collections;
  45. import java.util.Comparator;
  46. import java.util.Date;
  47. import java.util.List;
  48. import org.eclipse.jgit.generated.storage.dht.proto.GitStore;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. /** Connects an object to the chunk it is stored in. */
  51. public class ObjectInfo {
  52. /** Orders ObjectInfo by their time member, oldest first. */
  53. public static final Comparator<ObjectInfo> BY_TIME = new Comparator<ObjectInfo>() {
  54. public int compare(ObjectInfo a, ObjectInfo b) {
  55. return Long.signum(a.getTime() - b.getTime());
  56. }
  57. };
  58. /**
  59. * Sort the info list according to time, oldest member first.
  60. *
  61. * @param toSort
  62. * list to sort.
  63. */
  64. public static void sort(List<ObjectInfo> toSort) {
  65. Collections.sort(toSort, BY_TIME);
  66. }
  67. private final ChunkKey chunk;
  68. private final long time;
  69. private final GitStore.ObjectInfo data;
  70. /**
  71. * Wrap an ObjectInfo from the storage system.
  72. *
  73. * @param chunkKey
  74. * the chunk the object points to.
  75. * @param data
  76. * the data of the ObjectInfo.
  77. */
  78. public ObjectInfo(ChunkKey chunkKey, GitStore.ObjectInfo data) {
  79. this.chunk = chunkKey;
  80. this.time = 0;
  81. this.data = data;
  82. }
  83. /**
  84. * Wrap an ObjectInfo from the storage system.
  85. *
  86. * @param chunkKey
  87. * the chunk the object points to.
  88. * @param time
  89. * timestamp of the ObjectInfo.
  90. * @param data
  91. * the data of the ObjectInfo.
  92. */
  93. public ObjectInfo(ChunkKey chunkKey, long time, GitStore.ObjectInfo data) {
  94. this.chunk = chunkKey;
  95. this.time = time < 0 ? 0 : time;
  96. this.data = data;
  97. }
  98. /** @return the chunk this link points to. */
  99. public ChunkKey getChunkKey() {
  100. return chunk;
  101. }
  102. /** @return approximate time the object was created, in milliseconds. */
  103. public long getTime() {
  104. return time;
  105. }
  106. /** @return GitStore.ObjectInfo to embed in the database. */
  107. public GitStore.ObjectInfo getData() {
  108. return data;
  109. }
  110. /** @return type of the object, in OBJ_* constants. */
  111. public int getType() {
  112. return data.getObjectType().getNumber();
  113. }
  114. /** @return size of the object when fully inflated. */
  115. public long getSize() {
  116. return data.getInflatedSize();
  117. }
  118. /** @return true if the object storage uses delta compression. */
  119. public boolean isDelta() {
  120. return data.hasDeltaBase();
  121. }
  122. /** @return true if the object has been fragmented across chunks. */
  123. public boolean isFragmented() {
  124. return data.getIsFragmented();
  125. }
  126. int getOffset() {
  127. return data.getOffset();
  128. }
  129. long getPackedSize() {
  130. return data.getPackedSize();
  131. }
  132. ObjectId getDeltaBase() {
  133. if (data.hasDeltaBase())
  134. return ObjectId.fromRaw(data.getDeltaBase().toByteArray(), 0);
  135. return null;
  136. }
  137. @Override
  138. public String toString() {
  139. StringBuilder b = new StringBuilder();
  140. b.append("ObjectInfo:");
  141. b.append(chunk);
  142. if (0 < time)
  143. b.append(" @ ").append(new Date(time));
  144. b.append("\n");
  145. b.append(data.toString());
  146. return b.toString();
  147. }
  148. }