PackIndexV2.java

  1. /*
  2.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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.internal.storage.file;

  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.text.MessageFormat;
  47. import java.util.Arrays;
  48. import java.util.Iterator;
  49. import java.util.NoSuchElementException;
  50. import java.util.Set;

  51. import org.eclipse.jgit.errors.MissingObjectException;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  54. import org.eclipse.jgit.lib.AnyObjectId;
  55. import org.eclipse.jgit.lib.Constants;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.util.IO;
  58. import org.eclipse.jgit.util.NB;

  59. /** Support for the pack index v2 format. */
  60. class PackIndexV2 extends PackIndex {
  61.     private static final long IS_O64 = 1L << 31;

  62.     private static final int FANOUT = 256;

  63.     private static final int[] NO_INTS = {};

  64.     private static final byte[] NO_BYTES = {};

  65.     private long objectCnt;

  66.     private final long[] fanoutTable;

  67.     /** 256 arrays of contiguous object names. */
  68.     int[][] names;

  69.     /** 256 arrays of the 32 bit offset data, matching {@link #names}. */
  70.     byte[][] offset32;

  71.     /** 256 arrays of the CRC-32 of objects, matching {@link #names}. */
  72.     private byte[][] crc32;

  73.     /** 64 bit offset table. */
  74.     byte[] offset64;

  75.     PackIndexV2(final InputStream fd) throws IOException {
  76.         final byte[] fanoutRaw = new byte[4 * FANOUT];
  77.         IO.readFully(fd, fanoutRaw, 0, fanoutRaw.length);
  78.         fanoutTable = new long[FANOUT];
  79.         for (int k = 0; k < FANOUT; k++)
  80.             fanoutTable[k] = NB.decodeUInt32(fanoutRaw, k * 4);
  81.         objectCnt = fanoutTable[FANOUT - 1];

  82.         names = new int[FANOUT][];
  83.         offset32 = new byte[FANOUT][];
  84.         crc32 = new byte[FANOUT][];

  85.         // Object name table. The size we can permit per fan-out bucket
  86.         // is limited to Java's 2 GB per byte array limitation. That is
  87.         // no more than 107,374,182 objects per fan-out.
  88.         //
  89.         for (int k = 0; k < FANOUT; k++) {
  90.             final long bucketCnt;
  91.             if (k == 0)
  92.                 bucketCnt = fanoutTable[k];
  93.             else
  94.                 bucketCnt = fanoutTable[k] - fanoutTable[k - 1];

  95.             if (bucketCnt == 0) {
  96.                 names[k] = NO_INTS;
  97.                 offset32[k] = NO_BYTES;
  98.                 crc32[k] = NO_BYTES;
  99.                 continue;
  100.             } else if (bucketCnt < 0)
  101.                 throw new IOException(MessageFormat.format(
  102.                         JGitText.get().indexFileCorruptedNegativeBucketCount,
  103.                         Long.valueOf(bucketCnt)));

  104.             final long nameLen = bucketCnt * Constants.OBJECT_ID_LENGTH;
  105.             if (nameLen > Integer.MAX_VALUE - 8) // see http://stackoverflow.com/a/8381338
  106.                 throw new IOException(JGitText.get().indexFileIsTooLargeForJgit);

  107.             final int intNameLen = (int) nameLen;
  108.             final byte[] raw = new byte[intNameLen];
  109.             final int[] bin = new int[intNameLen >>> 2];
  110.             IO.readFully(fd, raw, 0, raw.length);
  111.             for (int i = 0; i < bin.length; i++)
  112.                 bin[i] = NB.decodeInt32(raw, i << 2);

  113.             names[k] = bin;
  114.             offset32[k] = new byte[(int) (bucketCnt * 4)];
  115.             crc32[k] = new byte[(int) (bucketCnt * 4)];
  116.         }

  117.         // CRC32 table.
  118.         for (int k = 0; k < FANOUT; k++)
  119.             IO.readFully(fd, crc32[k], 0, crc32[k].length);

  120.         // 32 bit offset table. Any entries with the most significant bit
  121.         // set require a 64 bit offset entry in another table.
  122.         //
  123.         int o64cnt = 0;
  124.         for (int k = 0; k < FANOUT; k++) {
  125.             final byte[] ofs = offset32[k];
  126.             IO.readFully(fd, ofs, 0, ofs.length);
  127.             for (int p = 0; p < ofs.length; p += 4)
  128.                 if (ofs[p] < 0)
  129.                     o64cnt++;
  130.         }

  131.         // 64 bit offset table. Most objects should not require an entry.
  132.         //
  133.         if (o64cnt > 0) {
  134.             offset64 = new byte[o64cnt * 8];
  135.             IO.readFully(fd, offset64, 0, offset64.length);
  136.         } else {
  137.             offset64 = NO_BYTES;
  138.         }

  139.         packChecksum = new byte[20];
  140.         IO.readFully(fd, packChecksum, 0, packChecksum.length);
  141.     }

  142.     /** {@inheritDoc} */
  143.     @Override
  144.     public long getObjectCount() {
  145.         return objectCnt;
  146.     }

  147.     /** {@inheritDoc} */
  148.     @Override
  149.     public long getOffset64Count() {
  150.         return offset64.length / 8;
  151.     }

  152.     private int findLevelOne(long nthPosition) {
  153.         int levelOne = Arrays.binarySearch(fanoutTable, nthPosition + 1);
  154.         if (levelOne >= 0) {
  155.             // If we hit the bucket exactly the item is in the bucket, or
  156.             // any bucket before it which has the same object count.
  157.             //
  158.             long base = fanoutTable[levelOne];
  159.             while (levelOne > 0 && base == fanoutTable[levelOne - 1])
  160.                 levelOne--;
  161.         } else {
  162.             // The item is in the bucket we would insert it into.
  163.             //
  164.             levelOne = -(levelOne + 1);
  165.         }
  166.         return levelOne;
  167.     }

  168.     private int getLevelTwo(long nthPosition, int levelOne) {
  169.         final long base = levelOne > 0 ? fanoutTable[levelOne - 1] : 0;
  170.         return (int) (nthPosition - base);
  171.     }

  172.     /** {@inheritDoc} */
  173.     @Override
  174.     public ObjectId getObjectId(long nthPosition) {
  175.         final int levelOne = findLevelOne(nthPosition);
  176.         final int p = getLevelTwo(nthPosition, levelOne);
  177.         final int p4 = p << 2;
  178.         return ObjectId.fromRaw(names[levelOne], p4 + p); // p * 5
  179.     }

  180.     /** {@inheritDoc} */
  181.     @Override
  182.     public long getOffset(long nthPosition) {
  183.         final int levelOne = findLevelOne(nthPosition);
  184.         final int levelTwo = getLevelTwo(nthPosition, levelOne);
  185.         return getOffset(levelOne, levelTwo);
  186.     }

  187.     /** {@inheritDoc} */
  188.     @Override
  189.     public long findOffset(AnyObjectId objId) {
  190.         final int levelOne = objId.getFirstByte();
  191.         final int levelTwo = binarySearchLevelTwo(objId, levelOne);
  192.         if (levelTwo == -1)
  193.             return -1;
  194.         return getOffset(levelOne, levelTwo);
  195.     }

  196.     private long getOffset(int levelOne, int levelTwo) {
  197.         final long p = NB.decodeUInt32(offset32[levelOne], levelTwo << 2);
  198.         if ((p & IS_O64) != 0)
  199.             return NB.decodeUInt64(offset64, (8 * (int) (p & ~IS_O64)));
  200.         return p;
  201.     }

  202.     /** {@inheritDoc} */
  203.     @Override
  204.     public long findCRC32(AnyObjectId objId) throws MissingObjectException {
  205.         final int levelOne = objId.getFirstByte();
  206.         final int levelTwo = binarySearchLevelTwo(objId, levelOne);
  207.         if (levelTwo == -1)
  208.             throw new MissingObjectException(objId.copy(), "unknown"); //$NON-NLS-1$
  209.         return NB.decodeUInt32(crc32[levelOne], levelTwo << 2);
  210.     }

  211.     /** {@inheritDoc} */
  212.     @Override
  213.     public boolean hasCRC32Support() {
  214.         return true;
  215.     }

  216.     /** {@inheritDoc} */
  217.     @Override
  218.     public Iterator<MutableEntry> iterator() {
  219.         return new EntriesIteratorV2();
  220.     }

  221.     /** {@inheritDoc} */
  222.     @Override
  223.     public void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
  224.             int matchLimit) throws IOException {
  225.         int[] data = names[id.getFirstByte()];
  226.         int max = offset32[id.getFirstByte()].length >>> 2;
  227.         int high = max;
  228.         if (high == 0)
  229.             return;
  230.         int low = 0;
  231.         do {
  232.             int p = (low + high) >>> 1;
  233.             final int cmp = id.prefixCompare(data, idOffset(p));
  234.             if (cmp < 0)
  235.                 high = p;
  236.             else if (cmp == 0) {
  237.                 // We may have landed in the middle of the matches.  Move
  238.                 // backwards to the start of matches, then walk forwards.
  239.                 //
  240.                 while (0 < p && id.prefixCompare(data, idOffset(p - 1)) == 0)
  241.                     p--;
  242.                 for (; p < max && id.prefixCompare(data, idOffset(p)) == 0; p++) {
  243.                     matches.add(ObjectId.fromRaw(data, idOffset(p)));
  244.                     if (matches.size() > matchLimit)
  245.                         break;
  246.                 }
  247.                 return;
  248.             } else
  249.                 low = p + 1;
  250.         } while (low < high);
  251.     }

  252.     private static int idOffset(int p) {
  253.         return (p << 2) + p; // p * 5
  254.     }

  255.     private int binarySearchLevelTwo(AnyObjectId objId, int levelOne) {
  256.         final int[] data = names[levelOne];
  257.         int high = offset32[levelOne].length >>> 2;
  258.         if (high == 0)
  259.             return -1;
  260.         int low = 0;
  261.         do {
  262.             final int mid = (low + high) >>> 1;
  263.             final int mid4 = mid << 2;
  264.             final int cmp;

  265.             cmp = objId.compareTo(data, mid4 + mid); // mid * 5
  266.             if (cmp < 0)
  267.                 high = mid;
  268.             else if (cmp == 0) {
  269.                 return mid;
  270.             } else
  271.                 low = mid + 1;
  272.         } while (low < high);
  273.         return -1;
  274.     }

  275.     private class EntriesIteratorV2 extends EntriesIterator {
  276.         int levelOne;

  277.         int levelTwo;

  278.         @Override
  279.         protected MutableEntry initEntry() {
  280.             return new MutableEntry() {
  281.                 @Override
  282.                 protected void ensureId() {
  283.                     idBuffer.fromRaw(names[levelOne], levelTwo
  284.                             - Constants.OBJECT_ID_LENGTH / 4);
  285.                 }
  286.             };
  287.         }

  288.         @Override
  289.         public MutableEntry next() {
  290.             for (; levelOne < names.length; levelOne++) {
  291.                 if (levelTwo < names[levelOne].length) {
  292.                     int idx = levelTwo / (Constants.OBJECT_ID_LENGTH / 4) * 4;
  293.                     long offset = NB.decodeUInt32(offset32[levelOne], idx);
  294.                     if ((offset & IS_O64) != 0) {
  295.                         idx = (8 * (int) (offset & ~IS_O64));
  296.                         offset = NB.decodeUInt64(offset64, idx);
  297.                     }
  298.                     entry.offset = offset;

  299.                     levelTwo += Constants.OBJECT_ID_LENGTH / 4;
  300.                     returnedNumber++;
  301.                     return entry;
  302.                 }
  303.                 levelTwo = 0;
  304.             }
  305.             throw new NoSuchElementException();
  306.         }
  307.     }

  308. }