Skip to content

Commit d9ecf26

Browse files
pks-tgitster
authored andcommitted
odb: embed base source in the "files" backend
The "files" backend is implemented as a pointer in the `struct odb_source`. This contradicts our typical pattern for pluggable backends like we use it for example in the ref store or for object database streams, where we typically embed the generic base structure in the specialized implementation. This pattern has a couple of small benefits: - We avoid an extra allocation. - We hide implementation details in the generic structure. - We can easily downcast from a generic backend to the specialized structure and vice versa because the offsets are known at compile time. - It becomes trivial to identify locations where we depend on backend specific logic because the cast needs to be explicit. Refactor our "files" object database source to do the same and embed the `struct odb_source` in the `struct odb_source_files`. There are still a bunch of sites in our code base where we do have to access internals of the "files" backend. The intent is that those will go away over time, but this will certainly take a while. Meanwhile, provide a `odb_source_files_downcast()` function that can convert a generic source into a "files" source. As we only have a single source the downcast succeeds unconditionally for now. Eventually though the intent is to make the cast `BUG()` in case the caller requests to downcast a non-"files" backend to a "files" backend. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent cb506a8 commit d9ecf26

18 files changed

Lines changed: 189 additions & 89 deletions

builtin/cat-file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,8 @@ static void batch_each_object(struct batch_options *opt,
882882
struct object_info oi = { 0 };
883883

884884
for (source = the_repository->objects->sources; source; source = source->next) {
885-
int ret = packfile_store_for_each_object(source->files->packed, &oi,
885+
struct odb_source_files *files = odb_source_files_downcast(source);
886+
int ret = packfile_store_for_each_object(files->packed, &oi,
886887
batch_one_object_oi, &payload, flags);
887888
if (ret)
888889
break;

builtin/fast-import.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@ static void end_packfile(void)
875875
running = 1;
876876
clear_delta_base_cache();
877877
if (object_count) {
878+
struct odb_source_files *files = odb_source_files_downcast(pack_data->repo->objects->sources);
878879
struct packed_git *new_p;
879880
struct object_id cur_pack_oid;
880881
char *idx_name;
@@ -900,8 +901,7 @@ static void end_packfile(void)
900901
idx_name = keep_pack(create_index());
901902

902903
/* Register the packfile with core git's machinery. */
903-
new_p = packfile_store_load_pack(pack_data->repo->objects->sources->files->packed,
904-
idx_name, 1);
904+
new_p = packfile_store_load_pack(files->packed, idx_name, 1);
905905
if (!new_p)
906906
die(_("core Git rejected index %s"), idx_name);
907907
all_packs[pack_id] = new_p;
@@ -982,7 +982,9 @@ static int store_object(
982982
}
983983

984984
for (source = the_repository->objects->sources; source; source = source->next) {
985-
if (!packfile_list_find_oid(packfile_store_get_packs(source->files->packed), &oid))
985+
struct odb_source_files *files = odb_source_files_downcast(source);
986+
987+
if (!packfile_list_find_oid(packfile_store_get_packs(files->packed), &oid))
986988
continue;
987989
e->type = type;
988990
e->pack_id = MAX_PACK_ID;
@@ -1187,7 +1189,9 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)
11871189
}
11881190

11891191
for (source = the_repository->objects->sources; source; source = source->next) {
1190-
if (!packfile_list_find_oid(packfile_store_get_packs(source->files->packed), &oid))
1192+
struct odb_source_files *files = odb_source_files_downcast(source);
1193+
1194+
if (!packfile_list_find_oid(packfile_store_get_packs(files->packed), &oid))
11911195
continue;
11921196
e->type = OBJ_BLOB;
11931197
e->pack_id = MAX_PACK_ID;

builtin/grep.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,8 +1218,10 @@ int cmd_grep(int argc,
12181218
struct odb_source *source;
12191219

12201220
odb_prepare_alternates(the_repository->objects);
1221-
for (source = the_repository->objects->sources; source; source = source->next)
1222-
packfile_store_prepare(source->files->packed);
1221+
for (source = the_repository->objects->sources; source; source = source->next) {
1222+
struct odb_source_files *files = odb_source_files_downcast(source);
1223+
packfile_store_prepare(files->packed);
1224+
}
12231225
}
12241226

12251227
start_threads(&opt);

builtin/index-pack.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,9 +1637,11 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
16371637
rename_tmp_packfile(&final_index_name, curr_index_name, &index_name,
16381638
hash, "idx", 1);
16391639

1640-
if (do_fsck_object && startup_info->have_repository)
1641-
packfile_store_load_pack(the_repository->objects->sources->files->packed,
1642-
final_index_name, 0);
1640+
if (do_fsck_object && startup_info->have_repository) {
1641+
struct odb_source_files *files =
1642+
odb_source_files_downcast(the_repository->objects->sources);
1643+
packfile_store_load_pack(files->packed, final_index_name, 0);
1644+
}
16431645

16441646
if (!from_stdin) {
16451647
printf("%s\n", hash_to_hex(hash));

builtin/pack-objects.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,8 @@ static int want_cruft_object_mtime(struct repository *r,
15311531
struct odb_source *source;
15321532

15331533
for (source = r->objects->sources; source; source = source->next) {
1534-
struct packed_git **cache = packfile_store_get_kept_pack_cache(source->files->packed, flags);
1534+
struct odb_source_files *files = odb_source_files_downcast(source);
1535+
struct packed_git **cache = packfile_store_get_kept_pack_cache(files->packed, flags);
15351536

15361537
for (; *cache; cache++) {
15371538
struct packed_git *p = *cache;
@@ -1753,11 +1754,13 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
17531754
}
17541755

17551756
for (source = the_repository->objects->sources; source; source = source->next) {
1756-
for (e = source->files->packed->packs.head; e; e = e->next) {
1757+
struct odb_source_files *files = odb_source_files_downcast(source);
1758+
1759+
for (e = files->packed->packs.head; e; e = e->next) {
17571760
struct packed_git *p = e->pack;
17581761
want = want_object_in_pack_one(p, oid, exclude, found_pack, found_offset, found_mtime);
17591762
if (!exclude && want > 0)
1760-
packfile_list_prepend(&source->files->packed->packs, p);
1763+
packfile_list_prepend(&files->packed->packs, p);
17611764
if (want != -1)
17621765
return want;
17631766
}
@@ -4337,10 +4340,12 @@ static void add_objects_in_unpacked_packs(void)
43374340

43384341
odb_prepare_alternates(to_pack.repo->objects);
43394342
for (source = to_pack.repo->objects->sources; source; source = source->next) {
4343+
struct odb_source_files *files = odb_source_files_downcast(source);
4344+
43404345
if (!source->local)
43414346
continue;
43424347

4343-
if (packfile_store_for_each_object(source->files->packed, &oi,
4348+
if (packfile_store_for_each_object(files->packed, &oi,
43444349
add_object_in_unpacked_pack, NULL,
43454350
ODB_FOR_EACH_OBJECT_PACK_ORDER |
43464351
ODB_FOR_EACH_OBJECT_LOCAL_ONLY |

commit-graph.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,9 +1980,11 @@ static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
19801980
ctx->approx_nr_objects);
19811981

19821982
odb_prepare_alternates(ctx->r->objects);
1983-
for (source = ctx->r->objects->sources; source; source = source->next)
1984-
packfile_store_for_each_object(source->files->packed, &oi, add_packed_commits_oi,
1983+
for (source = ctx->r->objects->sources; source; source = source->next) {
1984+
struct odb_source_files *files = odb_source_files_downcast(source);
1985+
packfile_store_for_each_object(files->packed, &oi, add_packed_commits_oi,
19851986
ctx, ODB_FOR_EACH_OBJECT_PACK_ORDER);
1987+
}
19861988

19871989
if (ctx->progress_done < ctx->approx_nr_objects)
19881990
display_progress(ctx->progress, ctx->approx_nr_objects);

http.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2543,8 +2543,9 @@ int finish_http_pack_request(struct http_pack_request *preq)
25432543
void http_install_packfile(struct packed_git *p,
25442544
struct packfile_list *list_to_remove_from)
25452545
{
2546+
struct odb_source_files *files = odb_source_files_downcast(the_repository->objects->sources);
25462547
packfile_list_remove(list_to_remove_from, p);
2547-
packfile_store_add_pack(the_repository->objects->sources->files->packed, p);
2548+
packfile_store_add_pack(files->packed, p);
25482549
}
25492550

25502551
struct http_pack_request *new_http_pack_request(

loose.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "path.h"
44
#include "object-file.h"
55
#include "odb.h"
6+
#include "odb/source-files.h"
67
#include "hex.h"
78
#include "repository.h"
89
#include "wrapper.h"
@@ -49,27 +50,29 @@ static int insert_loose_map(struct odb_source *source,
4950
const struct object_id *oid,
5051
const struct object_id *compat_oid)
5152
{
52-
struct loose_object_map *map = source->files->loose->map;
53+
struct odb_source_files *files = odb_source_files_downcast(source);
54+
struct loose_object_map *map = files->loose->map;
5355
int inserted = 0;
5456

5557
inserted |= insert_oid_pair(map->to_compat, oid, compat_oid);
5658
inserted |= insert_oid_pair(map->to_storage, compat_oid, oid);
5759
if (inserted)
58-
oidtree_insert(source->files->loose->cache, compat_oid);
60+
oidtree_insert(files->loose->cache, compat_oid);
5961

6062
return inserted;
6163
}
6264

6365
static int load_one_loose_object_map(struct repository *repo, struct odb_source *source)
6466
{
67+
struct odb_source_files *files = odb_source_files_downcast(source);
6568
struct strbuf buf = STRBUF_INIT, path = STRBUF_INIT;
6669
FILE *fp;
6770

68-
if (!source->files->loose->map)
69-
loose_object_map_init(&source->files->loose->map);
70-
if (!source->files->loose->cache) {
71-
ALLOC_ARRAY(source->files->loose->cache, 1);
72-
oidtree_init(source->files->loose->cache);
71+
if (!files->loose->map)
72+
loose_object_map_init(&files->loose->map);
73+
if (!files->loose->cache) {
74+
ALLOC_ARRAY(files->loose->cache, 1);
75+
oidtree_init(files->loose->cache);
7376
}
7477

7578
insert_loose_map(source, repo->hash_algo->empty_tree, repo->compat_hash_algo->empty_tree);
@@ -125,7 +128,8 @@ int repo_read_loose_object_map(struct repository *repo)
125128

126129
int repo_write_loose_object_map(struct repository *repo)
127130
{
128-
kh_oid_map_t *map = repo->objects->sources->files->loose->map->to_compat;
131+
struct odb_source_files *files = odb_source_files_downcast(repo->objects->sources);
132+
kh_oid_map_t *map = files->loose->map->to_compat;
129133
struct lock_file lock;
130134
int fd;
131135
khiter_t iter;
@@ -231,7 +235,8 @@ int repo_loose_object_map_oid(struct repository *repo,
231235
khiter_t pos;
232236

233237
for (source = repo->objects->sources; source; source = source->next) {
234-
struct loose_object_map *loose_map = source->files->loose->map;
238+
struct odb_source_files *files = odb_source_files_downcast(source);
239+
struct loose_object_map *loose_map = files->loose->map;
235240
if (!loose_map)
236241
continue;
237242
map = (to == repo->compat_hash_algo) ?

midx.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
9595

9696
struct multi_pack_index *get_multi_pack_index(struct odb_source *source)
9797
{
98-
packfile_store_prepare(source->files->packed);
99-
return source->files->packed->midx;
98+
struct odb_source_files *files = odb_source_files_downcast(source);
99+
packfile_store_prepare(files->packed);
100+
return files->packed->midx;
100101
}
101102

102103
static struct multi_pack_index *load_multi_pack_index_one(struct odb_source *source,
@@ -447,6 +448,7 @@ static uint32_t midx_for_pack(struct multi_pack_index **_m,
447448
int prepare_midx_pack(struct multi_pack_index *m,
448449
uint32_t pack_int_id)
449450
{
451+
struct odb_source_files *files = odb_source_files_downcast(m->source);
450452
struct strbuf pack_name = STRBUF_INIT;
451453
struct packed_git *p;
452454

@@ -457,10 +459,10 @@ int prepare_midx_pack(struct multi_pack_index *m,
457459
if (m->packs[pack_int_id])
458460
return 0;
459461

460-
strbuf_addf(&pack_name, "%s/pack/%s", m->source->path,
462+
strbuf_addf(&pack_name, "%s/pack/%s", files->base.path,
461463
m->pack_names[pack_int_id]);
462-
p = packfile_store_load_pack(m->source->files->packed,
463-
pack_name.buf, m->source->local);
464+
p = packfile_store_load_pack(files->packed,
465+
pack_name.buf, files->base.local);
464466
strbuf_release(&pack_name);
465467

466468
if (!p) {
@@ -703,18 +705,19 @@ int midx_preferred_pack(struct multi_pack_index *m, uint32_t *pack_int_id)
703705

704706
int prepare_multi_pack_index_one(struct odb_source *source)
705707
{
708+
struct odb_source_files *files = odb_source_files_downcast(source);
706709
struct repository *r = source->odb->repo;
707710

708711
prepare_repo_settings(r);
709712
if (!r->settings.core_multi_pack_index)
710713
return 0;
711714

712-
if (source->files->packed->midx)
715+
if (files->packed->midx)
713716
return 1;
714717

715-
source->files->packed->midx = load_multi_pack_index(source);
718+
files->packed->midx = load_multi_pack_index(source);
716719

717-
return !!source->files->packed->midx;
720+
return !!files->packed->midx;
718721
}
719722

720723
int midx_checksum_valid(struct multi_pack_index *m)
@@ -803,9 +806,10 @@ void clear_midx_file(struct repository *r)
803806
struct odb_source *source;
804807

805808
for (source = r->objects->sources; source; source = source->next) {
806-
if (source->files->packed->midx)
807-
close_midx(source->files->packed->midx);
808-
source->files->packed->midx = NULL;
809+
struct odb_source_files *files = odb_source_files_downcast(source);
810+
if (files->packed->midx)
811+
close_midx(files->packed->midx);
812+
files->packed->midx = NULL;
809813
}
810814
}
811815

object-file.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ static void *odb_source_loose_map_object(struct odb_source *source,
219219
const struct object_id *oid,
220220
unsigned long *size)
221221
{
222+
struct odb_source_files *files = odb_source_files_downcast(source);
222223
const char *p;
223-
int fd = open_loose_object(source->files->loose, oid, &p);
224+
int fd = open_loose_object(files->loose, oid, &p);
224225

225226
if (fd < 0)
226227
return NULL;
@@ -401,6 +402,7 @@ static int read_object_info_from_path(struct odb_source *source,
401402
struct object_info *oi,
402403
enum object_info_flags flags)
403404
{
405+
struct odb_source_files *files = odb_source_files_downcast(source);
404406
int ret;
405407
int fd;
406408
unsigned long mapsize;
@@ -423,7 +425,7 @@ static int read_object_info_from_path(struct odb_source *source,
423425
struct stat st;
424426

425427
if ((!oi || (!oi->disk_sizep && !oi->mtimep)) && (flags & OBJECT_INFO_QUICK)) {
426-
ret = quick_has_loose(source->files->loose, oid) ? 0 : -1;
428+
ret = quick_has_loose(files->loose, oid) ? 0 : -1;
427429
goto out;
428430
}
429431

@@ -1866,33 +1868,34 @@ static int append_loose_object(const struct object_id *oid,
18661868
struct oidtree *odb_source_loose_cache(struct odb_source *source,
18671869
const struct object_id *oid)
18681870
{
1871+
struct odb_source_files *files = odb_source_files_downcast(source);
18691872
int subdir_nr = oid->hash[0];
18701873
struct strbuf buf = STRBUF_INIT;
1871-
size_t word_bits = bitsizeof(source->files->loose->subdir_seen[0]);
1874+
size_t word_bits = bitsizeof(files->loose->subdir_seen[0]);
18721875
size_t word_index = subdir_nr / word_bits;
18731876
size_t mask = (size_t)1u << (subdir_nr % word_bits);
18741877
uint32_t *bitmap;
18751878

18761879
if (subdir_nr < 0 ||
1877-
(size_t) subdir_nr >= bitsizeof(source->files->loose->subdir_seen))
1880+
(size_t) subdir_nr >= bitsizeof(files->loose->subdir_seen))
18781881
BUG("subdir_nr out of range");
18791882

1880-
bitmap = &source->files->loose->subdir_seen[word_index];
1883+
bitmap = &files->loose->subdir_seen[word_index];
18811884
if (*bitmap & mask)
1882-
return source->files->loose->cache;
1883-
if (!source->files->loose->cache) {
1884-
ALLOC_ARRAY(source->files->loose->cache, 1);
1885-
oidtree_init(source->files->loose->cache);
1885+
return files->loose->cache;
1886+
if (!files->loose->cache) {
1887+
ALLOC_ARRAY(files->loose->cache, 1);
1888+
oidtree_init(files->loose->cache);
18861889
}
18871890
strbuf_addstr(&buf, source->path);
18881891
for_each_file_in_obj_subdir(subdir_nr, &buf,
18891892
source->odb->repo->hash_algo,
18901893
append_loose_object,
18911894
NULL, NULL,
1892-
source->files->loose->cache);
1895+
files->loose->cache);
18931896
*bitmap |= mask;
18941897
strbuf_release(&buf);
1895-
return source->files->loose->cache;
1898+
return files->loose->cache;
18961899
}
18971900

18981901
static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
@@ -1905,7 +1908,8 @@ static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
19051908

19061909
void odb_source_loose_reprepare(struct odb_source *source)
19071910
{
1908-
odb_source_loose_clear_cache(source->files->loose);
1911+
struct odb_source_files *files = odb_source_files_downcast(source);
1912+
odb_source_loose_clear_cache(files->loose);
19091913
}
19101914

19111915
static int check_stream_oid(git_zstream *stream,

0 commit comments

Comments
 (0)