]> source.dussan.org Git - jgit.git/commitdiff
DfsInserter: generate object size index if config says so 98/201198/19
authorIvan Frade <ifrade@google.com>
Wed, 12 Apr 2023 22:05:22 +0000 (15:05 -0700)
committerIvan Frade <ifrade@google.com>
Fri, 14 Jul 2023 17:34:46 +0000 (10:34 -0700)
DfsInserter receives objects and on flush() writes a pack and its
primary index.

Teach the DfsInserter to write also the object size index if the
config says so.

Change-Id: I89308312f8fd898d4c714a9b68ff948d3663800b

org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java

index a54c81be41c4a588dab0c81735070c589e363bc1..b84a0b00aefda0a98fe843a98cba11cf0d71209e 100644 (file)
@@ -10,6 +10,8 @@
 
 package org.eclipse.jgit.internal.storage.dfs;
 
+import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_MIN_BYTES_OBJ_SIZE_INDEX;
+import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_PACK_SECTION;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertSame;
@@ -289,6 +291,27 @@ public class DfsInserterTest {
                }
        }
 
+       @Test
+       public void testObjectSizeIndexOnInsert() throws IOException {
+               db.getConfig().setInt(CONFIG_PACK_SECTION, null,
+                               CONFIG_KEY_MIN_BYTES_OBJ_SIZE_INDEX, 0);
+
+               byte[] contents = Constants.encode("foo");
+               ObjectId fooId;
+               try (ObjectInserter ins = db.newObjectInserter()) {
+                       fooId = ins.insert(Constants.OBJ_BLOB, contents);
+                       ins.flush();
+               }
+
+               DfsReader reader = db.getObjectDatabase().newReader();
+               assertEquals(1, db.getObjectDatabase().listPacks().size());
+               DfsPackFile insertPack = db.getObjectDatabase().getPacks()[0];
+               assertEquals(PackSource.INSERT,
+                               insertPack.getPackDescription().getPackSource());
+               assertTrue(insertPack.hasObjectSizeIndex(reader));
+               assertEquals(contents.length, insertPack.getIndexedObjectSize(reader, fooId));
+       }
+
        private static String readString(ObjectLoader loader) throws IOException {
                return RawParseUtils.decode(readStream(loader));
        }
index 5f25905a5b99add9ee2859b2af185d8432fadc39..2f8d964e0aa734f2b91181142a33ce9a0151f9ce 100644 (file)
@@ -11,6 +11,7 @@
 package org.eclipse.jgit.internal.storage.dfs;
 
 import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
+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.lib.Constants.OBJ_OFS_DELTA;
 import static org.eclipse.jgit.lib.Constants.OBJ_REF_DELTA;
@@ -42,6 +43,7 @@ import org.eclipse.jgit.errors.LargeObjectException;
 import org.eclipse.jgit.internal.JGitText;
 import org.eclipse.jgit.internal.storage.file.PackIndex;
 import org.eclipse.jgit.internal.storage.file.PackIndexWriter;
+import org.eclipse.jgit.internal.storage.file.PackObjectSizeIndexWriter;
 import org.eclipse.jgit.internal.storage.pack.PackExt;
 import org.eclipse.jgit.lib.AbbreviatedObjectId;
 import org.eclipse.jgit.lib.AnyObjectId;
@@ -52,6 +54,7 @@ import org.eclipse.jgit.lib.ObjectInserter;
 import org.eclipse.jgit.lib.ObjectLoader;
 import org.eclipse.jgit.lib.ObjectReader;
 import org.eclipse.jgit.lib.ObjectStream;
+import org.eclipse.jgit.storage.pack.PackConfig;
 import org.eclipse.jgit.transport.PackedObjectInfo;
 import org.eclipse.jgit.util.BlockList;
 import org.eclipse.jgit.util.IO;
@@ -192,6 +195,11 @@ public class DfsInserter extends ObjectInserter {
                sortObjectsById();
 
                PackIndex index = writePackIndex(packDsc, packHash, objectList);
+               PackConfig pConfig = new PackConfig(db.getRepository().getConfig());
+               if (pConfig.isWriteObjSizeIndex()) {
+                       writeObjectSizeIndex(packDsc, objectList,
+                                       pConfig.getMinBytesForObjSizeIndex());
+               }
                db.commitPack(Collections.singletonList(packDsc), null);
                rollback = false;
 
@@ -314,6 +322,18 @@ public class DfsInserter extends ObjectInserter {
                PackIndexWriter.createVersion(out, INDEX_VERSION).write(list, packHash);
        }
 
+       void writeObjectSizeIndex(DfsPackDescription pack,
+                       List<PackedObjectInfo> packedObjs, int minSize) throws IOException {
+               try (DfsOutputStream os = db.writeFile(pack, PackExt.OBJECT_SIZE_INDEX);
+                               CountingOutputStream cnt = new CountingOutputStream(os)) {
+                       PackObjectSizeIndexWriter.createWriter(os, minSize)
+                                       .write(packedObjs);
+                       pack.addFileExt(OBJECT_SIZE_INDEX);
+                       pack.setBlockSize(OBJECT_SIZE_INDEX, os.blockSize());
+                       pack.setFileSize(OBJECT_SIZE_INDEX, cnt.getCount());
+               }
+       }
+
        private class PackStream extends OutputStream {
                private final DfsOutputStream out;
                private final MessageDigest md;