Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DeltaBaseCache.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 volatile int defaultMaxByteCount;
  52. private final int maxByteCount;
  53. private final Slot[] cache;
  54. private Slot lruHead;
  55. private Slot lruTail;
  56. private int openByteCount;
  57. static {
  58. DEAD = new SoftReference<Entry>(null);
  59. reconfigure(new WindowCacheConfig());
  60. }
  61. static void reconfigure(WindowCacheConfig cfg) {
  62. defaultMaxByteCount = cfg.getDeltaBaseCacheLimit();
  63. }
  64. DeltaBaseCache() {
  65. maxByteCount = defaultMaxByteCount;
  66. cache = new Slot[CACHE_SZ];
  67. }
  68. Entry get(final PackFile pack, final long position) {
  69. Slot e = cache[hash(position)];
  70. if (e == null)
  71. return null;
  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. 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. Slot e = cache[hash(position)];
  86. if (e == null) {
  87. e = new Slot();
  88. cache[hash(position)] = e;
  89. } else {
  90. clearEntry(e);
  91. }
  92. openByteCount += data.length;
  93. releaseMemory();
  94. e.provider = pack;
  95. e.position = position;
  96. e.sz = data.length;
  97. e.data = new SoftReference<Entry>(new Entry(data, objectType));
  98. moveToHead(e);
  99. }
  100. private void releaseMemory() {
  101. while (openByteCount > maxByteCount && lruTail != null) {
  102. final Slot currOldest = lruTail;
  103. final Slot nextOldest = currOldest.lruPrev;
  104. clearEntry(currOldest);
  105. currOldest.lruPrev = null;
  106. currOldest.lruNext = null;
  107. if (nextOldest == null)
  108. lruHead = null;
  109. else
  110. nextOldest.lruNext = null;
  111. lruTail = nextOldest;
  112. }
  113. }
  114. private void moveToHead(final Slot e) {
  115. unlink(e);
  116. e.lruPrev = null;
  117. e.lruNext = lruHead;
  118. if (lruHead != null)
  119. lruHead.lruPrev = e;
  120. else
  121. lruTail = e;
  122. lruHead = e;
  123. }
  124. private void unlink(final Slot e) {
  125. final Slot prev = e.lruPrev;
  126. final Slot next = e.lruNext;
  127. if (prev != null)
  128. prev.lruNext = next;
  129. if (next != null)
  130. next.lruPrev = prev;
  131. }
  132. private void clearEntry(final Slot e) {
  133. openByteCount -= e.sz;
  134. e.provider = null;
  135. e.data = DEAD;
  136. e.sz = 0;
  137. }
  138. static class Entry {
  139. final byte[] data;
  140. final int type;
  141. Entry(final byte[] aData, final int aType) {
  142. data = aData;
  143. type = aType;
  144. }
  145. }
  146. private static class Slot {
  147. Slot lruPrev;
  148. Slot lruNext;
  149. PackFile provider;
  150. long position;
  151. int sz;
  152. SoftReference<Entry> data = DEAD;
  153. }
  154. }