Skip to content

Commit 27bba42

Browse files
pks-tgitster
authored andcommitted
odb/source: generalize reprepare() callback
The `reprepare()` callback function can be used to flush caches of a given object source and then prepare it anew. This is for example used when a concurrent process may have written new objects. Ultimately, this can be seen as doing two separate steps: 1. We drop any caches. 2. We prepare the source. We have one callsite in git-grep(1) though that really only want to do (2). This is done by reaching into the "files" backend directly and then calling `odb_source_packed_prepare()`, which of course may not work with alternate backends. We could in theory just call `reprepare()` here, and that would likely not have any significant downside. But this would certainly feel like a code smell. Instead, generalize the `reprepare()` callback to `prepare()` with a flag that optionally instructs the backend to also flush the caches, which allows us to drop the external `odb_source_packed_prepare()` declaration. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 1bba44e commit 27bba42

11 files changed

Lines changed: 52 additions & 52 deletions

File tree

builtin/grep.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
#include "setup.h"
2626
#include "submodule.h"
2727
#include "submodule-config.h"
28-
#include "object-file.h"
2928
#include "object-name.h"
3029
#include "odb.h"
30+
#include "odb/source.h"
3131
#include "oid-array.h"
3232
#include "oidset.h"
33-
#include "packfile.h"
3433
#include "pager.h"
3534
#include "path.h"
3635
#include "promisor-remote.h"
@@ -1361,10 +1360,8 @@ int cmd_grep(int argc,
13611360
struct odb_source *source;
13621361

13631362
odb_prepare_alternates(the_repository->objects);
1364-
for (source = the_repository->objects->sources; source; source = source->next) {
1365-
struct odb_source_files *files = odb_source_files_downcast(source);
1366-
odb_source_packed_prepare(files->packed);
1367-
}
1363+
for (source = the_repository->objects->sources; source; source = source->next)
1364+
odb_source_prepare(source, 0);
13681365
}
13691366

13701367
start_threads(&opt);

midx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
101101

102102
struct multi_pack_index *get_multi_pack_index(struct odb_source_packed *source)
103103
{
104-
odb_source_packed_prepare(source);
104+
odb_source_prepare(&source->base, 0);
105105
return source->midx;
106106
}
107107

odb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ void odb_reprepare(struct object_database *o)
10861086
odb_prepare_alternates(o);
10871087

10881088
for (source = o->sources; source; source = source->next)
1089-
odb_source_reprepare(source);
1089+
odb_source_prepare(source, ODB_PREPARE_FLUSH_CACHES);
10901090

10911091
o->object_count_valid = 0;
10921092

odb.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ void odb_free(struct object_database *o);
124124
*/
125125
void odb_close(struct object_database *o);
126126

127+
enum odb_prepare_flags {
128+
/*
129+
* Flush caches, reload alternates and then re-prepare each object
130+
* source so that new objects may become accessible.
131+
*/
132+
ODB_PREPARE_FLUSH_CACHES = (1 << 0),
133+
};
134+
127135
/*
128136
* Clear caches, reload alternates and then reload object sources so that new
129137
* objects may become accessible.

odb/source-files.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ static void odb_source_files_close(struct odb_source *source)
4141
odb_source_close(&files->packed->base);
4242
}
4343

44-
static void odb_source_files_reprepare(struct odb_source *source)
44+
static void odb_source_files_prepare(struct odb_source *source,
45+
enum odb_prepare_flags flags)
4546
{
4647
struct odb_source_files *files = odb_source_files_downcast(source);
47-
odb_source_reprepare(&files->loose->base);
48-
odb_source_reprepare(&files->packed->base);
48+
odb_source_prepare(&files->loose->base, flags);
49+
odb_source_prepare(&files->packed->base, flags);
4950
}
5051

5152
static int odb_source_files_read_object_info(struct odb_source *source,
@@ -273,7 +274,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
273274

274275
files->base.free = odb_source_files_free;
275276
files->base.close = odb_source_files_close;
276-
files->base.reprepare = odb_source_files_reprepare;
277+
files->base.prepare = odb_source_files_prepare;
277278
files->base.read_object_info = odb_source_files_read_object_info;
278279
files->base.read_object_stream = odb_source_files_read_object_stream;
279280
files->base.for_each_object = odb_source_files_for_each_object;

odb/source-inmemory.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ static void odb_source_inmemory_close(struct odb_source *source UNUSED)
325325
{
326326
}
327327

328-
static void odb_source_inmemory_reprepare(struct odb_source *source UNUSED)
328+
static void odb_source_inmemory_prepare(struct odb_source *source UNUSED,
329+
enum odb_prepare_flags flags UNUSED)
329330
{
330331
}
331332

@@ -365,7 +366,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
365366

366367
source->base.free = odb_source_inmemory_free;
367368
source->base.close = odb_source_inmemory_close;
368-
source->base.reprepare = odb_source_inmemory_reprepare;
369+
source->base.prepare = odb_source_inmemory_prepare;
369370
source->base.read_object_info = odb_source_inmemory_read_object_info;
370371
source->base.read_object_stream = odb_source_inmemory_read_object_stream;
371372
source->base.for_each_object = odb_source_inmemory_for_each_object;

odb/source-loose.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,12 @@ static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
672672
sizeof(loose->subdir_seen));
673673
}
674674

675-
static void odb_source_loose_reprepare(struct odb_source *source)
675+
static void odb_source_loose_prepare(struct odb_source *source,
676+
enum odb_prepare_flags flags)
676677
{
677678
struct odb_source_loose *loose = odb_source_loose_downcast(source);
678-
odb_source_loose_clear_cache(loose);
679+
if (flags & ODB_PREPARE_FLUSH_CACHES)
680+
odb_source_loose_clear_cache(loose);
679681
}
680682

681683
static void odb_source_loose_close(struct odb_source *source UNUSED)
@@ -716,7 +718,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
716718

717719
loose->base.free = odb_source_loose_free;
718720
loose->base.close = odb_source_loose_close;
719-
loose->base.reprepare = odb_source_loose_reprepare;
721+
loose->base.prepare = odb_source_loose_prepare;
720722
loose->base.read_object_info = odb_source_loose_read_object_info;
721723
loose->base.read_object_stream = odb_source_loose_read_object_stream;
722724
loose->base.for_each_object = odb_source_loose_for_each_object;

odb/source-packed.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static int find_pack_entry(struct odb_source_packed *store,
1515
{
1616
struct packfile_list_entry *l;
1717

18-
odb_source_packed_prepare(store);
18+
odb_source_prepare(&store->base, 0);
1919
if (store->midx && fill_midx_entry(store->midx, oid, e))
2020
return 1;
2121

@@ -47,7 +47,7 @@ static int odb_source_packed_read_object_info(struct odb_source *source,
4747
* been added since the last time we have prepared the packfile store.
4848
*/
4949
if (flags & OBJECT_INFO_SECOND_READ)
50-
odb_source_reprepare(source);
50+
odb_source_prepare(source, ODB_PREPARE_FLUSH_CACHES);
5151

5252
if (!find_pack_entry(packed, oid, &e))
5353
return 1;
@@ -668,27 +668,25 @@ static int sort_pack(const struct packfile_list_entry *a,
668668
return -1;
669669
}
670670

671-
void odb_source_packed_prepare(struct odb_source_packed *source)
671+
static void odb_source_packed_prepare(struct odb_source *source,
672+
enum odb_prepare_flags flags)
672673
{
673-
if (source->initialized)
674+
struct odb_source_packed *packed = odb_source_packed_downcast(source);
675+
676+
if (flags & ODB_PREPARE_FLUSH_CACHES)
677+
packed->initialized = false;
678+
if (packed->initialized)
674679
return;
675680

676-
prepare_multi_pack_index_one(source);
677-
prepare_packed_git_one(source);
681+
prepare_multi_pack_index_one(packed);
682+
prepare_packed_git_one(packed);
678683

679-
sort_packs(&source->packs.head, sort_pack);
680-
for (struct packfile_list_entry *e = source->packs.head; e; e = e->next)
684+
sort_packs(&packed->packs.head, sort_pack);
685+
for (struct packfile_list_entry *e = packed->packs.head; e; e = e->next)
681686
if (!e->next)
682-
source->packs.tail = e;
687+
packed->packs.tail = e;
683688

684-
source->initialized = true;
685-
}
686-
687-
static void odb_source_packed_reprepare(struct odb_source *source)
688-
{
689-
struct odb_source_packed *packed = odb_source_packed_downcast(source);
690-
packed->initialized = false;
691-
odb_source_packed_prepare(packed);
689+
packed->initialized = true;
692690
}
693691

694692
static void odb_source_packed_reparent(const char *name UNUSED,
@@ -744,7 +742,7 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
744742

745743
packed->base.free = odb_source_packed_free;
746744
packed->base.close = odb_source_packed_close;
747-
packed->base.reprepare = odb_source_packed_reprepare;
745+
packed->base.prepare = odb_source_packed_prepare;
748746
packed->base.read_object_info = odb_source_packed_read_object_info;
749747
packed->base.read_object_stream = odb_source_packed_read_object_stream;
750748
packed->base.for_each_object = odb_source_packed_for_each_object;

odb/source-packed.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,4 @@ static inline struct odb_source_packed *odb_source_packed_downcast(struct odb_so
8282
return container_of(source, struct odb_source_packed, base);
8383
}
8484

85-
/*
86-
* Prepare the source by loading packfiles and multi-pack indices for
87-
* all alternates. This becomes a no-op if the source is already prepared.
88-
*
89-
* It shouldn't typically be necessary to call this function directly, as
90-
* functions that access the source know to prepare it.
91-
*/
92-
void odb_source_packed_prepare(struct odb_source_packed *source);
93-
9485
#endif

odb/source.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ struct odb_source {
8383
void (*close)(struct odb_source *source);
8484

8585
/*
86-
* This callback is expected to clear underlying caches of the object
87-
* database source. The function is called when the repository has for
88-
* example just been repacked so that new objects will become visible.
86+
* This callback is expected to prepare the source so that it becomes
87+
* ready for use. It optionally clears underlying caches of the object
88+
* database source.
8989
*/
90-
void (*reprepare)(struct odb_source *source);
90+
void (*prepare)(struct odb_source *source,
91+
enum odb_prepare_flags flags);
9192

9293
/*
9394
* This callback is expected to read object information from the object
@@ -308,13 +309,14 @@ static inline void odb_source_close(struct odb_source *source)
308309
}
309310

310311
/*
311-
* Reprepare the object database source and clear any caches. Depending on the
312+
* Prepare the object database source and clear any caches. Depending on the
312313
* backend used this may have the effect that concurrently-written objects
313314
* become visible.
314315
*/
315-
static inline void odb_source_reprepare(struct odb_source *source)
316+
static inline void odb_source_prepare(struct odb_source *source,
317+
enum odb_prepare_flags flags)
316318
{
317-
source->reprepare(source);
319+
source->prepare(source, flags);
318320
}
319321

320322
/*

0 commit comments

Comments
 (0)