Skip to content

Commit dfdd145

Browse files
pks-tgitster
authored andcommitted
reftable/reader: unify indexed and linear seeking
In `reader_seek_internal()` we either end up doing an indexed seek when there is one or a linear seek otherwise. These two code paths are disjunct without a good reason, where the indexed seek will cause us to exit early. Refactor the two code paths such that it becomes possible to share a bit more code between them. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9a59b65 commit dfdd145

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

reftable/reader.c

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ static int reader_seek_linear(struct table_iter *ti,
425425
struct strbuf want_key = STRBUF_INIT;
426426
struct strbuf got_key = STRBUF_INIT;
427427
struct reftable_record rec;
428-
int err = -1;
428+
int err;
429429

430430
reftable_record_init(&rec, reftable_record_type(want));
431431
reftable_record_key(want, &want_key);
@@ -499,8 +499,8 @@ static int reader_seek_linear(struct table_iter *ti,
499499
return err;
500500
}
501501

502-
static int reader_seek_indexed(struct reftable_reader *r,
503-
struct reftable_iterator *it,
502+
static int reader_seek_indexed(struct table_iter *ti,
503+
struct reftable_reader *r,
504504
struct reftable_record *rec)
505505
{
506506
struct reftable_record want_index = {
@@ -510,21 +510,17 @@ static int reader_seek_indexed(struct reftable_reader *r,
510510
.type = BLOCK_TYPE_INDEX,
511511
.u.idx = { .last_key = STRBUF_INIT },
512512
};
513-
struct table_iter ti = TABLE_ITER_INIT, *malloced;
514-
int err = 0;
513+
int err;
515514

516515
reftable_record_key(rec, &want_index.u.idx.last_key);
517-
err = reader_start(r, &ti, reftable_record_type(rec), 1);
518-
if (err < 0)
519-
goto done;
520516

521517
/*
522518
* The index may consist of multiple levels, where each level may have
523519
* multiple index blocks. We start by doing a linear search in the
524520
* highest layer that identifies the relevant index block as well as
525521
* the record inside that block that corresponds to our wanted key.
526522
*/
527-
err = reader_seek_linear(&ti, &want_index);
523+
err = reader_seek_linear(ti, &want_index);
528524
if (err < 0)
529525
goto done;
530526

@@ -550,36 +546,30 @@ static int reader_seek_indexed(struct reftable_reader *r,
550546
* all levels of the index only to find out that the key does
551547
* not exist.
552548
*/
553-
err = table_iter_next(&ti, &index_result);
549+
err = table_iter_next(ti, &index_result);
554550
if (err != 0)
555551
goto done;
556552

557-
err = reader_table_iter_at(r, &ti, index_result.u.idx.offset, 0);
553+
err = reader_table_iter_at(r, ti, index_result.u.idx.offset, 0);
558554
if (err != 0)
559555
goto done;
560556

561-
err = block_iter_seek_key(&ti.bi, &ti.br, &want_index.u.idx.last_key);
557+
err = block_iter_seek_key(&ti->bi, &ti->br, &want_index.u.idx.last_key);
562558
if (err < 0)
563559
goto done;
564560

565-
if (ti.typ == reftable_record_type(rec)) {
561+
if (ti->typ == reftable_record_type(rec)) {
566562
err = 0;
567563
break;
568564
}
569565

570-
if (ti.typ != BLOCK_TYPE_INDEX) {
566+
if (ti->typ != BLOCK_TYPE_INDEX) {
571567
err = REFTABLE_FORMAT_ERROR;
572568
goto done;
573569
}
574570
}
575571

576-
REFTABLE_ALLOC_ARRAY(malloced, 1);
577-
*malloced = ti;
578-
iterator_from_table_iter(it, malloced);
579-
580572
done:
581-
if (err)
582-
table_iter_close(&ti);
583573
reftable_record_release(&want_index);
584574
reftable_record_release(&index_result);
585575
return err;
@@ -595,15 +585,15 @@ static int reader_seek_internal(struct reftable_reader *r,
595585
struct table_iter ti = TABLE_ITER_INIT, *p;
596586
int err;
597587

598-
if (idx > 0)
599-
return reader_seek_indexed(r, it, rec);
600-
601-
err = reader_start(r, &ti, reftable_record_type(rec), 0);
588+
err = reader_start(r, &ti, reftable_record_type(rec), !!idx);
602589
if (err < 0)
603590
goto out;
604591

605-
err = reader_seek_linear(&ti, rec);
606-
if (err < 0)
592+
if (idx)
593+
err = reader_seek_indexed(&ti, r, rec);
594+
else
595+
err = reader_seek_linear(&ti, rec);
596+
if (err)
607597
goto out;
608598

609599
REFTABLE_ALLOC_ARRAY(p, 1);

0 commit comments

Comments
 (0)