oidmap: introduce high-level getter for values 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_oidmap_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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
diff --git a/src/cache.c b/src/cache.c
index 424aaae..ec3d9d1 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -151,16 +151,12 @@ static bool cache_should_store(git_object_t object_type, size_t object_size)
static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
{
- size_t pos;
- git_cached_obj *entry = NULL;
+ git_cached_obj *entry;
if (!git_cache__enabled || git_rwlock_rdlock(&cache->lock) < 0)
return NULL;
- pos = git_oidmap_lookup_index(cache->map, oid);
- if (git_oidmap_valid_index(cache->map, pos)) {
- entry = git_oidmap_value_at(cache->map, pos);
-
+ if ((entry = git_oidmap_get(cache->map, oid)) != NULL) {
if (flags && entry->flags != flags) {
entry = NULL;
} else {
@@ -175,7 +171,7 @@ static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
static void *cache_store(git_cache *cache, git_cached_obj *entry)
{
- size_t pos;
+ git_cached_obj *stored_entry;
git_cached_obj_incref(entry);
@@ -194,10 +190,8 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
if (git_cache__current_storage.val > git_cache__max_storage)
cache_evict_entries(cache);
- pos = git_oidmap_lookup_index(cache->map, &entry->oid);
-
/* not found */
- if (!git_oidmap_valid_index(cache->map, pos)) {
+ if ((stored_entry = git_oidmap_get(cache->map, &entry->oid)) == NULL) {
int rval;
git_oidmap_insert(cache->map, &entry->oid, entry, &rval);
@@ -209,19 +203,18 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
}
/* found */
else {
- git_cached_obj *stored_entry = git_oidmap_value_at(cache->map, pos);
-
if (stored_entry->flags == entry->flags) {
git_cached_obj_decref(entry);
git_cached_obj_incref(stored_entry);
entry = stored_entry;
} else if (stored_entry->flags == GIT_CACHE_STORE_RAW &&
- entry->flags == GIT_CACHE_STORE_PARSED) {
+ entry->flags == GIT_CACHE_STORE_PARSED) {
+ int rval;
+
git_cached_obj_decref(stored_entry);
git_cached_obj_incref(entry);
- git_oidmap_set_key_at(cache->map, pos, &entry->oid);
- git_oidmap_set_value_at(cache->map, pos, entry);
+ git_oidmap_insert(cache->map, &entry->oid, entry, &rval);
} else {
/* NO OP */
}
diff --git a/src/describe.c b/src/describe.c
index 2cfa75f..62d8860 100644
--- a/src/describe.c
+++ b/src/describe.c
@@ -36,12 +36,7 @@ struct commit_name {
static void *oidmap_value_bykey(git_oidmap *map, const git_oid *key)
{
- size_t pos = git_oidmap_lookup_index(map, key);
-
- if (!git_oidmap_valid_index(map, pos))
- return NULL;
-
- return git_oidmap_value_at(map, pos);
+ return git_oidmap_get(map, key);
}
static struct commit_name *find_commit_name(
diff --git a/src/merge.c b/src/merge.c
index 22df380..b8bb194 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -1104,14 +1104,13 @@ static void deletes_by_oid_free(git_oidmap *map) {
git_oidmap_free(map);
}
-static int deletes_by_oid_enqueue(git_oidmap *map, git_pool* pool, const git_oid *id, size_t idx) {
- size_t pos;
+static int deletes_by_oid_enqueue(git_oidmap *map, git_pool* pool, const git_oid *id, size_t idx)
+{
deletes_by_oid_queue *queue;
size_t *array_entry;
int error;
- pos = git_oidmap_lookup_index(map, id);
- if (!git_oidmap_valid_index(map, pos)) {
+ if ((queue = git_oidmap_get(map, id)) == NULL) {
queue = git_pool_malloc(pool, sizeof(deletes_by_oid_queue));
GIT_ERROR_CHECK_ALLOC(queue);
@@ -1123,7 +1122,6 @@ static int deletes_by_oid_enqueue(git_oidmap *map, git_pool* pool, const git_oid
if (error < 0)
return -1;
} else {
- queue = git_oidmap_value_at(map, pos);
array_entry = git_array_alloc(queue->arr);
GIT_ERROR_CHECK_ALLOC(array_entry);
*array_entry = idx;
@@ -1132,18 +1130,14 @@ static int deletes_by_oid_enqueue(git_oidmap *map, git_pool* pool, const git_oid
return 0;
}
-static int deletes_by_oid_dequeue(size_t *idx, git_oidmap *map, const git_oid *id) {
- size_t pos;
+static int deletes_by_oid_dequeue(size_t *idx, git_oidmap *map, const git_oid *id)
+{
deletes_by_oid_queue *queue;
size_t *array_entry;
- pos = git_oidmap_lookup_index(map, id);
-
- if (!git_oidmap_valid_index(map, pos))
+ if ((queue = git_oidmap_get(map, id)) == NULL)
return GIT_ENOTFOUND;
- queue = git_oidmap_value_at(map, pos);
-
if (queue->next_pos == 0) {
*idx = queue->first_entry;
} else {
diff --git a/src/odb_mempack.c b/src/odb_mempack.c
index c68e593..e66f2e9 100644
--- a/src/odb_mempack.c
+++ b/src/odb_mempack.c
@@ -79,15 +79,11 @@ static int impl__exists(git_odb_backend *backend, const git_oid *oid)
static int impl__read(void **buffer_p, size_t *len_p, git_object_t *type_p, git_odb_backend *backend, const git_oid *oid)
{
struct memory_packer_db *db = (struct memory_packer_db *)backend;
- struct memobject *obj = NULL;
- size_t pos;
+ struct memobject *obj;
- pos = git_oidmap_lookup_index(db->objects, oid);
- if (!git_oidmap_valid_index(db->objects, pos))
+ if ((obj = git_oidmap_get(db->objects, oid)) == NULL)
return GIT_ENOTFOUND;
- obj = git_oidmap_value_at(db->objects, pos);
-
*len_p = obj->len;
*type_p = obj->type;
*buffer_p = git__malloc(obj->len);
@@ -100,15 +96,11 @@ static int impl__read(void **buffer_p, size_t *len_p, git_object_t *type_p, git_
static int impl__read_header(size_t *len_p, git_object_t *type_p, git_odb_backend *backend, const git_oid *oid)
{
struct memory_packer_db *db = (struct memory_packer_db *)backend;
- struct memobject *obj = NULL;
- size_t pos;
+ struct memobject *obj;
- pos = git_oidmap_lookup_index(db->objects, oid);
- if (!git_oidmap_valid_index(db->objects, pos))
+ if ((obj = git_oidmap_get(db->objects, oid)) == NULL)
return GIT_ENOTFOUND;
- obj = git_oidmap_value_at(db->objects, pos);
-
*len_p = obj->len;
*type_p = obj->type;
return 0;
diff --git a/src/oidmap.c b/src/oidmap.c
index 6b21807..1e2b812 100644
--- a/src/oidmap.c
+++ b/src/oidmap.c
@@ -48,6 +48,15 @@ size_t git_oidmap_size(git_oidmap *map)
return kh_size(map);
}
+void *git_oidmap_get(git_oidmap *map, const git_oid *key)
+{
+ size_t idx = git_oidmap_lookup_index(map, key);
+ if (!git_oidmap_valid_index(map, idx) ||
+ !git_oidmap_has_data(map, idx))
+ return NULL;
+ return kh_val(map, idx);
+}
+
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
{
return kh_get(oid, map, key);
diff --git a/src/oidmap.h b/src/oidmap.h
index c44f722..799fec4 100644
--- a/src/oidmap.h
+++ b/src/oidmap.h
@@ -52,6 +52,15 @@ void git_oidmap_clear(git_oidmap *map);
*/
size_t git_oidmap_size(git_oidmap *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_oidmap_get(git_oidmap *map, const git_oid *key);
+
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key);
int git_oidmap_valid_index(git_oidmap *map, size_t idx);
diff --git a/src/pack-objects.c b/src/pack-objects.c
index 4ccc84d..094317a 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -512,15 +512,12 @@ static int cb_tag_foreach(const char *name, git_oid *oid, void *data)
{
git_packbuilder *pb = data;
git_pobject *po;
- size_t pos;
GIT_UNUSED(name);
- pos = git_oidmap_lookup_index(pb->object_ix, oid);
- if (!git_oidmap_valid_index(pb->object_ix, pos))
+ if ((po = git_oidmap_get(pb->object_ix, oid)) == NULL)
return 0;
- po = git_oidmap_value_at(pb->object_ix, pos);
po->tagged = 1;
/* TODO: peel objects */
@@ -1537,14 +1534,10 @@ static int lookup_walk_object(struct walk_object **out, git_packbuilder *pb, con
static int retrieve_object(struct walk_object **out, git_packbuilder *pb, const git_oid *id)
{
- int error;
- size_t pos;
struct walk_object *obj;
+ int error;
- pos = git_oidmap_lookup_index(pb->walk_objects, id);
- if (git_oidmap_valid_index(pb->walk_objects, pos)) {
- obj = git_oidmap_value_at(pb->walk_objects, pos);
- } else {
+ if ((obj = git_oidmap_get(pb->walk_objects, id)) == NULL) {
if ((error = lookup_walk_object(&obj, pb, id)) < 0)
return error;
diff --git a/src/pack.c b/src/pack.c
index 88af6af..650d203 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -957,14 +957,13 @@ git_off_t get_delta_base(
} else if (type == GIT_OBJECT_REF_DELTA) {
/* If we have the cooperative cache, search in it first */
if (p->has_cache) {
+ struct git_pack_entry *entry;
git_oid oid;
- size_t k;
git_oid_fromraw(&oid, base_info);
- k = git_oidmap_lookup_index(p->idx_cache, &oid);
- if (git_oidmap_valid_index(p->idx_cache, k)) {
+ if ((entry = git_oidmap_get(p->idx_cache, &oid)) != NULL) {
*curpos += 20;
- return ((struct git_pack_entry *)git_oidmap_value_at(p->idx_cache, k))->offset;
+ return entry->offset;
} else {
/* If we're building an index, don't try to find the pack
* entry; we just haven't seen it yet. We'll make
diff --git a/src/revwalk.c b/src/revwalk.c
index b28c341..a052a64 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -25,9 +25,8 @@ git_commit_list_node *git_revwalk__commit_lookup(
int ret;
/* lookup and reserve space if not already present */
- pos = git_oidmap_lookup_index(walk->commits, oid);
- if (git_oidmap_valid_index(walk->commits, pos))
- return git_oidmap_value_at(walk->commits, pos);
+ if ((commit = git_oidmap_get(walk->commits, oid)) != NULL)
+ return commit;
commit = git_commit_list_alloc_node(walk);
if (commit == NULL)
diff --git a/tests/core/oidmap.c b/tests/core/oidmap.c
index 519b587..0e66b72 100644
--- a/tests/core/oidmap.c
+++ b/tests/core/oidmap.c
@@ -104,3 +104,81 @@ void test_core_oidmap__hash_collision(void)
git_oidmap_free(map);
}
+
+void test_core_oidmap__get_succeeds_with_existing_keys(void)
+{
+ git_oidmap *map;
+ oidmap_item items[NITEMS];
+ uint32_t i, j;
+
+ for (i = 0; i < NITEMS; ++i) {
+ uint32_t segment = i / 8;
+ int modi = i - (segment * 8);
+
+ items[i].extra = i;
+
+ for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
+ items[i].oid.id[j * 4 ] = (unsigned char)modi;
+ items[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8);
+ items[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16);
+ items[i].oid.id[j * 4 + 3] = (unsigned char)(modi >> 24);
+ }
+
+ items[i].oid.id[ 8] = (unsigned char)i;
+ items[i].oid.id[ 9] = (unsigned char)(i >> 8);
+ items[i].oid.id[10] = (unsigned char)(i >> 16);
+ items[i].oid.id[11] = (unsigned char)(i >> 24);
+ }
+
+ cl_git_pass(git_oidmap_new(&map));
+
+ for (i = 0; i < NITEMS; ++i) {
+ int ret;
+ git_oidmap_insert(map, &items[i].oid, &items[i], &ret);
+ cl_assert(ret == 1);
+ }
+
+ for (i = 0; i < NITEMS; ++i)
+ cl_assert_equal_p(git_oidmap_get(map, &items[i].oid), &items[i]);
+
+ git_oidmap_free(map);
+}
+
+void test_core_oidmap__get_fails_with_nonexisting_key(void)
+{
+ git_oidmap *map;
+ oidmap_item items[NITEMS];
+ uint32_t i, j;
+
+ for (i = 0; i < NITEMS; ++i) {
+ uint32_t segment = i / 8;
+ int modi = i - (segment * 8);
+
+ items[i].extra = i;
+
+ for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
+ items[i].oid.id[j * 4 ] = (unsigned char)modi;
+ items[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8);
+ items[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16);
+ items[i].oid.id[j * 4 + 3] = (unsigned char)(modi >> 24);
+ }
+
+ items[i].oid.id[ 8] = (unsigned char)i;
+ items[i].oid.id[ 9] = (unsigned char)(i >> 8);
+ items[i].oid.id[10] = (unsigned char)(i >> 16);
+ items[i].oid.id[11] = (unsigned char)(i >> 24);
+ }
+
+ cl_git_pass(git_oidmap_new(&map));
+
+ /* Do _not_ add last OID to verify that we cannot look it up */
+ for (i = 0; i < NITEMS - 1; ++i) {
+ int ret;
+ git_oidmap_insert(map, &items[i].oid, &items[i], &ret);
+ cl_assert(ret == 1);
+ }
+
+ cl_assert_equal_p(git_oidmap_get(map, &items[NITEMS - 1].oid), NULL);
+
+ git_oidmap_free(map);
+}