]> source.dussan.org Git - jgit.git/commitdiff
DfsPackCompactor: write object size index 68/1203468/4
authorIvan Frade <ifrade@google.com>
Fri, 1 Nov 2024 15:58:27 +0000 (08:58 -0700)
committerIvan Frade <ifrade@google.com>
Fri, 1 Nov 2024 22:59:21 +0000 (15:59 -0700)
Currently the compactor is not writing the object size index for
packs. As it is using PackWriter to generate the packs, it needs to
explicitely call the writes of each extension.

Invoke writeObjectSizeIndex in the compactor. The pack writer will
write one if the configuration says so.

Change-Id: I8d6bbbb5bd67bfc7dd511aa76463512b1e86a45d

org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsPackCompacterTest.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackCompactor.java

index c516e30f50d3b8e322ff9fc03d433a7a1e3f0d52..c3b6aa85a2e665396abdf8ece962a38ee440e6bb 100644 (file)
@@ -12,13 +12,18 @@ package org.eclipse.jgit.internal.storage.dfs;
 
 import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.COMPACT;
 import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.INSERT;
+import static org.eclipse.jgit.internal.storage.pack.PackExt.OBJECT_SIZE_INDEX;
 import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.Optional;
 
 import org.eclipse.jgit.junit.TestRepository;
+import org.eclipse.jgit.lib.ConfigConstants;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.junit.Before;
 import org.junit.Test;
@@ -98,6 +103,40 @@ public class DfsPackCompacterTest {
                                pack.getPackDescription().getEstimatedPackSize());
        }
 
+       @Test
+       public void testObjectSizeIndexWritten() throws Exception {
+               writeObjectSizeIndex(repo, true);
+               RevCommit commit0 = commit().message("0").create();
+               RevCommit commit1 = commit().message("1").parent(commit0).create();
+               git.update("master", commit1);
+
+               compact();
+
+               Optional<DfsPackFile> compactPack = Arrays.stream(odb.getPacks())
+                               .filter(pack -> pack.getPackDescription()
+                                               .getPackSource() == COMPACT)
+                               .findFirst();
+               assertTrue(compactPack.isPresent());
+               assertTrue(compactPack.get().getPackDescription().hasFileExt(OBJECT_SIZE_INDEX));
+       }
+
+       @Test
+       public void testObjectSizeIndexNotWritten() throws Exception {
+               writeObjectSizeIndex(repo, false);
+               RevCommit commit0 = commit().message("0").create();
+               RevCommit commit1 = commit().message("1").parent(commit0).create();
+               git.update("master", commit1);
+
+               compact();
+
+               Optional<DfsPackFile> compactPack = Arrays.stream(odb.getPacks())
+                               .filter(pack -> pack.getPackDescription()
+                                               .getPackSource() == COMPACT)
+                               .findFirst();
+               assertTrue(compactPack.isPresent());
+               assertFalse(compactPack.get().getPackDescription().hasFileExt(OBJECT_SIZE_INDEX));
+       }
+
        private TestRepository<InMemoryRepository>.CommitBuilder commit() {
                return git.commit();
        }
@@ -108,4 +147,9 @@ public class DfsPackCompacterTest {
                compactor.compact(null);
                odb.clearCache();
        }
+
+       private static void writeObjectSizeIndex(DfsRepository repo, boolean should) {
+               repo.getConfig().setInt(ConfigConstants.CONFIG_PACK_SECTION, null,
+                               ConfigConstants.CONFIG_KEY_MIN_BYTES_OBJ_SIZE_INDEX, should ? 0 : -1);
+       }
 }
index b32cddae77120fc83fb609dc5e77156d2acde181..f9c01b9d6e04be869358694f54fb7f20097dff36 100644 (file)
@@ -12,6 +12,7 @@ package org.eclipse.jgit.internal.storage.dfs;
 
 import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.COMPACT;
 import static org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource.GC;
+import static org.eclipse.jgit.internal.storage.pack.PackExt.OBJECT_SIZE_INDEX;
 import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
 import static org.eclipse.jgit.internal.storage.pack.PackExt.REFTABLE;
 import static org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation.PACK_DELTA;
@@ -44,6 +45,7 @@ import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.storage.pack.PackConfig;
 import org.eclipse.jgit.storage.pack.PackStatistics;
 import org.eclipse.jgit.util.BlockList;
+import org.eclipse.jgit.util.io.CountingOutputStream;
 
 /**
  * Combine several pack files into one pack.
@@ -247,6 +249,7 @@ public class DfsPackCompactor {
                        try {
                                writePack(objdb, outDesc, pw, pm);
                                writeIndex(objdb, outDesc, pw);
+                               writeObjectSizeIndex(objdb, outDesc, pw);
 
                                PackStatistics stats = pw.getStatistics();
 
@@ -459,6 +462,20 @@ public class DfsPackCompactor {
                pw.writeIndex(objdb.getPackIndexWriter(pack, pw.getIndexVersion()));
        }
 
+       private static void writeObjectSizeIndex(DfsObjDatabase objdb,
+                                                                                        DfsPackDescription pack,
+                                                                                        PackWriter pw) throws IOException {
+               try (DfsOutputStream out = objdb.writeFile(pack, OBJECT_SIZE_INDEX)) {
+                       CountingOutputStream cnt = new CountingOutputStream(out);
+                       pw.writeObjectSizeIndex(cnt);
+                       if (cnt.getCount() > 0) {
+                               pack.addFileExt(OBJECT_SIZE_INDEX);
+                               pack.setFileSize(OBJECT_SIZE_INDEX, cnt.getCount());
+                               pack.setBlockSize(OBJECT_SIZE_INDEX, out.blockSize());
+                       }
+               }
+       }
+
        static ReftableConfig configureReftable(ReftableConfig cfg,
                        DfsOutputStream out) {
                int bs = out.blockSize();