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.

RecentChunks.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.HashMap;
  46. import org.eclipse.jgit.lib.AnyObjectId;
  47. import org.eclipse.jgit.lib.ObjectLoader;
  48. import org.eclipse.jgit.storage.dht.DhtReader.ChunkAndOffset;
  49. import org.eclipse.jgit.storage.dht.RefDataUtil.IdWithChunk;
  50. final class RecentChunks {
  51. private final DhtReader reader;
  52. private final DhtReader.Statistics stats;
  53. private final HashMap<ChunkKey, Node> byKey;
  54. private int maxBytes;
  55. private int curBytes;
  56. private Node lruHead;
  57. private Node lruTail;
  58. RecentChunks(DhtReader reader) {
  59. this.reader = reader;
  60. this.stats = reader.getStatistics();
  61. this.byKey = new HashMap<ChunkKey, Node>();
  62. this.maxBytes = reader.getOptions().getChunkLimit();
  63. }
  64. void setMaxBytes(int newMax) {
  65. maxBytes = Math.max(0, newMax);
  66. if (0 < maxBytes)
  67. prune();
  68. else
  69. clear();
  70. }
  71. PackChunk get(ChunkKey key) {
  72. Node n = byKey.get(key);
  73. if (n != null) {
  74. hit(n);
  75. stats.recentChunks_Hits++;
  76. return n.chunk;
  77. }
  78. stats.recentChunks_Miss++;
  79. return null;
  80. }
  81. void put(PackChunk chunk) {
  82. Node n = byKey.get(chunk.getChunkKey());
  83. if (n != null && n.chunk == chunk) {
  84. hit(n);
  85. return;
  86. }
  87. curBytes += chunk.getTotalSize();
  88. prune();
  89. n = new Node();
  90. n.chunk = chunk;
  91. byKey.put(chunk.getChunkKey(), n);
  92. first(n);
  93. }
  94. private void prune() {
  95. while (maxBytes < curBytes) {
  96. Node n = lruTail;
  97. if (n == null)
  98. break;
  99. PackChunk c = n.chunk;
  100. curBytes -= c.getTotalSize();
  101. byKey.remove(c.getChunkKey());
  102. remove(n);
  103. }
  104. }
  105. ObjectLoader open(RepositoryKey repo, AnyObjectId objId, int typeHint)
  106. throws IOException {
  107. if (objId instanceof IdWithChunk) {
  108. PackChunk chunk = get(((IdWithChunk) objId).getChunkKey());
  109. if (chunk != null) {
  110. int pos = chunk.findOffset(repo, objId);
  111. if (0 <= pos)
  112. return PackChunk.read(chunk, pos, reader, typeHint);
  113. }
  114. // IdWithChunk is only a hint, and can be wrong. Locally
  115. // searching is faster than looking in the Database.
  116. }
  117. for (Node n = lruHead; n != null; n = n.next) {
  118. int pos = n.chunk.findOffset(repo, objId);
  119. if (0 <= pos) {
  120. hit(n);
  121. stats.recentChunks_Hits++;
  122. return PackChunk.read(n.chunk, pos, reader, typeHint);
  123. }
  124. }
  125. return null;
  126. }
  127. ChunkAndOffset find(RepositoryKey repo, AnyObjectId objId) {
  128. if (objId instanceof IdWithChunk) {
  129. PackChunk chunk = get(((IdWithChunk) objId).getChunkKey());
  130. if (chunk != null) {
  131. int pos = chunk.findOffset(repo, objId);
  132. if (0 <= pos)
  133. return new ChunkAndOffset(chunk, pos);
  134. }
  135. // IdWithChunk is only a hint, and can be wrong. Locally
  136. // searching is faster than looking in the Database.
  137. }
  138. for (Node n = lruHead; n != null; n = n.next) {
  139. int pos = n.chunk.findOffset(repo, objId);
  140. if (0 <= pos) {
  141. hit(n);
  142. stats.recentChunks_Hits++;
  143. return new ChunkAndOffset(n.chunk, pos);
  144. }
  145. }
  146. return null;
  147. }
  148. boolean has(RepositoryKey repo, AnyObjectId objId) {
  149. for (Node n = lruHead; n != null; n = n.next) {
  150. int pos = n.chunk.findOffset(repo, objId);
  151. if (0 <= pos) {
  152. hit(n);
  153. stats.recentChunks_Hits++;
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. void clear() {
  160. curBytes = 0;
  161. lruHead = null;
  162. lruTail = null;
  163. byKey.clear();
  164. }
  165. private void hit(Node n) {
  166. if (lruHead != n) {
  167. remove(n);
  168. first(n);
  169. }
  170. }
  171. private void remove(Node node) {
  172. Node p = node.prev;
  173. Node n = node.next;
  174. if (p != null)
  175. p.next = n;
  176. if (n != null)
  177. n.prev = p;
  178. if (lruHead == node)
  179. lruHead = n;
  180. if (lruTail == node)
  181. lruTail = p;
  182. }
  183. private void first(Node node) {
  184. Node h = lruHead;
  185. node.prev = null;
  186. node.next = h;
  187. if (h != null)
  188. h.prev = node;
  189. else
  190. lruTail = node;
  191. lruHead = node;
  192. }
  193. private static class Node {
  194. PackChunk chunk;
  195. Node prev;
  196. Node next;
  197. }
  198. }