Skip to content

Commit ba620d2

Browse files
pks-tgitster
authored andcommitted
reftable/block: simplify how we track restart points
Restart points record the location of reftable records that do not use prefix compression and are used to perform a binary search inside of a block. These restart points are encoded at the end of a block, between the record data and the footer of a table. The block structure contains three different variables related to these restart points: - The block length contains the length of the reftable block up to the restart points. - The restart count contains the number of restart points contained in the block. - The restart bytes variable tracks where the restart point data begins. Tracking all three of these variables is unnecessary though as the data can be derived from one another: the block length without restart points is the exact same as the offset of the restart count data, which we already track via the `restart_bytes` data. Refactor the code so that we track the location of restart bytes not as a pointer, but instead as an offset. This allows us to trivially get rid of the `block_len` variable as described above. This avoids having the confusing `block_len` variable and allows us to do less bookkeeping overall. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1ac4e5e commit ba620d2

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

reftable/block.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,9 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block,
216216
uint32_t full_block_size = table_block_size;
217217
uint8_t typ = block->data[header_off];
218218
uint32_t sz = reftable_get_be24(block->data + header_off + 1);
219-
int err = 0;
220-
uint16_t restart_count = 0;
221-
uint32_t restart_start = 0;
222-
uint8_t *restart_bytes = NULL;
219+
uint16_t restart_count;
220+
uint32_t restart_off;
221+
int err;
223222

224223
block_source_return_block(&br->block);
225224

@@ -300,20 +299,20 @@ int block_reader_init(struct block_reader *br, struct reftable_block *block,
300299
}
301300

302301
restart_count = reftable_get_be16(block->data + sz - 2);
303-
restart_start = sz - 2 - 3 * restart_count;
304-
restart_bytes = block->data + restart_start;
302+
restart_off = sz - 2 - 3 * restart_count;
305303

306304
/* transfer ownership. */
307305
br->block = *block;
308306
block->data = NULL;
309307
block->len = 0;
310308

311309
br->hash_size = hash_size;
312-
br->block_len = restart_start;
310+
br->restart_off = restart_off;
313311
br->full_block_size = full_block_size;
314312
br->header_off = header_off;
315313
br->restart_count = restart_count;
316-
br->restart_bytes = restart_bytes;
314+
315+
err = 0;
317316

318317
done:
319318
return err;
@@ -337,7 +336,7 @@ int block_reader_first_key(const struct block_reader *br, struct reftable_buf *k
337336
int off = br->header_off + 4, n;
338337
struct string_view in = {
339338
.buf = br->block.data + off,
340-
.len = br->block_len - off,
339+
.len = br->restart_off - off,
341340
};
342341
uint8_t extra = 0;
343342

@@ -354,13 +353,13 @@ int block_reader_first_key(const struct block_reader *br, struct reftable_buf *k
354353

355354
static uint32_t block_reader_restart_offset(const struct block_reader *br, size_t idx)
356355
{
357-
return reftable_get_be24(br->restart_bytes + 3 * idx);
356+
return reftable_get_be24(br->block.data + br->restart_off + 3 * idx);
358357
}
359358

360359
void block_iter_seek_start(struct block_iter *it, const struct block_reader *br)
361360
{
362361
it->block = br->block.data;
363-
it->block_len = br->block_len;
362+
it->block_len = br->restart_off;
364363
it->hash_size = br->hash_size;
365364
reftable_buf_reset(&it->last_key);
366365
it->next_off = br->header_off + 4;
@@ -378,7 +377,7 @@ static int restart_needle_less(size_t idx, void *_args)
378377
uint32_t off = block_reader_restart_offset(args->reader, idx);
379378
struct string_view in = {
380379
.buf = args->reader->block.data + off,
381-
.len = args->reader->block_len - off,
380+
.len = args->reader->restart_off - off,
382381
};
383382
uint64_t prefix_len, suffix_len;
384383
uint8_t extra;
@@ -505,7 +504,7 @@ int block_iter_seek_key(struct block_iter *it, const struct block_reader *br,
505504
else
506505
it->next_off = br->header_off + 4;
507506
it->block = br->block.data;
508-
it->block_len = br->block_len;
507+
it->block_len = br->restart_off;
509508
it->hash_size = br->hash_size;
510509

511510
err = reftable_record_init(&rec, block_reader_type(br));

reftable/block.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ struct block_reader {
7979
unsigned char *uncompressed_data;
8080
size_t uncompressed_cap;
8181

82-
/* size of the data, excluding restart data. */
83-
uint32_t block_len;
84-
uint8_t *restart_bytes;
82+
/*
83+
* Restart point data. Restart points are located after the block's
84+
* record data.
85+
*/
8586
uint16_t restart_count;
87+
uint32_t restart_off;
8688

8789
/* size of the data in the file. For log blocks, this is the compressed
8890
* size. */

reftable/table.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ int reftable_table_print_blocks(const char *tablename)
838838
printf("%s:\n", sections[i].name);
839839

840840
while (1) {
841-
printf(" - length: %u\n", ti.br.block_len);
841+
printf(" - length: %u\n", ti.br.restart_off);
842842
printf(" restarts: %u\n", ti.br.restart_count);
843843

844844
err = table_iter_next_block(&ti);

0 commit comments

Comments
 (0)