Commit bd66925a615f3adaf803455693b0b0c79a50ea4c

Patrick Steinhardt 2018-12-01T10:29:32

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.

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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
diff --git a/src/oidmap.c b/src/oidmap.c
index 8893a8b..0ae8bf3 100644
--- a/src/oidmap.c
+++ b/src/oidmap.c
@@ -50,9 +50,8 @@ size_t git_oidmap_size(git_oidmap *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))
+	size_t idx = kh_get(oid, map, key);
+	if (idx == kh_end(map) || !kh_exist(map, idx))
 		return NULL;
 	return kh_val(map, idx);
 }
@@ -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)
 {
-	khiter_t idx = git_oidmap_lookup_index(map, key);
-	if (!git_oidmap_valid_index(map, idx))
+	khiter_t idx = kh_get(oid, map, key);
+	if (idx == kh_end(map))
 		return GIT_ENOTFOUND;
-	git_oidmap_delete_at(map, idx);
+	kh_del(oid, map, idx);
 	return 0;
 }
 
@@ -92,84 +91,17 @@ int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oi
 {
 	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++;
 
-	if (i >= git_oidmap_end(map))
+	if (i >= map->n_buckets)
 		return GIT_ITEROVER;
 
 	if (key)
-		*key = git_oidmap_key(map, i);
+		*key = kh_key(map, i);
 	if (value)
-		*value = git_oidmap_value_at(map, i);
+		*value = kh_value(map, i);
 	*iter = ++i;
 
 	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;
-}
diff --git a/src/oidmap.h b/src/oidmap.h
index 6f57131..b748f72 100644
--- a/src/oidmap.h
+++ b/src/oidmap.h
@@ -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);
 
-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;		\
 	while (git_oidmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) {	\
 		code;								\
diff --git a/tests/core/oidmap.c b/tests/core/oidmap.c
index 38b4c7d..7f98287 100644
--- a/tests/core/oidmap.c
+++ b/tests/core/oidmap.c
@@ -1,186 +1,93 @@
 #include "clar_libgit2.h"
 #include "oidmap.h"
 
-typedef struct {
+static struct {
 	git_oid oid;
 	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;
+	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) {
-			items[i].oid.id[j * 4    ] = (unsigned char)i;
-			items[i].oid.id[j * 4 + 1] = (unsigned char)(i >> 8);
-			items[i].oid.id[j * 4 + 2] = (unsigned char)(i >> 16);
-			items[i].oid.id[j * 4 + 3] = (unsigned char)(i >> 24);
+			test_oids[i].oid.id[j * 4    ] = (unsigned char)modi;
+			test_oids[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8);
+			test_oids[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16);
+			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);
-		cl_assert(ret != 0);
-
-		git_oidmap_set_value_at(map, pos, &items[i]);
+		test_oids[i].oid.id[ 8] = (unsigned char)i;
+		test_oids[i].oid.id[ 9] = (unsigned char)(i >> 8);
+		test_oids[i].oid.id[10] = (unsigned char)(i >> 16);
+		test_oids[i].oid.id[11] = (unsigned char)(i >> 24);
 	}
 
+	cl_git_pass(git_oidmap_new(&g_map));
+}
 
-	for (i = 0; i < NITEMS; ++i) {
-		size_t pos;
+void test_core_oidmap__cleanup(void)
+{
+	git_oidmap_free(g_map);
+}
 
-		pos = git_oidmap_lookup_index(map, &items[i].oid);
-		cl_assert(git_oidmap_valid_index(map, pos));
+void test_core_oidmap__basic(void)
+{
+	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)
 {
-	git_oidmap *map;
-	oidmap_item items[NITEMS];
-	uint32_t i, j;
+	size_t i;
 
-	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);
+	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]));
 	}
 
-	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);
-		cl_assert(ret != 0);
-
-		git_oidmap_set_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_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[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)
 {
-	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);
-	}
+	size_t i;
 
-	for (i = 0; i < NITEMS; ++i)
-		cl_assert_equal_p(git_oidmap_get(map, &items[i].oid), &items[i]);
+	for (i = 0; i < ARRAY_SIZE(test_oids); ++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)
 {
-	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));
+	size_t i;
 
 	/* 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);
-	}
+	for (i = 0; i < ARRAY_SIZE(test_oids) - 1; ++i)
+		cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
 
-	cl_assert_equal_p(git_oidmap_get(map, &items[NITEMS - 1].oid), NULL);
-
-	git_oidmap_free(map);
+	cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[ARRAY_SIZE(test_oids) - 1].oid), NULL);
 }
 
 void test_core_oidmap__setting_oid_persists(void)
@@ -190,18 +97,14 @@ void test_core_oidmap__setting_oid_persists(void)
 	    {{ 0x02 }},
 	    {{ 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_assert_equal_s(git_oidmap_get(map, &oids[1]), "two");
-	cl_assert_equal_s(git_oidmap_get(map, &oids[2]), "three");
+	cl_git_pass(git_oidmap_set(g_map, &oids[0], "one"));
+	cl_git_pass(git_oidmap_set(g_map, &oids[1], "two"));
+	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)
@@ -211,18 +114,14 @@ void test_core_oidmap__setting_existing_key_updates(void)
 	    {{ 0x02 }},
 	    {{ 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_assert_equal_i(git_oidmap_size(map), 3);
+	cl_git_pass(git_oidmap_set(g_map, &oids[0], "one"));
+	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");
 }