Skip to content

Commit

Permalink
modules/kvs: deprecate json-c interfaces
Browse files Browse the repository at this point in the history
Rename the kvs setter callbacks to RFC 7 approved names, and add
pointer to typedef.

Internally in libkvs, avoid some unnecessary type casting for KVS
watch handling with different types of setters by making the generic
callback pointer a (void *) instead of kvs_set_obj_f.

Define kvsdir_t and kvsitr_t as structs rather than pointers to
structs per RFC 7.

Rename the KVS functions that take (json_object *) to have a _obj
suffix and set the deprecated attribtue on them.
  kvs_get() -> kvs_get_obj()
  kvs_put() -> kvs_put_obj()
  kvs_watch() -> kvs_watch_obj()
  kvs_watch_once() -> kvs_watch_once_obj()
  kvsdir_get() -> kvsdir_get_obj()
  kvsdir_put() -> kvsdir_put_obj()

Add back versions of the above that take a json string argument under
the original names

Update users.
  • Loading branch information
garlick committed Aug 22, 2015
1 parent dd8f23c commit 92169c5
Show file tree
Hide file tree
Showing 19 changed files with 358 additions and 226 deletions.
4 changes: 2 additions & 2 deletions src/bindings/lua/flux-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ static int l_flux_new (lua_State *L)
static int l_flux_kvsdir_new (lua_State *L)
{
const char *path = ".";
kvsdir_t dir;
kvsdir_t *dir;
flux_t f = lua_get_flux (L, 1);

if (lua_isstring (L, 2)) {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ static int l_kvswatcher_add (lua_State *L)
assert (lua_isfunction (L, -1));

kw = l_flux_ref_create (L, f, 2, "kvswatcher");
kvs_watch (f, key, l_kvswatcher, (void *) kw);
kvs_watch_obj (f, key, l_kvswatcher, (void *) kw);

/*
* Return kvswatcher object to caller
Expand Down
48 changes: 24 additions & 24 deletions src/bindings/lua/kvs-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@

static int l_kvsdir_commit (lua_State *L);

static kvsdir_t lua_get_kvsdir (lua_State *L, int index)
static kvsdir_t *lua_get_kvsdir (lua_State *L, int index)
{
kvsdir_t *dirp = luaL_checkudata (L, index, "CMB.kvsdir");
kvsdir_t **dirp = luaL_checkudata (L, index, "CMB.kvsdir");
return (*dirp);
}

Expand All @@ -55,24 +55,24 @@ int l_kvsdir_instantiate (lua_State *L)

static int l_kvsdir_destroy (lua_State *L)
{
kvsdir_t d = lua_get_kvsdir (L, -1);
kvsdir_t *d = lua_get_kvsdir (L, -1);
if (d)
kvsdir_destroy (d);
return (0);
}

int l_push_kvsdir (lua_State *L, kvsdir_t dir)
int l_push_kvsdir (lua_State *L, kvsdir_t *dir)
{
kvsdir_t *new = lua_newuserdata (L, sizeof (*new));
kvsdir_t **new = lua_newuserdata (L, sizeof (*new));
*new = dir;
return l_kvsdir_instantiate (L);
}

static int l_kvsdir_kvsdir_new (lua_State *L)
{
const char *key;
kvsdir_t new;
kvsdir_t d;
kvsdir_t *new;
kvsdir_t *d;

d = lua_get_kvsdir (L, 1);
key = luaL_checkstring (L, 2);
Expand All @@ -85,22 +85,22 @@ static int l_kvsdir_kvsdir_new (lua_State *L)

static int l_kvsdir_tostring (lua_State *L)
{
kvsdir_t d = lua_get_kvsdir (L, 1);
kvsdir_t *d = lua_get_kvsdir (L, 1);
lua_pushstring (L, kvsdir_key (d));
return (1);
}

static int l_kvsdir_newindex (lua_State *L)
{
int rc;
kvsdir_t d = lua_get_kvsdir (L, 1);
kvsdir_t *d = lua_get_kvsdir (L, 1);
const char *key = lua_tostring (L, 2);

/*
* Process value;
*/
if (lua_isnil (L, 3))
rc = kvsdir_put (d, key, NULL);
rc = kvsdir_put_obj (d, key, NULL);
else if (lua_isnumber (L, 3)) {
double val = lua_tonumber (L, 3);
if (floor (val) == val)
Expand All @@ -115,7 +115,7 @@ static int l_kvsdir_newindex (lua_State *L)
else if (lua_istable (L, 3)) {
json_object *o;
lua_value_to_json (L, 3, &o);
rc = kvsdir_put (d, key, o);
rc = kvsdir_put_obj (d, key, o);
json_object_put (o);
}
else {
Expand All @@ -129,24 +129,24 @@ static int l_kvsdir_newindex (lua_State *L)
return (0);
}

static kvsitr_t lua_to_kvsitr (lua_State *L, int index)
static kvsitr_t *lua_to_kvsitr (lua_State *L, int index)
{
kvsitr_t *iptr = luaL_checkudata (L, index, "CMB.kvsitr");
kvsitr_t **iptr = luaL_checkudata (L, index, "CMB.kvsitr");
return (*iptr);
}

/* gc metamethod for iterator */
static int l_kvsitr_destroy (lua_State *L)
{
kvsitr_t i = lua_to_kvsitr (L, 1);
kvsitr_t *i = lua_to_kvsitr (L, 1);
kvsitr_destroy (i);
return (0);
}

static int l_kvsdir_iterator (lua_State *L)
{
const char *key;
kvsitr_t i;
kvsitr_t *i;

/* Get kvsitr from upvalue index on stack:
*/
Expand All @@ -165,8 +165,8 @@ static int l_kvsdir_iterator (lua_State *L)

static int l_kvsdir_next (lua_State *L)
{
kvsdir_t d = lua_get_kvsdir (L, 1);
kvsitr_t *iptr;
kvsdir_t *d = lua_get_kvsdir (L, 1);
kvsitr_t **iptr;

lua_pop (L, 1);

Expand All @@ -188,7 +188,7 @@ static int l_kvsdir_next (lua_State *L)

static int l_kvsdir_commit (lua_State *L)
{
kvsdir_t d = lua_get_kvsdir (L, 1);
kvsdir_t *d = lua_get_kvsdir (L, 1);
if (lua_isnoneornil (L, 2)) {
if (kvs_commit (kvsdir_handle (d)) < 0)
return lua_pusherror (L, "kvs_commit: %s", strerror (errno));
Expand All @@ -203,23 +203,23 @@ static int l_kvsdir_watch (lua_State *L)
void *h;
char *key;
json_object *o;
kvsdir_t dir;
kvsdir_t *dir;

dir = lua_get_kvsdir (L, 1);
h = kvsdir_handle (dir);
key = kvsdir_key_at (dir, lua_tostring (L, 2));

if (lua_isnoneornil (L, 3)) {
/* Need to fetch initial value */
if (((rc = kvs_get (h, key, &o)) < 0) && (errno != ENOENT))
if (((rc = kvs_get_obj (h, key, &o)) < 0) && (errno != ENOENT))
goto err;
}
else {
/* Otherwise, the alue at top of stack is initial json_object */
lua_value_to_json (L, -1, &o);
}

rc = kvs_watch_once (h, key, &o);
rc = kvs_watch_once_obj (h, key, &o);
err:
free (key);
if (rc < 0)
Expand All @@ -233,7 +233,7 @@ static int l_kvsdir_watch (lua_State *L)
static int l_kvsdir_watch_dir (lua_State *L)
{
flux_t h;
kvsdir_t dir;
kvsdir_t *dir;

dir = lua_get_kvsdir (L, 1);
h = kvsdir_handle (dir);
Expand All @@ -245,7 +245,7 @@ static int l_kvsdir_index (lua_State *L)
{
int rc;
flux_t f;
kvsdir_t d;
kvsdir_t *d;
const char *key = lua_tostring (L, 2);
char *fullkey = NULL;
json_object *o = NULL;
Expand All @@ -261,7 +261,7 @@ static int l_kvsdir_index (lua_State *L)
f = kvsdir_handle (d);
fullkey = kvsdir_key_at (d, key);

if (kvs_get (f, fullkey, &o) == 0)
if (kvs_get_obj (f, fullkey, &o) == 0)
rc = json_object_to_lua (L, o);
else if (errno == EISDIR)
rc = l_kvsdir_kvsdir_new (L);
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/lua/kvs-lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <lauxlib.h>

int luaopen_kvs (lua_State *L);
int l_push_kvsdir (lua_State *L, kvsdir_t dir);
int l_push_kvsdir (lua_State *L, kvsdir_t *dir);

#endif /* !HAVE_KVS_LUA_H */

12 changes: 6 additions & 6 deletions src/bindings/python/flux/kvs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class KVSWrapper(Wrapper):

def get_key_direct(flux_handle, key):
j = json_c.Jobj()
_raw.get(flux_handle, key, j.get_as_dptr())
_raw.get_obj(flux_handle, key, j.get_as_dptr())
return json.loads(j.as_str())


Expand Down Expand Up @@ -61,7 +61,7 @@ def get(flux_handle, key):

def put(flux_handle, key, value):
j = json_c.Jobj(json.dumps(value))
_raw.put(flux_handle, key, j.get())
_raw.put_obj(flux_handle, key, j.get())


def commit(flux_handle):
Expand Down Expand Up @@ -89,13 +89,13 @@ def __init__(self, flux_handle=None, path='.', handle=None):
raise ValueError(
"flux_handle must be a valid Flux object or handle must be a valid kvsdir cdata pointer")
if handle is None:
d = ffi.new("kvsdir_t [1]")
d = ffi.new("kvsdir_t *[1]")
_raw.kvs_get_dir(flux_handle, d, path)
handle = d[0]

super(self.__class__, self).__init__(ffi, lib,
handle=handle,
match=ffi.typeof('kvsdir_t'),
match=ffi.typeof('kvsdir_t *'),
prefixes=[
'kvsdir_',
], )
Expand Down Expand Up @@ -131,7 +131,7 @@ def __getitem__(self, key):
def __setitem__(self, key, value):
# Turn it into json
j = json_c.Jobj(json.dumps(value))
self.pimpl.put(key, j.get())
self.pimpl.put_obj(key, j.get())

def __delitem__(self, key):
self.pimpl.unlink(key)
Expand Down Expand Up @@ -253,7 +253,7 @@ def walk(directory, topdown=False, flux_handle=None):
yield x


@ffi.callback('KVSSetF')
@ffi.callback('kvs_set_obj_f')
def KVSWatchWrapper(key, value, arg, errnum):
j = Jobj(handle=value)
(cb, real_arg) = ffi.from_handle(arg)
Expand Down
26 changes: 13 additions & 13 deletions src/cmd/flux-kvs.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void cmd_type (flux_t h, int argc, char **argv)
if (argc == 0)
msg_exit ("get-type: specify one or more keys");
for (i = 0; i < argc; i++) {
if (kvs_get (h, argv[i], &o) < 0)
if (kvs_get_obj (h, argv[i], &o) < 0)
err_exit ("%s", argv[i]);
const char *type = "unknown";
switch (json_object_get_type (o)) {
Expand Down Expand Up @@ -199,7 +199,7 @@ void cmd_get (flux_t h, int argc, char **argv)
if (argc == 0)
msg_exit ("get: specify one or more keys");
for (i = 0; i < argc; i++) {
if (kvs_get (h, argv[i], &o) < 0)
if (kvs_get_obj (h, argv[i], &o) < 0)
err_exit ("%s", argv[i]);
switch (json_object_get_type (o)) {
case json_type_null:
Expand Down Expand Up @@ -241,7 +241,7 @@ void cmd_put (flux_t h, int argc, char **argv)
msg_exit ("put: you must specify a value as key=value");
*val++ = '\0';
if ((o = Jfromstr (val))) {
if (kvs_put (h, key, o) < 0)
if (kvs_put_obj (h, key, o) < 0)
err_exit ("%s", key);
} else {
if (kvs_put_string (h, key, val) < 0)
Expand Down Expand Up @@ -313,9 +313,9 @@ void cmd_mkdir (flux_t h, int argc, char **argv)
bool key_exists (flux_t h, const char *key)
{
JSON o = NULL;
kvsdir_t dir = NULL;
kvsdir_t *dir = NULL;

if (kvs_get (h, key, &o) == 0) {
if (kvs_get_obj (h, key, &o) == 0) {
Jput (o);
return true;
}
Expand Down Expand Up @@ -366,13 +366,13 @@ void cmd_watch (flux_t h, int argc, char **argv)
if (argc != 1)
msg_exit ("watch: specify one key");
key = argv[0];
if (kvs_get (h, key, &o) < 0 && errno != ENOENT)
if (kvs_get_obj (h, key, &o) < 0 && errno != ENOENT)
err_exit ("%s", key);
do {
printf ("%s\n", o ? Jtostr (o) : "NULL");
Jput (o);
o = NULL;
if (kvs_watch_once (h, argv[0], &o) < 0 && errno != ENOENT)
if (kvs_watch_once_obj (h, argv[0], &o) < 0 && errno != ENOENT)
err_exit ("%s", argv[0]);
} while (true);
}
Expand Down Expand Up @@ -418,7 +418,7 @@ void cmd_copy_tokvs (flux_t h, int argc, char **argv)
}
o = Jnew ();
util_json_object_add_data (o, "data", buf, len);
if (kvs_put (h, key, o) < 0)
if (kvs_put_obj (h, key, o) < 0)
err_exit ("%s", key);
if (kvs_commit (h) < 0)
err_exit ("kvs_commit");
Expand All @@ -437,7 +437,7 @@ void cmd_copy_fromkvs (flux_t h, int argc, char **argv)
msg_exit ("copy-fromkvs: specify key and filename");
key = argv[0];
file = argv[1];
if (kvs_get (h, key, &o) < 0)
if (kvs_get_obj (h, key, &o) < 0)
err_exit ("%s", key);
if (util_json_object_get_data (o, "data", &buf, &len) < 0)
err_exit ("%s: decode error", key);
Expand Down Expand Up @@ -486,8 +486,8 @@ static void dump_kvs_val (const char *key, JSON o)

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

Expand All @@ -511,7 +511,7 @@ static void dump_kvs_dir (flux_t h, bool ropt, const char *path)
printf ("%s.\n", key);
} else {
JSON o;
if (kvs_get (h, key, &o) < 0)
if (kvs_get_obj (h, key, &o) < 0)
err_exit ("%s", key);
dump_kvs_val (key, o);
Jput (o);
Expand All @@ -526,7 +526,7 @@ void cmd_watch_dir (flux_t h, int argc, char **argv)
{
bool ropt = false;
char *key;
kvsdir_t dir = NULL;
kvsdir_t *dir = NULL;
int rc;

if (argc > 0 && !strcmp (argv[0], "-r")) {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/flux-up.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ static ns_t *ns_fromkvs (flux_t h)
JSON o = NULL;
ns_t *ns = NULL;

if (kvs_get (h, "conf.live.status", &o) < 0)
if (kvs_get_obj (h, "conf.live.status", &o) < 0)
goto done;
ns = ns_fromjson (o);
done:
Expand Down
Loading

0 comments on commit 92169c5

Please sign in to comment.