Skip to content

Commit be4c070

Browse files
pks-tttaylorr
authored andcommitted
reftable: convert from strbuf to reftable_buf
Convert the reftable library to use the `reftable_buf` interface instead of the `strbuf` interface. This is mostly a mechanical change via sed(1) with some manual fixes where functions for `strbuf` and `reftable_buf` differ. The converted code does not yet handle allocation failures. This will be handled in subsequent commits. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 81eddda commit be4c070

24 files changed

+374
-371
lines changed

reftable/basics.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ int names_equal(const char **a, const char **b)
260260
return a[i] == b[i];
261261
}
262262

263-
int common_prefix_size(struct strbuf *a, struct strbuf *b)
263+
int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b)
264264
{
265265
int p = 0;
266266
for (; p < a->len && p < b->len; p++) {

reftable/basics.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ int reftable_buf_setlen(struct reftable_buf *buf, size_t len);
5757
int reftable_buf_cmp(const struct reftable_buf *a, const struct reftable_buf *b);
5858

5959
/*
60-
* Add the given bytes to the buffer. Returns 0 on success,
60+
* Append `len` bytes from `data` to the buffer. This function works with
61+
* arbitrary byte sequences, including ones that contain embedded NUL
62+
* characters. As such, we use `void *` as input type. Returns 0 on success,
6163
* REFTABLE_OUT_OF_MEMORY_ERROR on allocation failure.
6264
*/
6365
int reftable_buf_add(struct reftable_buf *buf, const void *data, size_t len);
@@ -144,8 +146,7 @@ char *reftable_strdup(const char *str);
144146
#endif
145147

146148
/* Find the longest shared prefix size of `a` and `b` */
147-
struct strbuf;
148-
int common_prefix_size(struct strbuf *a, struct strbuf *b);
149+
int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b);
149150

150151
int hash_size(uint32_t id);
151152

reftable/block.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int footer_size(int version)
3838
}
3939

4040
static int block_writer_register_restart(struct block_writer *w, int n,
41-
int is_restart, struct strbuf *key)
41+
int is_restart, struct reftable_buf *key)
4242
{
4343
int rlen = w->restart_len;
4444
if (rlen >= MAX_RESTARTS) {
@@ -59,8 +59,8 @@ static int block_writer_register_restart(struct block_writer *w, int n,
5959

6060
w->next += n;
6161

62-
strbuf_reset(&w->last_key);
63-
strbuf_add(&w->last_key, key->buf, key->len);
62+
reftable_buf_reset(&w->last_key);
63+
reftable_buf_add(&w->last_key, key->buf, key->len);
6464
w->entries++;
6565
return 0;
6666
}
@@ -98,8 +98,8 @@ uint8_t block_writer_type(struct block_writer *bw)
9898
empty key. */
9999
int block_writer_add(struct block_writer *w, struct reftable_record *rec)
100100
{
101-
struct strbuf empty = STRBUF_INIT;
102-
struct strbuf last =
101+
struct reftable_buf empty = REFTABLE_BUF_INIT;
102+
struct reftable_buf last =
103103
w->entries % w->restart_interval == 0 ? empty : w->last_key;
104104
struct string_view out = {
105105
.buf = w->buf + w->next,
@@ -109,7 +109,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
109109
struct string_view start = out;
110110

111111
int is_restart = 0;
112-
struct strbuf key = STRBUF_INIT;
112+
struct reftable_buf key = REFTABLE_BUF_INIT;
113113
int n = 0;
114114
int err = -1;
115115

@@ -133,7 +133,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
133133
err = block_writer_register_restart(w, start.len - out.len, is_restart,
134134
&key);
135135
done:
136-
strbuf_release(&key);
136+
reftable_buf_release(&key);
137137
return err;
138138
}
139139

@@ -325,7 +325,7 @@ uint8_t block_reader_type(const struct block_reader *r)
325325
return r->block.data[r->header_off];
326326
}
327327

328-
int block_reader_first_key(const struct block_reader *br, struct strbuf *key)
328+
int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key)
329329
{
330330
int off = br->header_off + 4, n;
331331
struct string_view in = {
@@ -334,7 +334,7 @@ int block_reader_first_key(const struct block_reader *br, struct strbuf *key)
334334
};
335335
uint8_t extra = 0;
336336

337-
strbuf_reset(key);
337+
reftable_buf_reset(key);
338338

339339
n = reftable_decode_key(key, &extra, in);
340340
if (n < 0)
@@ -355,13 +355,13 @@ void block_iter_seek_start(struct block_iter *it, const struct block_reader *br)
355355
it->block = br->block.data;
356356
it->block_len = br->block_len;
357357
it->hash_size = br->hash_size;
358-
strbuf_reset(&it->last_key);
358+
reftable_buf_reset(&it->last_key);
359359
it->next_off = br->header_off + 4;
360360
}
361361

362362
struct restart_needle_less_args {
363363
int error;
364-
struct strbuf needle;
364+
struct reftable_buf needle;
365365
const struct block_reader *reader;
366366
};
367367

@@ -433,7 +433,7 @@ int block_iter_next(struct block_iter *it, struct reftable_record *rec)
433433

434434
void block_iter_reset(struct block_iter *it)
435435
{
436-
strbuf_reset(&it->last_key);
436+
reftable_buf_reset(&it->last_key);
437437
it->next_off = 0;
438438
it->block = NULL;
439439
it->block_len = 0;
@@ -442,12 +442,12 @@ void block_iter_reset(struct block_iter *it)
442442

443443
void block_iter_close(struct block_iter *it)
444444
{
445-
strbuf_release(&it->last_key);
446-
strbuf_release(&it->scratch);
445+
reftable_buf_release(&it->last_key);
446+
reftable_buf_release(&it->scratch);
447447
}
448448

449449
int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
450-
struct strbuf *want)
450+
struct reftable_buf *want)
451451
{
452452
struct restart_needle_less_args args = {
453453
.needle = *want,
@@ -537,7 +537,7 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
537537
* with themselves.
538538
*/
539539
reftable_record_key(&rec, &it->last_key);
540-
if (strbuf_cmp(&it->last_key, want) >= 0) {
540+
if (reftable_buf_cmp(&it->last_key, want) >= 0) {
541541
it->next_off = prev_off;
542542
goto done;
543543
}
@@ -554,7 +554,7 @@ void block_writer_release(struct block_writer *bw)
554554
REFTABLE_FREE_AND_NULL(bw->zstream);
555555
REFTABLE_FREE_AND_NULL(bw->restarts);
556556
REFTABLE_FREE_AND_NULL(bw->compressed);
557-
strbuf_release(&bw->last_key);
557+
reftable_buf_release(&bw->last_key);
558558
/* the block is not owned. */
559559
}
560560

reftable/block.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct block_writer {
3838
uint32_t restart_len;
3939
uint32_t restart_cap;
4040

41-
struct strbuf last_key;
41+
struct reftable_buf last_key;
4242
int entries;
4343
};
4444

@@ -98,7 +98,7 @@ void block_reader_release(struct block_reader *br);
9898
uint8_t block_reader_type(const struct block_reader *r);
9999

100100
/* Decodes the first key in the block */
101-
int block_reader_first_key(const struct block_reader *br, struct strbuf *key);
101+
int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key);
102102

103103
/* Iterate over entries in a block */
104104
struct block_iter {
@@ -109,21 +109,21 @@ struct block_iter {
109109
int hash_size;
110110

111111
/* key for last entry we read. */
112-
struct strbuf last_key;
113-
struct strbuf scratch;
112+
struct reftable_buf last_key;
113+
struct reftable_buf scratch;
114114
};
115115

116116
#define BLOCK_ITER_INIT { \
117-
.last_key = STRBUF_INIT, \
118-
.scratch = STRBUF_INIT, \
117+
.last_key = REFTABLE_BUF_INIT, \
118+
.scratch = REFTABLE_BUF_INIT, \
119119
}
120120

121121
/* Position `it` at start of the block */
122122
void block_iter_seek_start(struct block_iter *it, const struct block_reader *br);
123123

124124
/* Position `it` to the `want` key in the block */
125125
int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
126-
struct strbuf *want);
126+
struct reftable_buf *want);
127127

128128
/* return < 0 for error, 0 for OK, > 0 for EOF. */
129129
int block_iter_next(struct block_iter *it, struct reftable_record *rec);

reftable/blocksource.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void strbuf_close(void *b UNUSED)
2727
static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off,
2828
uint32_t size)
2929
{
30-
struct strbuf *b = v;
30+
struct reftable_buf *b = v;
3131
assert(off + size <= b->len);
3232
REFTABLE_CALLOC_ARRAY(dest->data, size);
3333
if (!dest->data)
@@ -39,7 +39,7 @@ static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off,
3939

4040
static uint64_t strbuf_size(void *b)
4141
{
42-
return ((struct strbuf *)b)->len;
42+
return ((struct reftable_buf *)b)->len;
4343
}
4444

4545
static struct reftable_block_source_vtable strbuf_vtable = {
@@ -50,7 +50,7 @@ static struct reftable_block_source_vtable strbuf_vtable = {
5050
};
5151

5252
void block_source_from_strbuf(struct reftable_block_source *bs,
53-
struct strbuf *buf)
53+
struct reftable_buf *buf)
5454
{
5555
assert(!bs->ops);
5656
bs->ops = &strbuf_vtable;

reftable/blocksource.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ license that can be found in the LICENSE file or at
1212
#include "system.h"
1313

1414
struct reftable_block_source;
15+
struct reftable_buf;
1516

1617
/* Create an in-memory block source for reading reftables */
1718
void block_source_from_strbuf(struct reftable_block_source *bs,
18-
struct strbuf *buf);
19+
struct reftable_buf *buf);
1920

2021
#endif

reftable/iter.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void iterator_set_empty(struct reftable_iterator *it)
5555
static void filtering_ref_iterator_close(void *iter_arg)
5656
{
5757
struct filtering_ref_iterator *fri = iter_arg;
58-
strbuf_release(&fri->oid);
58+
reftable_buf_release(&fri->oid);
5959
reftable_iterator_destroy(&fri->it);
6060
}
6161

@@ -115,7 +115,7 @@ static void indexed_table_ref_iter_close(void *p)
115115
block_iter_close(&it->cur);
116116
reftable_block_done(&it->block_reader.block);
117117
reftable_free(it->offsets);
118-
strbuf_release(&it->oid);
118+
reftable_buf_release(&it->oid);
119119
}
120120

121121
static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it)
@@ -197,7 +197,7 @@ int indexed_table_ref_iter_new(struct indexed_table_ref_iter **dest,
197197

198198
*itr = empty;
199199
itr->r = r;
200-
strbuf_add(&itr->oid, oid, oid_len);
200+
reftable_buf_add(&itr->oid, oid, oid_len);
201201

202202
itr->offsets = offsets;
203203
itr->offset_len = offset_len;

reftable/iter.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ void iterator_set_empty(struct reftable_iterator *it);
4444

4545
/* iterator that produces only ref records that point to `oid` */
4646
struct filtering_ref_iterator {
47-
struct strbuf oid;
47+
struct reftable_buf oid;
4848
struct reftable_iterator it;
4949
};
5050
#define FILTERING_REF_ITERATOR_INIT \
5151
{ \
52-
.oid = STRBUF_INIT \
52+
.oid = REFTABLE_BUF_INIT \
5353
}
5454

5555
void iterator_from_filtering_ref_iterator(struct reftable_iterator *,
@@ -60,7 +60,7 @@ void iterator_from_filtering_ref_iterator(struct reftable_iterator *,
6060
*/
6161
struct indexed_table_ref_iter {
6262
struct reftable_reader *r;
63-
struct strbuf oid;
63+
struct reftable_buf oid;
6464

6565
/* mutable */
6666
uint64_t *offsets;
@@ -75,7 +75,7 @@ struct indexed_table_ref_iter {
7575

7676
#define INDEXED_TABLE_REF_ITER_INIT { \
7777
.cur = BLOCK_ITER_INIT, \
78-
.oid = STRBUF_INIT, \
78+
.oid = REFTABLE_BUF_INIT, \
7979
}
8080

8181
void iterator_from_indexed_table_ref_iter(struct reftable_iterator *it,

reftable/reader.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ static int table_iter_seek_start(struct table_iter *ti, uint8_t typ, int index)
350350
static int table_iter_seek_linear(struct table_iter *ti,
351351
struct reftable_record *want)
352352
{
353-
struct strbuf want_key = STRBUF_INIT;
354-
struct strbuf got_key = STRBUF_INIT;
353+
struct reftable_buf want_key = REFTABLE_BUF_INIT;
354+
struct reftable_buf got_key = REFTABLE_BUF_INIT;
355355
struct reftable_record rec;
356356
int err;
357357

@@ -401,7 +401,7 @@ static int table_iter_seek_linear(struct table_iter *ti,
401401
if (err < 0)
402402
goto done;
403403

404-
if (strbuf_cmp(&got_key, &want_key) > 0) {
404+
if (reftable_buf_cmp(&got_key, &want_key) > 0) {
405405
table_iter_block_done(&next);
406406
break;
407407
}
@@ -422,20 +422,20 @@ static int table_iter_seek_linear(struct table_iter *ti,
422422

423423
done:
424424
reftable_record_release(&rec);
425-
strbuf_release(&want_key);
426-
strbuf_release(&got_key);
425+
reftable_buf_release(&want_key);
426+
reftable_buf_release(&got_key);
427427
return err;
428428
}
429429

430430
static int table_iter_seek_indexed(struct table_iter *ti,
431431
struct reftable_record *rec)
432432
{
433433
struct reftable_record want_index = {
434-
.type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = STRBUF_INIT }
434+
.type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = REFTABLE_BUF_INIT }
435435
};
436436
struct reftable_record index_result = {
437437
.type = BLOCK_TYPE_INDEX,
438-
.u.idx = { .last_key = STRBUF_INIT },
438+
.u.idx = { .last_key = REFTABLE_BUF_INIT },
439439
};
440440
int err;
441441

@@ -765,7 +765,7 @@ static int reftable_reader_refs_for_unindexed(struct reftable_reader *r,
765765
}
766766
*filter = empty;
767767

768-
strbuf_add(&filter->oid, oid, oid_len);
768+
reftable_buf_add(&filter->oid, oid, oid_len);
769769
iterator_from_table_iter(&filter->it, ti);
770770

771771
iterator_from_filtering_ref_iterator(it, filter);

0 commit comments

Comments
 (0)