aboutsummaryrefslogtreecommitdiff
path: root/odb.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2026-03-05 15:19:43 +0100
committerJunio C Hamano <gitster@pobox.com>2026-03-05 11:45:15 -0800
commitd9ecf268ef3f69130fa269012318470d908978f6 (patch)
tree66550ddc707f4f2c1a562201b33a89140f581bc5 /odb.c
parentcb506a8a69c953f7b87bb3ae099e0bed8218d3ab (diff)
downloadgit-d9ecf268ef3f69130fa269012318470d908978f6.tar.xz
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>
Diffstat (limited to 'odb.c')
-rw-r--r--odb.c26
1 files changed, 18 insertions, 8 deletions
diff --git a/odb.c b/odb.c
index c9ebc7e741..e5aa8deb88 100644
--- a/odb.c
+++ b/odb.c
@@ -691,7 +691,8 @@ static int do_oid_object_info_extended(struct object_database *odb,
/* Most likely it's a loose object. */
for (source = odb->sources; source; source = source->next) {
- if (!packfile_store_read_object_info(source->files->packed, real, oi, flags) ||
+ struct odb_source_files *files = odb_source_files_downcast(source);
+ if (!packfile_store_read_object_info(files->packed, real, oi, flags) ||
!odb_source_loose_read_object_info(source, real, oi, flags))
return 0;
}
@@ -699,9 +700,11 @@ static int do_oid_object_info_extended(struct object_database *odb,
/* Not a loose object; someone else may have just packed it. */
if (!(flags & OBJECT_INFO_QUICK)) {
odb_reprepare(odb->repo->objects);
- for (source = odb->sources; source; source = source->next)
- if (!packfile_store_read_object_info(source->files->packed, real, oi, flags))
+ for (source = odb->sources; source; source = source->next) {
+ struct odb_source_files *files = odb_source_files_downcast(source);
+ if (!packfile_store_read_object_info(files->packed, real, oi, flags))
return 0;
+ }
}
/*
@@ -962,7 +965,9 @@ int odb_freshen_object(struct object_database *odb,
odb_prepare_alternates(odb);
for (source = odb->sources; source; source = source->next) {
- if (packfile_store_freshen_object(source->files->packed, oid))
+ struct odb_source_files *files = odb_source_files_downcast(source);
+
+ if (packfile_store_freshen_object(files->packed, oid))
return 1;
if (odb_source_loose_freshen_object(source, oid))
@@ -982,6 +987,8 @@ int odb_for_each_object(struct object_database *odb,
odb_prepare_alternates(odb);
for (struct odb_source *source = odb->sources; source; source = source->next) {
+ struct odb_source_files *files = odb_source_files_downcast(source);
+
if (flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
continue;
@@ -992,7 +999,7 @@ int odb_for_each_object(struct object_database *odb,
return ret;
}
- ret = packfile_store_for_each_object(source->files->packed, request,
+ ret = packfile_store_for_each_object(files->packed, request,
cb, cb_data, flags);
if (ret)
return ret;
@@ -1090,8 +1097,10 @@ struct object_database *odb_new(struct repository *repo,
void odb_close(struct object_database *o)
{
struct odb_source *source;
- for (source = o->sources; source; source = source->next)
- packfile_store_close(source->files->packed);
+ for (source = o->sources; source; source = source->next) {
+ struct odb_source_files *files = odb_source_files_downcast(source);
+ packfile_store_close(files->packed);
+ }
close_commit_graph(o);
}
@@ -1148,8 +1157,9 @@ void odb_reprepare(struct object_database *o)
odb_prepare_alternates(o);
for (source = o->sources; source; source = source->next) {
+ struct odb_source_files *files = odb_source_files_downcast(source);
odb_source_loose_reprepare(source);
- packfile_store_reprepare(source->files->packed);
+ packfile_store_reprepare(files->packed);
}
o->approximate_object_count_valid = 0;