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
This commit is contained in:
Ivan Frade 2023-04-12 15:05:22 -07:00
parent 4d2a003b91
commit cb99ff5bbb
2 changed files with 43 additions and 0 deletions

View 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 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));
}

View 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.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<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;