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.

OpenQueue.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.Iterator;
  49. import java.util.LinkedHashMap;
  50. import java.util.Map;
  51. import org.eclipse.jgit.errors.MissingObjectException;
  52. import org.eclipse.jgit.lib.AsyncObjectLoaderQueue;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.ObjectLoader;
  55. import org.eclipse.jgit.lib.ObjectReader;
  56. /**
  57. * Locates objects in large batches, then opens them clustered by chunk.
  58. * <p>
  59. * To simplify the implementation this method performs lookups for the
  60. * {@link ObjectInfo} in large batches, clusters those by ChunkKey, and loads
  61. * the chunks with a {@link Prefetcher}.
  62. * <p>
  63. * The lookup queue is completely spun out during the first invocation of
  64. * {@link #next()}, ensuring all chunks are known before any single chunk is
  65. * accessed. This is necessary to improve access locality and prevent thrashing
  66. * of the local ChunkCache. It also causes {@link MissingObjectException} to be
  67. * thrown at the start of traversal, until the lookup queue is exhausted.
  68. *
  69. * @param <T>
  70. * type of object to associate with the loader.
  71. */
  72. final class OpenQueue<T extends ObjectId> extends QueueObjectLookup<T>
  73. implements AsyncObjectLoaderQueue<T> {
  74. private Map<ChunkKey, Collection<ObjectWithInfo<T>>> byChunk;
  75. private Iterator<Collection<ObjectWithInfo<T>>> chunkItr;
  76. private Iterator<ObjectWithInfo<T>> objectItr;
  77. private Prefetcher prefetcher;
  78. private ObjectWithInfo<T> current;
  79. private PackChunk currChunk;
  80. OpenQueue(DhtReader reader, Iterable<T> objectIds, boolean reportMissing) {
  81. super(reader, reportMissing);
  82. setCacheLoadedInfo(true);
  83. setNeedChunkOnly(true);
  84. init(objectIds);
  85. byChunk = new LinkedHashMap<ChunkKey, Collection<ObjectWithInfo<T>>>();
  86. objectItr = Collections.<ObjectWithInfo<T>> emptyList().iterator();
  87. }
  88. public boolean next() throws MissingObjectException, IOException {
  89. if (chunkItr == null)
  90. init();
  91. if (!objectItr.hasNext()) {
  92. currChunk = null;
  93. if (!chunkItr.hasNext()) {
  94. release();
  95. return false;
  96. }
  97. objectItr = chunkItr.next().iterator();
  98. }
  99. current = objectItr.next();
  100. return true;
  101. }
  102. public T getCurrent() {
  103. return current.object;
  104. }
  105. public ObjectId getObjectId() {
  106. return getCurrent();
  107. }
  108. public ObjectLoader open() throws IOException {
  109. ChunkKey chunkKey = current.chunkKey;
  110. // Objects returned by the queue are clustered by chunk. This object
  111. // is either in the current chunk, or are the next chunk ready on the
  112. // prefetcher. Anything else is a programming error.
  113. //
  114. PackChunk chunk;
  115. if (currChunk != null && chunkKey.equals(currChunk.getChunkKey()))
  116. chunk = currChunk;
  117. else {
  118. chunk = prefetcher.get(chunkKey);
  119. if (chunk == null)
  120. throw new DhtMissingChunkException(chunkKey);
  121. currChunk = chunk;
  122. reader.recentChunk(chunk);
  123. }
  124. if (current.info != null) {
  125. int ptr = current.info.getOffset();
  126. int type = current.info.getType();
  127. return PackChunk.read(chunk, ptr, reader, type);
  128. } else {
  129. int ptr = chunk.findOffset(repo, current.object);
  130. if (ptr < 0)
  131. throw DhtReader.missing(current.object, ObjectReader.OBJ_ANY);
  132. return PackChunk.read(chunk, ptr, reader, ObjectReader.OBJ_ANY);
  133. }
  134. }
  135. @Override
  136. public boolean cancel(boolean mayInterruptIfRunning) {
  137. release();
  138. return true;
  139. }
  140. @Override
  141. public void release() {
  142. reader.getRecentChunks().setMaxBytes(reader.getOptions().getChunkLimit());
  143. prefetcher = null;
  144. currChunk = null;
  145. }
  146. private void init() throws IOException {
  147. ObjectWithInfo<T> c;
  148. while ((c = nextObjectWithInfo()) != null) {
  149. ChunkKey chunkKey = c.chunkKey;
  150. Collection<ObjectWithInfo<T>> list = byChunk.get(chunkKey);
  151. if (list == null) {
  152. list = new ArrayList<ObjectWithInfo<T>>();
  153. byChunk.put(chunkKey, list);
  154. if (prefetcher == null) {
  155. int limit = reader.getOptions().getChunkLimit();
  156. int ratio = reader.getOptions().getOpenQueuePrefetchRatio();
  157. int prefetchLimit = (int) (limit * (ratio / 100.0));
  158. reader.getRecentChunks().setMaxBytes(limit - prefetchLimit);
  159. prefetcher = new Prefetcher(reader, 0, prefetchLimit);
  160. }
  161. prefetcher.push(chunkKey);
  162. }
  163. list.add(c);
  164. }
  165. chunkItr = byChunk.values().iterator();
  166. }
  167. }