odb/source: make `close()` function pluggable

Introduce a new callback function in `struct odb_source` to make the function pluggable. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 5, 2026 at 15:19 UTC 3bc3177ad7a472dd5fc45cff16b8f57e5800ebc2
3 files changed +27 -4
odb.c
+2 -4
@@ -1062,10 +1062,8 @@ struct object_database *odb_new(struct repository *repo,
1062 void odb_close(struct object_database *o)
1063 {
1064 struct odb_source *source;
1065 - for (source = o->sources; source; source = source->next) {
1066 - struct odb_source_files *files = odb_source_files_downcast(source);
1067 - packfile_store_close(files->packed);
1068 - }
1065 + for (source = o->sources; source; source = source->next)
1066 + odb_source_close(source);
1067 close_commit_graph(o);
1068 }
1069
odb/source-files.c
+7
@@ -28,6 +28,12 @@ static void odb_source_files_free(struct odb_source *source)
28 free(files);
29 }
30
31 +static void odb_source_files_close(struct odb_source *source)
32 +{
33 + struct odb_source_files *files = odb_source_files_downcast(source);
34 + packfile_store_close(files->packed);
35 +}
36 +
37 static void odb_source_files_reprepare(struct odb_source *source)
38 {
39 struct odb_source_files *files = odb_source_files_downcast(source);
@@ -47,6 +53,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
53 files->packed = packfile_store_new(&files->base);
54
55 files->base.free = odb_source_files_free;
56 + files->base.close = odb_source_files_close;
57 files->base.reprepare = odb_source_files_reprepare;
58
59 /*
odb/source.h
+18
@@ -58,6 +58,14 @@ struct odb_source {
58 */
59 void (*free)(struct odb_source *source);
60
61 + /*
62 + * This callback is expected to close any open resources, like for
63 + * example file descriptors or connections. The source is expected to
64 + * still be usable after it has been closed. Closed resources may need
65 + * to be reopened in that case.
66 + */
67 + void (*close)(struct odb_source *source);
68 +
69 /*
70 * This callback is expected to clear underlying caches of the object
71 * database source. The function is called when the repository has for
@@ -103,6 +111,16 @@ void odb_source_free(struct odb_source *source);
111 */
112 void odb_source_release(struct odb_source *source);
113
114 +/*
115 + * Close the object database source without releasing he underlying data. The
116 + * source can still be used going forward, but it first needs to be reopened.
117 + * This can be useful to reduce resource usage.
118 + */
119 +static inline void odb_source_close(struct odb_source *source)
120 +{
121 + source->close(source);
122 +}
123 +
124 /*
125 * Reprepare the object database source and clear any caches. Depending on the
126 * backend used this may have the effect that concurrently-written objects