Skip to content

Commit

Permalink
reftable: convert from strbuf to reftable_buf
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pks-t authored and ttaylorr committed Oct 17, 2024
1 parent 81eddda commit be4c070
Show file tree
Hide file tree
Showing 24 changed files with 374 additions and 371 deletions.
2 changes: 1 addition & 1 deletion reftable/basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ int names_equal(const char **a, const char **b)
return a[i] == b[i];
}

int common_prefix_size(struct strbuf *a, struct strbuf *b)
int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b)
{
int p = 0;
for (; p < a->len && p < b->len; p++) {
Expand Down
7 changes: 4 additions & 3 deletions reftable/basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ int reftable_buf_setlen(struct reftable_buf *buf, size_t len);
int reftable_buf_cmp(const struct reftable_buf *a, const struct reftable_buf *b);

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

/* Find the longest shared prefix size of `a` and `b` */
struct strbuf;
int common_prefix_size(struct strbuf *a, struct strbuf *b);
int common_prefix_size(struct reftable_buf *a, struct reftable_buf *b);

int hash_size(uint32_t id);

Expand Down
34 changes: 17 additions & 17 deletions reftable/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int footer_size(int version)
}

static int block_writer_register_restart(struct block_writer *w, int n,
int is_restart, struct strbuf *key)
int is_restart, struct reftable_buf *key)
{
int rlen = w->restart_len;
if (rlen >= MAX_RESTARTS) {
Expand All @@ -59,8 +59,8 @@ static int block_writer_register_restart(struct block_writer *w, int n,

w->next += n;

strbuf_reset(&w->last_key);
strbuf_add(&w->last_key, key->buf, key->len);
reftable_buf_reset(&w->last_key);
reftable_buf_add(&w->last_key, key->buf, key->len);
w->entries++;
return 0;
}
Expand Down Expand Up @@ -98,8 +98,8 @@ uint8_t block_writer_type(struct block_writer *bw)
empty key. */
int block_writer_add(struct block_writer *w, struct reftable_record *rec)
{
struct strbuf empty = STRBUF_INIT;
struct strbuf last =
struct reftable_buf empty = REFTABLE_BUF_INIT;
struct reftable_buf last =
w->entries % w->restart_interval == 0 ? empty : w->last_key;
struct string_view out = {
.buf = w->buf + w->next,
Expand All @@ -109,7 +109,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
struct string_view start = out;

int is_restart = 0;
struct strbuf key = STRBUF_INIT;
struct reftable_buf key = REFTABLE_BUF_INIT;
int n = 0;
int err = -1;

Expand All @@ -133,7 +133,7 @@ int block_writer_add(struct block_writer *w, struct reftable_record *rec)
err = block_writer_register_restart(w, start.len - out.len, is_restart,
&key);
done:
strbuf_release(&key);
reftable_buf_release(&key);
return err;
}

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

int block_reader_first_key(const struct block_reader *br, struct strbuf *key)
int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key)
{
int off = br->header_off + 4, n;
struct string_view in = {
Expand All @@ -334,7 +334,7 @@ int block_reader_first_key(const struct block_reader *br, struct strbuf *key)
};
uint8_t extra = 0;

strbuf_reset(key);
reftable_buf_reset(key);

n = reftable_decode_key(key, &extra, in);
if (n < 0)
Expand All @@ -355,13 +355,13 @@ void block_iter_seek_start(struct block_iter *it, const struct block_reader *br)
it->block = br->block.data;
it->block_len = br->block_len;
it->hash_size = br->hash_size;
strbuf_reset(&it->last_key);
reftable_buf_reset(&it->last_key);
it->next_off = br->header_off + 4;
}

struct restart_needle_less_args {
int error;
struct strbuf needle;
struct reftable_buf needle;
const struct block_reader *reader;
};

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

void block_iter_reset(struct block_iter *it)
{
strbuf_reset(&it->last_key);
reftable_buf_reset(&it->last_key);
it->next_off = 0;
it->block = NULL;
it->block_len = 0;
Expand All @@ -442,12 +442,12 @@ void block_iter_reset(struct block_iter *it)

void block_iter_close(struct block_iter *it)
{
strbuf_release(&it->last_key);
strbuf_release(&it->scratch);
reftable_buf_release(&it->last_key);
reftable_buf_release(&it->scratch);
}

int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
struct strbuf *want)
struct reftable_buf *want)
{
struct restart_needle_less_args args = {
.needle = *want,
Expand Down Expand Up @@ -537,7 +537,7 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
* with themselves.
*/
reftable_record_key(&rec, &it->last_key);
if (strbuf_cmp(&it->last_key, want) >= 0) {
if (reftable_buf_cmp(&it->last_key, want) >= 0) {
it->next_off = prev_off;
goto done;
}
Expand All @@ -554,7 +554,7 @@ void block_writer_release(struct block_writer *bw)
REFTABLE_FREE_AND_NULL(bw->zstream);
REFTABLE_FREE_AND_NULL(bw->restarts);
REFTABLE_FREE_AND_NULL(bw->compressed);
strbuf_release(&bw->last_key);
reftable_buf_release(&bw->last_key);
/* the block is not owned. */
}

Expand Down
14 changes: 7 additions & 7 deletions reftable/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct block_writer {
uint32_t restart_len;
uint32_t restart_cap;

struct strbuf last_key;
struct reftable_buf last_key;
int entries;
};

Expand Down Expand Up @@ -98,7 +98,7 @@ void block_reader_release(struct block_reader *br);
uint8_t block_reader_type(const struct block_reader *r);

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

/* Iterate over entries in a block */
struct block_iter {
Expand All @@ -109,21 +109,21 @@ struct block_iter {
int hash_size;

/* key for last entry we read. */
struct strbuf last_key;
struct strbuf scratch;
struct reftable_buf last_key;
struct reftable_buf scratch;
};

#define BLOCK_ITER_INIT { \
.last_key = STRBUF_INIT, \
.scratch = STRBUF_INIT, \
.last_key = REFTABLE_BUF_INIT, \
.scratch = REFTABLE_BUF_INIT, \
}

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

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

/* return < 0 for error, 0 for OK, > 0 for EOF. */
int block_iter_next(struct block_iter *it, struct reftable_record *rec);
Expand Down
6 changes: 3 additions & 3 deletions reftable/blocksource.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static void strbuf_close(void *b UNUSED)
static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off,
uint32_t size)
{
struct strbuf *b = v;
struct reftable_buf *b = v;
assert(off + size <= b->len);
REFTABLE_CALLOC_ARRAY(dest->data, size);
if (!dest->data)
Expand All @@ -39,7 +39,7 @@ static int strbuf_read_block(void *v, struct reftable_block *dest, uint64_t off,

static uint64_t strbuf_size(void *b)
{
return ((struct strbuf *)b)->len;
return ((struct reftable_buf *)b)->len;
}

static struct reftable_block_source_vtable strbuf_vtable = {
Expand All @@ -50,7 +50,7 @@ static struct reftable_block_source_vtable strbuf_vtable = {
};

void block_source_from_strbuf(struct reftable_block_source *bs,
struct strbuf *buf)
struct reftable_buf *buf)
{
assert(!bs->ops);
bs->ops = &strbuf_vtable;
Expand Down
3 changes: 2 additions & 1 deletion reftable/blocksource.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ license that can be found in the LICENSE file or at
#include "system.h"

struct reftable_block_source;
struct reftable_buf;

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

#endif
6 changes: 3 additions & 3 deletions reftable/iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void iterator_set_empty(struct reftable_iterator *it)
static void filtering_ref_iterator_close(void *iter_arg)
{
struct filtering_ref_iterator *fri = iter_arg;
strbuf_release(&fri->oid);
reftable_buf_release(&fri->oid);
reftable_iterator_destroy(&fri->it);
}

Expand Down Expand Up @@ -115,7 +115,7 @@ static void indexed_table_ref_iter_close(void *p)
block_iter_close(&it->cur);
reftable_block_done(&it->block_reader.block);
reftable_free(it->offsets);
strbuf_release(&it->oid);
reftable_buf_release(&it->oid);
}

static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it)
Expand Down Expand Up @@ -197,7 +197,7 @@ int indexed_table_ref_iter_new(struct indexed_table_ref_iter **dest,

*itr = empty;
itr->r = r;
strbuf_add(&itr->oid, oid, oid_len);
reftable_buf_add(&itr->oid, oid, oid_len);

itr->offsets = offsets;
itr->offset_len = offset_len;
Expand Down
8 changes: 4 additions & 4 deletions reftable/iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ void iterator_set_empty(struct reftable_iterator *it);

/* iterator that produces only ref records that point to `oid` */
struct filtering_ref_iterator {
struct strbuf oid;
struct reftable_buf oid;
struct reftable_iterator it;
};
#define FILTERING_REF_ITERATOR_INIT \
{ \
.oid = STRBUF_INIT \
.oid = REFTABLE_BUF_INIT \
}

void iterator_from_filtering_ref_iterator(struct reftable_iterator *,
Expand All @@ -60,7 +60,7 @@ void iterator_from_filtering_ref_iterator(struct reftable_iterator *,
*/
struct indexed_table_ref_iter {
struct reftable_reader *r;
struct strbuf oid;
struct reftable_buf oid;

/* mutable */
uint64_t *offsets;
Expand All @@ -75,7 +75,7 @@ struct indexed_table_ref_iter {

#define INDEXED_TABLE_REF_ITER_INIT { \
.cur = BLOCK_ITER_INIT, \
.oid = STRBUF_INIT, \
.oid = REFTABLE_BUF_INIT, \
}

void iterator_from_indexed_table_ref_iter(struct reftable_iterator *it,
Expand Down
16 changes: 8 additions & 8 deletions reftable/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ static int table_iter_seek_start(struct table_iter *ti, uint8_t typ, int index)
static int table_iter_seek_linear(struct table_iter *ti,
struct reftable_record *want)
{
struct strbuf want_key = STRBUF_INIT;
struct strbuf got_key = STRBUF_INIT;
struct reftable_buf want_key = REFTABLE_BUF_INIT;
struct reftable_buf got_key = REFTABLE_BUF_INIT;
struct reftable_record rec;
int err;

Expand Down Expand Up @@ -401,7 +401,7 @@ static int table_iter_seek_linear(struct table_iter *ti,
if (err < 0)
goto done;

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

done:
reftable_record_release(&rec);
strbuf_release(&want_key);
strbuf_release(&got_key);
reftable_buf_release(&want_key);
reftable_buf_release(&got_key);
return err;
}

static int table_iter_seek_indexed(struct table_iter *ti,
struct reftable_record *rec)
{
struct reftable_record want_index = {
.type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = STRBUF_INIT }
.type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = REFTABLE_BUF_INIT }
};
struct reftable_record index_result = {
.type = BLOCK_TYPE_INDEX,
.u.idx = { .last_key = STRBUF_INIT },
.u.idx = { .last_key = REFTABLE_BUF_INIT },
};
int err;

Expand Down Expand Up @@ -765,7 +765,7 @@ static int reftable_reader_refs_for_unindexed(struct reftable_reader *r,
}
*filter = empty;

strbuf_add(&filter->oid, oid, oid_len);
reftable_buf_add(&filter->oid, oid, oid_len);
iterator_from_table_iter(&filter->it, ti);

iterator_from_filtering_ref_iterator(it, filter);
Expand Down
Loading

0 comments on commit be4c070

Please sign in to comment.