Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DhtCachedPack.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Clear the recent chunks because all of the reader's
  114. // chunk limit should be made available for prefetch.
  115. int cacheLimit = ctx.getOptions().getChunkLimit();
  116. ctx.getRecentChunks().setMaxBytes(0);
  117. try {
  118. Prefetcher p = new Prefetcher(ctx, 0, cacheLimit);
  119. p.push(Arrays.asList(keyList));
  120. copyPack(out, p, validate);
  121. } finally {
  122. ctx.getRecentChunks().setMaxBytes(cacheLimit);
  123. }
  124. }
  125. private void copyPack(PackOutputStream out, Prefetcher prefetcher,
  126. boolean validate) throws DhtException, DhtMissingChunkException,
  127. IOException {
  128. Map<String, Long> startsAt = new HashMap<String, Long>();
  129. for (ChunkKey key : keyList) {
  130. PackChunk chunk = prefetcher.get(key);
  131. // The prefetcher should always produce the chunk for us, if not
  132. // there is something seriously wrong with the ordering or
  133. // within the prefetcher code and aborting is more sane than
  134. // using slow synchronous lookups.
  135. //
  136. if (chunk == null)
  137. throw new DhtMissingChunkException(key);
  138. // Verify each long OFS_DELTA chunk appears at the right offset.
  139. // This is a cheap validation that the cached pack hasn't been
  140. // incorrectly created and would confuse the client.
  141. //
  142. long position = out.length();
  143. ChunkMeta meta = chunk.getMeta();
  144. if (meta != null && meta.getBaseChunkCount() != 0) {
  145. for (ChunkMeta.BaseChunk base : meta.getBaseChunkList()) {
  146. Long act = startsAt.get(base.getChunkKey());
  147. long exp = position - base.getRelativeStart();
  148. if (act == null) {
  149. throw new DhtException(MessageFormat.format(DhtText
  150. .get().wrongChunkPositionInCachedPack,
  151. rowKey(), base.getChunkKey(),
  152. "[not written]", key, Long.valueOf(exp)));
  153. }
  154. if (act.longValue() != exp) {
  155. throw new DhtException(MessageFormat.format(DhtText
  156. .get().wrongChunkPositionInCachedPack,
  157. rowKey(), base.getChunkKey(),
  158. act, key, Long.valueOf(exp)));
  159. }
  160. }
  161. }
  162. startsAt.put(key.asString(), Long.valueOf(position));
  163. chunk.copyEntireChunkAsIs(out, null, validate);
  164. }
  165. }
  166. private String rowKey() {
  167. return info.getName() + "." + info.getVersion();
  168. }
  169. }