Commit bd66925a by Patrick Steinhardt

oidmap: remove legacy low-level interface

Remove the low-level interface that was exposing implementation details of
`git_oidmap` to callers. From now on, only the high-level functions shall be
used to retrieve or modify values of a map. Adjust remaining existing callers.
parent 4713e7c8
...@@ -50,9 +50,8 @@ size_t git_oidmap_size(git_oidmap *map) ...@@ -50,9 +50,8 @@ size_t git_oidmap_size(git_oidmap *map)
void *git_oidmap_get(git_oidmap *map, const git_oid *key) void *git_oidmap_get(git_oidmap *map, const git_oid *key)
{ {
size_t idx = git_oidmap_lookup_index(map, key); size_t idx = kh_get(oid, map, key);
if (!git_oidmap_valid_index(map, idx) || if (idx == kh_end(map) || !kh_exist(map, idx))
!git_oidmap_has_data(map, idx))
return NULL; return NULL;
return kh_val(map, idx); return kh_val(map, idx);
} }
...@@ -76,10 +75,10 @@ int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value) ...@@ -76,10 +75,10 @@ int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value)
int git_oidmap_delete(git_oidmap *map, const git_oid *key) int git_oidmap_delete(git_oidmap *map, const git_oid *key)
{ {
khiter_t idx = git_oidmap_lookup_index(map, key); khiter_t idx = kh_get(oid, map, key);
if (!git_oidmap_valid_index(map, idx)) if (idx == kh_end(map))
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
git_oidmap_delete_at(map, idx); kh_del(oid, map, idx);
return 0; return 0;
} }
...@@ -92,84 +91,17 @@ int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oi ...@@ -92,84 +91,17 @@ int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oi
{ {
size_t i = *iter; size_t i = *iter;
while (i < git_oidmap_end(map) && !git_oidmap_has_data(map, i)) while (i < map->n_buckets && !kh_exist(map, i))
i++; i++;
if (i >= git_oidmap_end(map)) if (i >= map->n_buckets)
return GIT_ITEROVER; return GIT_ITEROVER;
if (key) if (key)
*key = git_oidmap_key(map, i); *key = kh_key(map, i);
if (value) if (value)
*value = git_oidmap_value_at(map, i); *value = kh_value(map, i);
*iter = ++i; *iter = ++i;
return 0; return 0;
} }
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
{
return kh_get(oid, map, key);
}
int git_oidmap_valid_index(git_oidmap *map, size_t idx)
{
return idx != kh_end(map);
}
int git_oidmap_has_data(git_oidmap *map, size_t idx)
{
return kh_exist(map, idx);
}
const git_oid *git_oidmap_key(git_oidmap *map, size_t idx)
{
return kh_key(map, idx);
}
void git_oidmap_set_key_at(git_oidmap *map, size_t idx, git_oid *key)
{
kh_key(map, idx) = key;
}
void *git_oidmap_value_at(git_oidmap *map, size_t idx)
{
return kh_val(map, idx);
}
void git_oidmap_set_value_at(git_oidmap *map, size_t idx, void *value)
{
kh_val(map, idx) = value;
}
void git_oidmap_delete_at(git_oidmap *map, size_t idx)
{
kh_del(oid, map, idx);
}
int git_oidmap_put(git_oidmap *map, const git_oid *key, int *err)
{
return kh_put(oid, map, key, err);
}
void git_oidmap_insert(git_oidmap *map, const git_oid *key, void *value, int *rval)
{
khiter_t idx = kh_put(oid, map, key, rval);
if ((*rval) >= 0) {
if ((*rval) == 0)
kh_key(map, idx) = key;
kh_val(map, idx) = value;
}
}
size_t git_oidmap_begin(git_oidmap *map)
{
GIT_UNUSED(map);
return 0;
}
size_t git_oidmap_end(git_oidmap *map)
{
return map->n_buckets;
}
...@@ -120,23 +120,6 @@ int git_oidmap_exists(git_oidmap *map, const git_oid *key); ...@@ -120,23 +120,6 @@ int git_oidmap_exists(git_oidmap *map, const git_oid *key);
*/ */
int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oid **key); int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, 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);
int git_oidmap_has_data(git_oidmap *map, size_t idx);
const git_oid *git_oidmap_key(git_oidmap *map, size_t idx);
void git_oidmap_set_key_at(git_oidmap *map, size_t idx, git_oid *key);
void *git_oidmap_value_at(git_oidmap *map, size_t idx);
void git_oidmap_set_value_at(git_oidmap *map, size_t idx, void *value);
void git_oidmap_delete_at(git_oidmap *map, size_t idx);
int git_oidmap_put(git_oidmap *map, const git_oid *key, int *err);
void git_oidmap_insert(git_oidmap *map, const git_oid *key, void *value, int *rval);
size_t git_oidmap_begin(git_oidmap *map);
size_t git_oidmap_end(git_oidmap *map);
#define git_oidmap_foreach_value(h, vvar, code) { size_t __i = 0; \ #define git_oidmap_foreach_value(h, vvar, code) { size_t __i = 0; \
while (git_oidmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \ while (git_oidmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
code; \ code; \
......
#include "clar_libgit2.h" #include "clar_libgit2.h"
#include "oidmap.h" #include "oidmap.h"
typedef struct { static struct {
git_oid oid; git_oid oid;
size_t extra; size_t extra;
} oidmap_item; } test_oids[0x0FFF];
#define NITEMS 0x0fff static git_oidmap *g_map;
void test_core_oidmap__basic(void) void test_core_oidmap__initialize(void)
{ {
git_oidmap *map;
oidmap_item items[NITEMS];
uint32_t i, j; uint32_t i, j;
for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
uint32_t segment = i / 8;
int modi = i - (segment * 8);
test_oids[i].extra = i;
for (i = 0; i < NITEMS; ++i) {
items[i].extra = i;
for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) { for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
items[i].oid.id[j * 4 ] = (unsigned char)i; test_oids[i].oid.id[j * 4 ] = (unsigned char)modi;
items[i].oid.id[j * 4 + 1] = (unsigned char)(i >> 8); test_oids[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8);
items[i].oid.id[j * 4 + 2] = (unsigned char)(i >> 16); test_oids[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16);
items[i].oid.id[j * 4 + 3] = (unsigned char)(i >> 24); test_oids[i].oid.id[j * 4 + 3] = (unsigned char)(modi >> 24);
} }
}
cl_git_pass(git_oidmap_new(&map));
for (i = 0; i < NITEMS; ++i) {
size_t pos;
int ret;
pos = git_oidmap_lookup_index(map, &items[i].oid);
cl_assert(!git_oidmap_valid_index(map, pos));
pos = git_oidmap_put(map, &items[i].oid, &ret); test_oids[i].oid.id[ 8] = (unsigned char)i;
cl_assert(ret != 0); test_oids[i].oid.id[ 9] = (unsigned char)(i >> 8);
test_oids[i].oid.id[10] = (unsigned char)(i >> 16);
git_oidmap_set_value_at(map, pos, &items[i]); test_oids[i].oid.id[11] = (unsigned char)(i >> 24);
} }
cl_git_pass(git_oidmap_new(&g_map));
}
for (i = 0; i < NITEMS; ++i) { void test_core_oidmap__cleanup(void)
size_t pos; {
git_oidmap_free(g_map);
}
pos = git_oidmap_lookup_index(map, &items[i].oid); void test_core_oidmap__basic(void)
cl_assert(git_oidmap_valid_index(map, pos)); {
size_t i;
cl_assert_equal_p(git_oidmap_value_at(map, pos), &items[i]); for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
cl_assert(!git_oidmap_exists(g_map, &test_oids[i].oid));
cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
} }
git_oidmap_free(map); for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
cl_assert(git_oidmap_exists(g_map, &test_oids[i].oid));
cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[i]);
}
} }
void test_core_oidmap__hash_collision(void) void test_core_oidmap__hash_collision(void)
{ {
git_oidmap *map; size_t i;
oidmap_item items[NITEMS];
uint32_t i, j;
for (i = 0; i < NITEMS; ++i) { for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
uint32_t segment = i / 8; cl_assert(!git_oidmap_exists(g_map, &test_oids[i].oid));
int modi = i - (segment * 8); cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
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 < ARRAY_SIZE(test_oids); ++i) {
cl_assert(git_oidmap_exists(g_map, &test_oids[i].oid));
for (i = 0; i < NITEMS; ++i) { cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[i]);
size_t pos;
int ret;
pos = git_oidmap_lookup_index(map, &items[i].oid);
cl_assert(!git_oidmap_valid_index(map, pos));
pos = git_oidmap_put(map, &items[i].oid, &ret);
cl_assert(ret != 0);
git_oidmap_set_value_at(map, pos, &items[i]);
} }
for (i = 0; i < NITEMS; ++i) {
size_t pos;
pos = git_oidmap_lookup_index(map, &items[i].oid);
cl_assert(git_oidmap_valid_index(map, pos));
cl_assert_equal_p(git_oidmap_value_at(map, pos), &items[i]);
}
git_oidmap_free(map);
} }
void test_core_oidmap__get_succeeds_with_existing_keys(void) void test_core_oidmap__get_succeeds_with_existing_keys(void)
{ {
git_oidmap *map; size_t i;
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) for (i = 0; i < ARRAY_SIZE(test_oids); ++i)
cl_assert_equal_p(git_oidmap_get(map, &items[i].oid), &items[i]); cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
git_oidmap_free(map); for (i = 0; i < ARRAY_SIZE(test_oids); ++i)
cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[i]);
} }
void test_core_oidmap__get_fails_with_nonexisting_key(void) void test_core_oidmap__get_fails_with_nonexisting_key(void)
{ {
git_oidmap *map; size_t i;
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 */ /* Do _not_ add last OID to verify that we cannot look it up */
for (i = 0; i < NITEMS - 1; ++i) { for (i = 0; i < ARRAY_SIZE(test_oids) - 1; ++i)
int ret; cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
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); cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[ARRAY_SIZE(test_oids) - 1].oid), NULL);
git_oidmap_free(map);
} }
void test_core_oidmap__setting_oid_persists(void) void test_core_oidmap__setting_oid_persists(void)
...@@ -190,18 +97,14 @@ void test_core_oidmap__setting_oid_persists(void) ...@@ -190,18 +97,14 @@ void test_core_oidmap__setting_oid_persists(void)
{{ 0x02 }}, {{ 0x02 }},
{{ 0x03 }} {{ 0x03 }}
}; };
git_oidmap *map;
cl_git_pass(git_oidmap_new(&map));
cl_git_pass(git_oidmap_set(map, &oids[0], "one"));
cl_git_pass(git_oidmap_set(map, &oids[1], "two"));
cl_git_pass(git_oidmap_set(map, &oids[2], "three"));
cl_assert_equal_s(git_oidmap_get(map, &oids[0]), "one"); cl_git_pass(git_oidmap_set(g_map, &oids[0], "one"));
cl_assert_equal_s(git_oidmap_get(map, &oids[1]), "two"); cl_git_pass(git_oidmap_set(g_map, &oids[1], "two"));
cl_assert_equal_s(git_oidmap_get(map, &oids[2]), "three"); cl_git_pass(git_oidmap_set(g_map, &oids[2], "three"));
git_oidmap_free(map); cl_assert_equal_s(git_oidmap_get(g_map, &oids[0]), "one");
cl_assert_equal_s(git_oidmap_get(g_map, &oids[1]), "two");
cl_assert_equal_s(git_oidmap_get(g_map, &oids[2]), "three");
} }
void test_core_oidmap__setting_existing_key_updates(void) void test_core_oidmap__setting_existing_key_updates(void)
...@@ -211,18 +114,14 @@ void test_core_oidmap__setting_existing_key_updates(void) ...@@ -211,18 +114,14 @@ void test_core_oidmap__setting_existing_key_updates(void)
{{ 0x02 }}, {{ 0x02 }},
{{ 0x03 }} {{ 0x03 }}
}; };
git_oidmap *map;
cl_git_pass(git_oidmap_new(&map));
cl_git_pass(git_oidmap_set(map, &oids[0], "one"));
cl_git_pass(git_oidmap_set(map, &oids[1], "two"));
cl_git_pass(git_oidmap_set(map, &oids[2], "three"));
cl_assert_equal_i(git_oidmap_size(map), 3);
cl_git_pass(git_oidmap_set(map, &oids[1], "other")); cl_git_pass(git_oidmap_set(g_map, &oids[0], "one"));
cl_assert_equal_i(git_oidmap_size(map), 3); cl_git_pass(git_oidmap_set(g_map, &oids[1], "two"));
cl_git_pass(git_oidmap_set(g_map, &oids[2], "three"));
cl_assert_equal_i(git_oidmap_size(g_map), 3);
cl_assert_equal_s(git_oidmap_get(map, &oids[1]), "other"); cl_git_pass(git_oidmap_set(g_map, &oids[1], "other"));
cl_assert_equal_i(git_oidmap_size(g_map), 3);
git_oidmap_free(map); cl_assert_equal_s(git_oidmap_get(g_map, &oids[1]), "other");
} }
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