Skip to content

Commit 12a9aa8

Browse files
pks-tgitster
authored andcommitted
reftable/block: rename block_reader to reftable_block
The `block_reader` structure is used to access parsed data of a reftable block. The structure is currently treated as an internal implementation detail and not exposed via our public interfaces. The functionality provided by the structure is useful to external users of the reftable library though, for example when implementing consistency checks that need to scan through the blocks manually. Rename the structure to `reftable_block` now that the name has been made available in the preceding commit. This name is in line with the naming schema used for other data structures like `reftable_table` in that it describes the underlying entity that it provides access to. The new data structure isn't yet exposed via the public interface, which is left for a subsequent commit. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2b3362c commit 12a9aa8

File tree

7 files changed

+172
-166
lines changed

7 files changed

+172
-166
lines changed

reftable/block.c

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ static int read_block(struct reftable_block_source *source,
222222
return block_source_read_data(source, dest, off, sz);
223223
}
224224

225-
int block_reader_init(struct block_reader *br,
226-
struct reftable_block_source *source,
227-
uint32_t offset, uint32_t header_size,
228-
uint32_t table_block_size, uint32_t hash_size)
225+
int reftable_block_init(struct reftable_block *block,
226+
struct reftable_block_source *source,
227+
uint32_t offset, uint32_t header_size,
228+
uint32_t table_block_size, uint32_t hash_size)
229229
{
230230
uint32_t guess_block_size = table_block_size ?
231231
table_block_size : DEFAULT_BLOCK_SIZE;
@@ -236,59 +236,59 @@ int block_reader_init(struct block_reader *br,
236236
uint8_t block_type;
237237
int err;
238238

239-
err = read_block(source, &br->block_data, offset, guess_block_size);
239+
err = read_block(source, &block->block_data, offset, guess_block_size);
240240
if (err < 0)
241241
goto done;
242242

243-
block_type = br->block_data.data[header_size];
243+
block_type = block->block_data.data[header_size];
244244
if (!reftable_is_block_type(block_type)) {
245245
err = REFTABLE_FORMAT_ERROR;
246246
goto done;
247247
}
248248

249-
block_size = reftable_get_be24(br->block_data.data + header_size + 1);
249+
block_size = reftable_get_be24(block->block_data.data + header_size + 1);
250250
if (block_size > guess_block_size) {
251-
err = read_block(source, &br->block_data, offset, block_size);
251+
err = read_block(source, &block->block_data, offset, block_size);
252252
if (err < 0)
253253
goto done;
254254
}
255255

256256
if (block_type == BLOCK_TYPE_LOG) {
257257
uint32_t block_header_skip = 4 + header_size;
258258
uLong dst_len = block_size - block_header_skip;
259-
uLong src_len = br->block_data.len - block_header_skip;
259+
uLong src_len = block->block_data.len - block_header_skip;
260260

261261
/* Log blocks specify the *uncompressed* size in their header. */
262-
REFTABLE_ALLOC_GROW_OR_NULL(br->uncompressed_data, block_size,
263-
br->uncompressed_cap);
264-
if (!br->uncompressed_data) {
262+
REFTABLE_ALLOC_GROW_OR_NULL(block->uncompressed_data, block_size,
263+
block->uncompressed_cap);
264+
if (!block->uncompressed_data) {
265265
err = REFTABLE_OUT_OF_MEMORY_ERROR;
266266
goto done;
267267
}
268268

269269
/* Copy over the block header verbatim. It's not compressed. */
270-
memcpy(br->uncompressed_data, br->block_data.data, block_header_skip);
270+
memcpy(block->uncompressed_data, block->block_data.data, block_header_skip);
271271

272-
if (!br->zstream) {
273-
REFTABLE_CALLOC_ARRAY(br->zstream, 1);
274-
if (!br->zstream) {
272+
if (!block->zstream) {
273+
REFTABLE_CALLOC_ARRAY(block->zstream, 1);
274+
if (!block->zstream) {
275275
err = REFTABLE_OUT_OF_MEMORY_ERROR;
276276
goto done;
277277
}
278278

279-
err = inflateInit(br->zstream);
279+
err = inflateInit(block->zstream);
280280
} else {
281-
err = inflateReset(br->zstream);
281+
err = inflateReset(block->zstream);
282282
}
283283
if (err != Z_OK) {
284284
err = REFTABLE_ZLIB_ERROR;
285285
goto done;
286286
}
287287

288-
br->zstream->next_in = br->block_data.data + block_header_skip;
289-
br->zstream->avail_in = src_len;
290-
br->zstream->next_out = br->uncompressed_data + block_header_skip;
291-
br->zstream->avail_out = dst_len;
288+
block->zstream->next_in = block->block_data.data + block_header_skip;
289+
block->zstream->avail_in = src_len;
290+
block->zstream->next_out = block->uncompressed_data + block_header_skip;
291+
block->zstream->avail_out = dst_len;
292292

293293
/*
294294
* We know both input as well as output size, and we know that
@@ -297,71 +297,71 @@ int block_reader_init(struct block_reader *br,
297297
* here to instruct zlib to inflate the data in one go, which
298298
* is more efficient than using `Z_NO_FLUSH`.
299299
*/
300-
err = inflate(br->zstream, Z_FINISH);
300+
err = inflate(block->zstream, Z_FINISH);
301301
if (err != Z_STREAM_END) {
302302
err = REFTABLE_ZLIB_ERROR;
303303
goto done;
304304
}
305305
err = 0;
306306

307-
if (br->zstream->total_out + block_header_skip != block_size) {
307+
if (block->zstream->total_out + block_header_skip != block_size) {
308308
err = REFTABLE_FORMAT_ERROR;
309309
goto done;
310310
}
311311

312312
/* We're done with the input data. */
313-
block_source_release_data(&br->block_data);
314-
br->block_data.data = br->uncompressed_data;
315-
br->block_data.len = block_size;
316-
full_block_size = src_len + block_header_skip - br->zstream->avail_in;
313+
block_source_release_data(&block->block_data);
314+
block->block_data.data = block->uncompressed_data;
315+
block->block_data.len = block_size;
316+
full_block_size = src_len + block_header_skip - block->zstream->avail_in;
317317
} else if (full_block_size == 0) {
318318
full_block_size = block_size;
319-
} else if (block_size < full_block_size && block_size < br->block_data.len &&
320-
br->block_data.data[block_size] != 0) {
319+
} else if (block_size < full_block_size && block_size < block->block_data.len &&
320+
block->block_data.data[block_size] != 0) {
321321
/* If the block is smaller than the full block size, it is
322322
padded (data followed by '\0') or the next block is
323323
unaligned. */
324324
full_block_size = block_size;
325325
}
326326

327-
restart_count = reftable_get_be16(br->block_data.data + block_size - 2);
327+
restart_count = reftable_get_be16(block->block_data.data + block_size - 2);
328328
restart_off = block_size - 2 - 3 * restart_count;
329329

330-
br->block_type = block_type;
331-
br->hash_size = hash_size;
332-
br->restart_off = restart_off;
333-
br->full_block_size = full_block_size;
334-
br->header_off = header_size;
335-
br->restart_count = restart_count;
330+
block->block_type = block_type;
331+
block->hash_size = hash_size;
332+
block->restart_off = restart_off;
333+
block->full_block_size = full_block_size;
334+
block->header_off = header_size;
335+
block->restart_count = restart_count;
336336

337337
err = 0;
338338

339339
done:
340340
if (err < 0)
341-
block_reader_release(br);
341+
reftable_block_release(block);
342342
return err;
343343
}
344344

345-
void block_reader_release(struct block_reader *br)
345+
void reftable_block_release(struct reftable_block *block)
346346
{
347-
inflateEnd(br->zstream);
348-
reftable_free(br->zstream);
349-
reftable_free(br->uncompressed_data);
350-
block_source_release_data(&br->block_data);
351-
memset(br, 0, sizeof(*br));
347+
inflateEnd(block->zstream);
348+
reftable_free(block->zstream);
349+
reftable_free(block->uncompressed_data);
350+
block_source_release_data(&block->block_data);
351+
memset(block, 0, sizeof(*block));
352352
}
353353

354-
uint8_t block_reader_type(const struct block_reader *r)
354+
uint8_t reftable_block_type(const struct reftable_block *b)
355355
{
356-
return r->block_data.data[r->header_off];
356+
return b->block_data.data[b->header_off];
357357
}
358358

359-
int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key)
359+
int reftable_block_first_key(const struct reftable_block *block, struct reftable_buf *key)
360360
{
361-
int off = br->header_off + 4, n;
361+
int off = block->header_off + 4, n;
362362
struct string_view in = {
363-
.buf = br->block_data.data + off,
364-
.len = br->restart_off - off,
363+
.buf = block->block_data.data + off,
364+
.len = block->restart_off - off,
365365
};
366366
uint8_t extra = 0;
367367

@@ -376,33 +376,33 @@ int block_reader_first_key(const struct block_reader *br, struct reftable_buf *k
376376
return 0;
377377
}
378378

379-
static uint32_t block_reader_restart_offset(const struct block_reader *br, size_t idx)
379+
static uint32_t block_restart_offset(const struct reftable_block *b, size_t idx)
380380
{
381-
return reftable_get_be24(br->block_data.data + br->restart_off + 3 * idx);
381+
return reftable_get_be24(b->block_data.data + b->restart_off + 3 * idx);
382382
}
383383

384-
void block_iter_seek_start(struct block_iter *it, const struct block_reader *br)
384+
void block_iter_seek_start(struct block_iter *it, const struct reftable_block *b)
385385
{
386-
it->block = br->block_data.data;
387-
it->block_len = br->restart_off;
388-
it->hash_size = br->hash_size;
386+
it->block = b->block_data.data;
387+
it->block_len = b->restart_off;
388+
it->hash_size = b->hash_size;
389389
reftable_buf_reset(&it->last_key);
390-
it->next_off = br->header_off + 4;
390+
it->next_off = b->header_off + 4;
391391
}
392392

393393
struct restart_needle_less_args {
394394
int error;
395395
struct reftable_buf needle;
396-
const struct block_reader *reader;
396+
const struct reftable_block *block;
397397
};
398398

399399
static int restart_needle_less(size_t idx, void *_args)
400400
{
401401
struct restart_needle_less_args *args = _args;
402-
uint32_t off = block_reader_restart_offset(args->reader, idx);
402+
uint32_t off = block_restart_offset(args->block, idx);
403403
struct string_view in = {
404-
.buf = args->reader->block_data.data + off,
405-
.len = args->reader->restart_off - off,
404+
.buf = args->block->block_data.data + off,
405+
.len = args->block->restart_off - off,
406406
};
407407
uint64_t prefix_len, suffix_len;
408408
uint8_t extra;
@@ -477,12 +477,12 @@ void block_iter_close(struct block_iter *it)
477477
reftable_buf_release(&it->scratch);
478478
}
479479

480-
int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
480+
int block_iter_seek_key(struct block_iter *it, const struct reftable_block *block,
481481
struct reftable_buf *want)
482482
{
483483
struct restart_needle_less_args args = {
484484
.needle = *want,
485-
.reader = br,
485+
.block = block,
486486
};
487487
struct reftable_record rec;
488488
int err = 0;
@@ -500,7 +500,7 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
500500
* restart point. While that works alright, we would end up scanning
501501
* too many record.
502502
*/
503-
i = binsearch(br->restart_count, &restart_needle_less, &args);
503+
i = binsearch(block->restart_count, &restart_needle_less, &args);
504504
if (args.error) {
505505
err = REFTABLE_FORMAT_ERROR;
506506
goto done;
@@ -525,21 +525,21 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
525525
* starting from the preceding restart point.
526526
*/
527527
if (i > 0)
528-
it->next_off = block_reader_restart_offset(br, i - 1);
528+
it->next_off = block_restart_offset(block, i - 1);
529529
else
530-
it->next_off = br->header_off + 4;
531-
it->block = br->block_data.data;
532-
it->block_len = br->restart_off;
533-
it->hash_size = br->hash_size;
530+
it->next_off = block->header_off + 4;
531+
it->block = block->block_data.data;
532+
it->block_len = block->restart_off;
533+
it->hash_size = block->hash_size;
534534

535-
err = reftable_record_init(&rec, block_reader_type(br));
535+
err = reftable_record_init(&rec, reftable_block_type(block));
536536
if (err < 0)
537537
goto done;
538538

539539
/*
540540
* We're looking for the last entry less than the wanted key so that
541541
* the next call to `block_reader_next()` would yield the wanted
542-
* record. We thus don't want to position our reader at the sought
542+
* record. We thus don't want to position our iterator at the sought
543543
* after record, but one before. To do so, we have to go one entry too
544544
* far and then back up.
545545
*/

reftable/block.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ void block_writer_release(struct block_writer *bw);
6464

6565
struct z_stream;
6666

67-
/* Read a block. */
68-
struct block_reader {
67+
/*
68+
* A block part of a reftable. Contains records as well as some metadata
69+
* describing them.
70+
*/
71+
struct reftable_block {
6972
/* offset of the block header; nonzero for the first block in a
7073
* reftable. */
7174
uint32_t header_off;
@@ -92,19 +95,21 @@ struct block_reader {
9295
uint8_t block_type;
9396
};
9497

95-
/* initializes a block reader. */
96-
int block_reader_init(struct block_reader *br,
97-
struct reftable_block_source *source,
98-
uint32_t offset, uint32_t header_size,
99-
uint32_t table_block_size, uint32_t hash_size);
98+
/*
99+
* Initialize a reftable block from the given block source.
100+
*/
101+
int reftable_block_init(struct reftable_block *b,
102+
struct reftable_block_source *source,
103+
uint32_t offset, uint32_t header_size,
104+
uint32_t table_block_size, uint32_t hash_size);
100105

101-
void block_reader_release(struct block_reader *br);
106+
void reftable_block_release(struct reftable_block *b);
102107

103108
/* Returns the block type (eg. 'r' for refs) */
104-
uint8_t block_reader_type(const struct block_reader *r);
109+
uint8_t reftable_block_type(const struct reftable_block *b);
105110

106111
/* Decodes the first key in the block */
107-
int block_reader_first_key(const struct block_reader *br, struct reftable_buf *key);
112+
int reftable_block_first_key(const struct reftable_block *b, struct reftable_buf *key);
108113

109114
/* Iterate over entries in a block */
110115
struct block_iter {
@@ -125,10 +130,10 @@ struct block_iter {
125130
}
126131

127132
/* Position `it` at start of the block */
128-
void block_iter_seek_start(struct block_iter *it, const struct block_reader *br);
133+
void block_iter_seek_start(struct block_iter *it, const struct reftable_block *block);
129134

130135
/* Position `it` to the `want` key in the block */
131-
int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
136+
int block_iter_seek_key(struct block_iter *it, const struct reftable_block *block,
132137
struct reftable_buf *want);
133138

134139
/* return < 0 for error, 0 for OK, > 0 for EOF. */

reftable/iter.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static void indexed_table_ref_iter_close(void *p)
114114
{
115115
struct indexed_table_ref_iter *it = p;
116116
block_iter_close(&it->cur);
117-
block_source_release_data(&it->block_reader.block_data);
117+
block_source_release_data(&it->block.block_data);
118118
reftable_free(it->offsets);
119119
reftable_buf_release(&it->oid);
120120
}
@@ -128,19 +128,18 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it)
128128
return 1;
129129
}
130130

131-
block_source_release_data(&it->block_reader.block_data);
131+
block_source_release_data(&it->block.block_data);
132132

133133
off = it->offsets[it->offset_idx++];
134-
err = table_init_block_reader(it->table, &it->block_reader, off,
135-
BLOCK_TYPE_REF);
134+
err = table_init_block(it->table, &it->block, off, BLOCK_TYPE_REF);
136135
if (err < 0) {
137136
return err;
138137
}
139138
if (err > 0) {
140139
/* indexed block does not exist. */
141140
return REFTABLE_FORMAT_ERROR;
142141
}
143-
block_iter_seek_start(&it->cur, &it->block_reader);
142+
block_iter_seek_start(&it->cur, &it->block);
144143
return 0;
145144
}
146145

reftable/iter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct indexed_table_ref_iter {
6868
/* Points to the next offset to read. */
6969
int offset_idx;
7070
int offset_len;
71-
struct block_reader block_reader;
71+
struct reftable_block block;
7272
struct block_iter cur;
7373
int is_finished;
7474
};

0 commit comments

Comments
 (0)