Skip to content

Commit 6eb10ff

Browse files
committed
Merge branch 'ps/odb-generalize-prepare'
The 'reprepare()' callback for object database sources has been generalized into a 'prepare()' callback with an optional flush cache flag, and a new 'odb_prepare()' wrapper has been introduced to allow pre-opening object database sources. * ps/odb-generalize-prepare: odb: introduce `odb_prepare()` odb/source: generalize `reprepare()` callback
2 parents 3c775ab + b7fe8f0 commit 6eb10ff

11 files changed

Lines changed: 69 additions & 64 deletions

File tree

builtin/grep.c

Lines changed: 3 additions & 11 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"
@@ -1357,15 +1356,8 @@ int cmd_grep(int argc,
13571356
if (recurse_submodules)
13581357
repo_read_gitmodules(the_repository, 1);
13591358

1360-
if (startup_info->have_repository) {
1361-
struct odb_source *source;
1362-
1363-
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-
}
1368-
}
1359+
if (startup_info->have_repository)
1360+
odb_prepare(the_repository->objects, 0);
13691361

13701362
start_threads(&opt);
13711363
} else {

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: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ void odb_free(struct object_database *o)
10701070
free(o);
10711071
}
10721072

1073-
void odb_reprepare(struct object_database *o)
1073+
void odb_prepare(struct object_database *o, enum odb_prepare_flags flags)
10741074
{
10751075
struct odb_source *source;
10761076

@@ -1082,13 +1082,19 @@ void odb_reprepare(struct object_database *o)
10821082
* the linked list, so existing odbs will continue to exist for
10831083
* the lifetime of the process.
10841084
*/
1085-
o->loaded_alternates = 0;
1086-
odb_prepare_alternates(o);
1085+
if (flags & ODB_PREPARE_FLUSH_CACHES) {
1086+
o->loaded_alternates = 0;
1087+
o->object_count_valid = 0;
1088+
}
10871089

1090+
odb_prepare_alternates(o);
10881091
for (source = o->sources; source; source = source->next)
1089-
odb_source_reprepare(source);
1090-
1091-
o->object_count_valid = 0;
1092+
odb_source_prepare(source, flags);
10921093

10931094
obj_read_unlock();
10941095
}
1096+
1097+
void odb_reprepare(struct object_database *o)
1098+
{
1099+
odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
1100+
}

odb.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,22 @@ 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
/*
128-
* Clear caches, reload alternates and then reload object sources so that new
129-
* objects may become accessible.
136+
* Prepare the object database for use. Calling this function is generally not
137+
* needed, but can be useful in case the caller wants to pre-open individual
138+
* sources.
130139
*/
140+
void odb_prepare(struct object_database *o, enum odb_prepare_flags flags);
141+
142+
/* Equivalent to `odb_prepare(o, ODB_PREPARE_FLUSH_CACHES)`. */
131143
void odb_reprepare(struct object_database *o);
132144

133145
/*

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
@@ -664,10 +664,12 @@ static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
664664
sizeof(loose->subdir_seen));
665665
}
666666

667-
static void odb_source_loose_reprepare(struct odb_source *source)
667+
static void odb_source_loose_prepare(struct odb_source *source,
668+
enum odb_prepare_flags flags)
668669
{
669670
struct odb_source_loose *loose = odb_source_loose_downcast(source);
670-
odb_source_loose_clear_cache(loose);
671+
if (flags & ODB_PREPARE_FLUSH_CACHES)
672+
odb_source_loose_clear_cache(loose);
671673
}
672674

673675
static void odb_source_loose_close(struct odb_source *source UNUSED)
@@ -708,7 +710,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
708710

709711
loose->base.free = odb_source_loose_free;
710712
loose->base.close = odb_source_loose_close;
711-
loose->base.reprepare = odb_source_loose_reprepare;
713+
loose->base.prepare = odb_source_loose_prepare;
712714
loose->base.read_object_info = odb_source_loose_read_object_info;
713715
loose->base.read_object_stream = odb_source_loose_read_object_stream;
714716
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;
@@ -692,27 +692,25 @@ static int sort_pack(const struct packfile_list_entry *a,
692692
return -1;
693693
}
694694

695-
void odb_source_packed_prepare(struct odb_source_packed *source)
695+
static void odb_source_packed_prepare(struct odb_source *source,
696+
enum odb_prepare_flags flags)
696697
{
697-
if (source->initialized)
698+
struct odb_source_packed *packed = odb_source_packed_downcast(source);
699+
700+
if (flags & ODB_PREPARE_FLUSH_CACHES)
701+
packed->initialized = false;
702+
if (packed->initialized)
698703
return;
699704

700-
prepare_multi_pack_index_one(source);
701-
prepare_packed_git_one(source);
705+
prepare_multi_pack_index_one(packed);
706+
prepare_packed_git_one(packed);
702707

703-
sort_packs(&source->packs.head, sort_pack);
704-
for (struct packfile_list_entry *e = source->packs.head; e; e = e->next)
708+
sort_packs(&packed->packs.head, sort_pack);
709+
for (struct packfile_list_entry *e = packed->packs.head; e; e = e->next)
705710
if (!e->next)
706-
source->packs.tail = e;
711+
packed->packs.tail = e;
707712

708-
source->initialized = true;
709-
}
710-
711-
static void odb_source_packed_reprepare(struct odb_source *source)
712-
{
713-
struct odb_source_packed *packed = odb_source_packed_downcast(source);
714-
packed->initialized = false;
715-
odb_source_packed_prepare(packed);
713+
packed->initialized = true;
716714
}
717715

718716
static void odb_source_packed_reparent(const char *name UNUSED,
@@ -768,7 +766,7 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
768766

769767
packed->base.free = odb_source_packed_free;
770768
packed->base.close = odb_source_packed_close;
771-
packed->base.reprepare = odb_source_packed_reprepare;
769+
packed->base.prepare = odb_source_packed_prepare;
772770
packed->base.read_object_info = odb_source_packed_read_object_info;
773771
packed->base.read_object_stream = odb_source_packed_read_object_stream;
774772
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)