[JGIT PATCH 04/21] Refactor PackWriter to hold onto the sorted object list

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

 



When creating pack files we sometimes need the sorted object list
of all contents for two reasons.  The first is to get the name of
the pack, which computeName() returns today.  The other reason is
to generate a corresponding .idx file for this pack, to support
random access into the data.

Since not all uses of PackWriter require the sorted object name list
(for example streaming the pack to a network socket) the sorting is
done on demand, and cached to avoid needing to do it a second time.

Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx>
---
 .../src/org/spearce/jgit/lib/PackWriter.java       |   24 ++++++++++++-------
 1 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/PackWriter.java b/org.spearce.jgit/src/org/spearce/jgit/lib/PackWriter.java
index ccc6cfe..0f4cbb4 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/PackWriter.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PackWriter.java
@@ -180,6 +180,8 @@ public class PackWriter {
 
 	private final WindowCursor windowCursor = new WindowCursor();
 
+	private List<ObjectToPack> sortedByName;
+
 	private boolean reuseDeltas = DEFAULT_REUSE_DELTAS;
 
 	private boolean reuseObjects = DEFAULT_REUSE_OBJECTS;
@@ -470,22 +472,26 @@ public class PackWriter {
 	 * @return ObjectId representing SHA-1 name of a pack that was created.
 	 */
 	public ObjectId computeName() {
-		final ArrayList<ObjectToPack> sorted = new ArrayList<ObjectToPack>(
-				objectsMap.size());
-		for (List<ObjectToPack> list : objectsLists) {
-			for (ObjectToPack otp : list)
-				sorted.add(otp);
-		}
-
 		final MessageDigest md = Constants.newMessageDigest();
-		Collections.sort(sorted);
-		for (ObjectToPack otp : sorted) {
+		for (ObjectToPack otp : sortByName()) {
 			otp.copyRawTo(buf, 0);
 			md.update(buf, 0, Constants.OBJECT_ID_LENGTH);
 		}
 		return ObjectId.fromRaw(md.digest());
 	}
 
+	private List<ObjectToPack> sortByName() {
+		if (sortedByName == null) {
+			sortedByName = new ArrayList<ObjectToPack>(objectsMap.size());
+			for (List<ObjectToPack> list : objectsLists) {
+				for (ObjectToPack otp : list)
+					sortedByName.add(otp);
+			}
+			Collections.sort(sortedByName);
+		}
+		return sortedByName;
+	}
+
 	private void writePackInternal() throws IOException {
 		if (reuseDeltas || reuseObjects)
 			searchForReuse();
-- 
1.5.6.74.g8a5e

--
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