123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- /*
- * Copyright (C) 2008-2009, Google Inc.
- * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
- * Copyright (C) 2007-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
- * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
- * 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.lib;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
-
- import org.eclipse.jgit.errors.CorruptObjectException;
- import org.eclipse.jgit.util.IO;
- import org.eclipse.jgit.util.NB;
-
- class PackIndexV1 extends PackIndex {
- private static final int IDX_HDR_LEN = 256 * 4;
-
- private final long[] idxHeader;
-
- private byte[][] idxdata;
-
- private long objectCnt;
-
- PackIndexV1(final InputStream fd, final byte[] hdr)
- throws CorruptObjectException, IOException {
- final byte[] fanoutTable = new byte[IDX_HDR_LEN];
- System.arraycopy(hdr, 0, fanoutTable, 0, hdr.length);
- IO.readFully(fd, fanoutTable, hdr.length, IDX_HDR_LEN - hdr.length);
-
- idxHeader = new long[256]; // really unsigned 32-bit...
- for (int k = 0; k < idxHeader.length; k++)
- idxHeader[k] = NB.decodeUInt32(fanoutTable, k * 4);
- idxdata = new byte[idxHeader.length][];
- for (int k = 0; k < idxHeader.length; k++) {
- int n;
- if (k == 0) {
- n = (int) (idxHeader[k]);
- } else {
- n = (int) (idxHeader[k] - idxHeader[k - 1]);
- }
- if (n > 0) {
- idxdata[k] = new byte[n * (Constants.OBJECT_ID_LENGTH + 4)];
- IO.readFully(fd, idxdata[k], 0, idxdata[k].length);
- }
- }
- objectCnt = idxHeader[255];
-
- packChecksum = new byte[20];
- IO.readFully(fd, packChecksum, 0, packChecksum.length);
- }
-
- long getObjectCount() {
- return objectCnt;
- }
-
- @Override
- long getOffset64Count() {
- long n64 = 0;
- for (final MutableEntry e : this) {
- if (e.getOffset() >= Integer.MAX_VALUE)
- n64++;
- }
- return n64;
- }
-
- @Override
- ObjectId getObjectId(final long nthPosition) {
- int levelOne = Arrays.binarySearch(idxHeader, nthPosition + 1);
- long base;
- if (levelOne >= 0) {
- // If we hit the bucket exactly the item is in the bucket, or
- // any bucket before it which has the same object count.
- //
- base = idxHeader[levelOne];
- while (levelOne > 0 && base == idxHeader[levelOne - 1])
- levelOne--;
- } else {
- // The item is in the bucket we would insert it into.
- //
- levelOne = -(levelOne + 1);
- }
-
- base = levelOne > 0 ? idxHeader[levelOne - 1] : 0;
- final int p = (int) (nthPosition - base);
- final int dataIdx = ((4 + Constants.OBJECT_ID_LENGTH) * p) + 4;
- return ObjectId.fromRaw(idxdata[levelOne], dataIdx);
- }
-
- long findOffset(final AnyObjectId objId) {
- final int levelOne = objId.getFirstByte();
- byte[] data = idxdata[levelOne];
- if (data == null)
- return -1;
- int high = data.length / (4 + Constants.OBJECT_ID_LENGTH);
- int low = 0;
- do {
- final int mid = (low + high) >>> 1;
- final int pos = ((4 + Constants.OBJECT_ID_LENGTH) * mid) + 4;
- final int cmp = objId.compareTo(data, pos);
- if (cmp < 0)
- high = mid;
- else if (cmp == 0) {
- int b0 = data[pos - 4] & 0xff;
- int b1 = data[pos - 3] & 0xff;
- int b2 = data[pos - 2] & 0xff;
- int b3 = data[pos - 1] & 0xff;
- return (((long) b0) << 24) | (b1 << 16) | (b2 << 8) | (b3);
- } else
- low = mid + 1;
- } while (low < high);
- return -1;
- }
-
- @Override
- long findCRC32(AnyObjectId objId) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- boolean hasCRC32Support() {
- return false;
- }
-
- public Iterator<MutableEntry> iterator() {
- return new IndexV1Iterator();
- }
-
- private class IndexV1Iterator extends EntriesIterator {
- private int levelOne;
-
- private int levelTwo;
-
- @Override
- protected MutableEntry initEntry() {
- return new MutableEntry() {
- protected void ensureId() {
- idBuffer.fromRaw(idxdata[levelOne], levelTwo
- - Constants.OBJECT_ID_LENGTH);
- }
- };
- }
-
- public MutableEntry next() {
- for (; levelOne < idxdata.length; levelOne++) {
- if (idxdata[levelOne] == null)
- continue;
- if (levelTwo < idxdata[levelOne].length) {
- entry.offset = NB.decodeUInt32(idxdata[levelOne], levelTwo);
- levelTwo += Constants.OBJECT_ID_LENGTH + 4;
- returnedNumber++;
- return entry;
- }
- levelTwo = 0;
- }
- throw new NoSuchElementException();
- }
- }
- }
|