View Javadoc
1   /*
2    * Copyright (C) 2010, 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  
44  package org.eclipse.jgit.internal.storage.pack;
45  
46  import java.lang.ref.ReferenceQueue;
47  import java.lang.ref.SoftReference;
48  
49  import org.eclipse.jgit.storage.pack.PackConfig;
50  
51  class DeltaCache {
52  	private final long size;
53  
54  	private final int entryLimit;
55  
56  	private final ReferenceQueue<byte[]> queue;
57  
58  	private long used;
59  
60  	DeltaCache(PackConfig pc) {
61  		size = pc.getDeltaCacheSize();
62  		entryLimit = pc.getDeltaCacheLimit();
63  		queue = new ReferenceQueue<>();
64  	}
65  
66  	boolean canCache(int length, ObjectToPack src, ObjectToPack res) {
67  		// If the cache would overflow, don't store.
68  		//
69  		if (0 < size && size < used + length) {
70  			checkForGarbageCollectedObjects();
71  			if (0 < size && size < used + length)
72  				return false;
73  		}
74  
75  		if (length < entryLimit) {
76  			used += length;
77  			return true;
78  		}
79  
80  		// If the combined source files are multiple megabytes but the delta
81  		// is on the order of a kilobyte or two, this was likely costly to
82  		// construct. Cache it anyway, even though its over the limit.
83  		//
84  		if (length >> 10 < (src.getWeight() >> 20) + (res.getWeight() >> 21)) {
85  			used += length;
86  			return true;
87  		}
88  
89  		return false;
90  	}
91  
92  	void credit(int reservedSize) {
93  		used -= reservedSize;
94  	}
95  
96  	Ref cache(byte[] data, int actLen, int reservedSize) {
97  		// The caller may have had to allocate more space than is
98  		// required. If we are about to waste anything, shrink it.
99  		//
100 		data = resize(data, actLen);
101 
102 		// When we reserved space for this item we did it for the
103 		// inflated size of the delta, but we were just given the
104 		// compressed version. Adjust the cache cost to match.
105 		//
106 		if (reservedSize != data.length) {
107 			used -= reservedSize;
108 			used += data.length;
109 		}
110 		return new Ref(data, queue);
111 	}
112 
113 	byte[] resize(byte[] data, int actLen) {
114 		if (data.length != actLen) {
115 			byte[] nbuf = new byte[actLen];
116 			System.arraycopy(data, 0, nbuf, 0, actLen);
117 			data = nbuf;
118 		}
119 		return data;
120 	}
121 
122 	private void checkForGarbageCollectedObjects() {
123 		Ref r;
124 		while ((r = (Ref) queue.poll()) != null)
125 			used -= r.cost;
126 	}
127 
128 	static class Ref extends SoftReference<byte[]> {
129 		final int cost;
130 
131 		Ref(byte[] array, ReferenceQueue<byte[]> queue) {
132 			super(array, queue);
133 			cost = array.length;
134 		}
135 	}
136 }