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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. @Override
  112. public boolean hasNext() {
  113. return idx < cnt;
  114. }
  115. @Override
  116. public Note next() {
  117. if (hasNext())
  118. return notes[idx++];
  119. else
  120. throw new NoSuchElementException();
  121. }
  122. @Override
  123. public void remove() {
  124. throw new UnsupportedOperationException();
  125. }
  126. };
  127. }
  128. @Override
  129. int estimateSize(AnyObjectId noteOn, ObjectReader or) throws IOException {
  130. return cnt;
  131. }
  132. @Override
  133. InMemoryNoteBucket set(AnyObjectId noteOn, AnyObjectId noteData,
  134. ObjectReader or) throws IOException {
  135. int p = search(noteOn);
  136. if (0 <= p) {
  137. if (noteData != null) {
  138. notes[p].setData(noteData.copy());
  139. return this;
  140. } else {
  141. System.arraycopy(notes, p + 1, notes, p, cnt - p - 1);
  142. cnt--;
  143. return 0 < cnt ? this : null;
  144. }
  145. } else if (noteData != null) {
  146. if (shouldSplit()) {
  147. return split().set(noteOn, noteData, or);
  148. } else {
  149. growIfFull();
  150. p = -(p + 1);
  151. if (p < cnt)
  152. System.arraycopy(notes, p, notes, p + 1, cnt - p);
  153. notes[p] = new Note(noteOn, noteData.copy());
  154. cnt++;
  155. return this;
  156. }
  157. } else {
  158. return this;
  159. }
  160. }
  161. @Override
  162. ObjectId writeTree(ObjectInserter inserter) throws IOException {
  163. return inserter.insert(build());
  164. }
  165. @Override
  166. ObjectId getTreeId() {
  167. try (Formatter f = new ObjectInserter.Formatter()) {
  168. return f.idFor(build());
  169. }
  170. }
  171. private TreeFormatter build() {
  172. byte[] nameBuf = new byte[OBJECT_ID_STRING_LENGTH];
  173. int nameLen = OBJECT_ID_STRING_LENGTH - prefixLen;
  174. TreeFormatter fmt = new TreeFormatter(treeSize(nameLen));
  175. NonNoteEntry e = nonNotes;
  176. for (int i = 0; i < cnt; i++) {
  177. Note n = notes[i];
  178. n.copyTo(nameBuf, 0);
  179. while (e != null
  180. && e.pathCompare(nameBuf, prefixLen, nameLen, REGULAR_FILE) < 0) {
  181. e.format(fmt);
  182. e = e.next;
  183. }
  184. fmt.append(nameBuf, prefixLen, nameLen, REGULAR_FILE, n.getData());
  185. }
  186. for (; e != null; e = e.next)
  187. e.format(fmt);
  188. return fmt;
  189. }
  190. private int treeSize(final int nameLen) {
  191. int sz = cnt * TreeFormatter.entrySize(REGULAR_FILE, nameLen);
  192. for (NonNoteEntry e = nonNotes; e != null; e = e.next)
  193. sz += e.treeEntrySize();
  194. return sz;
  195. }
  196. void parseOneEntry(AnyObjectId noteOn, AnyObjectId noteData) {
  197. growIfFull();
  198. notes[cnt++] = new Note(noteOn, noteData.copy());
  199. }
  200. @Override
  201. InMemoryNoteBucket append(Note note) {
  202. if (shouldSplit()) {
  203. return split().append(note);
  204. } else {
  205. growIfFull();
  206. notes[cnt++] = note;
  207. return this;
  208. }
  209. }
  210. private void growIfFull() {
  211. if (notes.length == cnt) {
  212. Note[] n = new Note[notes.length * 2];
  213. System.arraycopy(notes, 0, n, 0, cnt);
  214. notes = n;
  215. }
  216. }
  217. private boolean shouldSplit() {
  218. return MAX_SIZE <= cnt && prefixLen + 2 < OBJECT_ID_STRING_LENGTH;
  219. }
  220. FanoutBucket split() {
  221. FanoutBucket n = new FanoutBucket(prefixLen);
  222. for (int i = 0; i < cnt; i++)
  223. n.append(notes[i]);
  224. n.nonNotes = nonNotes;
  225. return n;
  226. }
  227. }