You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NoteMap.java 14KB

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