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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.io.IOException;
  45. import java.text.MessageFormat;
  46. import java.util.Arrays;
  47. import java.util.Collections;
  48. import java.util.HashMap;
  49. import java.util.HashSet;
  50. import java.util.Map;
  51. import java.util.Set;
  52. import org.eclipse.jgit.generated.storage.dht.proto.GitStore.CachedPackInfo;
  53. import org.eclipse.jgit.generated.storage.dht.proto.GitStore.CachedPackInfo.ChunkList;
  54. import org.eclipse.jgit.generated.storage.dht.proto.GitStore.ChunkMeta;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.storage.pack.CachedPack;
  57. import org.eclipse.jgit.storage.pack.ObjectToPack;
  58. import org.eclipse.jgit.storage.pack.PackOutputStream;
  59. import org.eclipse.jgit.storage.pack.StoredObjectRepresentation;
  60. /** A cached pack stored by the DHT. */
  61. public class DhtCachedPack extends CachedPack {
  62. private final CachedPackInfo info;
  63. private Set<ObjectId> tips;
  64. private Set<ChunkKey> keySet;
  65. private ChunkKey[] keyList;
  66. DhtCachedPack(CachedPackInfo info) {
  67. this.info = info;
  68. }
  69. @Override
  70. public Set<ObjectId> getTips() {
  71. if (tips == null) {
  72. tips = new HashSet<ObjectId>();
  73. for (String idString : info.getTipList().getObjectNameList())
  74. tips.add(ObjectId.fromString(idString));
  75. tips = Collections.unmodifiableSet(tips);
  76. }
  77. return tips;
  78. }
  79. @Override
  80. public long getObjectCount() {
  81. return info.getObjectsTotal();
  82. }
  83. @Override
  84. public long getDeltaCount() throws IOException {
  85. return info.getObjectsDelta();
  86. }
  87. /** @return information describing this cached pack. */
  88. public CachedPackInfo getCachedPackInfo() {
  89. return info;
  90. }
  91. @Override
  92. public boolean hasObject(ObjectToPack obj, StoredObjectRepresentation rep) {
  93. DhtObjectRepresentation objrep = (DhtObjectRepresentation) rep;
  94. if (keySet == null)
  95. init();
  96. return keySet.contains(objrep.getChunkKey());
  97. }
  98. private void init() {
  99. ChunkList chunkList = info.getChunkList();
  100. int cnt = chunkList.getChunkKeyCount();
  101. keySet = new HashSet<ChunkKey>();
  102. keyList = new ChunkKey[cnt];
  103. for (int i = 0; i < cnt; i++) {
  104. ChunkKey key = ChunkKey.fromString(chunkList.getChunkKey(i));
  105. keySet.add(key);
  106. keyList[i] = key;
  107. }
  108. }
  109. void copyAsIs(PackOutputStream out, boolean validate, DhtReader ctx)
  110. throws IOException {
  111. if (keyList == null)
  112. init();
  113. Prefetcher p = new Prefetcher(ctx, 0);
  114. p.push(Arrays.asList(keyList));
  115. copyPack(out, p, validate);
  116. }
  117. private void copyPack(PackOutputStream out, Prefetcher prefetcher,
  118. boolean validate) throws DhtException, DhtMissingChunkException,
  119. IOException {
  120. Map<String, Long> startsAt = new HashMap<String, Long>();
  121. for (ChunkKey key : keyList) {
  122. PackChunk chunk = prefetcher.get(key);
  123. // The prefetcher should always produce the chunk for us, if not
  124. // there is something seriously wrong with the ordering or
  125. // within the prefetcher code and aborting is more sane than
  126. // using slow synchronous lookups.
  127. //
  128. if (chunk == null)
  129. throw new DhtMissingChunkException(key);
  130. // Verify each long OFS_DELTA chunk appears at the right offset.
  131. // This is a cheap validation that the cached pack hasn't been
  132. // incorrectly created and would confuse the client.
  133. //
  134. long position = out.length();
  135. ChunkMeta meta = chunk.getMeta();
  136. if (meta != null && meta.getBaseChunkCount() != 0) {
  137. for (ChunkMeta.BaseChunk base : meta.getBaseChunkList()) {
  138. Long act = startsAt.get(base.getChunkKey());
  139. long exp = position - base.getRelativeStart();
  140. if (act == null) {
  141. throw new DhtException(MessageFormat.format(DhtText
  142. .get().wrongChunkPositionInCachedPack,
  143. rowKey(), base.getChunkKey(),
  144. "[not written]", key, Long.valueOf(exp)));
  145. }
  146. if (act.longValue() != exp) {
  147. throw new DhtException(MessageFormat.format(DhtText
  148. .get().wrongChunkPositionInCachedPack,
  149. rowKey(), base.getChunkKey(),
  150. act, key, Long.valueOf(exp)));
  151. }
  152. }
  153. }
  154. startsAt.put(key.asString(), Long.valueOf(position));
  155. chunk.copyEntireChunkAsIs(out, null, validate);
  156. }
  157. }
  158. private String rowKey() {
  159. return info.getName() + "." + info.getVersion();
  160. }
  161. }