Reverse pack index implementation: PackReverseIndex
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / PackIndexV1.java
blobb58dfdf7e8a255d9b055362d792ec8bd98d3a0a2
1 /*
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.lib;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.util.Arrays;
44 import java.util.Iterator;
45 import java.util.NoSuchElementException;
47 import org.spearce.jgit.errors.CorruptObjectException;
48 import org.spearce.jgit.util.NB;
50 class PackIndexV1 extends PackIndex {
51 private static final int IDX_HDR_LEN = 256 * 4;
53 private final long[] idxHeader;
55 private byte[][] idxdata;
57 private long objectCnt;
59 PackIndexV1(final InputStream fd, final byte[] hdr)
60 throws CorruptObjectException, IOException {
61 final byte[] fanoutTable = new byte[IDX_HDR_LEN];
62 System.arraycopy(hdr, 0, fanoutTable, 0, hdr.length);
63 NB.readFully(fd, fanoutTable, hdr.length, IDX_HDR_LEN - hdr.length);
65 idxHeader = new long[256]; // really unsigned 32-bit...
66 for (int k = 0; k < idxHeader.length; k++)
67 idxHeader[k] = NB.decodeUInt32(fanoutTable, k * 4);
68 idxdata = new byte[idxHeader.length][];
69 for (int k = 0; k < idxHeader.length; k++) {
70 int n;
71 if (k == 0) {
72 n = (int) (idxHeader[k]);
73 } else {
74 n = (int) (idxHeader[k] - idxHeader[k - 1]);
76 if (n > 0) {
77 idxdata[k] = new byte[n * (Constants.OBJECT_ID_LENGTH + 4)];
78 NB.readFully(fd, idxdata[k], 0, idxdata[k].length);
81 objectCnt = idxHeader[255];
84 long getObjectCount() {
85 return objectCnt;
88 @Override
89 long getOffset64Count() {
90 long n64 = 0;
91 for (final MutableEntry e : this) {
92 if (e.getOffset() >= Integer.MAX_VALUE)
93 n64++;
95 return n64;
98 @Override
99 ObjectId getObjectId(final long nthPosition) {
100 int levelOne = Arrays.binarySearch(idxHeader, nthPosition + 1);
101 long base;
102 if (levelOne >= 0) {
103 // If we hit the bucket exactly the item is in the bucket, or
104 // any bucket before it which has the same object count.
106 base = idxHeader[levelOne];
107 while (levelOne > 0 && base == idxHeader[levelOne - 1])
108 levelOne--;
109 } else {
110 // The item is in the bucket we would insert it into.
112 levelOne = -(levelOne + 1);
115 base = levelOne > 0 ? idxHeader[levelOne - 1] : 0;
116 final int p = (int) (nthPosition - base);
117 final int dataIdx = ((4 + Constants.OBJECT_ID_LENGTH) * p) + 4;
118 return ObjectId.fromRaw(idxdata[levelOne], dataIdx);
121 long findOffset(final AnyObjectId objId) {
122 final int levelOne = objId.getFirstByte();
123 byte[] data = idxdata[levelOne];
124 if (data == null)
125 return -1;
126 int high = data.length / (4 + Constants.OBJECT_ID_LENGTH);
127 int low = 0;
128 do {
129 final int mid = (low + high) / 2;
130 final int pos = ((4 + Constants.OBJECT_ID_LENGTH) * mid) + 4;
131 final int cmp = objId.compareTo(data, pos);
132 if (cmp < 0)
133 high = mid;
134 else if (cmp == 0) {
135 int b0 = data[pos - 4] & 0xff;
136 int b1 = data[pos - 3] & 0xff;
137 int b2 = data[pos - 2] & 0xff;
138 int b3 = data[pos - 1] & 0xff;
139 return (((long) b0) << 24) | (b1 << 16) | (b2 << 8) | (b3);
140 } else
141 low = mid + 1;
142 } while (low < high);
143 return -1;
146 public Iterator<MutableEntry> iterator() {
147 return new IndexV1Iterator();
150 private class IndexV1Iterator extends EntriesIterator {
151 private int levelOne;
153 private int levelTwo;
155 public MutableEntry next() {
156 for (; levelOne < idxdata.length; levelOne++) {
157 if (idxdata[levelOne] == null)
158 continue;
160 if (levelTwo < idxdata[levelOne].length) {
161 long offset = NB.decodeUInt32(idxdata[levelOne], levelTwo);
162 objectId.setOffset(offset);
163 objectId.fromRaw(idxdata[levelOne], levelTwo + 4);
164 levelTwo += Constants.OBJECT_ID_LENGTH + 4;
165 returnedNumber++;
166 return objectId;
167 } else {
168 levelTwo = 0;
171 throw new NoSuchElementException();