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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
  45. import static org.eclipse.jgit.lib.FileMode.REGULAR_FILE;
  46. import java.io.IOException;
  47. import java.util.Iterator;
  48. import java.util.NoSuchElementException;
  49. import org.eclipse.jgit.lib.AnyObjectId;
  50. import org.eclipse.jgit.lib.ObjectId;
  51. import org.eclipse.jgit.lib.ObjectInserter;
  52. import org.eclipse.jgit.lib.ObjectInserter.Formatter;
  53. import org.eclipse.jgit.lib.ObjectReader;
  54. import org.eclipse.jgit.lib.TreeFormatter;
  55. /**
  56. * A note tree holding only notes, with no subtrees.
  57. *
  58. * The leaf bucket contains on average less than 256 notes, all of whom share
  59. * the same leading prefix. If a notes branch has less than 256 notes, the top
  60. * level tree of the branch should be a LeafBucket. Once a notes branch has more
  61. * than 256 notes, the root should be a {@link FanoutBucket} and the LeafBucket
  62. * will appear only as a cell of a FanoutBucket.
  63. *
  64. * Entries within the LeafBucket are stored sorted by ObjectId, and lookup is
  65. * performed using binary search. As the entry list should contain fewer than
  66. * 256 elements, the average number of compares to find an element should be
  67. * less than 8 due to the O(log N) lookup behavior.
  68. *
  69. * A LeafBucket must be parsed from a tree object by {@link NoteParser}.
  70. */
  71. class LeafBucket extends InMemoryNoteBucket {
  72. static final int MAX_SIZE = 256;
  73. /** All note blobs in this bucket, sorted sequentially. */
  74. private Note[] notes;
  75. /** Number of items in {@link #notes}. */
  76. private int cnt;
  77. LeafBucket(int prefixLen) {
  78. super(prefixLen);
  79. notes = new Note[4];
  80. }
  81. private int search(AnyObjectId objId) {
  82. int low = 0;
  83. int high = cnt;
  84. while (low < high) {
  85. int mid = (low + high) >>> 1;
  86. int cmp = objId.compareTo(notes[mid]);
  87. if (cmp < 0)
  88. high = mid;
  89. else if (cmp == 0)
  90. return mid;
  91. else
  92. low = mid + 1;
  93. }
  94. return -(low + 1);
  95. }
  96. @Override
  97. Note getNote(AnyObjectId objId, ObjectReader or) {
  98. int idx = search(objId);
  99. return 0 <= idx ? notes[idx] : null;
  100. }
  101. Note get(int index) {
  102. return notes[index];
  103. }
  104. int size() {
  105. return cnt;
  106. }
  107. @Override
  108. Iterator<Note> iterator(AnyObjectId objId, ObjectReader reader) {
  109. return new Iterator<Note>() {
  110. private int idx;
  111. public boolean hasNext() {
  112. return idx < cnt;
  113. }
  114. public Note next() {
  115. if (hasNext())
  116. return notes[idx++];
  117. else
  118. throw new NoSuchElementException();
  119. }
  120. public void remove() {
  121. throw new UnsupportedOperationException();
  122. }
  123. };
  124. }
  125. @Override
  126. int estimateSize(AnyObjectId noteOn, ObjectReader or) throws IOException {
  127. return cnt;
  128. }
  129. InMemoryNoteBucket set(AnyObjectId noteOn, AnyObjectId noteData,
  130. ObjectReader or) throws IOException {
  131. int p = search(noteOn);
  132. if (0 <= p) {
  133. if (noteData != null) {
  134. notes[p].setData(noteData.copy());
  135. return this;
  136. } else {
  137. System.arraycopy(notes, p + 1, notes, p, cnt - p - 1);
  138. cnt--;
  139. return 0 < cnt ? this : null;
  140. }
  141. } else if (noteData != null) {
  142. if (shouldSplit()) {
  143. return split().set(noteOn, noteData, or);
  144. } else {
  145. growIfFull();
  146. p = -(p + 1);
  147. if (p < cnt)
  148. System.arraycopy(notes, p, notes, p + 1, cnt - p);
  149. notes[p] = new Note(noteOn, noteData.copy());
  150. cnt++;
  151. return this;
  152. }
  153. } else {
  154. return this;
  155. }
  156. }
  157. @Override
  158. ObjectId writeTree(ObjectInserter inserter) throws IOException {
  159. return inserter.insert(build());
  160. }
  161. @Override
  162. ObjectId getTreeId() {
  163. try (Formatter f = new ObjectInserter.Formatter()) {
  164. return f.idFor(build());
  165. }
  166. }
  167. private TreeFormatter build() {
  168. byte[] nameBuf = new byte[OBJECT_ID_STRING_LENGTH];
  169. int nameLen = OBJECT_ID_STRING_LENGTH - prefixLen;
  170. TreeFormatter fmt = new TreeFormatter(treeSize(nameLen));
  171. NonNoteEntry e = nonNotes;
  172. for (int i = 0; i < cnt; i++) {
  173. Note n = notes[i];
  174. n.copyTo(nameBuf, 0);
  175. while (e != null
  176. && e.pathCompare(nameBuf, prefixLen, nameLen, REGULAR_FILE) < 0) {
  177. e.format(fmt);
  178. e = e.next;
  179. }
  180. fmt.append(nameBuf, prefixLen, nameLen, REGULAR_FILE, n.getData());
  181. }
  182. for (; e != null; e = e.next)
  183. e.format(fmt);
  184. return fmt;
  185. }
  186. private int treeSize(final int nameLen) {
  187. int sz = cnt * TreeFormatter.entrySize(REGULAR_FILE, nameLen);
  188. for (NonNoteEntry e = nonNotes; e != null; e = e.next)
  189. sz += e.treeEntrySize();
  190. return sz;
  191. }
  192. void parseOneEntry(AnyObjectId noteOn, AnyObjectId noteData) {
  193. growIfFull();
  194. notes[cnt++] = new Note(noteOn, noteData.copy());
  195. }
  196. @Override
  197. InMemoryNoteBucket append(Note note) {
  198. if (shouldSplit()) {
  199. return split().append(note);
  200. } else {
  201. growIfFull();
  202. notes[cnt++] = note;
  203. return this;
  204. }
  205. }
  206. private void growIfFull() {
  207. if (notes.length == cnt) {
  208. Note[] n = new Note[notes.length * 2];
  209. System.arraycopy(notes, 0, n, 0, cnt);
  210. notes = n;
  211. }
  212. }
  213. private boolean shouldSplit() {
  214. return MAX_SIZE <= cnt && prefixLen + 2 < OBJECT_ID_STRING_LENGTH;
  215. }
  216. FanoutBucket split() {
  217. FanoutBucket n = new FanoutBucket(prefixLen);
  218. for (int i = 0; i < cnt; i++)
  219. n.append(notes[i]);
  220. n.nonNotes = nonNotes;
  221. return n;
  222. }
  223. }