Skip to content

Commit 48aebcd

Browse files
KarthikNayakgitster
authored andcommitted
reftable/stack: add function to check if optimization is required
The reftable backend performs auto-compaction as part of its regular flow, which is required to keep the number of tables part of a stack at bay. This allows it to stay optimized. Compaction can also be triggered voluntarily by the user via the 'git pack-refs' or the 'git refs optimize' command. However, currently there is no way for the user to check if optimization is required without actually performing it. Add and expose `reftable_stack_compaction_required()` which will allow users to check if the reftable backend can be optimized. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e9c1a08 commit 48aebcd

3 files changed

Lines changed: 46 additions & 2 deletions

File tree

reftable/reftable-stack.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ struct reftable_log_expiry_config {
123123
int reftable_stack_compact_all(struct reftable_stack *st,
124124
struct reftable_log_expiry_config *config);
125125

126+
/*
127+
* Check if compaction is required.
128+
*
129+
* When `use_heuristics` is false, check if all tables can be compacted to a
130+
* single table. If true, use heuristics to determine if the tables need to be
131+
* compacted to maintain geometric progression.
132+
*/
133+
int reftable_stack_compaction_required(struct reftable_stack *st,
134+
bool use_heuristics,
135+
bool *required);
136+
126137
/* heuristically compact unbalanced table stack. */
127138
int reftable_stack_auto_compact(struct reftable_stack *st);
128139

reftable/stack.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,31 @@ static int stack_segments_for_compaction(struct reftable_stack *st,
16471647
return 0;
16481648
}
16491649

1650+
int reftable_stack_compaction_required(struct reftable_stack *st,
1651+
bool use_heuristics,
1652+
bool *required)
1653+
{
1654+
struct segment seg;
1655+
int err = 0;
1656+
1657+
if (st->merged->tables_len < 2) {
1658+
*required = false;
1659+
return 0;
1660+
}
1661+
1662+
if (!use_heuristics) {
1663+
*required = true;
1664+
return 0;
1665+
}
1666+
1667+
err = stack_segments_for_compaction(st, &seg);
1668+
if (err)
1669+
return err;
1670+
1671+
*required = segment_size(&seg) > 0;
1672+
return 0;
1673+
}
1674+
16501675
int reftable_stack_auto_compact(struct reftable_stack *st)
16511676
{
16521677
struct segment seg;

t/unit-tests/u-reftable-stack.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,7 @@ void test_reftable_stack__add_performs_auto_compaction(void)
10671067
.value_type = REFTABLE_REF_SYMREF,
10681068
.value.symref = (char *) "master",
10691069
};
1070+
bool required = false;
10701071
char buf[128];
10711072

10721073
/*
@@ -1087,10 +1088,17 @@ void test_reftable_stack__add_performs_auto_compaction(void)
10871088
* auto compaction is disabled. When enabled, we should merge
10881089
* all tables in the stack.
10891090
*/
1090-
if (i != n)
1091+
cl_assert_equal_i(reftable_stack_compaction_required(st, true, &required), 0);
1092+
if (i != n) {
10911093
cl_assert_equal_i(st->merged->tables_len, i + 1);
1092-
else
1094+
if (i < 1)
1095+
cl_assert_equal_b(required, false);
1096+
else
1097+
cl_assert_equal_b(required, true);
1098+
} else {
10931099
cl_assert_equal_i(st->merged->tables_len, 1);
1100+
cl_assert_equal_b(required, false);
1101+
}
10941102
}
10951103

10961104
reftable_stack_destroy(st);

0 commit comments

Comments
 (0)