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
  63. * {@link org.eclipse.jgit.lib.ObjectReader} that it borrows/shares with the
  64. * caller. The reader can be used during any call, and is not released by this
  65. * class. The caller should arrange for releasing the shared
  66. * {@code ObjectReader} at the proper times.
  67. */
  68. public class NoteMap implements Iterable<Note> {
  69. /**
  70. * Construct a new empty note map.
  71. *
  72. * @return an empty note map.
  73. */
  74. public static NoteMap newEmptyMap() {
  75. NoteMap r = new NoteMap(null /* no reader */);
  76. r.root = new LeafBucket(0);
  77. return r;
  78. }
  79. /**
  80. * Shorten the note ref name by trimming off the
  81. * {@link org.eclipse.jgit.lib.Constants#R_NOTES} prefix if it exists.
  82. *
  83. * @param noteRefName
  84. * a {@link java.lang.String} object.
  85. * @return a more user friendly note name
  86. */
  87. public static String shortenRefName(String noteRefName) {
  88. if (noteRefName.startsWith(Constants.R_NOTES))
  89. return noteRefName.substring(Constants.R_NOTES.length());
  90. return noteRefName;
  91. }
  92. /**
  93. * Load a collection of notes from a branch.
  94. *
  95. * @param reader
  96. * reader to scan the note branch with. This reader may be
  97. * retained by the NoteMap for the life of the map in order to
  98. * support lazy loading of entries.
  99. * @param commit
  100. * the revision of the note branch to read.
  101. * @return the note map read from the commit.
  102. * @throws java.io.IOException
  103. * the repository cannot be accessed through the reader.
  104. * @throws org.eclipse.jgit.errors.CorruptObjectException
  105. * a tree object is corrupt and cannot be read.
  106. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  107. * a tree object wasn't actually a tree.
  108. * @throws org.eclipse.jgit.errors.MissingObjectException
  109. * a reference tree object doesn't exist.
  110. */
  111. public static NoteMap read(ObjectReader reader, RevCommit commit)
  112. throws MissingObjectException, IncorrectObjectTypeException,
  113. CorruptObjectException, IOException {
  114. return read(reader, commit.getTree());
  115. }
  116. /**
  117. * Load a collection of notes from a tree.
  118. *
  119. * @param reader
  120. * reader to scan the note branch with. This reader may be
  121. * retained by the NoteMap for the life of the map in order to
  122. * support lazy loading of entries.
  123. * @param tree
  124. * the note tree to read.
  125. * @return the note map read from the tree.
  126. * @throws java.io.IOException
  127. * the repository cannot be accessed through the reader.
  128. * @throws org.eclipse.jgit.errors.CorruptObjectException
  129. * a tree object is corrupt and cannot be read.
  130. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  131. * a tree object wasn't actually a tree.
  132. * @throws org.eclipse.jgit.errors.MissingObjectException
  133. * a reference tree object doesn't exist.
  134. */
  135. public static NoteMap read(ObjectReader reader, RevTree tree)
  136. throws MissingObjectException, IncorrectObjectTypeException,
  137. CorruptObjectException, IOException {
  138. return readTree(reader, tree);
  139. }
  140. /**
  141. * Load a collection of notes from a tree.
  142. *
  143. * @param reader
  144. * reader to scan the note branch with. This reader may be
  145. * retained by the NoteMap for the life of the map in order to
  146. * support lazy loading of entries.
  147. * @param treeId
  148. * the note tree to read.
  149. * @return the note map read from the tree.
  150. * @throws java.io.IOException
  151. * the repository cannot be accessed through the reader.
  152. * @throws org.eclipse.jgit.errors.CorruptObjectException
  153. * a tree object is corrupt and cannot be read.
  154. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  155. * a tree object wasn't actually a tree.
  156. * @throws org.eclipse.jgit.errors.MissingObjectException
  157. * a reference tree object doesn't exist.
  158. */
  159. public static NoteMap readTree(ObjectReader reader, ObjectId treeId)
  160. throws MissingObjectException, IncorrectObjectTypeException,
  161. CorruptObjectException, IOException {
  162. NoteMap map = new NoteMap(reader);
  163. map.load(treeId);
  164. return map;
  165. }
  166. /**
  167. * Construct a new note map from an existing note bucket.
  168. *
  169. * @param root
  170. * the root bucket of this note map
  171. * @param reader
  172. * reader to scan the note branch with. This reader may be
  173. * retained by the NoteMap for the life of the map in order to
  174. * support lazy loading of entries.
  175. * @return the note map built from the note bucket
  176. */
  177. static NoteMap newMap(InMemoryNoteBucket root, ObjectReader reader) {
  178. NoteMap map = new NoteMap(reader);
  179. map.root = root;
  180. return map;
  181. }
  182. /** Borrowed reader to access the repository. */
  183. private final ObjectReader reader;
  184. /** All of the notes that have been loaded. */
  185. private InMemoryNoteBucket root;
  186. private NoteMap(ObjectReader reader) {
  187. this.reader = reader;
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  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 java.io.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 java.io.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 java.io.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 org.eclipse.jgit.errors.LargeObjectException
  255. * the note data is larger than {@code sizeLimit}.
  256. * @throws org.eclipse.jgit.errors.MissingObjectException
  257. * the note's blob does not exist in the repository.
  258. * @throws java.io.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. }
  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 java.io.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 java.io.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 java.io.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 java.io.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. }