View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.internal.storage.file;
46  
47  import java.io.IOException;
48  import java.util.Collection;
49  import java.util.Collections;
50  import java.util.HashSet;
51  import java.util.List;
52  import java.util.Set;
53  import java.util.zip.DataFormatException;
54  import java.util.zip.Inflater;
55  
56  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
57  import org.eclipse.jgit.errors.MissingObjectException;
58  import org.eclipse.jgit.errors.StoredObjectRepresentationNotAvailableException;
59  import org.eclipse.jgit.internal.JGitText;
60  import org.eclipse.jgit.internal.storage.pack.CachedPack;
61  import org.eclipse.jgit.internal.storage.pack.ObjectReuseAsIs;
62  import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
63  import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
64  import org.eclipse.jgit.internal.storage.pack.PackWriter;
65  import org.eclipse.jgit.lib.AbbreviatedObjectId;
66  import org.eclipse.jgit.lib.AnyObjectId;
67  import org.eclipse.jgit.lib.BitmapIndex;
68  import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
69  import org.eclipse.jgit.lib.Constants;
70  import org.eclipse.jgit.lib.InflaterCache;
71  import org.eclipse.jgit.lib.ObjectId;
72  import org.eclipse.jgit.lib.ObjectLoader;
73  import org.eclipse.jgit.lib.ObjectReader;
74  import org.eclipse.jgit.lib.ProgressMonitor;
75  
76  /** Active handle to a ByteWindow. */
77  final class WindowCursor extends ObjectReader implements ObjectReuseAsIs {
78  	/** Temporary buffer large enough for at least one raw object id. */
79  	final byte[] tempId = new byte[Constants.OBJECT_ID_LENGTH];
80  
81  	private Inflater inf;
82  
83  	private ByteWindow window;
84  
85  	private DeltaBaseCache baseCache;
86  
87  	final FileObjectDatabase db;
88  
89  	WindowCursor(FileObjectDatabase db) {
90  		this.db = db;
91  	}
92  
93  	DeltaBaseCache getDeltaBaseCache() {
94  		if (baseCache == null)
95  			baseCache = new DeltaBaseCache();
96  		return baseCache;
97  	}
98  
99  	@Override
100 	public ObjectReader newReader() {
101 		return new WindowCursor(db);
102 	}
103 
104 	@Override
105 	public BitmapIndex getBitmapIndex() throws IOException {
106 		for (PackFile pack : db.getPacks()) {
107 			PackBitmapIndex index = pack.getBitmapIndex();
108 			if (index != null)
109 				return new BitmapIndexImpl(index);
110 		}
111 		return null;
112 	}
113 
114 	public Collection<CachedPack> getCachedPacksAndUpdate(
115 			BitmapBuilder needBitmap) throws IOException {
116 		for (PackFile pack : db.getPacks()) {
117 			PackBitmapIndex index = pack.getBitmapIndex();
118 			if (needBitmap.removeAllOrNone(index))
119 				return Collections.<CachedPack> singletonList(
120 						new LocalCachedPack(Collections.singletonList(pack)));
121 		}
122 		return Collections.emptyList();
123 	}
124 
125 	@Override
126 	public Collection<ObjectId> resolve(AbbreviatedObjectId id)
127 			throws IOException {
128 		if (id.isComplete())
129 			return Collections.singleton(id.toObjectId());
130 		HashSet<ObjectId> matches = new HashSet<ObjectId>(4);
131 		db.resolve(matches, id);
132 		return matches;
133 	}
134 
135 	public boolean has(AnyObjectId objectId) throws IOException {
136 		return db.has(objectId);
137 	}
138 
139 	public ObjectLoader open(AnyObjectId objectId, int typeHint)
140 			throws MissingObjectException, IncorrectObjectTypeException,
141 			IOException {
142 		final ObjectLoader ldr = db.openObject(this, objectId);
143 		if (ldr == null) {
144 			if (typeHint == OBJ_ANY)
145 				throw new MissingObjectException(objectId.copy(),
146 						JGitText.get().unknownObjectType2);
147 			throw new MissingObjectException(objectId.copy(), typeHint);
148 		}
149 		if (typeHint != OBJ_ANY && ldr.getType() != typeHint)
150 			throw new IncorrectObjectTypeException(objectId.copy(), typeHint);
151 		return ldr;
152 	}
153 
154 	@Override
155 	public Set<ObjectId> getShallowCommits() throws IOException {
156 		return db.getShallowCommits();
157 	}
158 
159 	public long getObjectSize(AnyObjectId objectId, int typeHint)
160 			throws MissingObjectException, IncorrectObjectTypeException,
161 			IOException {
162 		long sz = db.getObjectSize(this, objectId);
163 		if (sz < 0) {
164 			if (typeHint == OBJ_ANY)
165 				throw new MissingObjectException(objectId.copy(),
166 						JGitText.get().unknownObjectType2);
167 			throw new MissingObjectException(objectId.copy(), typeHint);
168 		}
169 		return sz;
170 	}
171 
172 	public LocalObjectToPack newObjectToPack(AnyObjectId objectId, int type) {
173 		return new LocalObjectToPack(objectId, type);
174 	}
175 
176 	public void selectObjectRepresentation(PackWriter packer,
177 			ProgressMonitor monitor, Iterable<ObjectToPack> objects)
178 			throws IOException, MissingObjectException {
179 		for (ObjectToPack otp : objects) {
180 			db.selectObjectRepresentation(packer, otp, this);
181 			monitor.update(1);
182 		}
183 	}
184 
185 	public void copyObjectAsIs(PackOutputStream out, ObjectToPack otp,
186 			boolean validate) throws IOException,
187 			StoredObjectRepresentationNotAvailableException {
188 		LocalObjectToPack src = (LocalObjectToPack) otp;
189 		src.pack.copyAsIs(out, src, validate, this);
190 	}
191 
192 	public void writeObjects(PackOutputStream out, List<ObjectToPack> list)
193 			throws IOException {
194 		for (ObjectToPack otp : list)
195 			out.writeObject(otp);
196 	}
197 
198 	/**
199 	 * Copy bytes from the window to a caller supplied buffer.
200 	 *
201 	 * @param pack
202 	 *            the file the desired window is stored within.
203 	 * @param position
204 	 *            position within the file to read from.
205 	 * @param dstbuf
206 	 *            destination buffer to copy into.
207 	 * @param dstoff
208 	 *            offset within <code>dstbuf</code> to start copying into.
209 	 * @param cnt
210 	 *            number of bytes to copy. This value may exceed the number of
211 	 *            bytes remaining in the window starting at offset
212 	 *            <code>pos</code>.
213 	 * @return number of bytes actually copied; this may be less than
214 	 *         <code>cnt</code> if <code>cnt</code> exceeded the number of bytes
215 	 *         available.
216 	 * @throws IOException
217 	 *             this cursor does not match the provider or id and the proper
218 	 *             window could not be acquired through the provider's cache.
219 	 */
220 	int copy(final PackFile pack, long position, final byte[] dstbuf,
221 			int dstoff, final int cnt) throws IOException {
222 		final long length = pack.length;
223 		int need = cnt;
224 		while (need > 0 && position < length) {
225 			pin(pack, position);
226 			final int r = window.copy(position, dstbuf, dstoff, need);
227 			position += r;
228 			dstoff += r;
229 			need -= r;
230 		}
231 		return cnt - need;
232 	}
233 
234 	public void copyPackAsIs(PackOutputStream out, CachedPack pack)
235 			throws IOException {
236 		((LocalCachedPack) pack).copyAsIs(out, this);
237 	}
238 
239 	void copyPackAsIs(final PackFile pack, final long length,
240 			final PackOutputStream out) throws IOException {
241 		long position = 12;
242 		long remaining = length - (12 + 20);
243 		while (0 < remaining) {
244 			pin(pack, position);
245 
246 			int ptr = (int) (position - window.start);
247 			int n = (int) Math.min(window.size() - ptr, remaining);
248 			window.write(out, position, n);
249 			position += n;
250 			remaining -= n;
251 		}
252 	}
253 
254 	/**
255 	 * Inflate a region of the pack starting at {@code position}.
256 	 *
257 	 * @param pack
258 	 *            the file the desired window is stored within.
259 	 * @param position
260 	 *            position within the file to read from.
261 	 * @param dstbuf
262 	 *            destination buffer the inflater should output decompressed
263 	 *            data to. Must be large enough to store the entire stream,
264 	 *            unless headerOnly is true.
265 	 * @param headerOnly
266 	 *            if true the caller wants only {@code dstbuf.length} bytes.
267 	 * @return number of bytes inflated into <code>dstbuf</code>.
268 	 * @throws IOException
269 	 *             this cursor does not match the provider or id and the proper
270 	 *             window could not be acquired through the provider's cache.
271 	 * @throws DataFormatException
272 	 *             the inflater encountered an invalid chunk of data. Data
273 	 *             stream corruption is likely.
274 	 */
275 	int inflate(final PackFile pack, long position, final byte[] dstbuf,
276 			boolean headerOnly) throws IOException, DataFormatException {
277 		prepareInflater();
278 		pin(pack, position);
279 		position += window.setInput(position, inf);
280 		for (int dstoff = 0;;) {
281 			int n = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
282 			dstoff += n;
283 			if (inf.finished() || (headerOnly && dstoff == dstbuf.length))
284 				return dstoff;
285 			if (inf.needsInput()) {
286 				pin(pack, position);
287 				position += window.setInput(position, inf);
288 			} else if (n == 0)
289 				throw new DataFormatException();
290 		}
291 	}
292 
293 	ByteArrayWindow quickCopy(PackFile p, long pos, long cnt)
294 			throws IOException {
295 		pin(p, pos);
296 		if (window instanceof ByteArrayWindow
297 				&& window.contains(p, pos + (cnt - 1)))
298 			return (ByteArrayWindow) window;
299 		return null;
300 	}
301 
302 	Inflater inflater() {
303 		prepareInflater();
304 		return inf;
305 	}
306 
307 	private void prepareInflater() {
308 		if (inf == null)
309 			inf = InflaterCache.get();
310 		else
311 			inf.reset();
312 	}
313 
314 	void pin(final PackFile pack, final long position)
315 			throws IOException {
316 		final ByteWindow w = window;
317 		if (w == null || !w.contains(pack, position)) {
318 			// If memory is low, we may need what is in our window field to
319 			// be cleaned up by the GC during the get for the next window.
320 			// So we always clear it, even though we are just going to set
321 			// it again.
322 			//
323 			window = null;
324 			window = WindowCache.get(pack, position);
325 		}
326 	}
327 
328 	int getStreamFileThreshold() {
329 		return WindowCache.getStreamFileThreshold();
330 	}
331 
332 	/** Release the current window cursor. */
333 	@Override
334 	public void close() {
335 		window = null;
336 		baseCache = null;
337 		try {
338 			InflaterCache.release(inf);
339 		} finally {
340 			inf = null;
341 		}
342 	}
343 }