Skip to content

Commit 1cfc514

Browse files
pks-tgitster
authored andcommitted
streaming: refactor interface to be object-database-centric
Refactor the streaming interface to be centered around object databases instead of centered around the repository. Rename the functions accordingly. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d56cdfe commit 1cfc514

File tree

7 files changed

+71
-51
lines changed

7 files changed

+71
-51
lines changed

archive-tar.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,16 @@ static int stream_blocked(struct repository *r, const struct object_id *oid)
135135
char buf[BLOCKSIZE];
136136
ssize_t readlen;
137137

138-
st = open_istream(r, oid, &type, &sz, NULL);
138+
st = odb_read_object_stream(r->objects, oid, &type, &sz, NULL);
139139
if (!st)
140140
return error(_("cannot stream blob %s"), oid_to_hex(oid));
141141
for (;;) {
142-
readlen = read_istream(st, buf, sizeof(buf));
142+
readlen = odb_read_stream_read(st, buf, sizeof(buf));
143143
if (readlen <= 0)
144144
break;
145145
do_write_blocked(buf, readlen);
146146
}
147-
close_istream(st);
147+
odb_read_stream_close(st);
148148
if (!readlen)
149149
finish_record();
150150
return readlen;

archive-zip.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ static int write_zip_entry(struct archiver_args *args,
348348

349349
if (!buffer) {
350350
enum object_type type;
351-
stream = open_istream(args->repo, oid, &type, &size,
352-
NULL);
351+
stream = odb_read_object_stream(args->repo->objects, oid,
352+
&type, &size, NULL);
353353
if (!stream)
354354
return error(_("cannot stream blob %s"),
355355
oid_to_hex(oid));
@@ -429,7 +429,7 @@ static int write_zip_entry(struct archiver_args *args,
429429
ssize_t readlen;
430430

431431
for (;;) {
432-
readlen = read_istream(stream, buf, sizeof(buf));
432+
readlen = odb_read_stream_read(stream, buf, sizeof(buf));
433433
if (readlen <= 0)
434434
break;
435435
crc = crc32(crc, buf, readlen);
@@ -439,7 +439,7 @@ static int write_zip_entry(struct archiver_args *args,
439439
buf, readlen);
440440
write_or_die(1, buf, readlen);
441441
}
442-
close_istream(stream);
442+
odb_read_stream_close(stream);
443443
if (readlen)
444444
return readlen;
445445

@@ -462,7 +462,7 @@ static int write_zip_entry(struct archiver_args *args,
462462
zstream.avail_out = sizeof(compressed);
463463

464464
for (;;) {
465-
readlen = read_istream(stream, buf, sizeof(buf));
465+
readlen = odb_read_stream_read(stream, buf, sizeof(buf));
466466
if (readlen <= 0)
467467
break;
468468
crc = crc32(crc, buf, readlen);
@@ -486,7 +486,7 @@ static int write_zip_entry(struct archiver_args *args,
486486
}
487487

488488
}
489-
close_istream(stream);
489+
odb_read_stream_close(stream);
490490
if (readlen)
491491
return readlen;
492492

builtin/index-pack.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ static int compare_objects(const unsigned char *buf, unsigned long size,
779779
}
780780

781781
while (size) {
782-
ssize_t len = read_istream(data->st, data->buf, size);
782+
ssize_t len = odb_read_stream_read(data->st, data->buf, size);
783783
if (len == 0)
784784
die(_("SHA1 COLLISION FOUND WITH %s !"),
785785
oid_to_hex(&data->entry->idx.oid));
@@ -807,15 +807,15 @@ static int check_collison(struct object_entry *entry)
807807

808808
memset(&data, 0, sizeof(data));
809809
data.entry = entry;
810-
data.st = open_istream(the_repository, &entry->idx.oid, &type, &size,
811-
NULL);
810+
data.st = odb_read_object_stream(the_repository->objects, &entry->idx.oid,
811+
&type, &size, NULL);
812812
if (!data.st)
813813
return -1;
814814
if (size != entry->size || type != entry->type)
815815
die(_("SHA1 COLLISION FOUND WITH %s !"),
816816
oid_to_hex(&entry->idx.oid));
817817
unpack_data(entry, compare_objects, &data);
818-
close_istream(data.st);
818+
odb_read_stream_close(data.st);
819819
free(data.buf);
820820
return 0;
821821
}

builtin/pack-objects.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ static unsigned long write_large_blob_data(struct odb_read_stream *st, struct ha
417417
for (;;) {
418418
ssize_t readlen;
419419
int zret = Z_OK;
420-
readlen = read_istream(st, ibuf, sizeof(ibuf));
420+
readlen = odb_read_stream_read(st, ibuf, sizeof(ibuf));
421421
if (readlen == -1)
422422
die(_("unable to read %s"), oid_to_hex(oid));
423423

@@ -520,8 +520,8 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
520520
if (oe_type(entry) == OBJ_BLOB &&
521521
oe_size_greater_than(&to_pack, entry,
522522
repo_settings_get_big_file_threshold(the_repository)) &&
523-
(st = open_istream(the_repository, &entry->idx.oid, &type,
524-
&size, NULL)) != NULL)
523+
(st = odb_read_object_stream(the_repository->objects, &entry->idx.oid,
524+
&type, &size, NULL)) != NULL)
525525
buf = NULL;
526526
else {
527527
buf = odb_read_object(the_repository->objects,
@@ -577,7 +577,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
577577
dheader[--pos] = 128 | (--ofs & 127);
578578
if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
579579
if (st)
580-
close_istream(st);
580+
odb_read_stream_close(st);
581581
free(buf);
582582
return 0;
583583
}
@@ -591,7 +591,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
591591
*/
592592
if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
593593
if (st)
594-
close_istream(st);
594+
odb_read_stream_close(st);
595595
free(buf);
596596
return 0;
597597
}
@@ -601,15 +601,15 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
601601
} else {
602602
if (limit && hdrlen + datalen + hashsz >= limit) {
603603
if (st)
604-
close_istream(st);
604+
odb_read_stream_close(st);
605605
free(buf);
606606
return 0;
607607
}
608608
hashwrite(f, header, hdrlen);
609609
}
610610
if (st) {
611611
datalen = write_large_blob_data(st, f, &entry->idx.oid);
612-
close_istream(st);
612+
odb_read_stream_close(st);
613613
} else {
614614
hashwrite(f, buf, datalen);
615615
free(buf);

object-file.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ int stream_object_signature(struct repository *r, const struct object_id *oid)
139139
char hdr[MAX_HEADER_LEN];
140140
int hdrlen;
141141

142-
st = open_istream(r, oid, &obj_type, &size, NULL);
142+
st = odb_read_object_stream(r->objects, oid, &obj_type, &size, NULL);
143143
if (!st)
144144
return -1;
145145

@@ -151,18 +151,18 @@ int stream_object_signature(struct repository *r, const struct object_id *oid)
151151
git_hash_update(&c, hdr, hdrlen);
152152
for (;;) {
153153
char buf[1024 * 16];
154-
ssize_t readlen = read_istream(st, buf, sizeof(buf));
154+
ssize_t readlen = odb_read_stream_read(st, buf, sizeof(buf));
155155

156156
if (readlen < 0) {
157-
close_istream(st);
157+
odb_read_stream_close(st);
158158
return -1;
159159
}
160160
if (!readlen)
161161
break;
162162
git_hash_update(&c, buf, readlen);
163163
}
164164
git_hash_final_oid(&real_oid, &c);
165-
close_istream(st);
165+
odb_read_stream_close(st);
166166
return !oideq(oid, &real_oid) ? -1 : 0;
167167
}
168168

streaming.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static int close_istream_filtered(struct odb_read_stream *_fs)
3535
{
3636
struct odb_filtered_read_stream *fs = (struct odb_filtered_read_stream *)_fs;
3737
free_stream_filter(fs->filter);
38-
return close_istream(fs->upstream);
38+
return odb_read_stream_close(fs->upstream);
3939
}
4040

4141
static ssize_t read_istream_filtered(struct odb_read_stream *_fs, char *buf,
@@ -87,7 +87,7 @@ static ssize_t read_istream_filtered(struct odb_read_stream *_fs, char *buf,
8787

8888
/* refill the input from the upstream */
8989
if (!fs->input_finished) {
90-
fs->i_end = read_istream(fs->upstream, fs->ibuf, FILTER_BUFFER);
90+
fs->i_end = odb_read_stream_read(fs->upstream, fs->ibuf, FILTER_BUFFER);
9191
if (fs->i_end < 0)
9292
return -1;
9393
if (fs->i_end)
@@ -149,7 +149,7 @@ static ssize_t read_istream_incore(struct odb_read_stream *_st, char *buf, size_
149149
}
150150

151151
static int open_istream_incore(struct odb_read_stream **out,
152-
struct repository *r,
152+
struct object_database *odb,
153153
const struct object_id *oid)
154154
{
155155
struct object_info oi = OBJECT_INFO_INIT;
@@ -163,7 +163,7 @@ static int open_istream_incore(struct odb_read_stream **out,
163163
oi.typep = &stream.base.type;
164164
oi.sizep = &stream.base.size;
165165
oi.contentp = (void **)&stream.buf;
166-
ret = odb_read_object_info_extended(r->objects, oid, &oi,
166+
ret = odb_read_object_info_extended(odb, oid, &oi,
167167
OBJECT_INFO_DIE_IF_CORRUPT);
168168
if (ret)
169169
return ret;
@@ -180,57 +180,57 @@ static int open_istream_incore(struct odb_read_stream **out,
180180
*****************************************************************************/
181181

182182
static int istream_source(struct odb_read_stream **out,
183-
struct repository *r,
183+
struct object_database *odb,
184184
const struct object_id *oid)
185185
{
186186
struct odb_source *source;
187187

188-
if (!packfile_store_read_object_stream(out, r->objects->packfiles, oid))
188+
if (!packfile_store_read_object_stream(out, odb->packfiles, oid))
189189
return 0;
190190

191-
odb_prepare_alternates(r->objects);
192-
for (source = r->objects->sources; source; source = source->next)
191+
odb_prepare_alternates(odb);
192+
for (source = odb->sources; source; source = source->next)
193193
if (!odb_source_loose_read_object_stream(out, source, oid))
194194
return 0;
195195

196-
return open_istream_incore(out, r, oid);
196+
return open_istream_incore(out, odb, oid);
197197
}
198198

199199
/****************************************************************
200200
* Users of streaming interface
201201
****************************************************************/
202202

203-
int close_istream(struct odb_read_stream *st)
203+
int odb_read_stream_close(struct odb_read_stream *st)
204204
{
205205
int r = st->close(st);
206206
free(st);
207207
return r;
208208
}
209209

210-
ssize_t read_istream(struct odb_read_stream *st, void *buf, size_t sz)
210+
ssize_t odb_read_stream_read(struct odb_read_stream *st, void *buf, size_t sz)
211211
{
212212
return st->read(st, buf, sz);
213213
}
214214

215-
struct odb_read_stream *open_istream(struct repository *r,
216-
const struct object_id *oid,
217-
enum object_type *type,
218-
unsigned long *size,
219-
struct stream_filter *filter)
215+
struct odb_read_stream *odb_read_object_stream(struct object_database *odb,
216+
const struct object_id *oid,
217+
enum object_type *type,
218+
unsigned long *size,
219+
struct stream_filter *filter)
220220
{
221221
struct odb_read_stream *st;
222-
const struct object_id *real = lookup_replace_object(r, oid);
222+
const struct object_id *real = lookup_replace_object(odb->repo, oid);
223223
int ret;
224224

225-
ret = istream_source(&st, r, real);
225+
ret = istream_source(&st, odb, real);
226226
if (ret)
227227
return NULL;
228228

229229
if (filter) {
230230
/* Add "&& !is_null_stream_filter(filter)" for performance */
231231
struct odb_read_stream *nst = attach_stream_filter(st, filter);
232232
if (!nst) {
233-
close_istream(st);
233+
odb_read_stream_close(st);
234234
return NULL;
235235
}
236236
st = nst;
@@ -253,7 +253,7 @@ int odb_stream_blob_to_fd(struct object_database *odb,
253253
ssize_t kept = 0;
254254
int result = -1;
255255

256-
st = open_istream(odb->repo, oid, &type, &sz, filter);
256+
st = odb_read_object_stream(odb, oid, &type, &sz, filter);
257257
if (!st) {
258258
if (filter)
259259
free_stream_filter(filter);
@@ -264,7 +264,7 @@ int odb_stream_blob_to_fd(struct object_database *odb,
264264
for (;;) {
265265
char buf[1024 * 16];
266266
ssize_t wrote, holeto;
267-
ssize_t readlen = read_istream(st, buf, sizeof(buf));
267+
ssize_t readlen = odb_read_stream_read(st, buf, sizeof(buf));
268268

269269
if (readlen < 0)
270270
goto close_and_exit;
@@ -295,6 +295,6 @@ int odb_stream_blob_to_fd(struct object_database *odb,
295295
result = 0;
296296

297297
close_and_exit:
298-
close_istream(st);
298+
odb_read_stream_close(st);
299299
return result;
300300
}

streaming.h

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,31 @@ struct odb_read_stream {
2424
unsigned long size; /* inflated size of full object */
2525
};
2626

27-
struct odb_read_stream *open_istream(struct repository *, const struct object_id *,
28-
enum object_type *, unsigned long *,
29-
struct stream_filter *);
30-
int close_istream(struct odb_read_stream *);
31-
ssize_t read_istream(struct odb_read_stream *, void *, size_t);
27+
/*
28+
* Create a new object stream for the given object database. Populates the type
29+
* and size pointers with the object's info. An optional filter can be used to
30+
* transform the object's content.
31+
*
32+
* Returns the stream on success, a `NULL` pointer otherwise.
33+
*/
34+
struct odb_read_stream *odb_read_object_stream(struct object_database *odb,
35+
const struct object_id *oid,
36+
enum object_type *type,
37+
unsigned long *size,
38+
struct stream_filter *filter);
39+
40+
/*
41+
* Close the given read stream and release all resources associated with it.
42+
* Returns 0 on success, a negative error code otherwise.
43+
*/
44+
int odb_read_stream_close(struct odb_read_stream *stream);
45+
46+
/*
47+
* Read data from the stream into the buffer. Returns 0 on EOF and the number
48+
* of bytes read on success. Returns a negative error code in case reading from
49+
* the stream fails.
50+
*/
51+
ssize_t odb_read_stream_read(struct odb_read_stream *stream, void *buf, size_t len);
3252

3353
/*
3454
* Look up the object by its ID and write the full contents to the file

0 commit comments

Comments
 (0)