Commit ef507bc7 by Patrick Steinhardt

strmap: introduce `git_strmap_get` and use it throughout the tree

The current way of looking up an entry from a map is tightly coupled with the
map implementation, as one first has to look up the index of the key and then
retrieve the associated value by using the index. As a caller, you usually do
not care about any indices at all, though, so this is more complicated than
really necessary. Furthermore, it invites for errors to happen if the correct
error checking sequence is not being followed.

Introduce a new high-level function `git_strmap_get` that takes a map and a key
and returns a pointer to the associated value if such a key exists. Otherwise,
a `NULL` pointer is returned. Adjust all callers that can trivially be
converted.
parent 7e926ef3
...@@ -33,12 +33,7 @@ GIT_INLINE(void) attr_cache_unlock(git_attr_cache *cache) ...@@ -33,12 +33,7 @@ GIT_INLINE(void) attr_cache_unlock(git_attr_cache *cache)
GIT_INLINE(git_attr_file_entry *) attr_cache_lookup_entry( GIT_INLINE(git_attr_file_entry *) attr_cache_lookup_entry(
git_attr_cache *cache, const char *path) git_attr_cache *cache, const char *path)
{ {
size_t pos = git_strmap_lookup_index(cache->files, path); return git_strmap_get(cache->files, path);
if (git_strmap_valid_index(cache->files, pos))
return git_strmap_value_at(cache->files, pos);
else
return NULL;
} }
int git_attr_cache__alloc_file_entry( int git_attr_cache__alloc_file_entry(
...@@ -265,19 +260,15 @@ bool git_attr_cache__is_cached( ...@@ -265,19 +260,15 @@ bool git_attr_cache__is_cached(
const char *filename) const char *filename)
{ {
git_attr_cache *cache = git_repository_attr_cache(repo); git_attr_cache *cache = git_repository_attr_cache(repo);
git_strmap *files;
size_t pos;
git_attr_file_entry *entry; git_attr_file_entry *entry;
git_strmap *files;
if (!cache || !(files = cache->files)) if (!cache || !(files = cache->files))
return false; return false;
pos = git_strmap_lookup_index(files, filename); if ((entry = git_strmap_get(files, filename)) == NULL)
if (!git_strmap_valid_index(files, pos))
return false; return false;
entry = git_strmap_value_at(files, pos);
return entry && (entry->file[source] != NULL); return entry && (entry->file[source] != NULL);
} }
...@@ -457,13 +448,6 @@ git_attr_rule *git_attr_cache__lookup_macro( ...@@ -457,13 +448,6 @@ git_attr_rule *git_attr_cache__lookup_macro(
git_repository *repo, const char *name) git_repository *repo, const char *name)
{ {
git_strmap *macros = git_repository_attr_cache(repo)->macros; git_strmap *macros = git_repository_attr_cache(repo)->macros;
size_t pos;
pos = git_strmap_lookup_index(macros, name); return git_strmap_get(macros, name);
if (!git_strmap_valid_index(macros, pos))
return NULL;
return (git_attr_rule *)git_strmap_value_at(macros, pos);
} }
...@@ -133,14 +133,12 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent ...@@ -133,14 +133,12 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent
{ {
config_entry_list *existing, *var; config_entry_list *existing, *var;
int error = 0; int error = 0;
size_t pos;
var = git__calloc(1, sizeof(config_entry_list)); var = git__calloc(1, sizeof(config_entry_list));
GIT_ERROR_CHECK_ALLOC(var); GIT_ERROR_CHECK_ALLOC(var);
var->entry = entry; var->entry = entry;
pos = git_strmap_lookup_index(entries->map, entry->name); if ((existing = git_strmap_get(entries->map, entry->name)) == NULL) {
if (!git_strmap_valid_index(entries->map, pos)) {
/* /*
* We only ever inspect `last` from the first config * We only ever inspect `last` from the first config
* entry in a multivar. In case where this new entry is * entry in a multivar. In case where this new entry is
...@@ -157,7 +155,6 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent ...@@ -157,7 +155,6 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent
if (error > 0) if (error > 0)
error = 0; error = 0;
} else { } else {
existing = git_strmap_value_at(entries->map, pos);
config_entry_list_append(&existing, var); config_entry_list_append(&existing, var);
} }
...@@ -171,15 +168,12 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent ...@@ -171,15 +168,12 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent
int config_entry_get(config_entry_list **out, git_config_entries *entries, const char *key) int config_entry_get(config_entry_list **out, git_config_entries *entries, const char *key)
{ {
size_t pos; config_entry_list *list;
pos = git_strmap_lookup_index(entries->map, key);
/* no error message; the config system will write one */ if ((list = git_strmap_get(entries->map, key)) == NULL)
if (!git_strmap_valid_index(entries->map, pos))
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
*out = git_strmap_value_at(entries->map, pos); *out = list;
return 0; return 0;
} }
......
...@@ -233,8 +233,8 @@ static int git_diff_driver_load( ...@@ -233,8 +233,8 @@ static int git_diff_driver_load(
{ {
int error = 0; int error = 0;
git_diff_driver_registry *reg; git_diff_driver_registry *reg;
git_diff_driver *drv = NULL; git_diff_driver *drv;
size_t namelen, pos; size_t namelen;
git_config *cfg = NULL; git_config *cfg = NULL;
git_buf name = GIT_BUF_INIT; git_buf name = GIT_BUF_INIT;
git_config_entry *ce = NULL; git_config_entry *ce = NULL;
...@@ -243,9 +243,8 @@ static int git_diff_driver_load( ...@@ -243,9 +243,8 @@ static int git_diff_driver_load(
if ((reg = git_repository_driver_registry(repo)) == NULL) if ((reg = git_repository_driver_registry(repo)) == NULL)
return -1; return -1;
pos = git_strmap_lookup_index(reg->drivers, driver_name); if ((drv = git_strmap_get(reg->drivers, driver_name)) != NULL) {
if (git_strmap_valid_index(reg->drivers, pos)) { *out = drv;
*out = git_strmap_value_at(reg->drivers, pos);
return 0; return 0;
} }
......
...@@ -49,10 +49,9 @@ int git_mwindow_global_init(void) ...@@ -49,10 +49,9 @@ int git_mwindow_global_init(void)
int git_mwindow_get_pack(struct git_pack_file **out, const char *path) int git_mwindow_get_pack(struct git_pack_file **out, const char *path)
{ {
int error;
char *packname;
size_t pos;
struct git_pack_file *pack; struct git_pack_file *pack;
char *packname;
int error;
if ((error = git_packfile__name(&packname, path)) < 0) if ((error = git_packfile__name(&packname, path)) < 0)
return error; return error;
...@@ -62,13 +61,11 @@ int git_mwindow_get_pack(struct git_pack_file **out, const char *path) ...@@ -62,13 +61,11 @@ int git_mwindow_get_pack(struct git_pack_file **out, const char *path)
return -1; return -1;
} }
pos = git_strmap_lookup_index(git__pack_cache, packname); pack = git_strmap_get(git__pack_cache, packname);
git__free(packname); git__free(packname);
if (git_strmap_valid_index(git__pack_cache, pos)) { if (pack != NULL) {
pack = git_strmap_value_at(git__pack_cache, pos);
git_atomic_inc(&pack->refcount); git_atomic_inc(&pack->refcount);
git_mutex_unlock(&git__mwindow_mutex); git_mutex_unlock(&git__mwindow_mutex);
*out = pack; *out = pack;
return 0; return 0;
......
...@@ -276,11 +276,8 @@ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key) ...@@ -276,11 +276,8 @@ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key)
size_t keylen, itemlen; size_t keylen, itemlen;
char *item_key; char *item_key;
pos = git_strmap_lookup_index(sc->map, key); if ((item = git_strmap_get(sc->map, key)) != NULL)
if (git_strmap_valid_index(sc->map, pos)) {
item = git_strmap_value_at(sc->map, pos);
goto done; goto done;
}
keylen = strlen(key); keylen = strlen(key);
itemlen = sc->item_path_offset + keylen + 1; itemlen = sc->item_path_offset + keylen + 1;
...@@ -320,10 +317,7 @@ done: ...@@ -320,10 +317,7 @@ done:
/* lookup item by key */ /* lookup item by key */
void *git_sortedcache_lookup(const git_sortedcache *sc, const char *key) void *git_sortedcache_lookup(const git_sortedcache *sc, const char *key)
{ {
size_t pos = git_strmap_lookup_index(sc->map, key); return git_strmap_get(sc->map, key);
if (git_strmap_valid_index(sc->map, pos))
return git_strmap_value_at(sc->map, pos);
return NULL;
} }
/* find out how many items are in the cache */ /* find out how many items are in the cache */
......
...@@ -41,6 +41,15 @@ size_t git_strmap_size(git_strmap *map) ...@@ -41,6 +41,15 @@ size_t git_strmap_size(git_strmap *map)
return kh_size(map); return kh_size(map);
} }
void *git_strmap_get(git_strmap *map, const char *key)
{
size_t idx = git_strmap_lookup_index(map, key);
if (!git_strmap_valid_index(map, idx) ||
!git_strmap_has_data(map, idx))
return NULL;
return kh_val(map, idx);
}
size_t git_strmap_lookup_index(git_strmap *map, const char *key) size_t git_strmap_lookup_index(git_strmap *map, const char *key)
{ {
return kh_get(str, map, key); return kh_get(str, map, key);
......
...@@ -50,6 +50,15 @@ void git_strmap_clear(git_strmap *map); ...@@ -50,6 +50,15 @@ void git_strmap_clear(git_strmap *map);
*/ */
size_t git_strmap_size(git_strmap *map); size_t git_strmap_size(git_strmap *map);
/**
* Return value associated with the given key.
*
* @param map map to search key in
* @param key key to search for
* @return value associated with the given key or NULL if the key was not found
*/
void *git_strmap_get(git_strmap *map, const char *key);
size_t git_strmap_lookup_index(git_strmap *map, const char *key); size_t git_strmap_lookup_index(git_strmap *map, const char *key);
int git_strmap_valid_index(git_strmap *map, size_t idx); int git_strmap_valid_index(git_strmap *map, size_t idx);
......
...@@ -267,10 +267,9 @@ int git_submodule_lookup( ...@@ -267,10 +267,9 @@ int git_submodule_lookup(
} }
if (repo->submodule_cache != NULL) { if (repo->submodule_cache != NULL) {
size_t pos = git_strmap_lookup_index(repo->submodule_cache, name); if ((sm = git_strmap_get(repo->submodule_cache, name)) != NULL) {
if (git_strmap_valid_index(repo->submodule_cache, pos)) {
if (out) { if (out) {
*out = git_strmap_value_at(repo->submodule_cache, pos); *out = sm;
GIT_REFCOUNT_INC(*out); GIT_REFCOUNT_INC(*out);
} }
return 0; return 0;
...@@ -399,11 +398,8 @@ static int submodule_get_or_create(git_submodule **out, git_repository *repo, gi ...@@ -399,11 +398,8 @@ static int submodule_get_or_create(git_submodule **out, git_repository *repo, gi
size_t pos; size_t pos;
git_submodule *sm = NULL; git_submodule *sm = NULL;
pos = git_strmap_lookup_index(map, name); if ((sm = git_strmap_get(map, name)) != NULL)
if (git_strmap_valid_index(map, pos)) {
sm = git_strmap_value_at(map, pos);
goto done; goto done;
}
/* if the submodule doesn't exist yet in the map, create it */ /* if the submodule doesn't exist yet in the map, create it */
if ((error = submodule_alloc(&sm, repo, name)) < 0) if ((error = submodule_alloc(&sm, repo, name)) < 0)
...@@ -439,26 +435,18 @@ static int submodules_from_index(git_strmap *map, git_index *idx, git_config *cf ...@@ -439,26 +435,18 @@ static int submodules_from_index(git_strmap *map, git_index *idx, git_config *cf
goto done; goto done;
while (!(error = git_iterator_advance(&entry, i))) { while (!(error = git_iterator_advance(&entry, i))) {
size_t pos = git_strmap_lookup_index(map, entry->path);
git_submodule *sm; git_submodule *sm;
if (git_strmap_valid_index(map, pos)) { if ((sm = git_strmap_get(map, entry->path)) != NULL) {
sm = git_strmap_value_at(map, pos);
if (S_ISGITLINK(entry->mode)) if (S_ISGITLINK(entry->mode))
submodule_update_from_index_entry(sm, entry); submodule_update_from_index_entry(sm, entry);
else else
sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE; sm->flags |= GIT_SUBMODULE_STATUS__INDEX_NOT_SUBMODULE;
} else if (S_ISGITLINK(entry->mode)) { } else if (S_ISGITLINK(entry->mode)) {
size_t name_pos;
const char *name; const char *name;
name_pos = git_strmap_lookup_index(names, entry->path); if ((name = git_strmap_get(names, entry->path)) == NULL)
if (git_strmap_valid_index(names, name_pos)) {
name = git_strmap_value_at(names, name_pos);
} else {
name = entry->path; name = entry->path;
}
if (!submodule_get_or_create(&sm, git_index_owner(idx), map, name)) { if (!submodule_get_or_create(&sm, git_index_owner(idx), map, name)) {
submodule_update_from_index_entry(sm, entry); submodule_update_from_index_entry(sm, entry);
...@@ -491,26 +479,18 @@ static int submodules_from_head(git_strmap *map, git_tree *head, git_config *cfg ...@@ -491,26 +479,18 @@ static int submodules_from_head(git_strmap *map, git_tree *head, git_config *cfg
goto done; goto done;
while (!(error = git_iterator_advance(&entry, i))) { while (!(error = git_iterator_advance(&entry, i))) {
size_t pos = git_strmap_lookup_index(map, entry->path);
git_submodule *sm; git_submodule *sm;
if (git_strmap_valid_index(map, pos)) { if ((sm = git_strmap_get(map, entry->path)) != NULL) {
sm = git_strmap_value_at(map, pos);
if (S_ISGITLINK(entry->mode)) if (S_ISGITLINK(entry->mode))
submodule_update_from_head_data(sm, entry->mode, &entry->id); submodule_update_from_head_data(sm, entry->mode, &entry->id);
else else
sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE; sm->flags |= GIT_SUBMODULE_STATUS__HEAD_NOT_SUBMODULE;
} else if (S_ISGITLINK(entry->mode)) { } else if (S_ISGITLINK(entry->mode)) {
size_t name_pos;
const char *name; const char *name;
name_pos = git_strmap_lookup_index(names, entry->path); if ((name = git_strmap_get(names, entry->path)) == NULL)
if (git_strmap_valid_index(names, name_pos)) {
name = git_strmap_value_at(names, name_pos);
} else {
name = entry->path; name = entry->path;
}
if (!submodule_get_or_create(&sm, git_tree_owner(head), map, name)) { if (!submodule_get_or_create(&sm, git_tree_owner(head), map, name)) {
submodule_update_from_head_data( submodule_update_from_head_data(
......
...@@ -134,16 +134,12 @@ cleanup: ...@@ -134,16 +134,12 @@ cleanup:
static int find_locked(transaction_node **out, git_transaction *tx, const char *refname) static int find_locked(transaction_node **out, git_transaction *tx, const char *refname)
{ {
transaction_node *node; transaction_node *node;
size_t pos;
pos = git_strmap_lookup_index(tx->locks, refname); if ((node = git_strmap_get(tx->locks, refname)) == NULL) {
if (!git_strmap_valid_index(tx->locks, pos)) {
git_error_set(GIT_ERROR_REFERENCE, "the specified reference is not locked"); git_error_set(GIT_ERROR_REFERENCE, "the specified reference is not locked");
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
} }
node = git_strmap_value_at(tx->locks, pos);
*out = node; *out = node;
return 0; return 0;
} }
......
...@@ -723,16 +723,13 @@ int git_treebuilder_insert( ...@@ -723,16 +723,13 @@ int git_treebuilder_insert(
{ {
git_tree_entry *entry; git_tree_entry *entry;
int error; int error;
size_t pos;
assert(bld && id && filename); assert(bld && id && filename);
if ((error = check_entry(bld->repo, filename, id, filemode)) < 0) if ((error = check_entry(bld->repo, filename, id, filemode)) < 0)
return error; return error;
pos = git_strmap_lookup_index(bld->map, filename); if ((entry = git_strmap_get(bld->map, filename)) != NULL) {
if (git_strmap_valid_index(bld->map, pos)) {
entry = git_strmap_value_at(bld->map, pos);
git_oid_cpy((git_oid *) entry->oid, id); git_oid_cpy((git_oid *) entry->oid, id);
} else { } else {
entry = alloc_entry(filename, strlen(filename), id); entry = alloc_entry(filename, strlen(filename), id);
...@@ -757,16 +754,8 @@ int git_treebuilder_insert( ...@@ -757,16 +754,8 @@ int git_treebuilder_insert(
static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename) static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename)
{ {
git_tree_entry *entry = NULL;
size_t pos;
assert(bld && filename); assert(bld && filename);
return git_strmap_get(bld->map, filename);
pos = git_strmap_lookup_index(bld->map, filename);
if (git_strmap_valid_index(bld->map, pos))
entry = git_strmap_value_at(bld->map, pos);
return entry;
} }
const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename) const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename)
......
#include "clar_libgit2.h" #include "clar_libgit2.h"
#include "strmap.h" #include "strmap.h"
git_strmap *g_table; static git_strmap *g_table;
void test_core_strmap__initialize(void) void test_core_strmap__initialize(void)
{ {
...@@ -97,3 +97,35 @@ void test_core_strmap__3(void) ...@@ -97,3 +97,35 @@ void test_core_strmap__3(void)
git_strmap_foreach_value(g_table, str, { i++; free(str); }); git_strmap_foreach_value(g_table, str, { i++; free(str); });
cl_assert(i == 10000); cl_assert(i == 10000);
} }
void test_core_strmap__get_succeeds_with_existing_entries(void)
{
const char *keys[] = { "foo", "bar", "gobble" };
char *values[] = { "oof", "rab", "elbbog" };
int error;
size_t i;
for (i = 0; i < ARRAY_SIZE(keys); i++) {
git_strmap_insert(g_table, keys[i], values[i], &error);
cl_assert_equal_i(error, 1);
}
cl_assert_equal_s(git_strmap_get(g_table, "foo"), "oof");
cl_assert_equal_s(git_strmap_get(g_table, "bar"), "rab");
cl_assert_equal_s(git_strmap_get(g_table, "gobble"), "elbbog");
}
void test_core_strmap__get_returns_null_on_nonexisting_key(void)
{
const char *keys[] = { "foo", "bar", "gobble" };
char *values[] = { "oof", "rab", "elbbog" };
int error;
size_t i;
for (i = 0; i < ARRAY_SIZE(keys); i++) {
git_strmap_insert(g_table, keys[i], values[i], &error);
cl_assert_equal_i(error, 1);
}
cl_assert_equal_p(git_strmap_get(g_table, "other"), NULL);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment