DfsInserter: populate full size on object insertion

We need the full size of the object to populate the object size index
later.

Save the size the PackedObjectInfo while adding objects to the
pack. Then we don't need to re-read it from the pack at indexing time.

Change-Id: I5bd7ad402df60b4637038def8ef7be2ab45faf87
This commit is contained in:
Ivan Frade 2022-10-07 14:21:48 -07:00
parent 12a4a4ccaa
commit 4d2a003b91
2 changed files with 58 additions and 6 deletions

View File

@ -29,11 +29,16 @@
import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.junit.TestRng;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.TagBuilder;
import org.eclipse.jgit.lib.TreeFormatter;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils;
import org.junit.Before;
@ -240,6 +245,50 @@ public void testNoCheckExisting() throws IOException {
}
}
@Test
public void testObjectSizePopulated() throws IOException {
// Blob
byte[] contents = Constants.encode("foo");
// Commit
PersonIdent person = new PersonIdent("Committer a", "jgit@eclipse.org");
CommitBuilder c = new CommitBuilder();
c.setAuthor(person);
c.setCommitter(person);
c.setTreeId(ObjectId
.fromString("45c4c6767a3945815371a7016532751dd558be40"));
c.setMessage("commit message");
// Tree
TreeFormatter treeBuilder = new TreeFormatter(2);
treeBuilder.append("filea", FileMode.REGULAR_FILE, ObjectId
.fromString("45c4c6767a3945815371a7016532751dd558be40"));
treeBuilder.append("fileb", FileMode.GITLINK, ObjectId
.fromString("1c458e25ca624bb8d4735bec1379a4a29ba786d0"));
// Tag
TagBuilder tagBuilder = new TagBuilder();
tagBuilder.setObjectId(
ObjectId.fromString("c97fe131649e80de55bd153e9a8d8629f7ca6932"),
Constants.OBJ_COMMIT);
tagBuilder.setTag("short name");
try (DfsInserter ins = (DfsInserter) db.newObjectInserter()) {
ObjectId aBlob = ins.insert(Constants.OBJ_BLOB, contents);
assertEquals(contents.length,
ins.objectMap.get(aBlob).getFullSize());
ObjectId aCommit = ins.insert(c);
assertEquals(174, ins.objectMap.get(aCommit).getFullSize());
ObjectId tree = ins.insert(treeBuilder);
assertEquals(66, ins.objectMap.get(tree).getFullSize());
ObjectId tag = ins.insert(tagBuilder);
assertEquals(76, ins.objectMap.get(tag).getFullSize());
}
}
private static String readString(ObjectLoader loader) throws IOException {
return RawParseUtils.decode(readStream(loader));
}

View File

@ -129,7 +129,7 @@ public ObjectId insert(int type, byte[] data, int off, int len)
long offset = beginObject(type, len);
packOut.compress.write(data, off, len);
packOut.compress.finish();
return endObject(id, offset);
return endObject(id, offset, len, type);
}
@Override
@ -148,16 +148,17 @@ public ObjectId insert(int type, long len, InputStream in)
md.update(Constants.encodeASCII(len));
md.update((byte) 0);
while (0 < len) {
int n = in.read(buf, 0, (int) Math.min(buf.length, len));
long inLength = len;
while (0 < inLength) {
int n = in.read(buf, 0, (int) Math.min(buf.length, inLength));
if (n <= 0)
throw new EOFException();
md.update(buf, 0, n);
packOut.compress.write(buf, 0, n);
len -= n;
inLength -= n;
}
packOut.compress.finish();
return endObject(md.toObjectId(), offset);
return endObject(md.toObjectId(), offset, len, type);
}
private byte[] insertBuffer(long len) {
@ -238,10 +239,12 @@ private long beginObject(int type, long len) throws IOException {
return offset;
}
private ObjectId endObject(ObjectId id, long offset) {
private ObjectId endObject(ObjectId id, long offset, long inflatedSize, int type) {
PackedObjectInfo obj = new PackedObjectInfo(id);
obj.setType(type);
obj.setOffset(offset);
obj.setCRC((int) packOut.crc32.getValue());
obj.setFullSize(inflatedSize);
objectList.add(obj);
objectMap.addIfAbsent(obj);
return id;