From cb99ff5bbb61a44e08de5c1d8ce01ffe2393344c Mon Sep 17 00:00:00 2001 From: Ivan Frade Date: Wed, 12 Apr 2023 15:05:22 -0700 Subject: [PATCH] DfsInserter: generate object size index if config says so 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 --- .../internal/storage/dfs/DfsInserterTest.java | 23 +++++++++++++++++++ .../internal/storage/dfs/DfsInserter.java | 20 ++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java index a54c81be41..b84a0b00ae 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/dfs/DfsInserterTest.java @@ -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)); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java index 5f25905a5b..2f8d964e0a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java @@ -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 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; -- 2.39.5