Skip to content

Commit b259f21

Browse files
pks-tgitster
authored andcommitted
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>
1 parent 2b24db1 commit b259f21

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

odb/source-files.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ static int odb_source_files_for_each_object(struct odb_source *source,
9393
return 0;
9494
}
9595

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+
96125
static int odb_source_files_freshen_object(struct odb_source *source,
97126
const struct object_id *oid)
98127
{
@@ -220,6 +249,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
220249
files->base.read_object_info = odb_source_files_read_object_info;
221250
files->base.read_object_stream = odb_source_files_read_object_stream;
222251
files->base.for_each_object = odb_source_files_for_each_object;
252+
files->base.count_objects = odb_source_files_count_objects;
223253
files->base.freshen_object = odb_source_files_freshen_object;
224254
files->base.write_object = odb_source_files_write_object;
225255
files->base.write_object_stream = odb_source_files_write_object_stream;

odb/source.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,21 @@ struct odb_source {
142142
void *cb_data,
143143
unsigned flags);
144144

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+
145160
/*
146161
* This callback is expected to freshen the given object so that its
147162
* 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,
333348
return source->for_each_object(source, request, cb, cb_data, flags);
334349
}
335350

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+
336363
/*
337364
* Freshen an object in the object database by updating its timestamp.
338365
* Returns 1 in case the object has been freshened, 0 in case the object does

packfile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ struct packfile_list_entry *packfile_store_get_packs(struct packfile_store *stor
11021102
}
11031103

11041104
int packfile_store_count_objects(struct packfile_store *store,
1105+
enum odb_count_objects_flags flags UNUSED,
11051106
unsigned long *out)
11061107
{
11071108
struct packfile_list_entry *e;
@@ -1146,10 +1147,9 @@ unsigned long repo_approximate_object_count(struct repository *r)
11461147

11471148
odb_prepare_alternates(r->objects);
11481149
for (source = r->objects->sources; source; source = source->next) {
1149-
struct odb_source_files *files = odb_source_files_downcast(source);
11501150
unsigned long c;
11511151

1152-
if (!packfile_store_count_objects(files->packed, &c))
1152+
if (!odb_source_count_objects(source, ODB_COUNT_OBJECTS_APPROXIMATE, &c))
11531153
count += c;
11541154
}
11551155

packfile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ enum kept_pack_type {
275275
* Return 0 on success, a negative error code otherwise.
276276
*/
277277
int packfile_store_count_objects(struct packfile_store *store,
278+
enum odb_count_objects_flags flags,
278279
unsigned long *out);
279280

280281
/*

0 commit comments

Comments
 (0)