Skip to content

Commit

Permalink
Merge pull request #824 from garlick/kvs_getat
Browse files Browse the repository at this point in the history
kvs: add kvs_getat() and related functions
  • Loading branch information
grondo authored Oct 3, 2016
2 parents 069c18d + fe5c0a0 commit a8793ce
Show file tree
Hide file tree
Showing 14 changed files with 624 additions and 276 deletions.
15 changes: 15 additions & 0 deletions doc/man1/flux-kvs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ Associate a new treeobj with 'key', overwriting the previous treeobj,
if any. A treeobj should be treated as an opaque string value, which
may contain spaces.

*getat* 'treeobj' 'key'::
Retrieve the value stored under 'key', starting the lookup at 'treeobj'.
If nothing has been stored under 'key', display an error message.

*dirat* 'treeobj' ['key']::
Display all keys and their values under the directory 'key', starting
lookup at 'treeobj'. If 'key' does not exist or is not a directory,
display an error message. If 'key' is not provided, "." (root of
the namespace) is assumed.

*readlinkat* 'treeobj' 'key'::
Retrieve the key a link refers to rather than its value, as would be
returned by *get*, starting lookup at 'treeobj'.


AUTHOR
------
This page is maintained by the Flux community.
Expand Down
4 changes: 4 additions & 0 deletions doc/test/spell.en.pws
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,7 @@ hwm
recurse
treeobj
unlinked
getat
lookup
dirat
readlinkat
1 change: 1 addition & 0 deletions src/broker/hello.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ static void join_request (flux_t h, flux_msg_handler_t *w,
log_msg_exit ("hello: error decoding join request");
if (flux_reduce_append (hello->reduce, (void *)(uintptr_t)count, batch) < 0)
log_err_exit ("hello: flux_reduce_append");
Jput (in);
}

/* Reduction ops
Expand Down
109 changes: 93 additions & 16 deletions src/cmd/flux-kvs.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ void cmd_dir (flux_t h, int argc, char **argv);
void cmd_dirsize (flux_t h, int argc, char **argv);
void cmd_get_treeobj (flux_t h, int argc, char **argv);
void cmd_put_treeobj (flux_t h, int argc, char **argv);
void cmd_getat (flux_t h, int argc, char **argv);
void cmd_dirat (flux_t h, int argc, char **argv);
void cmd_readlinkat (flux_t h, int argc, char **argv);


void usage (void)
Expand All @@ -80,7 +83,7 @@ void usage (void)
" flux-kvs mkdir key [key...]\n"
" flux-kvs exists key\n"
" flux-kvs watch key\n"
" flux-kvs watch-dir [-r] key\n"
" flux-kvs watch-dir [-r] [count] key\n"
" flux-kvs copy-tokvs key file\n"
" flux-kvs copy-fromkvs key file\n"
" flux-kvs copy srckey dstkey\n"
Expand All @@ -93,6 +96,9 @@ void usage (void)
" flux-kvs dropcache-all\n"
" flux-kvs get-treeobj key\n"
" flux-kvs put-treeobj key=treeobj\n"
" flux-kvs getat treeobj key\n"
" flux-kvs dirat [-r] treeobj [key]\n"
" flux-kvs readlinkat treeobj key\n"
);
exit (1);
}
Expand Down Expand Up @@ -166,6 +172,12 @@ int main (int argc, char *argv[])
cmd_get_treeobj (h, argc - optind, argv + optind);
else if (!strcmp (cmd, "put-treeobj"))
cmd_put_treeobj (h, argc - optind, argv + optind);
else if (!strcmp (cmd, "getat"))
cmd_getat (h, argc - optind, argv + optind);
else if (!strcmp (cmd, "dirat"))
cmd_dirat (h, argc - optind, argv + optind);
else if (!strcmp (cmd, "readlinkat"))
cmd_readlinkat (h, argc - optind, argv + optind);
else
usage ();

Expand Down Expand Up @@ -517,42 +529,41 @@ static void dump_kvs_val (const char *key, const char *json_str)
Jput (o);
}

static void dump_kvs_dir (flux_t h, bool ropt, const char *path)
static void dump_kvs_dir (kvsdir_t *dir, bool ropt)
{
kvsdir_t *dir;
kvsitr_t *itr;
const char *name;
char *key;

if (kvs_get_dir (h, &dir, "%s", path) < 0)
log_err_exit ("%s", path);

itr = kvsitr_create (dir);
while ((name = kvsitr_next (itr))) {
key = kvsdir_key_at (dir, name);
if (kvsdir_issymlink (dir, name)) {
char *link;
if (kvs_get_symlink (h, key, &link) < 0)
if (kvsdir_get_symlink (dir, name, &link) < 0)
log_err_exit ("%s", key);
printf ("%s -> %s\n", key, link);
free (link);

} else if (kvsdir_isdir (dir, name)) {
if (ropt)
dump_kvs_dir (h, ropt, key);
else
if (ropt) {
kvsdir_t *ndir;
if (kvsdir_get_dir (dir, &ndir, "%s", name) < 0)
log_err_exit ("%s", key);
dump_kvs_dir (ndir, ropt);
kvsdir_destroy (ndir);
} else
printf ("%s.\n", key);
} else {
char *json_str;
if (kvs_get (h, key, &json_str) < 0)
if (kvsdir_get (dir, name, &json_str) < 0)
log_err_exit ("%s", key);
dump_kvs_val (key, json_str);
free (json_str);
}
free (key);
}
kvsitr_destroy (itr);
kvsdir_destroy (dir);
}

void cmd_watch_dir (flux_t h, int argc, char **argv)
Expand All @@ -561,12 +572,18 @@ void cmd_watch_dir (flux_t h, int argc, char **argv)
char *key;
kvsdir_t *dir = NULL;
int rc;
int count = -1;

if (argc > 0 && !strcmp (argv[0], "-r")) {
ropt = true;
argc--;
argv++;
}
if (argc == 2) {
count = strtoul (argv[0], NULL, 10);
argc--;
argv++;
}
if (argc != 1)
log_msg_exit ("watchdir: specify one directory");
key = argv[0];
Expand All @@ -579,30 +596,63 @@ void cmd_watch_dir (flux_t h, int argc, char **argv)
kvsdir_destroy (dir);
dir = NULL;
} else {
dump_kvs_dir (h, ropt, key);
dump_kvs_dir (dir, ropt);
printf ("======================\n");
fflush (stdout);
}
if (--count == 0)
goto done;
rc = kvs_watch_once_dir (h, &dir, "%s", key);
}
log_err_exit ("%s", key);

done:
kvsdir_destroy (dir);
}

void cmd_dir (flux_t h, int argc, char **argv)
{
bool ropt = false;
char *key;
kvsdir_t *dir;

if (argc > 0 && !strcmp (argv[0], "-r")) {
ropt = true;
argc--;
argv++;
}
if (argc == 0)
dump_kvs_dir (h, ropt, ".");
key = ".";
else if (argc == 1)
dump_kvs_dir (h, ropt, argv[0]);
key = argv[0];
else
log_msg_exit ("dir: specify zero or one directory");
if (kvs_get_dir (h, &dir, "%s", key) < 0)
log_err_exit ("%s", key);
dump_kvs_dir (dir, ropt);
kvsdir_destroy (dir);
}

void cmd_dirat (flux_t h, int argc, char **argv)
{
bool ropt = false;
char *key;
kvsdir_t *dir;

if (argc > 0 && !strcmp (argv[0], "-r")) {
ropt = true;
argc--;
argv++;
}
if (argc == 1)
key = ".";
else if (argc == 2)
key = argv[1];
else
log_msg_exit ("dir: specify treeobj and zero or one directory");
if (kvs_get_dirat (h, argv[0], key, &dir) < 0)
log_err_exit ("%s", key);
dump_kvs_dir (dir, ropt);
kvsdir_destroy (dir);
}

void cmd_dirsize (flux_t h, int argc, char **argv)
Expand Down Expand Up @@ -647,6 +697,17 @@ void cmd_get_treeobj (flux_t h, int argc, char **argv)
free (treeobj);
}

void cmd_getat (flux_t h, int argc, char **argv)
{
char *val = NULL;
if (argc != 2)
log_msg_exit ("getat: specify treeobj and key");
if (kvs_getat (h, argv[0], argv[1], &val) < 0)
log_err_exit ("kvs_getat %s %s", argv[0], argv[1]);
printf ("%s\n", val);
free (val);
}

void cmd_put_treeobj (flux_t h, int argc, char **argv)
{
if (argc != 1)
Expand All @@ -663,6 +724,22 @@ void cmd_put_treeobj (flux_t h, int argc, char **argv)

}

void cmd_readlinkat (flux_t h, int argc, char **argv)
{
int i;
char *target;

if (argc < 2)
log_msg_exit ("readlink: specify treeobj and one or more keys");
for (i = 1; i < argc; i++) {
if (kvs_get_symlinkat (h, argv[0], argv[i], &target) < 0)
log_err_exit ("%s", argv[i]);
else
printf ("%s\n", target);
free (target);
}
}

/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/
2 changes: 1 addition & 1 deletion src/common/libpmi/single.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ int pmi_single_get_rank (struct pmi_single *pmi, int *rank)

int pmi_single_get_appnum (struct pmi_single *pmi, int *appnum)
{
*appnum = -1;
*appnum = getpid ();
return PMI_SUCCESS;
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/libpmi/test/single.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ int main (int argc, char *argv[])
"pmi_single_get_rank works, rank == 0");
appnum = -2;
rc = pmi_single_get_appnum (pmi, &appnum);
ok (rc == PMI_SUCCESS && appnum == -1,
"pmi_single_get_appnum works, appnum == -1");
ok (rc == PMI_SUCCESS && appnum >= 0,
"pmi_single_get_appnum works, appnum positive number");
size = -1;
rc = pmi_single_get_universe_size (pmi, &size);
ok (rc == PMI_SUCCESS && size == 1,
Expand Down
Loading

0 comments on commit a8793ce

Please sign in to comment.