Skip to content

Commit 0dbf4ad

Browse files
committed
Merge branch 'ps/odb-make-creation-pluggable' into seen
The creation of the on-disk data structures for the object database has been made pluggable, allowing future backends to customize their setup. As part of this, the initialization of the object database has been deferred, and the loading of the loose-object map has been detangled from repository initialization. * ps/odb-make-creation-pluggable: odb: make creation of on-disk structures pluggable odb/source: introduce function to map source type to name setup: defer object database creation setup: detangle loading of loose object maps loose: load loose object map for the correct source
2 parents e2f575f + 2691faa commit 0dbf4ad

13 files changed

Lines changed: 148 additions & 38 deletions

loose.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,17 @@ static int insert_loose_map(struct odb_source_loose *loose,
6161
return inserted;
6262
}
6363

64-
static int load_one_loose_object_map(struct repository *repo, struct odb_source_loose *loose)
64+
int loose_object_map_load(struct odb_source_loose *loose)
6565
{
66-
struct strbuf buf = STRBUF_INIT, path = STRBUF_INIT;
66+
struct repository *repo = loose->base.odb->repo;
67+
struct strbuf buf = STRBUF_INIT;
68+
char *path;
6769
FILE *fp;
6870
int ret = -1;
6971

72+
if (!should_use_loose_object_map(repo))
73+
return 0;
74+
7075
if (!loose->map)
7176
loose_object_map_init(&loose->map);
7277
if (!loose->cache) {
@@ -78,10 +83,10 @@ static int load_one_loose_object_map(struct repository *repo, struct odb_source_
7883
insert_loose_map(loose, repo->hash_algo->empty_blob, repo->compat_hash_algo->empty_blob);
7984
insert_loose_map(loose, repo->hash_algo->null_oid, repo->compat_hash_algo->null_oid);
8085

81-
repo_common_path_replace(repo, &path, "objects/loose-object-idx");
82-
fp = fopen(path.buf, "rb");
86+
path = xstrfmt("%s/loose-object-idx", loose->base.path);
87+
fp = fopen(path, "rb");
8388
if (!fp) {
84-
strbuf_release(&path);
89+
free(path);
8590
return 0;
8691
}
8792

@@ -102,25 +107,21 @@ static int load_one_loose_object_map(struct repository *repo, struct odb_source_
102107
err:
103108
fclose(fp);
104109
strbuf_release(&buf);
105-
strbuf_release(&path);
110+
free(path);
106111
return ret;
107112
}
108113

109114
int repo_read_loose_object_map(struct repository *repo)
110115
{
111116
struct odb_source *source;
112117

113-
if (!should_use_loose_object_map(repo))
114-
return 0;
115-
116118
odb_prepare_alternates(repo->objects);
117-
118119
for (source = repo->objects->sources; source; source = source->next) {
119120
struct odb_source_files *files = odb_source_files_downcast(source);
120-
if (load_one_loose_object_map(repo, files->loose) < 0) {
121+
if (loose_object_map_load(files->loose) < 0)
121122
return -1;
122-
}
123123
}
124+
124125
return 0;
125126
}
126127

loose.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct loose_object_map {
1313

1414
void loose_object_map_init(struct loose_object_map **map);
1515
void loose_object_map_clear(struct loose_object_map **map);
16+
int loose_object_map_load(struct odb_source_loose *loose);
1617
int repo_loose_object_map_oid(struct repository *repo,
1718
const struct object_id *src,
1819
const struct git_hash_algo *dest_algo,

odb/source-files.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,23 @@ static void odb_source_files_close(struct odb_source *source)
5151
odb_source_close(&files->packed->base);
5252
}
5353

54+
static int odb_source_files_create_on_disk(struct odb_source *source)
55+
{
56+
struct strbuf path = STRBUF_INIT;
57+
58+
safe_create_dir(source->odb->repo, source->path, 1);
59+
60+
strbuf_addf(&path, "%s/pack", source->path);
61+
safe_create_dir(source->odb->repo, path.buf, 1);
62+
63+
strbuf_reset(&path);
64+
strbuf_addf(&path, "%s/info", source->path);
65+
safe_create_dir(source->odb->repo, path.buf, 1);
66+
67+
strbuf_release(&path);
68+
return 0;
69+
}
70+
5471
static void odb_source_files_prepare(struct odb_source *source,
5572
enum odb_prepare_flags flags)
5673
{
@@ -742,6 +759,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
742759

743760
files->base.free = odb_source_files_free;
744761
files->base.close = odb_source_files_close;
762+
files->base.create_on_disk = odb_source_files_create_on_disk;
745763
files->base.prepare = odb_source_files_prepare;
746764
files->base.read_object_info = odb_source_files_read_object_info;
747765
files->base.read_object_stream = odb_source_files_read_object_stream;

odb/source-files.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ bool odb_source_files_optimize_required(struct odb_source *source,
4343
static inline struct odb_source_files *odb_source_files_downcast(struct odb_source *source)
4444
{
4545
if (source->type != ODB_SOURCE_FILES)
46-
BUG("trying to downcast source of type '%d' to files", source->type);
46+
BUG("trying to downcast source of type '%s' to '%s'",
47+
odb_source_type_to_name(source->type),
48+
odb_source_type_to_name(ODB_SOURCE_FILES));
4749
return container_of(source, struct odb_source_files, base);
4850
}
4951

odb/source-inmemory.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
2626
static inline struct odb_source_inmemory *odb_source_inmemory_downcast(struct odb_source *source)
2727
{
2828
if (source->type != ODB_SOURCE_INMEMORY)
29-
BUG("trying to downcast source of type '%d' to in-memory", source->type);
29+
BUG("trying to downcast source of type '%s' to '%s'",
30+
odb_source_type_to_name(source->type),
31+
odb_source_type_to_name(ODB_SOURCE_INMEMORY));
3032
return container_of(source, struct odb_source_inmemory, base);
3133
}
3234

odb/source-loose.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,5 +1055,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
10551055
if (!is_absolute_path(loose->base.path))
10561056
chdir_notify_register(NULL, odb_source_loose_reparent, loose);
10571057

1058+
loose_object_map_load(loose);
1059+
10581060
return loose;
10591061
}

odb/source-loose.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
4141
static inline struct odb_source_loose *odb_source_loose_downcast(struct odb_source *source)
4242
{
4343
if (source->type != ODB_SOURCE_LOOSE)
44-
BUG("trying to downcast source of type '%d' to loose", source->type);
44+
BUG("trying to downcast source of type '%s' to '%s'",
45+
odb_source_type_to_name(source->type),
46+
odb_source_type_to_name(ODB_SOURCE_LOOSE));
4547
return container_of(source, struct odb_source_loose, base);
4648
}
4749

odb/source-packed.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
7878
static inline struct odb_source_packed *odb_source_packed_downcast(struct odb_source *source)
7979
{
8080
if (source->type != ODB_SOURCE_PACKED)
81-
BUG("trying to downcast source of type '%d' to packed", source->type);
81+
BUG("trying to downcast source of type '%s' to '%s'",
82+
odb_source_type_to_name(source->type),
83+
odb_source_type_to_name(ODB_SOURCE_PACKED));
8284
return container_of(source, struct odb_source_packed, base);
8385
}
8486

odb/source.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
#include "odb/source.h"
55
#include "packfile.h"
66

7+
static const char * const odb_source_names_by_type[] = {
8+
[ODB_SOURCE_UNKNOWN] = "unknown",
9+
[ODB_SOURCE_FILES] = "files",
10+
[ODB_SOURCE_LOOSE] = "loose",
11+
[ODB_SOURCE_PACKED] = "packed",
12+
[ODB_SOURCE_INMEMORY] = "inmemory",
13+
};
14+
15+
const char *odb_source_type_to_name(enum odb_source_type type)
16+
{
17+
const char *name;
18+
if (type < 0 || type >= ARRAY_SIZE(odb_source_names_by_type))
19+
type = ODB_SOURCE_UNKNOWN;
20+
name = odb_source_names_by_type[type];
21+
if (!name)
22+
BUG("name missing in `odb_source_names_by_type` for '%d'", type);
23+
return name;
24+
}
25+
726
struct odb_source *odb_source_new(struct object_database *odb,
827
const char *path,
928
bool local)

odb/source.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ enum odb_source_type {
2525
ODB_SOURCE_INMEMORY,
2626
};
2727

28+
/*
29+
* Convert between the enum and its name. Returns the equivalent of "unknown"
30+
* for unknown types.
31+
*/
32+
const char *odb_source_type_to_name(enum odb_source_type type);
33+
2834
struct object_id;
2935
struct odb_read_stream;
3036
struct strvec;
@@ -83,6 +89,18 @@ struct odb_source {
8389
*/
8490
void (*close)(struct odb_source *source);
8591

92+
/*
93+
* This callback is expected to create on-disk data structures that are
94+
* required for this source to operate.
95+
*
96+
* The callback is expected to return 0 on success, a negative error
97+
* code otherwise.
98+
*
99+
* This callback may be NULL in case the source does not need any
100+
* on-disk setup.
101+
*/
102+
int (*create_on_disk)(struct odb_source *source);
103+
86104
/*
87105
* This callback is expected to prepare the source so that it becomes
88106
* ready for use. It optionally clears underlying caches of the object
@@ -327,6 +345,17 @@ static inline void odb_source_close(struct odb_source *source)
327345
source->close(source);
328346
}
329347

348+
/*
349+
* Create on-disk data structures that are required for this source to operate
350+
* correctly. Returns 0 on success, a negative error code otherwise.
351+
*/
352+
static inline int odb_source_create_on_disk(struct odb_source *source)
353+
{
354+
if (!source->create_on_disk)
355+
return 0;
356+
return source->create_on_disk(source);
357+
}
358+
330359
/*
331360
* Prepare the object database source and clear any caches. Depending on the
332361
* backend used this may have the effect that concurrently-written objects

0 commit comments

Comments
 (0)