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.9KB

Merging Git notes Merging Git notes branches has several differences from merging "normal" branches. Although Git notes are initially stored as one flat tree the tree may fanout when the number of notes becomes too large for efficient access. In this case the first two hex digits of the note name will be used as a subdirectory name and the rest 38 hex digits as the file name under that directory. Similarly, when number of notes decreases a fanout tree may collapse back into a flat tree. The Git notes merge algorithm must take into account possibly different tree structures in different note branches and must properly match them against each other. Any conflict on a Git note is, by default, resolved by concatenating the two conflicting versions of the note. A delete-edit conflict is, by default, resolved by keeping the edit version. The note merge logic is pluggable and the caller may provide custom note merger that will perform different merging strategy. Additionally, it is possible to have non-note entries inside a notes tree. The merge algorithm must also take this fact into account and will try to merge such non-note entries. However, in case of any merge conflicts the merge operation will fail. Git notes merge algorithm is currently not trying to do content merge of non-note entries. Thanks to Shawn Pearce for patiently answering my questions related to this topic, giving hints and providing code snippets. Change-Id: I3b2335c76c766fd7ea25752e54087f9b19d69c88 Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
13 年之前
Merging Git notes Merging Git notes branches has several differences from merging "normal" branches. Although Git notes are initially stored as one flat tree the tree may fanout when the number of notes becomes too large for efficient access. In this case the first two hex digits of the note name will be used as a subdirectory name and the rest 38 hex digits as the file name under that directory. Similarly, when number of notes decreases a fanout tree may collapse back into a flat tree. The Git notes merge algorithm must take into account possibly different tree structures in different note branches and must properly match them against each other. Any conflict on a Git note is, by default, resolved by concatenating the two conflicting versions of the note. A delete-edit conflict is, by default, resolved by keeping the edit version. The note merge logic is pluggable and the caller may provide custom note merger that will perform different merging strategy. Additionally, it is possible to have non-note entries inside a notes tree. The merge algorithm must also take this fact into account and will try to merge such non-note entries. However, in case of any merge conflicts the merge operation will fail. Git notes merge algorithm is currently not trying to do content merge of non-note entries. Thanks to Shawn Pearce for patiently answering my questions related to this topic, giving hints and providing code snippets. Change-Id: I3b2335c76c766fd7ea25752e54087f9b19d69c88 Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
13 年之前
Merging Git notes Merging Git notes branches has several differences from merging "normal" branches. Although Git notes are initially stored as one flat tree the tree may fanout when the number of notes becomes too large for efficient access. In this case the first two hex digits of the note name will be used as a subdirectory name and the rest 38 hex digits as the file name under that directory. Similarly, when number of notes decreases a fanout tree may collapse back into a flat tree. The Git notes merge algorithm must take into account possibly different tree structures in different note branches and must properly match them against each other. Any conflict on a Git note is, by default, resolved by concatenating the two conflicting versions of the note. A delete-edit conflict is, by default, resolved by keeping the edit version. The note merge logic is pluggable and the caller may provide custom note merger that will perform different merging strategy. Additionally, it is possible to have non-note entries inside a notes tree. The merge algorithm must also take this fact into account and will try to merge such non-note entries. However, in case of any merge conflicts the merge operation will fail. Git notes merge algorithm is currently not trying to do content merge of non-note entries. Thanks to Shawn Pearce for patiently answering my questions related to this topic, giving hints and providing code snippets. Change-Id: I3b2335c76c766fd7ea25752e54087f9b19d69c88 Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
13 年之前
Merging Git notes Merging Git notes branches has several differences from merging "normal" branches. Although Git notes are initially stored as one flat tree the tree may fanout when the number of notes becomes too large for efficient access. In this case the first two hex digits of the note name will be used as a subdirectory name and the rest 38 hex digits as the file name under that directory. Similarly, when number of notes decreases a fanout tree may collapse back into a flat tree. The Git notes merge algorithm must take into account possibly different tree structures in different note branches and must properly match them against each other. Any conflict on a Git note is, by default, resolved by concatenating the two conflicting versions of the note. A delete-edit conflict is, by default, resolved by keeping the edit version. The note merge logic is pluggable and the caller may provide custom note merger that will perform different merging strategy. Additionally, it is possible to have non-note entries inside a notes tree. The merge algorithm must also take this fact into account and will try to merge such non-note entries. However, in case of any merge conflicts the merge operation will fail. Git notes merge algorithm is currently not trying to do content merge of non-note entries. Thanks to Shawn Pearce for patiently answering my questions related to this topic, giving hints and providing code snippets. Change-Id: I3b2335c76c766fd7ea25752e54087f9b19d69c88 Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
13 年之前
Merging Git notes Merging Git notes branches has several differences from merging "normal" branches. Although Git notes are initially stored as one flat tree the tree may fanout when the number of notes becomes too large for efficient access. In this case the first two hex digits of the note name will be used as a subdirectory name and the rest 38 hex digits as the file name under that directory. Similarly, when number of notes decreases a fanout tree may collapse back into a flat tree. The Git notes merge algorithm must take into account possibly different tree structures in different note branches and must properly match them against each other. Any conflict on a Git note is, by default, resolved by concatenating the two conflicting versions of the note. A delete-edit conflict is, by default, resolved by keeping the edit version. The note merge logic is pluggable and the caller may provide custom note merger that will perform different merging strategy. Additionally, it is possible to have non-note entries inside a notes tree. The merge algorithm must also take this fact into account and will try to merge such non-note entries. However, in case of any merge conflicts the merge operation will fail. Git notes merge algorithm is currently not trying to do content merge of non-note entries. Thanks to Shawn Pearce for patiently answering my questions related to this topic, giving hints and providing code snippets. Change-Id: I3b2335c76c766fd7ea25752e54087f9b19d69c88 Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
13 年之前
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. @Override
  112. public boolean hasNext() {
  113. return idx < cnt;
  114. }
  115. @Override
  116. public Note next() {
  117. if (hasNext()) {
  118. return notes[idx++];
  119. }
  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. }
  141. System.arraycopy(notes, p + 1, notes, p, cnt - p - 1);
  142. cnt--;
  143. return 0 < cnt ? this : null;
  144. } else if (noteData != null) {
  145. if (shouldSplit()) {
  146. return split().set(noteOn, noteData, or);
  147. }
  148. growIfFull();
  149. p = -(p + 1);
  150. if (p < cnt) {
  151. System.arraycopy(notes, p, notes, p + 1, cnt - p);
  152. }
  153. notes[p] = new Note(noteOn, noteData.copy());
  154. cnt++;
  155. return this;
  156. } else {
  157. return this;
  158. }
  159. }
  160. @Override
  161. ObjectId writeTree(ObjectInserter inserter) throws IOException {
  162. return inserter.insert(build());
  163. }
  164. @Override
  165. ObjectId getTreeId() {
  166. try (Formatter f = new ObjectInserter.Formatter()) {
  167. return f.idFor(build());
  168. }
  169. }
  170. private TreeFormatter build() {
  171. byte[] nameBuf = new byte[OBJECT_ID_STRING_LENGTH];
  172. int nameLen = OBJECT_ID_STRING_LENGTH - prefixLen;
  173. TreeFormatter fmt = new TreeFormatter(treeSize(nameLen));
  174. NonNoteEntry e = nonNotes;
  175. for (int i = 0; i < cnt; i++) {
  176. Note n = notes[i];
  177. n.copyTo(nameBuf, 0);
  178. while (e != null
  179. && e.pathCompare(nameBuf, prefixLen, nameLen, REGULAR_FILE) < 0) {
  180. e.format(fmt);
  181. e = e.next;
  182. }
  183. fmt.append(nameBuf, prefixLen, nameLen, REGULAR_FILE, n.getData());
  184. }
  185. for (; e != null; e = e.next)
  186. e.format(fmt);
  187. return fmt;
  188. }
  189. private int treeSize(int nameLen) {
  190. int sz = cnt * TreeFormatter.entrySize(REGULAR_FILE, nameLen);
  191. for (NonNoteEntry e = nonNotes; e != null; e = e.next)
  192. sz += e.treeEntrySize();
  193. return sz;
  194. }
  195. void parseOneEntry(AnyObjectId noteOn, AnyObjectId noteData) {
  196. growIfFull();
  197. notes[cnt++] = new Note(noteOn, noteData.copy());
  198. }
  199. @Override
  200. InMemoryNoteBucket append(Note note) {
  201. if (shouldSplit()) {
  202. return split().append(note);
  203. }
  204. growIfFull();
  205. notes[cnt++] = note;
  206. return this;
  207. }
  208. private void growIfFull() {
  209. if (notes.length == cnt) {
  210. Note[] n = new Note[notes.length * 2];
  211. System.arraycopy(notes, 0, n, 0, cnt);
  212. notes = n;
  213. }
  214. }
  215. private boolean shouldSplit() {
  216. return MAX_SIZE <= cnt && prefixLen + 2 < OBJECT_ID_STRING_LENGTH;
  217. }
  218. FanoutBucket split() {
  219. FanoutBucket n = new FanoutBucket(prefixLen);
  220. for (int i = 0; i < cnt; i++)
  221. n.append(notes[i]);
  222. n.nonNotes = nonNotes;
  223. return n;
  224. }
  225. }