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.

LeafBucket.java 6.8KB

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