odb/source: introduce generic object counting

Introduce generic object counting on the object database source level with a new backend-specific callback function. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Mar 12, 2026 at 09:43 UTC b259f2175b0ccd5574fc6b06b8ec5cbeaa860610
4 files changed +60 -2
odb/source-files.c
+30
@@ -93,6 +93,35 @@ static int odb_source_files_for_each_object(struct odb_source *source,
93 return 0;
94 }
95
96 +static int odb_source_files_count_objects(struct odb_source *source,
97 + enum odb_count_objects_flags flags,
98 + unsigned long *out)
99 +{
100 + struct odb_source_files *files = odb_source_files_downcast(source);
101 + unsigned long count;
102 + int ret;
103 +
104 + ret = packfile_store_count_objects(files->packed, flags, &count);
105 + if (ret < 0)
106 + goto out;
107 +
108 + if (!(flags & ODB_COUNT_OBJECTS_APPROXIMATE)) {
109 + unsigned long loose_count;
110 +
111 + ret = odb_source_loose_count_objects(source, flags, &loose_count);
112 + if (ret < 0)
113 + goto out;
114 +
115 + count += loose_count;
116 + }
117 +
118 + *out = count;
119 + ret = 0;
120 +
121 +out:
122 + return ret;
123 +}
124 +
125 static int odb_source_files_freshen_object(struct odb_source *source,
126 const struct object_id *oid)
127 {
@@ -220,6 +249,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
249 files->base.read_object_info = odb_source_files_read_object_info;
250 files->base.read_object_stream = odb_source_files_read_object_stream;
251 files->base.for_each_object = odb_source_files_for_each_object;
252 + files->base.count_objects = odb_source_files_count_objects;
253 files->base.freshen_object = odb_source_files_freshen_object;
254 files->base.write_object = odb_source_files_write_object;
255 files->base.write_object_stream = odb_source_files_write_object_stream;
odb/source.h
+27
@@ -142,6 +142,21 @@ struct odb_source {
142 void *cb_data,
143 unsigned flags);
144
145 + /*
146 + * This callback is expected to count objects in the given object
147 + * database source. The callback function does not have to guarantee
148 + * that only unique objects are counted. The result shall be assigned
149 + * to the `out` pointer.
150 + *
151 + * Accepts `enum odb_count_objects_flag` flags to alter the behaviour.
152 + *
153 + * The callback is expected to return 0 on success, or a negative error
154 + * code otherwise.
155 + */
156 + int (*count_objects)(struct odb_source *source,
157 + enum odb_count_objects_flags flags,
158 + unsigned long *out);
159 +
160 /*
161 * This callback is expected to freshen the given object so that its
162 * last access time is set to the current time. This is used to ensure
@@ -333,6 +348,18 @@ static inline int odb_source_for_each_object(struct odb_source *source,
348 return source->for_each_object(source, request, cb, cb_data, flags);
349 }
350
351 +/*
352 + * Count the number of objects in the given object database source.
353 + *
354 + * Returns 0 on success, a negative error code otherwise.
355 + */
356 +static inline int odb_source_count_objects(struct odb_source *source,
357 + enum odb_count_objects_flags flags,
358 + unsigned long *out)
359 +{
360 + return source->count_objects(source, flags, out);
361 +}
362 +
363 /*
364 * Freshen an object in the object database by updating its timestamp.
365 * Returns 1 in case the object has been freshened, 0 in case the object does
packfile.c
+2 -2
@@ -1102,6 +1102,7 @@ struct packfile_list_entry *packfile_store_get_packs(struct packfile_store *stor
1102 }
1103
1104 int packfile_store_count_objects(struct packfile_store *store,
1105 + enum odb_count_objects_flags flags UNUSED,
1106 unsigned long *out)
1107 {
1108 struct packfile_list_entry *e;
@@ -1146,10 +1147,9 @@ unsigned long repo_approximate_object_count(struct repository *r)
1147
1148 odb_prepare_alternates(r->objects);
1149 for (source = r->objects->sources; source; source = source->next) {
1149 - struct odb_source_files *files = odb_source_files_downcast(source);
1150 unsigned long c;
1151
1152 - if (!packfile_store_count_objects(files->packed, &c))
1152 + if (!odb_source_count_objects(source, ODB_COUNT_OBJECTS_APPROXIMATE, &c))
1153 count += c;
1154 }
1155
packfile.h
+1
@@ -275,6 +275,7 @@ enum kept_pack_type {
275 * Return 0 on success, a negative error code otherwise.
276 */
277 int packfile_store_count_objects(struct packfile_store *store,
278 + enum odb_count_objects_flags flags,
279 unsigned long *out);
280
281 /*