From 3ca9aeb4072c5804fc08279c0a29bd55cfaf2ed0 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Thu, 15 Sep 2016 00:10:46 +0000 Subject: [PATCH] modules/kvs: add kvs_getat() Add a new function kvs_getat() which is like kvs_get() but has a new "treeobj" argument that specifies the specific root (or other directory) "snapshot" that the key will be looked up in. Partially fixes #64 --- src/modules/kvs/kvs.h | 5 +++++ src/modules/kvs/libkvs.c | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/modules/kvs/kvs.h b/src/modules/kvs/kvs.h index 7a1cb62d80f7..6009fb30ef22 100644 --- a/src/modules/kvs/kvs.h +++ b/src/modules/kvs/kvs.h @@ -47,6 +47,11 @@ int kvs_get_symlink (flux_t h, const char *key, char **valp); */ int kvs_get_treeobj (flux_t h, const char *key, char **treeobj); +/* Like kvs_get() but lookup is relative to 'treeobj'. + */ +int kvs_getat (flux_t h, const char *treeobj, + const char *key, char **json_str); + /* kvs_watch* is like kvs_get* except the registered callback is called * to set the value. It will be called immediately to set the initial * value and again each time the value changes. diff --git a/src/modules/kvs/libkvs.c b/src/modules/kvs/libkvs.c index a66f446a361b..ee05d5910d16 100644 --- a/src/modules/kvs/libkvs.c +++ b/src/modules/kvs/libkvs.c @@ -380,6 +380,29 @@ int kvs_get (flux_t h, const char *key, char **val) return 0; } +int kvs_getat (flux_t h, const char *treeobj, + const char *key, char **val) +{ + JSON v = NULL; + JSON dirent = NULL; + + if (!treeobj || !key || !(dirent = Jfromstr (treeobj)) + || dirent_validate (dirent) < 0) { + errno = EINVAL; + goto error; + } + if (getobj (h, dirent, key, 0, &v) < 0) + goto error; + if (val) + *val = xstrdup (Jtostr (v)); + Jput (dirent); + return 0; +error: + Jput (v); + Jput (dirent); + return -1; +} + /* deprecated */ int kvs_get_obj (flux_t h, const char *key, JSON *val) {