Skip to content

Commit 90a93f9

Browse files
pks-tgitster
authored andcommitted
object-file: move loose object cache into loose source
Our loose objects use a cache that (optionally) stores all objects for each of the opened sharding directories. This cache is located in the `struct odb_source`, but now that we have `struct odb_source_loose` it makes sense to move it into the latter structure so that all state that relates to loose objects is entirely self-contained. Do so. While at it, rename corresponding functions to have a prefix that relates to `struct odb_source_loose`. Note that despite this prefix, the functions still accept a `struct odb_source` as input. This is done intentionally: once we introduce pluggable object databases, we will continue to accept this struct but then do a cast inside these functions to `struct odb_source_loose`. This design is similar to how we do it for our ref backends. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent ece43d9 commit 90a93f9

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

loose.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "git-compat-util.h"
22
#include "hash.h"
33
#include "path.h"
4+
#include "object-file.h"
45
#include "odb.h"
56
#include "hex.h"
67
#include "repository.h"
@@ -54,7 +55,7 @@ static int insert_loose_map(struct odb_source *source,
5455
inserted |= insert_oid_pair(map->to_compat, oid, compat_oid);
5556
inserted |= insert_oid_pair(map->to_storage, compat_oid, oid);
5657
if (inserted)
57-
oidtree_insert(source->loose_objects_cache, compat_oid);
58+
oidtree_insert(source->loose->cache, compat_oid);
5859

5960
return inserted;
6061
}
@@ -66,9 +67,9 @@ static int load_one_loose_object_map(struct repository *repo, struct odb_source
6667

6768
if (!source->loose_map)
6869
loose_object_map_init(&source->loose_map);
69-
if (!source->loose_objects_cache) {
70-
ALLOC_ARRAY(source->loose_objects_cache, 1);
71-
oidtree_init(source->loose_objects_cache);
70+
if (!source->loose->cache) {
71+
ALLOC_ARRAY(source->loose->cache, 1);
72+
oidtree_init(source->loose->cache);
7273
}
7374

7475
insert_loose_map(source, repo->hash_algo->empty_tree, repo->compat_hash_algo->empty_tree);

object-file.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ static int quick_has_loose(struct repository *r,
223223

224224
odb_prepare_alternates(r->objects);
225225
for (source = r->objects->sources; source; source = source->next) {
226-
if (oidtree_contains(odb_loose_cache(source, oid), oid))
226+
if (oidtree_contains(odb_source_loose_cache(source, oid), oid))
227227
return 1;
228228
}
229229
return 0;
@@ -1802,44 +1802,44 @@ static int append_loose_object(const struct object_id *oid,
18021802
return 0;
18031803
}
18041804

1805-
struct oidtree *odb_loose_cache(struct odb_source *source,
1806-
const struct object_id *oid)
1805+
struct oidtree *odb_source_loose_cache(struct odb_source *source,
1806+
const struct object_id *oid)
18071807
{
18081808
int subdir_nr = oid->hash[0];
18091809
struct strbuf buf = STRBUF_INIT;
1810-
size_t word_bits = bitsizeof(source->loose_objects_subdir_seen[0]);
1810+
size_t word_bits = bitsizeof(source->loose->subdir_seen[0]);
18111811
size_t word_index = subdir_nr / word_bits;
18121812
size_t mask = (size_t)1u << (subdir_nr % word_bits);
18131813
uint32_t *bitmap;
18141814

18151815
if (subdir_nr < 0 ||
1816-
(size_t) subdir_nr >= bitsizeof(source->loose_objects_subdir_seen))
1816+
(size_t) subdir_nr >= bitsizeof(source->loose->subdir_seen))
18171817
BUG("subdir_nr out of range");
18181818

1819-
bitmap = &source->loose_objects_subdir_seen[word_index];
1819+
bitmap = &source->loose->subdir_seen[word_index];
18201820
if (*bitmap & mask)
1821-
return source->loose_objects_cache;
1822-
if (!source->loose_objects_cache) {
1823-
ALLOC_ARRAY(source->loose_objects_cache, 1);
1824-
oidtree_init(source->loose_objects_cache);
1821+
return source->loose->cache;
1822+
if (!source->loose->cache) {
1823+
ALLOC_ARRAY(source->loose->cache, 1);
1824+
oidtree_init(source->loose->cache);
18251825
}
18261826
strbuf_addstr(&buf, source->path);
18271827
for_each_file_in_obj_subdir(subdir_nr, &buf,
18281828
source->odb->repo->hash_algo,
18291829
append_loose_object,
18301830
NULL, NULL,
1831-
source->loose_objects_cache);
1831+
source->loose->cache);
18321832
*bitmap |= mask;
18331833
strbuf_release(&buf);
1834-
return source->loose_objects_cache;
1834+
return source->loose->cache;
18351835
}
18361836

18371837
void odb_clear_loose_cache(struct odb_source *source)
18381838
{
1839-
oidtree_clear(source->loose_objects_cache);
1840-
FREE_AND_NULL(source->loose_objects_cache);
1841-
memset(&source->loose_objects_subdir_seen, 0,
1842-
sizeof(source->loose_objects_subdir_seen));
1839+
oidtree_clear(source->loose->cache);
1840+
FREE_AND_NULL(source->loose->cache);
1841+
memset(&source->loose->subdir_seen, 0,
1842+
sizeof(source->loose->subdir_seen));
18431843
}
18441844

18451845
static int check_stream_oid(git_zstream *stream,
@@ -2006,5 +2006,8 @@ struct odb_source_loose *odb_source_loose_new(struct odb_source *source)
20062006

20072007
void odb_source_loose_free(struct odb_source_loose *loose)
20082008
{
2009+
if (!loose)
2010+
return;
2011+
odb_clear_loose_cache(loose->source);
20092012
free(loose);
20102013
}

object-file.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ struct odb_source;
2020

2121
struct odb_source_loose {
2222
struct odb_source *source;
23+
24+
/*
25+
* Used to store the results of readdir(3) calls when we are OK
26+
* sacrificing accuracy due to races for speed. That includes
27+
* object existence with OBJECT_INFO_QUICK, as well as
28+
* our search for unique abbreviated hashes. Don't use it for tasks
29+
* requiring greater accuracy!
30+
*
31+
* Be sure to call odb_load_loose_cache() before using.
32+
*/
33+
uint32_t subdir_seen[8]; /* 256 bits */
34+
struct oidtree *cache;
2335
};
2436

2537
struct odb_source_loose *odb_source_loose_new(struct odb_source *source);
@@ -29,8 +41,8 @@ void odb_source_loose_free(struct odb_source_loose *loose);
2941
* Populate and return the loose object cache array corresponding to the
3042
* given object ID.
3143
*/
32-
struct oidtree *odb_loose_cache(struct odb_source *source,
33-
const struct object_id *oid);
44+
struct oidtree *odb_source_loose_cache(struct odb_source *source,
45+
const struct object_id *oid);
3446

3547
/* Empty the loose object cache for the specified object directory. */
3648
void odb_clear_loose_cache(struct odb_source *source);

object-name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static void find_short_object_filename(struct disambiguate_state *ds)
116116
struct odb_source *source;
117117

118118
for (source = ds->repo->objects->sources; source && !ds->ambiguous; source = source->next)
119-
oidtree_each(odb_loose_cache(source, &ds->bin_pfx),
119+
oidtree_each(odb_source_loose_cache(source, &ds->bin_pfx),
120120
&ds->bin_pfx, ds->len, match_prefix, ds);
121121
}
122122

odb.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ static void odb_source_free(struct odb_source *source)
370370
{
371371
free(source->path);
372372
odb_source_loose_free(source->loose);
373-
odb_clear_loose_cache(source);
374373
loose_object_map_clear(&source->loose_map);
375374
free(source);
376375
}

odb.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,6 @@ struct odb_source {
5151
/* Private state for loose objects. */
5252
struct odb_source_loose *loose;
5353

54-
/*
55-
* Used to store the results of readdir(3) calls when we are OK
56-
* sacrificing accuracy due to races for speed. That includes
57-
* object existence with OBJECT_INFO_QUICK, as well as
58-
* our search for unique abbreviated hashes. Don't use it for tasks
59-
* requiring greater accuracy!
60-
*
61-
* Be sure to call odb_load_loose_cache() before using.
62-
*/
63-
uint32_t loose_objects_subdir_seen[8]; /* 256 bits */
64-
struct oidtree *loose_objects_cache;
65-
6654
/* Map between object IDs for loose objects. */
6755
struct loose_object_map *loose_map;
6856

0 commit comments

Comments
 (0)