Skip to content

Commit 0c3da97

Browse files
committed
resource: remove unused functions from rutil
Problem: Many functions in src/modules/resource/rutil.c are unused besides their own unit tests. Remove the following functions that have no users: rutil_idset_decode_test rutil_idkey_insert_idset rutil_idkey_insert_id rutil_idkey_merge Update the unit test accordingly.
1 parent ac11b99 commit 0c3da97

File tree

3 files changed

+21
-275
lines changed

3 files changed

+21
-275
lines changed

src/modules/resource/rutil.c

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,6 @@ int rutil_set_json_idset (json_t *o, const char *key, const struct idset *ids)
9999
return -1;
100100
}
101101

102-
bool rutil_idset_decode_test (const char *idset, unsigned long id)
103-
{
104-
struct idset *ids;
105-
bool result;
106-
107-
if (!(ids = idset_decode (idset)))
108-
return false;
109-
result = idset_test (ids, id);
110-
idset_destroy (ids);
111-
return result;
112-
}
113-
114102
char *rutil_read_file (const char *path, flux_error_t *errp)
115103
{
116104
int fd;
@@ -242,109 +230,6 @@ json_t *rutil_load_xml_dir (const char *path, flux_error_t *errp)
242230
return o;
243231
}
244232

245-
static int idkey_remove_id (json_t *obj, unsigned int id)
246-
{
247-
const char *key;
248-
json_t *val;
249-
250-
json_object_foreach (obj, key, val) {
251-
struct idset *ids;
252-
char *new_key;
253-
254-
if (!(ids = idset_decode (key)))
255-
return -1;
256-
if (idset_test (ids, id)) {
257-
idset_clear (ids, id);
258-
if (idset_count (ids) > 0) {
259-
if (!(new_key = idset_encode (ids, IDSET_FLAG_RANGE))) {
260-
idset_destroy (ids);
261-
return -1;
262-
}
263-
if (json_object_set (obj, new_key, val) < 0) {
264-
free (new_key);
265-
idset_destroy (ids);
266-
errno = ENOMEM;
267-
return -1;
268-
}
269-
free (new_key);
270-
}
271-
(void)json_object_del (obj, key);
272-
idset_destroy (ids);
273-
break;
274-
}
275-
idset_destroy (ids);
276-
}
277-
return 0;
278-
}
279-
280-
/* Insert 'new_ids' into 'obj' as follows:
281-
* 1) remove 'new_ids' from any existing keys in 'obj'
282-
* 2) look for existing entry with value same as 'val'
283-
* 3) if found, update key to include 'new_ids'
284-
* 4) if not found, add key consisting only of 'new_ids' to object
285-
*/
286-
int rutil_idkey_insert_idset (json_t *obj, struct idset *new_ids, json_t *val)
287-
{
288-
const char *orig_key;
289-
json_t *orig_val;
290-
bool found = false;
291-
struct idset *ids = NULL;
292-
char *key = NULL;
293-
unsigned int id;
294-
295-
id = idset_first (new_ids);
296-
while (id != IDSET_INVALID_ID) {
297-
if (idkey_remove_id (obj, id) < 0)
298-
return -1;
299-
id = idset_next (new_ids, id);
300-
}
301-
json_object_foreach (obj, orig_key, orig_val) {
302-
if (json_equal (orig_val, val)) {
303-
found = true;
304-
break;
305-
}
306-
}
307-
if (found) {
308-
if (!(ids = idset_decode (orig_key)))
309-
return -1;
310-
if (idset_add (ids, new_ids) < 0)
311-
goto error;
312-
if (!(key = idset_encode (ids, IDSET_FLAG_RANGE)))
313-
goto error;
314-
if (json_object_set (obj, key, val) < 0)
315-
goto error;
316-
(void)json_object_del (obj, orig_key);
317-
}
318-
else {
319-
if (!(key = idset_encode (new_ids, IDSET_FLAG_RANGE)))
320-
return -1;
321-
if (json_object_set (obj, key, val) < 0)
322-
goto error;
323-
}
324-
free (key);
325-
idset_destroy (ids);
326-
return 0;
327-
error:
328-
ERRNO_SAFE_WRAP (free, key);
329-
idset_destroy (ids);
330-
return -1;
331-
}
332-
333-
int rutil_idkey_insert_id (json_t *obj, unsigned int id, json_t *val)
334-
{
335-
struct idset *new_ids;
336-
int rc = -1;
337-
338-
if (!(new_ids = idset_create (0, IDSET_FLAG_AUTOGROW)))
339-
return -1;
340-
if (idset_set (new_ids, id) < 0)
341-
goto done;
342-
rc = rutil_idkey_insert_idset (obj, new_ids, val);
343-
done:
344-
idset_destroy (new_ids);
345-
return rc;
346-
}
347-
348233
int rutil_idkey_map (json_t *obj, rutil_idkey_map_f map, void *arg)
349234
{
350235
const char *key;
@@ -368,18 +253,6 @@ int rutil_idkey_map (json_t *obj, rutil_idkey_map_f map, void *arg)
368253
return 0;
369254
}
370255

371-
static int idkey_merge_map (unsigned int id, json_t *val, void *arg)
372-
{
373-
json_t *obj = arg;
374-
375-
return rutil_idkey_insert_id (obj, id, val);
376-
}
377-
378-
int rutil_idkey_merge (json_t *obj1, json_t *obj2)
379-
{
380-
return rutil_idkey_map (obj2, idkey_merge_map, obj1);
381-
}
382-
383256
static int idkey_count_map (unsigned int id, json_t *val, void *arg)
384257
{
385258
int *count = arg;

src/modules/resource/rutil.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ int rutil_idset_diff (const struct idset *old_set,
2020
struct idset **add,
2121
struct idset **sub);
2222

23-
/* Check whether id is a member of encoded idset
24-
*/
25-
bool rutil_idset_decode_test (const char *idset, unsigned long id);
26-
2723
/* Set key=val in a json object, where val is the string
2824
* representation of 'ids', or the empty string if 'ids' is NULL.
2925
*/
@@ -42,28 +38,16 @@ char *rutil_read_file (const char *path, flux_error_t *errp);
4238
json_t *rutil_load_file (const char *path, flux_error_t *errp);
4339
json_t *rutil_load_xml_dir (const char *path, flux_error_t *errp);
4440

45-
/* Build object with idset keys.
46-
* Start with empty json_t object, then insert objects by id.
47-
* If json_equal() returns true on values, they are combined.
48-
*/
49-
int rutil_idkey_insert_id (json_t *obj, unsigned int id, json_t *val);
50-
int rutil_idkey_insert_idset (json_t *obj, struct idset *ids, json_t *val);
51-
5241
/* Map over object with idset keys, calling 'map' for each id.
5342
* map function returns 0 on success, -1 with errno set to abort.
5443
*/
5544
typedef int (*rutil_idkey_map_f)(unsigned int id, json_t *val, void *arg);
5645
int rutil_idkey_map (json_t *obj, rutil_idkey_map_f map, void *arg);
5746

58-
/* Merge obj2 into obj1, where both are objects with idset keys.
59-
*/
60-
int rutil_idkey_merge (json_t *obj1, json_t *obj2);
61-
6247
/* Count ranks represented in idkey object.
6348
*/
6449
int rutil_idkey_count (json_t *obj);
6550

66-
6751
#endif /* !_FLUX_RESOURCE_RUTIL_H */
6852

6953

src/modules/resource/test/rutil.c

Lines changed: 21 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,6 @@ void test_set_json_idset (void)
147147
idset_destroy (ids);
148148
}
149149

150-
void test_idset_decode_test (void)
151-
{
152-
ok (rutil_idset_decode_test (NULL, 0) == false,
153-
"rutil_idset_decode_test idset=NULL returns false");
154-
ok (rutil_idset_decode_test ("", 0) == false,
155-
"rutil_idset_decode_test idset=\"\" id=0 returns false");
156-
ok (rutil_idset_decode_test ("0", 0) == true,
157-
"rutil_idset_decode_test idset=\"0\" id=0 returns true");
158-
ok (rutil_idset_decode_test ("0", 1) == false,
159-
"rutil_idset_decode_test idset=\"0\" id=1 returns false");
160-
}
161-
162150
static char *create_tmp_file (const char *content)
163151
{
164152
char *path;
@@ -310,45 +298,33 @@ int mapit (unsigned int id, json_t *val, void *arg)
310298
return 0;
311299
}
312300

313-
314-
void test_idkey_basic (void)
301+
void test_idkey_map (void)
315302
{
316303
json_t *obj;
317-
json_t *obj2;
318-
json_t *val1;
319-
json_t *val2;
320-
json_t *val3;
321304
int map_count;
322-
323-
if (!(obj = json_object ()))
324-
BAIL_OUT ("json_object failed");
325-
if (!(obj2 = json_object ()))
326-
BAIL_OUT ("json_object failed");
327-
if (!(val1 = json_pack ("{s:s s:i}", "foo", "xyz", "bar", 42)))
328-
BAIL_OUT ("json_pack failed");
329-
if (!(val2 = json_pack ("{s:s s:i}", "foo", "xyz", "bar", 43)))
330-
BAIL_OUT ("json_pack failed");
331-
if (!(val3 = json_pack ("{s:s s:i}", "foo", "ZZZ", "bar", 42)))
332-
BAIL_OUT ("json_pack failed");
333-
334-
ok (rutil_idkey_insert_id (obj, 0, val1) == 0,
335-
"rutil_idkey_insert_id 0=val1 works");
336-
ok (rutil_idkey_insert_id (obj, 1, val2) == 0,
337-
"rutil_idkey_insert_id 1=val2 works");
338-
ok (rutil_idkey_insert_id (obj, 2, val1) == 0,
339-
"rutil_idkey_insert_id 2=val1 works");
340-
ok (rutil_idkey_insert_id (obj, 3, val2) == 0,
341-
"rutil_idkey_insert_id 3=val2 works");
305+
json_error_t error;
306+
307+
/* Recreate test object for map here.
308+
* This was created step-wise with rutil_idkey_insert_id before,
309+
* but that function has since been removed.
310+
*/
311+
if (!(obj = json_pack_ex (&error, 0,
312+
"{s:{s:s s:i} s:{s:s s:i} s:{s:s s:i}}",
313+
"0",
314+
"foo", "ZZZ",
315+
"bar", 42,
316+
"2",
317+
"foo", "xyz",
318+
"bar", 42,
319+
"1,3",
320+
"foo", "xyz",
321+
"bar", 43)))
322+
BAIL_OUT ("json_pack failed: %s", error.text);
342323

343324
diag_obj ("obj", obj);
344325

345-
ok (json_object_size (obj) == 2,
346-
"identical objects were compressed -> 2 keys");
347-
348-
ok (rutil_idkey_insert_id (obj, 0, val3) == 0,
349-
"rutil_idkey_insert_id 0=val3 works");
350326
ok (json_object_size (obj) == 3,
351-
"object update caused split -> 3 keys");
327+
"object size 3 keys");
352328

353329
map_count = 0;
354330
map_exit_iter = -1;
@@ -366,91 +342,6 @@ void test_idkey_basic (void)
366342
"rutil_idkey_map fails when map function returns -1 with errno set");
367343
map_exit_iter = -1;
368344

369-
ok (rutil_idkey_merge (obj2, obj) == 0
370-
&& rutil_idkey_count (obj2) == 4
371-
&& json_object_size (obj) == 3,
372-
"rutil_idkey_merge into empty object works");
373-
374-
ok (rutil_idkey_merge (obj2, obj) == 0
375-
&& rutil_idkey_count (obj2) == 4
376-
&& json_object_size (obj) == 3,
377-
"rutil_idkey_merge again has no effect");
378-
379-
json_decref (obj);
380-
json_decref (obj2);
381-
json_decref (val1);
382-
json_decref (val2);
383-
json_decref (val3);
384-
}
385-
386-
struct idkey {
387-
const char *ids;
388-
const char *val;
389-
int ids_count;
390-
int obj_count;
391-
};
392-
393-
struct idkey testinput[] = {
394-
{ "0", "{\"a\": 0}", 1, 1 },
395-
{ "1", "{\"a\": 1}", 2, 2 },
396-
{ "2", "{\"a\": 0}", 3, 2 },
397-
{ "3", "{\"a\": 1}", 4, 2 },
398-
{ "3-15", "{\"a\": 2}", 16, 3 },
399-
{ "10-12", "{\"a\": 3}", 16, 4 },
400-
{ "10-12", "{\"a\": 2}", 16, 3 },
401-
{ "3-15", "{\"a\": 1}", 16, 2 },
402-
{ "0-15", "{\"a\": 4}", 16, 1 },
403-
{ "8-1023", "{\"a\": 5}", 1024, 2 },
404-
{ "0-48", "{\"a\": 6}", 1024, 2 },
405-
{ 0 },
406-
};
407-
408-
void test_idkey_one (json_t *obj, struct idkey *idk)
409-
{
410-
json_t *val;
411-
int rc;
412-
json_t *orig;
413-
414-
if (!(orig = json_deep_copy (obj)))
415-
BAIL_OUT ("json_deep_copy failed");
416-
if (!(val = json_loads (idk->val, 0, NULL)))
417-
BAIL_OUT ("json_loads %s failed", idk->val);
418-
if (strlen (idk->ids) == 1) {
419-
unsigned long id = strtoul (idk->ids, NULL, 10);
420-
rc = rutil_idkey_insert_id (obj, id, val);
421-
}
422-
else {
423-
struct idset *ids = idset_decode (idk->ids);
424-
if (!ids)
425-
BAIL_OUT ("idset_decode %s failed", idk->ids);
426-
rc = rutil_idkey_insert_idset (obj, ids, val);
427-
idset_destroy (ids);
428-
}
429-
int ids_count = rutil_idkey_count (obj);
430-
int obj_count = json_object_size (obj);
431-
ok (rc == 0 && ids_count == idk->ids_count && obj_count == idk->obj_count,
432-
"rutil_idkey_insert \"%s\":%s: %d obj %d ids",
433-
idk->ids, idk->val, idk->ids_count, idk->obj_count);
434-
if (rc != 0 || ids_count != idk->ids_count || obj_count != idk->obj_count){
435-
diag_obj ("before", orig);
436-
diag ("rc=%d ids_count=%d obj_count=%d", rc, ids_count, obj_count);
437-
diag_obj ("after", obj);
438-
}
439-
440-
json_decref (val);
441-
json_decref (orig);
442-
}
443-
444-
void test_idkey (void)
445-
{
446-
json_t *obj;
447-
int i;
448-
449-
if (!(obj = json_object ()))
450-
BAIL_OUT ("json_object failed");
451-
452-
for (i = 0; testinput[i].ids != NULL; i++)
453-
test_idkey_one (obj, &testinput[i]);
454345
json_decref (obj);
455346
}
456347

@@ -460,14 +351,12 @@ int main (int argc, char *argv[])
460351

461352
test_idset_diff ();
462353
test_set_json_idset ();
463-
test_idset_decode_test ();
464354

465355
test_read_file ();
466356
test_load_file ();
467357
test_load_xml_dir ();
468358

469-
test_idkey_basic ();
470-
test_idkey ();
359+
test_idkey_map ();
471360

472361
done_testing ();
473362
return (0);

0 commit comments

Comments
 (0)