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.

DeltaBaseCache.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.lang.ref.SoftReference;
  45. /**
  46. * Caches recently used objects for {@link DhtReader}.
  47. * <p>
  48. * This cache is not thread-safe. Each reader should have its own cache.
  49. */
  50. final class DeltaBaseCache {
  51. private final DhtReader.Statistics stats;
  52. private int maxByteCount;
  53. private final Slot[] table;
  54. private Slot lruHead;
  55. private Slot lruTail;
  56. private int curByteCount;
  57. DeltaBaseCache(DhtReader reader) {
  58. stats = reader.getStatistics();
  59. DhtReaderOptions options = reader.getOptions();
  60. maxByteCount = options.getDeltaBaseCacheLimit();
  61. table = new Slot[options.getDeltaBaseCacheSize()];
  62. }
  63. Entry get(ChunkKey key, int position) {
  64. Slot e = table[hash(key, position)];
  65. for (; e != null; e = e.tableNext) {
  66. if (e.offset == position && key.equals(e.chunkKey)) {
  67. Entry buf = e.data.get();
  68. if (buf != null) {
  69. moveToHead(e);
  70. stats.deltaBaseCache_Hits++;
  71. return buf;
  72. }
  73. }
  74. }
  75. stats.deltaBaseCache_Miss++;
  76. return null;
  77. }
  78. void put(ChunkKey key, int offset, int objectType, byte[] data) {
  79. if (data.length > maxByteCount)
  80. return; // Too large to cache.
  81. curByteCount += data.length;
  82. releaseMemory();
  83. int tableIdx = hash(key, offset);
  84. Slot e = new Slot(key, offset, data.length);
  85. e.data = new SoftReference<Entry>(new Entry(data, objectType));
  86. e.tableNext = table[tableIdx];
  87. table[tableIdx] = e;
  88. moveToHead(e);
  89. }
  90. private void releaseMemory() {
  91. while (curByteCount > maxByteCount && lruTail != null) {
  92. Slot currOldest = lruTail;
  93. Slot nextOldest = currOldest.lruPrev;
  94. curByteCount -= currOldest.size;
  95. unlink(currOldest);
  96. removeFromTable(currOldest);
  97. if (nextOldest == null)
  98. lruHead = null;
  99. else
  100. nextOldest.lruNext = null;
  101. lruTail = nextOldest;
  102. }
  103. }
  104. private void removeFromTable(Slot e) {
  105. int tableIdx = hash(e.chunkKey, e.offset);
  106. Slot p = table[tableIdx];
  107. if (p == e) {
  108. table[tableIdx] = e.tableNext;
  109. return;
  110. }
  111. for (; p != null; p = p.tableNext) {
  112. if (p.tableNext == e) {
  113. p.tableNext = e.tableNext;
  114. return;
  115. }
  116. }
  117. }
  118. private void moveToHead(final Slot e) {
  119. unlink(e);
  120. e.lruPrev = null;
  121. e.lruNext = lruHead;
  122. if (lruHead != null)
  123. lruHead.lruPrev = e;
  124. else
  125. lruTail = e;
  126. lruHead = e;
  127. }
  128. private void unlink(final Slot e) {
  129. Slot prev = e.lruPrev;
  130. Slot next = e.lruNext;
  131. if (prev != null)
  132. prev.lruNext = next;
  133. if (next != null)
  134. next.lruPrev = prev;
  135. }
  136. private int hash(ChunkKey key, int position) {
  137. return (((key.hashCode() & 0xfffff000) + position) >>> 1) % table.length;
  138. }
  139. static class Entry {
  140. final byte[] data;
  141. final int type;
  142. Entry(final byte[] aData, final int aType) {
  143. data = aData;
  144. type = aType;
  145. }
  146. }
  147. private static class Slot {
  148. final ChunkKey chunkKey;
  149. final int offset;
  150. final int size;
  151. Slot tableNext;
  152. Slot lruPrev;
  153. Slot lruNext;
  154. SoftReference<Entry> data;
  155. Slot(ChunkKey key, int offset, int size) {
  156. this.chunkKey = key;
  157. this.offset = offset;
  158. this.size = size;
  159. }
  160. }
  161. }