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 a54c81be4..b84a0b00a 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 void testObjectSizePopulated() throws IOException { } } + @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 5f25905a5..2f8d964e0 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.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.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 void flush() throws IOException { 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 @@ private static void index(OutputStream out, byte[] packHash, 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;