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 @Override
205 public Iterator<Note> iterator() {
206 try {
207 return root.iterator(new MutableObjectId(), reader);
208 } catch (IOException e) {
209 throw new RuntimeException(e);
210 }
211 }
212
213 /**
214 * Lookup a note for a specific ObjectId.
215 *
216 * @param id
217 * the object to look for.
218 * @return the note's blob ObjectId, or null if no note exists.
219 * @throws IOException
220 * a portion of the note space is not accessible.
221 */
222 public ObjectId get(AnyObjectId id) throws IOException {
223 Note n = root.getNote(id, reader);
224 return n == null ? null : n.getData();
225 }
226
227 /**
228 * Lookup a note for a specific ObjectId.
229 *
230 * @param id
231 * the object to look for.
232 * @return the note for the given object id, or null if no note exists.
233 * @throws IOException
234 * a portion of the note space is not accessible.
235 */
236 public Note getNote(AnyObjectId id) throws IOException {
237 return root.getNote(id, reader);
238 }
239
240 /**
241 * Determine if a note exists for the specified ObjectId.
242 *
243 * @param id
244 * the object to look for.
245 * @return true if a note exists; false if there is no note.
246 * @throws IOException
247 * a portion of the note space is not accessible.
248 */
249 public boolean contains(AnyObjectId id) throws IOException {
250 return get(id) != null;
251 }
252
253 /**
254 * Open and return the content of an object's note.
255 *
256 * This method assumes the note is fairly small and can be accessed
257 * efficiently. Larger notes should be accessed by streaming:
258 *
259 * <pre>
260 * ObjectId dataId = thisMap.get(id);
261 * if (dataId != null)
262 * reader.open(dataId).openStream();
263 * </pre>
264 *
265 * @param id
266 * object to lookup the note of.
267 * @param sizeLimit
268 * maximum number of bytes to return. If the note data size is
269 * larger than this limit, LargeObjectException will be thrown.
270 * @return if a note is defined for {@code id}, the note content. If no note
271 * is defined, null.
272 * @throws LargeObjectException
273 * the note data is larger than {@code sizeLimit}.
274 * @throws MissingObjectException
275 * the note's blob does not exist in the repository.
276 * @throws IOException
277 * the note's blob cannot be read from the repository
278 */
279 public byte[] getCachedBytes(AnyObjectId id, int sizeLimit)
280 throws LargeObjectException, MissingObjectException, IOException {
281 ObjectId dataId = get(id);
282 if (dataId != null)
283 return reader.open(dataId).getCachedBytes(sizeLimit);
284 else
285 return null;
286 }
287
288 /**
289 * Attach (or remove) a note on an object.
290 *
291 * If no note exists, a new note is stored. If a note already exists for the
292 * given object, it is replaced (or removed).
293 *
294 * This method only updates the map in memory.
295 *
296 * If the caller wants to attach a UTF-8 encoded string message to an
297 * object, {@link #set(AnyObjectId, String, ObjectInserter)} is a convenient
298 * way to encode and update a note in one step.
299 *
300 * @param noteOn
301 * the object to attach the note to. This same ObjectId can later
302 * be used as an argument to {@link #get(AnyObjectId)} or
303 * {@link #getCachedBytes(AnyObjectId, int)} to read back the
304 * {@code noteData}.
305 * @param noteData
306 * data to associate with the note. This must be the ObjectId of
307 * a blob that already exists in the repository. If null the note
308 * will be deleted, if present.
309 * @throws IOException
310 * a portion of the note space is not accessible.
311 */
312 public void set(AnyObjectId noteOn, ObjectId noteData) throws IOException {
313 InMemoryNoteBucket newRoot = root.set(noteOn, noteData, reader);
314 if (newRoot == null) {
315 newRoot = new LeafBucket(0);
316 newRoot.nonNotes = root.nonNotes;
317 }
318 root = newRoot;
319 }
320
321 /**
322 * Attach a note to an object.
323 *
324 * If no note exists, a new note is stored. If a note already exists for the
325 * given object, it is replaced (or removed).
326 *
327 * @param noteOn
328 * the object to attach the note to. This same ObjectId can later
329 * be used as an argument to {@link #get(AnyObjectId)} or
330 * {@link #getCachedBytes(AnyObjectId, int)} to read back the
331 * {@code noteData}.
332 * @param noteData
333 * text to store in the note. The text will be UTF-8 encoded when
334 * stored in the repository. If null the note will be deleted, if
335 * the empty string a note with the empty string will be stored.
336 * @param ins
337 * inserter to write the encoded {@code noteData} out as a blob.
338 * The caller must ensure the inserter is flushed before the
339 * updated note map is made available for reading.
340 * @throws IOException
341 * the note data could not be stored in the repository.
342 */
343 public void set(AnyObjectId noteOn, String noteData, ObjectInserter ins)
344 throws IOException {
345 ObjectId dataId;
346 if (noteData != null) {
347 byte[] dataUTF8 = Constants.encode(noteData);
348 dataId = ins.insert(Constants.OBJ_BLOB, dataUTF8);
349 } else {
350 dataId = null;
351 }
352 set(noteOn, dataId);
353 }
354
355 /**
356 * Remove a note from an object.
357 *
358 * If no note exists, no action is performed.
359 *
360 * This method only updates the map in memory.
361 *
362 * @param noteOn
363 * the object to remove the note from.
364 * @throws IOException
365 * a portion of the note space is not accessible.
366 */
367 public void remove(AnyObjectId noteOn) throws IOException {
368 set(noteOn, null);
369 }
370
371 /**
372 * Write this note map as a tree.
373 *
374 * @param inserter
375 * inserter to use when writing trees to the object database.
376 * Caller is responsible for flushing the inserter before trying
377 * to read the objects, or exposing them through a reference.
378 * @return the top level tree.
379 * @throws IOException
380 * a tree could not be written.
381 */
382 public ObjectId writeTree(ObjectInserter inserter) throws IOException {
383 return root.writeTree(inserter);
384 }
385
386 /** @return the root note bucket */
387 InMemoryNoteBucket getRoot() {
388 return root;
389 }
390
391 private void load(ObjectId rootTree) throws MissingObjectException,
392 IncorrectObjectTypeException, CorruptObjectException, IOException {
393 AbbreviatedObjectId none = AbbreviatedObjectId.fromString(""); //$NON-NLS-1$
394 root = NoteParser.parse(none, rootTree, reader);
395 }
396 }