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.

FanoutBucket.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.FileMode.TREE;
  45. import java.io.IOException;
  46. import java.util.Iterator;
  47. import java.util.NoSuchElementException;
  48. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  49. import org.eclipse.jgit.lib.AnyObjectId;
  50. import org.eclipse.jgit.lib.MutableObjectId;
  51. import org.eclipse.jgit.lib.ObjectId;
  52. import org.eclipse.jgit.lib.ObjectInserter;
  53. import org.eclipse.jgit.lib.ObjectReader;
  54. import org.eclipse.jgit.lib.TreeFormatter;
  55. /**
  56. * A note tree holding only note subtrees, each named using a 2 digit hex name.
  57. *
  58. * The fanout buckets/trees contain on average 256 subtrees, naming the subtrees
  59. * by a slice of the ObjectId contained within them, from "00" through "ff".
  60. *
  61. * Each fanout bucket has a {@link #prefixLen} that defines how many digits it
  62. * skips in an ObjectId before it gets to the digits matching {@link #table}.
  63. *
  64. * The root tree has {@code prefixLen == 0}, and thus does not skip any digits.
  65. * For ObjectId "c0ffee...", the note (if it exists) will be stored within the
  66. * bucket {@code table[0xc0]}.
  67. *
  68. * The first level tree has {@code prefixLen == 2}, and thus skips the first two
  69. * digits. For the same example "c0ffee..." object, its note would be found
  70. * within the {@code table[0xff]} bucket (as first 2 digits "c0" are skipped).
  71. *
  72. * Each subtree is loaded on-demand, reducing startup latency for reads that
  73. * only need to examine a few objects. However, due to the rather uniform
  74. * distribution of the SHA-1 hash that is used for ObjectIds, accessing 256
  75. * objects is very likely to load all of the subtrees into memory.
  76. *
  77. * A FanoutBucket must be parsed from a tree object by {@link NoteParser}.
  78. */
  79. class FanoutBucket extends InMemoryNoteBucket {
  80. /**
  81. * Fan-out table similar to the PackIndex structure.
  82. *
  83. * Notes for an object are stored within the sub-bucket that is held here as
  84. * {@code table[ objectId.getByte( prefixLen / 2 ) ]}. If the slot is null
  85. * there are no notes with that prefix.
  86. */
  87. private final NoteBucket[] table;
  88. /** Number of non-null slots in {@link #table}. */
  89. private int cnt;
  90. FanoutBucket(int prefixLen) {
  91. super(prefixLen);
  92. table = new NoteBucket[256];
  93. }
  94. void setBucket(int cell, ObjectId id) {
  95. table[cell] = new LazyNoteBucket(id);
  96. cnt++;
  97. }
  98. void setBucket(int cell, InMemoryNoteBucket bucket) {
  99. table[cell] = bucket;
  100. cnt++;
  101. }
  102. @Override
  103. Note getNote(AnyObjectId objId, ObjectReader or) throws IOException {
  104. NoteBucket b = table[cell(objId)];
  105. return b != null ? b.getNote(objId, or) : null;
  106. }
  107. NoteBucket getBucket(int cell) {
  108. return table[cell];
  109. }
  110. static InMemoryNoteBucket loadIfLazy(NoteBucket b, AnyObjectId prefix,
  111. ObjectReader or) throws IOException {
  112. if (b == null)
  113. return null;
  114. if (b instanceof InMemoryNoteBucket)
  115. return (InMemoryNoteBucket) b;
  116. return ((LazyNoteBucket) b).load(prefix, or);
  117. }
  118. @Override
  119. Iterator<Note> iterator(AnyObjectId objId, final ObjectReader reader)
  120. throws IOException {
  121. final MutableObjectId id = new MutableObjectId();
  122. id.fromObjectId(objId);
  123. return new Iterator<Note>() {
  124. private int cell;
  125. private Iterator<Note> itr;
  126. public boolean hasNext() {
  127. if (itr != null && itr.hasNext())
  128. return true;
  129. for (; cell < table.length; cell++) {
  130. NoteBucket b = table[cell];
  131. if (b == null)
  132. continue;
  133. try {
  134. id.setByte(prefixLen >> 1, cell);
  135. itr = b.iterator(id, reader);
  136. } catch (IOException err) {
  137. throw new RuntimeException(err);
  138. }
  139. if (itr.hasNext()) {
  140. cell++;
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. public Note next() {
  147. if (hasNext())
  148. return itr.next();
  149. else
  150. throw new NoSuchElementException();
  151. }
  152. public void remove() {
  153. throw new UnsupportedOperationException();
  154. }
  155. };
  156. }
  157. @Override
  158. int estimateSize(AnyObjectId noteOn, ObjectReader or) throws IOException {
  159. // If most of this fan-out is full, estimate it should still be split.
  160. if (LeafBucket.MAX_SIZE * 3 / 4 <= cnt)
  161. return 1 + LeafBucket.MAX_SIZE;
  162. // Due to the uniform distribution of ObjectIds, having less nodes full
  163. // indicates a good chance the total number of children below here
  164. // is less than the MAX_SIZE split point. Get a more accurate count.
  165. MutableObjectId id = new MutableObjectId();
  166. id.fromObjectId(noteOn);
  167. int sz = 0;
  168. for (int cell = 0; cell < 256; cell++) {
  169. NoteBucket b = table[cell];
  170. if (b == null)
  171. continue;
  172. id.setByte(prefixLen >> 1, cell);
  173. sz += b.estimateSize(id, or);
  174. if (LeafBucket.MAX_SIZE < sz)
  175. break;
  176. }
  177. return sz;
  178. }
  179. @Override
  180. InMemoryNoteBucket set(AnyObjectId noteOn, AnyObjectId noteData,
  181. ObjectReader or) throws IOException {
  182. int cell = cell(noteOn);
  183. NoteBucket b = table[cell];
  184. if (b == null) {
  185. if (noteData == null)
  186. return this;
  187. LeafBucket n = new LeafBucket(prefixLen + 2);
  188. table[cell] = n.set(noteOn, noteData, or);
  189. cnt++;
  190. return this;
  191. } else {
  192. NoteBucket n = b.set(noteOn, noteData, or);
  193. if (n == null) {
  194. table[cell] = null;
  195. cnt--;
  196. if (cnt == 0)
  197. return null;
  198. return contractIfTooSmall(noteOn, or);
  199. } else if (n != b) {
  200. table[cell] = n;
  201. }
  202. return this;
  203. }
  204. }
  205. InMemoryNoteBucket contractIfTooSmall(AnyObjectId noteOn, ObjectReader or)
  206. throws IOException {
  207. if (estimateSize(noteOn, or) < LeafBucket.MAX_SIZE) {
  208. // We are small enough to just contract to a single leaf.
  209. InMemoryNoteBucket r = new LeafBucket(prefixLen);
  210. for (Iterator<Note> i = iterator(noteOn, or); i.hasNext();)
  211. r = r.append(i.next());
  212. r.nonNotes = nonNotes;
  213. return r;
  214. }
  215. return this;
  216. }
  217. private static final byte[] hexchar = { '0', '1', '2', '3', '4', '5', '6',
  218. '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  219. @Override
  220. ObjectId writeTree(ObjectInserter inserter) throws IOException {
  221. return inserter.insert(build(true, inserter));
  222. }
  223. ObjectId getTreeId() {
  224. try {
  225. return new ObjectInserter.Formatter().idFor(build(false, null));
  226. } catch (IOException e) {
  227. // should never happen as we are not inserting
  228. throw new RuntimeException(e);
  229. }
  230. }
  231. private TreeFormatter build(boolean insert, ObjectInserter inserter)
  232. throws IOException {
  233. byte[] nameBuf = new byte[2];
  234. TreeFormatter fmt = new TreeFormatter(treeSize());
  235. NonNoteEntry e = nonNotes;
  236. for (int cell = 0; cell < 256; cell++) {
  237. NoteBucket b = table[cell];
  238. if (b == null)
  239. continue;
  240. nameBuf[0] = hexchar[cell >>> 4];
  241. nameBuf[1] = hexchar[cell & 0x0f];
  242. while (e != null && e.pathCompare(nameBuf, 0, 2, TREE) < 0) {
  243. e.format(fmt);
  244. e = e.next;
  245. }
  246. ObjectId id;
  247. if (insert) {
  248. id = b.writeTree(inserter);
  249. } else {
  250. id = b.getTreeId();
  251. }
  252. fmt.append(nameBuf, 0, 2, TREE, id);
  253. }
  254. for (; e != null; e = e.next)
  255. e.format(fmt);
  256. return fmt;
  257. }
  258. private int treeSize() {
  259. int sz = cnt * TreeFormatter.entrySize(TREE, 2);
  260. for (NonNoteEntry e = nonNotes; e != null; e = e.next)
  261. sz += e.treeEntrySize();
  262. return sz;
  263. }
  264. @Override
  265. InMemoryNoteBucket append(Note note) {
  266. int cell = cell(note);
  267. InMemoryNoteBucket b = (InMemoryNoteBucket) table[cell];
  268. if (b == null) {
  269. LeafBucket n = new LeafBucket(prefixLen + 2);
  270. table[cell] = n.append(note);
  271. cnt++;
  272. } else {
  273. InMemoryNoteBucket n = b.append(note);
  274. if (n != b)
  275. table[cell] = n;
  276. }
  277. return this;
  278. }
  279. private int cell(AnyObjectId id) {
  280. return id.getByte(prefixLen >> 1);
  281. }
  282. private class LazyNoteBucket extends NoteBucket {
  283. private final ObjectId treeId;
  284. LazyNoteBucket(ObjectId treeId) {
  285. this.treeId = treeId;
  286. }
  287. @Override
  288. Note getNote(AnyObjectId objId, ObjectReader or) throws IOException {
  289. return load(objId, or).getNote(objId, or);
  290. }
  291. @Override
  292. Iterator<Note> iterator(AnyObjectId objId, ObjectReader reader)
  293. throws IOException {
  294. return load(objId, reader).iterator(objId, reader);
  295. }
  296. @Override
  297. int estimateSize(AnyObjectId objId, ObjectReader or) throws IOException {
  298. return load(objId, or).estimateSize(objId, or);
  299. }
  300. @Override
  301. InMemoryNoteBucket set(AnyObjectId noteOn, AnyObjectId noteData,
  302. ObjectReader or) throws IOException {
  303. return load(noteOn, or).set(noteOn, noteData, or);
  304. }
  305. @Override
  306. ObjectId writeTree(ObjectInserter inserter) {
  307. return treeId;
  308. }
  309. @Override
  310. ObjectId getTreeId() {
  311. return treeId;
  312. }
  313. private InMemoryNoteBucket load(AnyObjectId prefix, ObjectReader or)
  314. throws IOException {
  315. AbbreviatedObjectId p = prefix.abbreviate(prefixLen + 2);
  316. InMemoryNoteBucket self = NoteParser.parse(p, treeId, or);
  317. table[cell(prefix)] = self;
  318. return self;
  319. }
  320. }
  321. }