[EGIT PATCH 08/20] CRC32 support for PackIndex

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add findCRC32() and hasCRC32Support() methods in PackIndex with
implementation for index v2.

Signed-off-by: Marek Zawirski <marek.zawirski@xxxxxxxxx>
---
 .../src/org/spearce/jgit/lib/PackIndex.java        |   23 ++++++++++++
 .../src/org/spearce/jgit/lib/PackIndexV1.java      |   10 +++++
 .../src/org/spearce/jgit/lib/PackIndexV2.java      |   38 ++++++++++++-------
 3 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndex.java b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndex.java
index 3935d4f..e34cd36 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndex.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndex.java
@@ -44,6 +44,7 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.Iterator;
 
+import org.spearce.jgit.errors.MissingObjectException;
 import org.spearce.jgit.util.NB;
 
 /**
@@ -151,6 +152,28 @@ public abstract class PackIndex implements Iterable<PackIndex.MutableEntry> {
 	abstract long findOffset(AnyObjectId objId);
 
 	/**
+	 * Retrieve stored CRC32 checksum of the requested object raw-data
+	 * (including header).
+	 * 
+	 * @param objId
+	 *            id of object to look for
+	 * @return CRC32 checksum of specified object (at 32 less significant bits)
+	 * @throws MissingObjectException
+	 *             when requested ObjectId was not found in this index
+	 * @throws UnsupportedOperationException
+	 *             when this index doesn't support CRC32 checksum
+	 */
+	abstract long findCRC32(AnyObjectId objId) throws MissingObjectException,
+			UnsupportedOperationException;
+
+	/**
+	 * Check whether this index supports (has) CRC32 checksums for objects.
+	 * 
+	 * @return true if CRC32 is stored, false otherwise
+	 */
+	abstract boolean hasCRC32Support();
+
+	/**
 	 * Represent mutable entry of pack index consisting of object id and offset
 	 * in pack (both mutable).
 	 * 
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV1.java b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV1.java
index b8d9de3..86b939a 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV1.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV1.java
@@ -107,6 +107,16 @@ class PackIndexV1 extends PackIndex {
 		return -1;
 	}
 
+	@Override
+	long findCRC32(AnyObjectId objId) {
+		throw new UnsupportedOperationException();
+	}
+
+	@Override
+	boolean hasCRC32Support() {
+		return false;
+	}
+
 	public Iterator<MutableEntry> iterator() {
 		return new IndexV1Iterator();
 	}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV2.java b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV2.java
index a0b9827..fc1f08b 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV2.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV2.java
@@ -37,12 +37,12 @@
 
 package org.spearce.jgit.lib;
 
-import java.io.EOFException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
+import org.spearce.jgit.errors.MissingObjectException;
 import org.spearce.jgit.util.NB;
 
 /** Support for the pack index v2 format. */
@@ -63,6 +63,9 @@ class PackIndexV2 extends PackIndex {
 	/** 256 arrays of the 32 bit offset data, matching {@link #names}. */
 	private byte[][] offset32;
 
+	/** 256 arrays of the CRC-32 of objects, matching {@link #names}. */
+	private byte[][] crc32;
+
 	/** 64 bit offset table. */
 	private byte[] offset64;
 
@@ -76,6 +79,7 @@ class PackIndexV2 extends PackIndex {
 
 		names = new int[FANOUT][];
 		offset32 = new byte[FANOUT][];
+		crc32 = new byte[FANOUT][];
 
 		// Object name table. The size we can permit per fan-out bucket
 		// is limited to Java's 2 GB per byte array limitation. That is
@@ -91,6 +95,7 @@ class PackIndexV2 extends PackIndex {
 			if (bucketCnt == 0) {
 				names[k] = NO_INTS;
 				offset32[k] = NO_BYTES;
+				crc32[k] = NO_BYTES;
 				continue;
 			}
 
@@ -107,11 +112,12 @@ class PackIndexV2 extends PackIndex {
 
 			names[k] = bin;
 			offset32[k] = new byte[(int) (bucketCnt * 4)];
+			crc32[k] = new byte[(int) (bucketCnt * 4)];
 		}
 
-		// CRC32 table. Currently unused.
-		//
-		skipFully(fd, objectCnt * 4);
+		// CRC32 table.
+		for (int k = 0; k < FANOUT; k++)
+			NB.readFully(fd, crc32[k], 0, crc32[k].length);
 
 		// 32 bit offset table. Any entries with the most significant bit
 		// set require a 64 bit offset entry in another table.
@@ -135,16 +141,6 @@ class PackIndexV2 extends PackIndex {
 		}
 	}
 
-	private static void skipFully(final InputStream fd, long toSkip)
-			throws IOException {
-		while (toSkip > 0) {
-			final long r = fd.skip(toSkip);
-			if (r <= 0)
-				throw new EOFException("Cannot skip index section.");
-			toSkip -= r;
-		}
-	}
-
 	@Override
 	long getObjectCount() {
 		return objectCnt;
@@ -162,6 +158,20 @@ class PackIndexV2 extends PackIndex {
 		return p;
 	}
 
+	@Override
+	long findCRC32(AnyObjectId objId) throws MissingObjectException {
+		final int levelOne = objId.getFirstByte();
+		final int levelTwo = binarySearchLevelTwo(objId, levelOne);
+		if (levelTwo == -1)
+			throw new MissingObjectException(objId.copy(), "unknown");
+		return NB.decodeUInt32(crc32[levelOne], levelTwo << 2);
+	}
+
+	@Override
+	boolean hasCRC32Support() {
+		return true;
+	}
+
 	public Iterator<MutableEntry> iterator() {
 		return new EntriesIteratorV2();
 	}
-- 
1.5.5.1

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux