Skip to content

Commit 4dbccd3

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_loose_source_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 6b6a842 commit 4dbccd3

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
int status;
364364
};
365365

366-
static const void *feed_input_zstream(struct input_stream *in_stream,
366+
static const void *feed_input_zstream(struct odb_write_stream *in_stream,
367367
unsigned long *readlen)
368368
{
369369
struct input_zstream_data *data = in_stream->data;
@@ -393,7 +393,7 @@ static void stream_blob(unsigned long size, unsigned nr)
393393
{
394394
git_zstream zstream = { 0 };
395395
struct input_zstream_data data = { 0 };
396-
struct input_stream in_stream = {
396+
struct odb_write_stream in_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_loose_source_freshen_object(struct odb_source *source,
974974
return !!check_and_freshen_source(source, oid, 1);
975975
}
976976

977-
int stream_loose_object(struct odb_source *source,
978-
struct input_stream *in_stream, size_t len,
979-
struct object_id *oid)
977+
int odb_loose_source_write_stream(struct odb_source *source,
978+
struct odb_write_stream *in_stream, size_t len,
979+
struct object_id *oid)
980980
{
981981
const struct git_hash_algo *compat = source->odb->repo->compat_hash_algo;
982982
struct object_id compat_oid;

object-file.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ int odb_loose_source_write_object(struct odb_source *source,
6767
enum object_type type, struct object_id *oid,
6868
struct object_id *compat_oid_in, unsigned flags);
6969

70+
int odb_loose_source_write_stream(struct odb_source *source,
71+
struct odb_write_stream *stream, size_t len,
72+
struct object_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
struct object_info;
174178
int parse_loose_header(const char *hdr, struct object_info *oi);
175179

176-
struct input_stream {
177-
const void *(*read)(struct input_stream *, unsigned long *len);
178-
void *data;
179-
int is_finished;
180-
};
181-
182-
int stream_loose_object(struct odb_source *source,
183-
struct input_stream *in_stream, size_t len,
184-
struct object_id *oid);
185-
186180
int force_object_loose(struct odb_source *source,
187181
const struct object_id *oid, time_t mtime);
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+
int odb_write_object_stream(struct object_database *odb,
1029+
struct odb_write_stream *stream, size_t len,
1030+
struct object_id *oid)
1031+
{
1032+
return odb_loose_source_write_stream(odb->sources, stream, len, oid);
1033+
}
1034+
10281035
struct object_database *odb_new(struct repository *repo)
10291036
{
10301037
struct object_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
return odb_write_object_ext(odb, buf, len, type, oid, NULL, 0);
493493
}
494494

495+
struct odb_write_stream {
496+
const void *(*read)(struct odb_write_stream *, unsigned long *len);
497+
void *data;
498+
int is_finished;
499+
};
500+
501+
int odb_write_object_stream(struct object_database *odb,
502+
struct odb_write_stream *stream, size_t len,
503+
struct object_id *oid);
504+
495505
#endif /* ODB_H */

0 commit comments

Comments
 (0)