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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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.internal.storage.file;
  44. import java.lang.ref.SoftReference;
  45. import org.eclipse.jgit.storage.file.WindowCacheConfig;
  46. class DeltaBaseCache {
  47. private static final int CACHE_SZ = 1024;
  48. static final SoftReference<Entry> DEAD;
  49. private static int hash(long position) {
  50. return (((int) position) << 22) >>> 22;
  51. }
  52. private static volatile int defaultMaxByteCount;
  53. private final int maxByteCount;
  54. private final Slot[] cache;
  55. private Slot lruHead;
  56. private Slot lruTail;
  57. private int openByteCount;
  58. static {
  59. DEAD = new SoftReference<>(null);
  60. reconfigure(new WindowCacheConfig());
  61. }
  62. static void reconfigure(WindowCacheConfig cfg) {
  63. defaultMaxByteCount = cfg.getDeltaBaseCacheLimit();
  64. }
  65. DeltaBaseCache() {
  66. maxByteCount = defaultMaxByteCount;
  67. cache = new Slot[CACHE_SZ];
  68. }
  69. Entry get(PackFile pack, long position) {
  70. Slot e = cache[hash(position)];
  71. if (e == null)
  72. return null;
  73. if (e.provider == pack && e.position == position) {
  74. final Entry buf = e.data.get();
  75. if (buf != null) {
  76. moveToHead(e);
  77. return buf;
  78. }
  79. }
  80. return null;
  81. }
  82. void store(final PackFile pack, final long position,
  83. final byte[] data, final int objectType) {
  84. if (data.length > maxByteCount)
  85. return; // Too large to cache.
  86. Slot e = cache[hash(position)];
  87. if (e == null) {
  88. e = new Slot();
  89. cache[hash(position)] = e;
  90. } else {
  91. clearEntry(e);
  92. }
  93. openByteCount += data.length;
  94. releaseMemory();
  95. e.provider = pack;
  96. e.position = position;
  97. e.sz = data.length;
  98. e.data = new SoftReference<>(new Entry(data, objectType));
  99. moveToHead(e);
  100. }
  101. private void releaseMemory() {
  102. while (openByteCount > maxByteCount && lruTail != null) {
  103. final Slot currOldest = lruTail;
  104. final Slot nextOldest = currOldest.lruPrev;
  105. clearEntry(currOldest);
  106. currOldest.lruPrev = null;
  107. currOldest.lruNext = null;
  108. if (nextOldest == null)
  109. lruHead = null;
  110. else
  111. nextOldest.lruNext = null;
  112. lruTail = nextOldest;
  113. }
  114. }
  115. private void moveToHead(Slot e) {
  116. unlink(e);
  117. e.lruPrev = null;
  118. e.lruNext = lruHead;
  119. if (lruHead != null)
  120. lruHead.lruPrev = e;
  121. else
  122. lruTail = e;
  123. lruHead = e;
  124. }
  125. private void unlink(Slot e) {
  126. final Slot prev = e.lruPrev;
  127. final Slot next = e.lruNext;
  128. if (prev != null)
  129. prev.lruNext = next;
  130. if (next != null)
  131. next.lruPrev = prev;
  132. }
  133. private void clearEntry(Slot e) {
  134. openByteCount -= e.sz;
  135. e.provider = null;
  136. e.data = DEAD;
  137. e.sz = 0;
  138. }
  139. static class Entry {
  140. final byte[] data;
  141. final int type;
  142. Entry(byte[] aData, int aType) {
  143. data = aData;
  144. type = aType;
  145. }
  146. }
  147. private static class Slot {
  148. Slot lruPrev;
  149. Slot lruNext;
  150. PackFile provider;
  151. long position;
  152. int sz;
  153. SoftReference<Entry> data = DEAD;
  154. }
  155. }