aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java
diff options
context:
space:
mode:
authorShawn O. Pearce <spearce@spearce.org>2010-11-04 18:23:40 -0700
committerChris Aniszczyk <caniszczyk@gmail.com>2010-11-11 10:23:38 -0600
commitb81b97fbddaab4bc2615fea5301f3eb39e3b03bd (patch)
tree6eb452d7841e9a26cc55de243154dc26ec82f0ac /org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java
parent936820988fa9e46c358af7971806faa6515546f6 (diff)
downloadjgit-b81b97fbddaab4bc2615fea5301f3eb39e3b03bd.tar.gz
jgit-b81b97fbddaab4bc2615fea5301f3eb39e3b03bd.zip
Lazy load note subtrees from fanout levels
Instead of reading a note tree recursively up front when the NoteMap is loaded, read only the root tree and load subtrees on demand when they are accessed by the application. This gives a lower latency to read a note for the recent commits on a branch, as only the paths that are needed get read. Given a 2/38 style fanout, the tree will fully load when 256 objects have been accessed by the application. But unlike the prior version of NoteMap, the NoteMap will load faster and answer lookups sooner, as the loading time for all 256 levels is spread out across each of the get() requests. Given a 2/2/36 style fanout, the tree won't need to fully load until about 65,536 objects are accessed. To simplify the implementation we only support the flat layout (all notes in the top level tree), or a 2/38, 2/2/36, 2/2/2/34, through 2/.../2 style fanout. Unlike C Git we don't support reading the old experimental 4/36 fanout. This is sufficient because C Git won't create the 4/36 style fanout when creating or updating a notes tree, and there really aren't any in the wild today. Change-Id: I6099b35916a8404762f31e9c11f632e43e0c1bfd Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java111
1 files changed, 111 insertions, 0 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java b/org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java
new file mode 100644
index 0000000000..66d773a05d
--- /dev/null
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/notes/LeafBucket.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2010, Google Inc.
+ * and other copyright owners as documented in the project's IP log.
+ *
+ * This program and the accompanying materials are made available
+ * under the terms of the Eclipse Distribution License v1.0 which
+ * accompanies this distribution, is reproduced below, and is
+ * available at http://www.eclipse.org/org/documents/edl-v10.php
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * - Neither the name of the Eclipse Foundation, Inc. nor the
+ * names of its contributors may be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.eclipse.jgit.notes;
+
+import org.eclipse.jgit.lib.AnyObjectId;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.ObjectReader;
+
+/**
+ * A note tree holding only notes, with no subtrees.
+ *
+ * The leaf bucket contains on average less than 256 notes, all of whom share
+ * the same leading prefix. If a notes branch has less than 256 notes, the top
+ * level tree of the branch should be a LeafBucket. Once a notes branch has more
+ * than 256 notes, the root should be a {@link FanoutBucket} and the LeafBucket
+ * will appear only as a cell of a FanoutBucket.
+ *
+ * Entries within the LeafBucket are stored sorted by ObjectId, and lookup is
+ * performed using binary search. As the entry list should contain fewer than
+ * 256 elements, the average number of compares to find an element should be
+ * less than 8 due to the O(log N) lookup behavior.
+ *
+ * A LeafBucket must be parsed from a tree object by {@link NoteParser}.
+ */
+class LeafBucket extends InMemoryNoteBucket {
+ /** All note blobs in this bucket, sorted sequentially. */
+ private Note[] notes;
+
+ /** Number of items in {@link #notes}. */
+ private int cnt;
+
+ LeafBucket(int prefixLen) {
+ super(prefixLen);
+ notes = new Note[4];
+ }
+
+ private int search(AnyObjectId objId) {
+ int low = 0;
+ int high = cnt;
+ while (low < high) {
+ int mid = (low + high) >>> 1;
+ int cmp = objId.compareTo(notes[mid]);
+ if (cmp < 0)
+ high = mid;
+ else if (cmp == 0)
+ return mid;
+ else
+ low = mid + 1;
+ }
+ return -(low + 1);
+ }
+
+ ObjectId get(AnyObjectId objId, ObjectReader or) {
+ int idx = search(objId);
+ return 0 <= idx ? notes[idx].getData() : null;
+ }
+
+ void parseOneEntry(AnyObjectId noteOn, AnyObjectId noteData) {
+ growIfFull();
+ notes[cnt++] = new Note(noteOn, noteData.copy());
+ }
+
+ private void growIfFull() {
+ if (notes.length == cnt) {
+ Note[] n = new Note[notes.length * 2];
+ System.arraycopy(notes, 0, n, 0, cnt);
+ notes = n;
+ }
+ }
+}