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.notes;
45  
46  import java.io.IOException;
47  import java.util.Iterator;
48  
49  import org.eclipse.jgit.errors.CorruptObjectException;
50  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
51  import org.eclipse.jgit.errors.LargeObjectException;
52  import org.eclipse.jgit.errors.MissingObjectException;
53  import org.eclipse.jgit.lib.AbbreviatedObjectId;
54  import org.eclipse.jgit.lib.AnyObjectId;
55  import org.eclipse.jgit.lib.Constants;
56  import org.eclipse.jgit.lib.MutableObjectId;
57  import org.eclipse.jgit.lib.ObjectId;
58  import org.eclipse.jgit.lib.ObjectInserter;
59  import org.eclipse.jgit.lib.ObjectReader;
60  import org.eclipse.jgit.revwalk.RevCommit;
61  import org.eclipse.jgit.revwalk.RevTree;
62  
63  /**
64   * Index of notes from a note branch.
65   *
66   * This class is not thread-safe, and relies on an {@link ObjectReader} that it
67   * borrows/shares with the caller. The reader can be used during any call, and
68   * is not released by this class. The caller should arrange for releasing the
69   * shared {@code ObjectReader} at the proper times.
70   */
71  public class NoteMap implements Iterable<Note> {
72  	/**
73  	 * Construct a new empty note map.
74  	 *
75  	 * @return an empty note map.
76  	 */
77  	public static NoteMap newEmptyMap() {
78  		NoteMap r = new NoteMap(null /* no reader */);
79  		r.root = new LeafBucket(0);
80  		return r;
81  	}
82  
83  	/**
84  	 * Shorten the note ref name by trimming off the {@link Constants#R_NOTES}
85  	 * prefix if it exists.
86  	 *
87  	 * @param noteRefName
88  	 * @return a more user friendly note name
89  	 */
90  	public static String shortenRefName(String noteRefName) {
91  		if (noteRefName.startsWith(Constants.R_NOTES))
92  			return noteRefName.substring(Constants.R_NOTES.length());
93  		return noteRefName;
94  	}
95  
96  	/**
97  	 * Load a collection of notes from a branch.
98  	 *
99  	 * @param reader
100 	 *            reader to scan the note branch with. This reader may be
101 	 *            retained by the NoteMap for the life of the map in order to
102 	 *            support lazy loading of entries.
103 	 * @param commit
104 	 *            the revision of the note branch to read.
105 	 * @return the note map read from the commit.
106 	 * @throws IOException
107 	 *             the repository cannot be accessed through the reader.
108 	 * @throws CorruptObjectException
109 	 *             a tree object is corrupt and cannot be read.
110 	 * @throws IncorrectObjectTypeException
111 	 *             a tree object wasn't actually a tree.
112 	 * @throws MissingObjectException
113 	 *             a reference tree object doesn't exist.
114 	 */
115 	public static NoteMap read(ObjectReader reader, RevCommit commit)
116 			throws MissingObjectException, IncorrectObjectTypeException,
117 			CorruptObjectException, IOException {
118 		return read(reader, commit.getTree());
119 	}
120 
121 	/**
122 	 * Load a collection of notes from a tree.
123 	 *
124 	 * @param reader
125 	 *            reader to scan the note branch with. This reader may be
126 	 *            retained by the NoteMap for the life of the map in order to
127 	 *            support lazy loading of entries.
128 	 * @param tree
129 	 *            the note tree to read.
130 	 * @return the note map read from the tree.
131 	 * @throws IOException
132 	 *             the repository cannot be accessed through the reader.
133 	 * @throws CorruptObjectException
134 	 *             a tree object is corrupt and cannot be read.
135 	 * @throws IncorrectObjectTypeException
136 	 *             a tree object wasn't actually a tree.
137 	 * @throws MissingObjectException
138 	 *             a reference tree object doesn't exist.
139 	 */
140 	public static NoteMap read(ObjectReader reader, RevTree tree)
141 			throws MissingObjectException, IncorrectObjectTypeException,
142 			CorruptObjectException, IOException {
143 		return readTree(reader, tree);
144 	}
145 
146 	/**
147 	 * Load a collection of notes from a tree.
148 	 *
149 	 * @param reader
150 	 *            reader to scan the note branch with. This reader may be
151 	 *            retained by the NoteMap for the life of the map in order to
152 	 *            support lazy loading of entries.
153 	 * @param treeId
154 	 *            the note tree to read.
155 	 * @return the note map read from the tree.
156 	 * @throws IOException
157 	 *             the repository cannot be accessed through the reader.
158 	 * @throws CorruptObjectException
159 	 *             a tree object is corrupt and cannot be read.
160 	 * @throws IncorrectObjectTypeException
161 	 *             a tree object wasn't actually a tree.
162 	 * @throws MissingObjectException
163 	 *             a reference tree object doesn't exist.
164 	 */
165 	public static NoteMap readTree(ObjectReader reader, ObjectId treeId)
166 			throws MissingObjectException, IncorrectObjectTypeException,
167 			CorruptObjectException, IOException {
168 		NoteMap map = new NoteMap(reader);
169 		map.load(treeId);
170 		return map;
171 	}
172 
173 	/**
174 	 * Construct a new note map from an existing note bucket.
175 	 *
176 	 * @param root
177 	 *            the root bucket of this note map
178 	 * @param reader
179 	 *            reader to scan the note branch with. This reader may be
180 	 *            retained by the NoteMap for the life of the map in order to
181 	 *            support lazy loading of entries.
182 	 * @return the note map built from the note bucket
183 	 */
184 	static NoteMap newMap(InMemoryNoteBucket root, ObjectReader reader) {
185 		NoteMap map = new NoteMap(reader);
186 		map.root = root;
187 		return map;
188 	}
189 
190 	/** Borrowed reader to access the repository. */
191 	private final ObjectReader reader;
192 
193 	/** All of the notes that have been loaded. */
194 	private InMemoryNoteBucket root;
195 
196 	private NoteMap(ObjectReader reader) {
197 		this.reader = reader;
198 	}
199 
200 	/**
201 	 * @return an iterator that iterates over notes of this NoteMap. Non note
202 	 *         entries are ignored by this iterator.
203 	 */
204 	public Iterator<Note> iterator() {
205 		try {
206 			return root.iterator(new MutableObjectId(), reader);
207 		} catch (IOException e) {
208 			throw new RuntimeException(e);
209 		}
210 	}
211 
212 	/**
213 	 * Lookup a note for a specific ObjectId.
214 	 *
215 	 * @param id
216 	 *            the object to look for.
217 	 * @return the note's blob ObjectId, or null if no note exists.
218 	 * @throws IOException
219 	 *             a portion of the note space is not accessible.
220 	 */
221 	public ObjectId get(AnyObjectId id) throws IOException {
222 		Note n = root.getNote(id, reader);
223 		return n == null ? null : n.getData();
224 	}
225 
226 	/**
227 	 * Lookup a note for a specific ObjectId.
228 	 *
229 	 * @param id
230 	 *            the object to look for.
231 	 * @return the note for the given object id, or null if no note exists.
232 	 * @throws IOException
233 	 *             a portion of the note space is not accessible.
234 	 */
235 	public Note getNote(AnyObjectId id) throws IOException {
236 		return root.getNote(id, reader);
237 	}
238 
239 	/**
240 	 * Determine if a note exists for the specified ObjectId.
241 	 *
242 	 * @param id
243 	 *            the object to look for.
244 	 * @return true if a note exists; false if there is no note.
245 	 * @throws IOException
246 	 *             a portion of the note space is not accessible.
247 	 */
248 	public boolean contains(AnyObjectId id) throws IOException {
249 		return get(id) != null;
250 	}
251 
252 	/**
253 	 * Open and return the content of an object's note.
254 	 *
255 	 * This method assumes the note is fairly small and can be accessed
256 	 * efficiently. Larger notes should be accessed by streaming:
257 	 *
258 	 * <pre>
259 	 * ObjectId dataId = thisMap.get(id);
260 	 * if (dataId != null)
261 	 * 	reader.open(dataId).openStream();
262 	 * </pre>
263 	 *
264 	 * @param id
265 	 *            object to lookup the note of.
266 	 * @param sizeLimit
267 	 *            maximum number of bytes to return. If the note data size is
268 	 *            larger than this limit, LargeObjectException will be thrown.
269 	 * @return if a note is defined for {@code id}, the note content. If no note
270 	 *         is defined, null.
271 	 * @throws LargeObjectException
272 	 *             the note data is larger than {@code sizeLimit}.
273 	 * @throws MissingObjectException
274 	 *             the note's blob does not exist in the repository.
275 	 * @throws IOException
276 	 *             the note's blob cannot be read from the repository
277 	 */
278 	public byte[] getCachedBytes(AnyObjectId id, int sizeLimit)
279 			throws LargeObjectException, MissingObjectException, IOException {
280 		ObjectId dataId = get(id);
281 		if (dataId != null)
282 			return reader.open(dataId).getCachedBytes(sizeLimit);
283 		else
284 			return null;
285 	}
286 
287 	/**
288 	 * Attach (or remove) a note on an object.
289 	 *
290 	 * If no note exists, a new note is stored. If a note already exists for the
291 	 * given object, it is replaced (or removed).
292 	 *
293 	 * This method only updates the map in memory.
294 	 *
295 	 * If the caller wants to attach a UTF-8 encoded string message to an
296 	 * object, {@link #set(AnyObjectId, String, ObjectInserter)} is a convenient
297 	 * way to encode and update a note in one step.
298 	 *
299 	 * @param noteOn
300 	 *            the object to attach the note to. This same ObjectId can later
301 	 *            be used as an argument to {@link #get(AnyObjectId)} or
302 	 *            {@link #getCachedBytes(AnyObjectId, int)} to read back the
303 	 *            {@code noteData}.
304 	 * @param noteData
305 	 *            data to associate with the note. This must be the ObjectId of
306 	 *            a blob that already exists in the repository. If null the note
307 	 *            will be deleted, if present.
308 	 * @throws IOException
309 	 *             a portion of the note space is not accessible.
310 	 */
311 	public void set(AnyObjectId noteOn, ObjectId noteData) throws IOException {
312 		InMemoryNoteBucket newRoot = root.set(noteOn, noteData, reader);
313 		if (newRoot == null) {
314 			newRoot = new LeafBucket(0);
315 			newRoot.nonNotes = root.nonNotes;
316 		}
317 		root = newRoot;
318 	}
319 
320 	/**
321 	 * Attach a note to an object.
322 	 *
323 	 * If no note exists, a new note is stored. If a note already exists for the
324 	 * given object, it is replaced (or removed).
325 	 *
326 	 * @param noteOn
327 	 *            the object to attach the note to. This same ObjectId can later
328 	 *            be used as an argument to {@link #get(AnyObjectId)} or
329 	 *            {@link #getCachedBytes(AnyObjectId, int)} to read back the
330 	 *            {@code noteData}.
331 	 * @param noteData
332 	 *            text to store in the note. The text will be UTF-8 encoded when
333 	 *            stored in the repository. If null the note will be deleted, if
334 	 *            the empty string a note with the empty string will be stored.
335 	 * @param ins
336 	 *            inserter to write the encoded {@code noteData} out as a blob.
337 	 *            The caller must ensure the inserter is flushed before the
338 	 *            updated note map is made available for reading.
339 	 * @throws IOException
340 	 *             the note data could not be stored in the repository.
341 	 */
342 	public void set(AnyObjectId noteOn, String noteData, ObjectInserter ins)
343 			throws IOException {
344 		ObjectId dataId;
345 		if (noteData != null) {
346 			byte[] dataUTF8 = Constants.encode(noteData);
347 			dataId = ins.insert(Constants.OBJ_BLOB, dataUTF8);
348 		} else {
349 			dataId = null;
350 		}
351 		set(noteOn, dataId);
352 	}
353 
354 	/**
355 	 * Remove a note from an object.
356 	 *
357 	 * If no note exists, no action is performed.
358 	 *
359 	 * This method only updates the map in memory.
360 	 *
361 	 * @param noteOn
362 	 *            the object to remove the note from.
363 	 * @throws IOException
364 	 *             a portion of the note space is not accessible.
365 	 */
366 	public void remove(AnyObjectId noteOn) throws IOException {
367 		set(noteOn, null);
368 	}
369 
370 	/**
371 	 * Write this note map as a tree.
372 	 *
373 	 * @param inserter
374 	 *            inserter to use when writing trees to the object database.
375 	 *            Caller is responsible for flushing the inserter before trying
376 	 *            to read the objects, or exposing them through a reference.
377 	 * @return the top level tree.
378 	 * @throws IOException
379 	 *             a tree could not be written.
380 	 */
381 	public ObjectId writeTree(ObjectInserter inserter) throws IOException {
382 		return root.writeTree(inserter);
383 	}
384 
385 	/** @return the root note bucket */
386 	InMemoryNoteBucket getRoot() {
387 		return root;
388 	}
389 
390 	private void load(ObjectId rootTree) throws MissingObjectException,
391 			IncorrectObjectTypeException, CorruptObjectException, IOException {
392 		AbbreviatedObjectId none = AbbreviatedObjectId.fromString(""); //$NON-NLS-1$
393 		root = NoteParser.parse(none, rootTree, reader);
394 	}
395 }