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.

PackIndexV1.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2007-2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  5. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.util.Arrays;
  50. import java.util.Iterator;
  51. import java.util.NoSuchElementException;
  52. import org.eclipse.jgit.errors.CorruptObjectException;
  53. import org.eclipse.jgit.util.IO;
  54. import org.eclipse.jgit.util.NB;
  55. class PackIndexV1 extends PackIndex {
  56. private static final int IDX_HDR_LEN = 256 * 4;
  57. private final long[] idxHeader;
  58. private byte[][] idxdata;
  59. private long objectCnt;
  60. PackIndexV1(final InputStream fd, final byte[] hdr)
  61. throws CorruptObjectException, IOException {
  62. final byte[] fanoutTable = new byte[IDX_HDR_LEN];
  63. System.arraycopy(hdr, 0, fanoutTable, 0, hdr.length);
  64. IO.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]);
  75. }
  76. if (n > 0) {
  77. idxdata[k] = new byte[n * (Constants.OBJECT_ID_LENGTH + 4)];
  78. IO.readFully(fd, idxdata[k], 0, idxdata[k].length);
  79. }
  80. }
  81. objectCnt = idxHeader[255];
  82. packChecksum = new byte[20];
  83. IO.readFully(fd, packChecksum, 0, packChecksum.length);
  84. }
  85. long getObjectCount() {
  86. return objectCnt;
  87. }
  88. @Override
  89. long getOffset64Count() {
  90. long n64 = 0;
  91. for (final MutableEntry e : this) {
  92. if (e.getOffset() >= Integer.MAX_VALUE)
  93. n64++;
  94. }
  95. return n64;
  96. }
  97. @Override
  98. ObjectId getObjectId(final long nthPosition) {
  99. int levelOne = Arrays.binarySearch(idxHeader, nthPosition + 1);
  100. long base;
  101. if (levelOne >= 0) {
  102. // If we hit the bucket exactly the item is in the bucket, or
  103. // any bucket before it which has the same object count.
  104. //
  105. base = idxHeader[levelOne];
  106. while (levelOne > 0 && base == idxHeader[levelOne - 1])
  107. levelOne--;
  108. } else {
  109. // The item is in the bucket we would insert it into.
  110. //
  111. levelOne = -(levelOne + 1);
  112. }
  113. base = levelOne > 0 ? idxHeader[levelOne - 1] : 0;
  114. final int p = (int) (nthPosition - base);
  115. final int dataIdx = ((4 + Constants.OBJECT_ID_LENGTH) * p) + 4;
  116. return ObjectId.fromRaw(idxdata[levelOne], dataIdx);
  117. }
  118. long findOffset(final AnyObjectId objId) {
  119. final int levelOne = objId.getFirstByte();
  120. byte[] data = idxdata[levelOne];
  121. if (data == null)
  122. return -1;
  123. int high = data.length / (4 + Constants.OBJECT_ID_LENGTH);
  124. int low = 0;
  125. do {
  126. final int mid = (low + high) >>> 1;
  127. final int pos = ((4 + Constants.OBJECT_ID_LENGTH) * mid) + 4;
  128. final int cmp = objId.compareTo(data, pos);
  129. if (cmp < 0)
  130. high = mid;
  131. else if (cmp == 0) {
  132. int b0 = data[pos - 4] & 0xff;
  133. int b1 = data[pos - 3] & 0xff;
  134. int b2 = data[pos - 2] & 0xff;
  135. int b3 = data[pos - 1] & 0xff;
  136. return (((long) b0) << 24) | (b1 << 16) | (b2 << 8) | (b3);
  137. } else
  138. low = mid + 1;
  139. } while (low < high);
  140. return -1;
  141. }
  142. @Override
  143. long findCRC32(AnyObjectId objId) {
  144. throw new UnsupportedOperationException();
  145. }
  146. @Override
  147. boolean hasCRC32Support() {
  148. return false;
  149. }
  150. public Iterator<MutableEntry> iterator() {
  151. return new IndexV1Iterator();
  152. }
  153. private class IndexV1Iterator extends EntriesIterator {
  154. private int levelOne;
  155. private int levelTwo;
  156. @Override
  157. protected MutableEntry initEntry() {
  158. return new MutableEntry() {
  159. protected void ensureId() {
  160. idBuffer.fromRaw(idxdata[levelOne], levelTwo
  161. - Constants.OBJECT_ID_LENGTH);
  162. }
  163. };
  164. }
  165. public MutableEntry next() {
  166. for (; levelOne < idxdata.length; levelOne++) {
  167. if (idxdata[levelOne] == null)
  168. continue;
  169. if (levelTwo < idxdata[levelOne].length) {
  170. entry.offset = NB.decodeUInt32(idxdata[levelOne], levelTwo);
  171. levelTwo += Constants.OBJECT_ID_LENGTH + 4;
  172. returnedNumber++;
  173. return entry;
  174. }
  175. levelTwo = 0;
  176. }
  177. throw new NoSuchElementException();
  178. }
  179. }
  180. }