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

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