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