object-store: factor out odb_loose_cache()

Add and use a function for loading the entries of a loose object subdirectory for a given object ID. It frees callers from deriving the fanout key; they can use the returned oid_array reference for lookups or forward range scans. Suggested-by: Jeff King <peff@peff.net> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jan 6, 2019 at 17:45 UTC 0000d6543f1c2ceea017161a2807167cdfbf8c0b
3 files changed +21 -8
object-store.h
+7
@@ -54,6 +54,13 @@ void add_to_alternates_memory(const char *dir);
54 */
55 void odb_load_loose_cache(struct object_directory *odb, int subdir_nr);
56
57 +/*
58 + * Populate and return the loose object cache array corresponding to the
59 + * given object ID.
60 + */
61 +struct oid_array *odb_loose_cache(struct object_directory *odb,
62 + const struct object_id *oid);
63 +
64 struct packed_git {
65 struct packed_git *next;
66 struct list_head mru;
sha1-file.c
+9 -3
@@ -924,7 +924,6 @@ static int open_sha1_file(struct repository *r,
924 static int quick_has_loose(struct repository *r,
925 const unsigned char *sha1)
926 {
927 - int subdir_nr = sha1[0];
927 struct object_id oid;
928 struct object_directory *odb;
929
@@ -932,8 +931,7 @@ static int quick_has_loose(struct repository *r,
931
932 prepare_alt_odb(r);
933 for (odb = r->objects->odb; odb; odb = odb->next) {
935 - odb_load_loose_cache(odb, subdir_nr);
936 - if (oid_array_lookup(&odb->loose_objects_cache, &oid) >= 0)
934 + if (oid_array_lookup(odb_loose_cache(odb, &oid), &oid) >= 0)
935 return 1;
936 }
937 return 0;
@@ -2152,6 +2150,14 @@ static int append_loose_object(const struct object_id *oid, const char *path,
2150 return 0;
2151 }
2152
2153 +struct oid_array *odb_loose_cache(struct object_directory *odb,
2154 + const struct object_id *oid)
2155 +{
2156 + int subdir_nr = oid->hash[0];
2157 + odb_load_loose_cache(odb, subdir_nr);
2158 + return &odb->loose_objects_cache;
2159 +}
2160 +
2161 void odb_load_loose_cache(struct object_directory *odb, int subdir_nr)
2162 {
2163 struct strbuf buf = STRBUF_INIT;
sha1-name.c
+5 -5
@@ -87,21 +87,21 @@ static int match_sha(unsigned, const unsigned char *, const unsigned char *);
87
88 static void find_short_object_filename(struct disambiguate_state *ds)
89 {
90 - int subdir_nr = ds->bin_pfx.hash[0];
90 struct object_directory *odb;
91
92 for (odb = the_repository->objects->odb;
93 odb && !ds->ambiguous;
94 odb = odb->next) {
95 int pos;
96 + struct oid_array *loose_objects;
97
98 - odb_load_loose_cache(odb, subdir_nr);
99 - pos = oid_array_lookup(&odb->loose_objects_cache, &ds->bin_pfx);
98 + loose_objects = odb_loose_cache(odb, &ds->bin_pfx);
99 + pos = oid_array_lookup(loose_objects, &ds->bin_pfx);
100 if (pos < 0)
101 pos = -1 - pos;
102 - while (!ds->ambiguous && pos < odb->loose_objects_cache.nr) {
102 + while (!ds->ambiguous && pos < loose_objects->nr) {
103 const struct object_id *oid;
104 - oid = odb->loose_objects_cache.oid + pos;
104 + oid = loose_objects->oid + pos;
105 if (!match_sha(ds->len, ds->bin_pfx.hash, oid->hash))
106 break;
107 update_candidates(ds, oid);