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 13KB

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