Skip to content

Commit

Permalink
modules/kvs: Add kvs_util_json_encoded_size()
Browse files Browse the repository at this point in the history
Add new kvs_util_json_encoded_size() function determine the
size of an object consistent to the format used by kvs_util_json_dumps().

Use this function instead of fileval_big() in commit API.

Add unit tests appropriately.
  • Loading branch information
chu11 committed Jul 25, 2017
1 parent 7b0f6f2 commit e464830
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/modules/kvs/commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,6 @@ static int store_cache (commit_t *c, int current_epoch, json_t *o,
return rc;
}

/* -1 on error, 0 on false, 1 on true */
static int fileval_big (json_t *value)
{
char *s = json_dumps (value, JSON_ENCODE_ANY);
if (!s) {
errno = ENOMEM;
return -1;
}
int rc = (strlen (s) > BLOBREF_MAX_STRING_SIZE) ? 1 : 0;
free (s);
return rc;
}

/* Store DIRVAL objects, converting them to DIRREFs.
* Store (large) FILEVAL objects, converting them to FILEREFs.
* Return 0 on success, -1 on error
Expand Down Expand Up @@ -269,9 +256,10 @@ static int commit_unroll (commit_t *c, int current_epoch, json_t *dir)
}
}
else if ((key_value = json_object_get (value, "FILEVAL"))) {
if ((ret = fileval_big (key_value)) < 0)
size_t size;
if (kvs_util_json_encoded_size (key_value, &size) < 0)
goto done;
if (ret) {
if (size > BLOBREF_MAX_STRING_SIZE) {
json_incref (key_value);
if ((ret = store_cache (c, current_epoch, key_value,
ref, &hp)) < 0)
Expand Down
13 changes: 13 additions & 0 deletions src/modules/kvs/kvs_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ char *kvs_util_json_dumps (json_t *o)
return s;
}

int kvs_util_json_encoded_size (json_t *o, size_t *size)
{
char *s = kvs_util_json_dumps (o);
if (!s) {
errno = ENOMEM;
return -1;
}
if (size)
*size = strlen (s);
free (s);
return 0;
}

int kvs_util_json_hash (const char *hash_name, json_t *o, href_t ref)
{
char *s;
Expand Down
3 changes: 3 additions & 0 deletions src/modules/kvs/kvs_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
*/
char *kvs_util_json_dumps (json_t *o);

/* returns 0 on success, -1 on failure */
int kvs_util_json_encoded_size (json_t *o, size_t *size);

/* Calculate hash of a json object
*
* Returns -1 on error, 0 on success
Expand Down
23 changes: 23 additions & 0 deletions src/modules/kvs/test/kvs_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ int main (int argc, char *argv[])
json_t *obj;
href_t ref;
char *s1, *s2;
size_t size;

plan (NO_PLAN);

Expand Down Expand Up @@ -43,6 +44,15 @@ int main (int argc, char *argv[])
ok (!strcmp (s1, s2),
"kvs_util_json_dumps dumps correct string");

ok (kvs_util_json_encoded_size (obj, NULL) == 0,
"kvs_util_json_encoded_size works w/ NULL size param");

ok (kvs_util_json_encoded_size (obj, &size) == 0,
"kvs_util_json_encoded_size works");

ok (size == strlen (s2),
"kvs_util_json_encoded_size returns correct size");

free (s1);
s1 = NULL;
json_decref (obj);
Expand All @@ -57,6 +67,12 @@ int main (int argc, char *argv[])
ok (!strcmp (s1, s2),
"kvs_util_json_dumps works on null object");

ok (kvs_util_json_encoded_size (obj, &size) == 0,
"kvs_util_json_encoded_size works");

ok (size == strlen (s2),
"kvs_util_json_encoded_size returns correct size");

free (s1);
s1 = NULL;
json_decref (obj);
Expand All @@ -69,9 +85,16 @@ int main (int argc, char *argv[])
ok (!strcmp (s1, s2),
"kvs_util_json_dumps works on NULL pointer");

ok (kvs_util_json_encoded_size (NULL, &size) == 0,
"kvs_util_json_encoded_size works on NULL pointer");

ok (size == strlen (s2),
"kvs_util_json_encoded_size returns correct size");

free (s1);
s1 = NULL;


done_testing ();
return (0);
}
Expand Down

0 comments on commit e464830

Please sign in to comment.