Skip to content

Commit 3e5e360

Browse files
pks-tgitster
authored andcommitted
object-file: refactor writing objects via a stream
We have two different ways to write an object into the database: - We either provide the full buffer and write the object all at once. - Or we provide an input stream that has a `read()` function so that we can chunk the object. The latter is especially used for large objects, where it may be too expensive to hold the complete object in memory all at once. While we already have `odb_write_object()` at the ODB-layer, we don't have an equivalent for streaming an object. Introduce a new function `odb_write_object_stream()` to address this gap so that callers don't have to be aware of the inner workings of how to stream an object to disk with a specific object source. Rename `stream_loose_object()` to `odb_source_loose_write_stream()` to clarify its scope. This matches our modern best practices around how to name functions. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bfb1b2b commit 3e5e360

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

‎builtin/unpack-objects.c‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ struct input_zstream_data{
363363
intstatus;
364364
};
365365

366-
staticconstvoid*feed_input_zstream(structinput_stream*in_stream,
366+
staticconstvoid*feed_input_zstream(structodb_write_stream*in_stream,
367367
unsigned long*readlen)
368368
{
369369
structinput_zstream_data*data=in_stream->data;
@@ -393,7 +393,7 @@ static void stream_blob(unsigned long size, unsigned nr)
393393
{
394394
git_zstreamzstream={0 };
395395
structinput_zstream_datadata={0 };
396-
structinput_streamin_stream={
396+
structodb_write_streamin_stream={
397397
.read=feed_input_zstream,
398398
.data=&data,
399399
};
@@ -402,8 +402,7 @@ static void stream_blob(unsigned long size, unsigned nr)
402402
data.zstream=&zstream;
403403
git_inflate_init(&zstream);
404404

405-
if (stream_loose_object(the_repository->objects->sources,
406-
&in_stream, size, &info->oid))
405+
if (odb_write_object_stream(the_repository->objects, &in_stream, size, &info->oid))
407406
die(_("failed to write object in stream"));
408407

409408
if (data.status!=Z_STREAM_END)

‎object-file.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -974,9 +974,9 @@ int odb_source_loose_freshen_object(struct odb_source *source,
974974
return !!check_and_freshen_source(source, oid, 1);
975975
}
976976

977-
intstream_loose_object(structodb_source*source,
978-
structinput_stream*in_stream, size_tlen,
979-
structobject_id*oid)
977+
intodb_source_loose_write_stream(structodb_source*source,
978+
structodb_write_stream*in_stream, size_tlen,
979+
structobject_id*oid)
980980
{
981981
conststructgit_hash_algo*compat=source->odb->repo->compat_hash_algo;
982982
structobject_idcompat_oid;

‎object-file.h‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ int odb_source_loose_write_object(struct odb_source *source,
6767
enumobject_typetype, structobject_id*oid,
6868
structobject_id*compat_oid_in, unsignedflags);
6969

70+
intodb_source_loose_write_stream(structodb_source*source,
71+
structodb_write_stream*stream, size_tlen,
72+
structobject_id*oid);
73+
7074
/*
7175
* Populate and return the loose object cache array corresponding to the
7276
* given object ID.
@@ -173,16 +177,6 @@ enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
173177
structobject_info;
174178
intparse_loose_header(constchar*hdr, structobject_info*oi);
175179

176-
structinput_stream{
177-
constvoid*(*read)(structinput_stream*, unsigned long*len);
178-
void*data;
179-
intis_finished;
180-
};
181-
182-
intstream_loose_object(structodb_source*source,
183-
structinput_stream*in_stream, size_tlen,
184-
structobject_id*oid);
185-
186180
intforce_object_loose(structodb_source*source,
187181
conststructobject_id*oid, time_tmtime);
188182

‎odb.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,13 @@ int odb_write_object_ext(struct object_database *odb,
10251025
oid, compat_oid, flags);
10261026
}
10271027

1028+
intodb_write_object_stream(structobject_database*odb,
1029+
structodb_write_stream*stream, size_tlen,
1030+
structobject_id*oid)
1031+
{
1032+
returnodb_source_loose_write_stream(odb->sources, stream, len, oid);
1033+
}
1034+
10281035
structobject_database*odb_new(structrepository*repo)
10291036
{
10301037
structobject_database*o=xmalloc(sizeof(*o));

‎odb.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,14 @@ static inline int odb_write_object(struct object_database *odb,
492492
returnodb_write_object_ext(odb, buf, len, type, oid, NULL, 0);
493493
}
494494

495+
structodb_write_stream{
496+
constvoid*(*read)(structodb_write_stream*, unsigned long*len);
497+
void*data;
498+
intis_finished;
499+
};
500+
501+
intodb_write_object_stream(structobject_database*odb,
502+
structodb_write_stream*stream, size_tlen,
503+
structobject_id*oid);
504+
495505
#endif/* ODB_H */

0 commit comments

Comments
(0)