[JGIT PATCH 04/21] Refactor PackWriter to hold onto the sorted object list
- From
Shawn O. Pearce <spearce@spearce.org>
- Date
- Jun 29, 2008, 07:59 UTC
- Message-ID
- <1214726371-93520-5-git-send-email-spearce@spearce.org>
- In-Reply-To
- <1214726371-93520-4-git-send-email-spearce@spearce.org>
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@spearce.org> --- .../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