Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DeltaBaseCache.java 4.9KB

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