Skip to content

Commit

Permalink
* use the same json token parsing function everywhere
Browse files Browse the repository at this point in the history
* cleanup now un-used functions in jsonc_missing
* minor function naming cleanup in mod.c
* add missing prototype to mod.h
  • Loading branch information
aaronhurt committed Nov 3, 2014
1 parent a744d91 commit 4e03556
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 47 deletions.
3 changes: 0 additions & 3 deletions src/modules/rlm_couchbase/config.h.in
Expand Up @@ -18,9 +18,6 @@
/* Define to 1 if you have the `json_tokener_get_error' function. */
#undef HAVE_JSON_TOKENER_GET_ERROR

/* Define to 1 if you have the `json_tokener_parse_verbose' function. */
#undef HAVE_JSON_TOKENER_PARSE_VERBOSE

/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT

Expand Down
1 change: 0 additions & 1 deletion src/modules/rlm_couchbase/configure
Expand Up @@ -3493,7 +3493,6 @@ $as_echo "$as_me: WARNING: json-c libraries not found. Use --with-jsonc-lib-dir=
json_object_get_string_len \
json_object_object_get_ex \
json_object_new_int64 \
json_tokener_parse_verbose \
json_tokener_error_desc \
json_tokener_get_error
Expand Down
1 change: 0 additions & 1 deletion src/modules/rlm_couchbase/configure.ac
Expand Up @@ -107,7 +107,6 @@ if test x$with_[]modname != xno; then
json_object_get_string_len \
json_object_object_get_ex \
json_object_new_int64 \
json_tokener_parse_verbose \
json_tokener_error_desc \
json_tokener_get_error
)
Expand Down
32 changes: 25 additions & 7 deletions src/modules/rlm_couchbase/couchbase.c
Expand Up @@ -96,9 +96,9 @@ void couchbase_get_callback(lcb_t instance, const void *cookie, lcb_error_t erro
/* debug */
DEBUG("rlm_couchbase: (get_callback) got %zu bytes", nbytes);
/* build json object */
c->jobj = json_tokener_parse_verbose(bytes, &c->jerr);
c->jobj = json_tokener_parse_ex(c->jtok, bytes, nbytes);
/* switch on current error status */
switch (c->jerr) {
switch ((c->jerr = json_tokener_get_error(c->jtok))) {
case json_tokener_continue:
/* do nothing */
break;
Expand Down Expand Up @@ -312,9 +312,12 @@ lcb_error_t couchbase_set_key(lcb_t instance, const char *key, const char *docum
*/
lcb_error_t couchbase_get_key(lcb_t instance, const void *cookie, const char *key)
{
lcb_error_t error; /* couchbase command return */
lcb_get_cmd_t cmd; /* get command struct */
const lcb_get_cmd_t *commands[1]; /* get commands array */
cookie_u cu; /* union of const and non const pointers */
cu.cdata = cookie; /* set const union member to cookie passed from couchbase */
cookie_t *c = (cookie_t *) cu.data; /* set our cookie struct using non-const member */
lcb_error_t error; /* couchbase command return */
lcb_get_cmd_t cmd; /* get command struct */
const lcb_get_cmd_t *commands[1]; /* get commands array */

/* init commands */
commands[0] = &cmd;
Expand All @@ -324,12 +327,18 @@ lcb_error_t couchbase_get_key(lcb_t instance, const void *cookie, const char *ke
cmd.v.v0.key = key;
cmd.v.v0.nkey = strlen(cmd.v.v0.key);

/* allocate token */
c->jtok = json_tokener_new();

/* get document */
if ((error = lcb_get(instance, cookie, 1, commands)) == LCB_SUCCESS) {
if ((error = lcb_get(instance, c, 1, commands)) == LCB_SUCCESS) {
/* enter event loop on success */
lcb_wait(instance);
}

/* free token */
json_tokener_free(c->jtok);

/* return error */
return error;
}
Expand All @@ -346,6 +355,9 @@ lcb_error_t couchbase_get_key(lcb_t instance, const void *cookie, const char *ke
*/
lcb_error_t couchbase_query_view(lcb_t instance, const void *cookie, const char *path, const char *post)
{
cookie_u cu; /* union of const and non const pointers */
cu.cdata = cookie; /* set const union member to cookie passed from couchbase */
cookie_t *c = (cookie_t *) cu.data; /* set our cookie struct using non-const member */
lcb_error_t error; /* couchbase command return */
lcb_http_cmd_t cmd; /* http command struct */
const lcb_http_cmd_t *commands; /* http commands array */
Expand All @@ -362,12 +374,18 @@ lcb_error_t couchbase_query_view(lcb_t instance, const void *cookie, const char
cmd.v.v0.chunked = 1;
cmd.v.v0.content_type = "application/json";

/* allocate token */
c->jtok = json_tokener_new();

/* query the view */
if ((error = lcb_make_http_request(instance, cookie, LCB_HTTP_TYPE_VIEW, commands, NULL)) == LCB_SUCCESS) {
if ((error = lcb_make_http_request(instance, c, LCB_HTTP_TYPE_VIEW, commands, NULL)) == LCB_SUCCESS) {
/* enter event loop on success */
lcb_wait(instance);
}

/* free token */
json_tokener_free(c->jtok);

/* return error */
return error;
}
21 changes: 0 additions & 21 deletions src/modules/rlm_couchbase/jsonc_missing.c
Expand Up @@ -66,27 +66,6 @@ int json_object_object_get_ex(struct json_object *jso, const char *key, struct j
}
#endif

#ifndef HAVE_JSON_TOKENER_PARSE_VERBOSE
struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokener_error *error) {
struct json_tokener* tok;
struct json_object* obj;

tok = json_tokener_new();
if (!tok)
return NULL;
obj = json_tokener_parse_ex(tok, str, -1);
*error = tok->err;
if(tok->err != json_tokener_success) {
if (obj != NULL)
json_object_put(obj);
obj = NULL;
}

json_tokener_free(tok);
return obj;
}
#endif

#ifndef HAVE_JSON_TOKENER_GET_ERROR
enum json_tokener_error json_tokener_get_error(json_tokener *tok) {
return tok->err;
Expand Down
4 changes: 0 additions & 4 deletions src/modules/rlm_couchbase/jsonc_missing.h
Expand Up @@ -49,10 +49,6 @@ RCSIDH(jsonc_missing_h, "$Id$");
int json_object_object_get_ex(struct json_object* jso, const char *key, struct json_object **value);
#endif

#ifndef HAVE_JSON_TOKENER_PARSE_VERBOSE
struct json_object* json_tokener_parse_verbose(const char *str, enum json_tokener_error *error);
#endif

#ifndef HAVE_JSON_TOKENER_ERROR_DESC
const char *json_tokener_error_desc(enum json_tokener_error jerr);
#endif
Expand Down
12 changes: 3 additions & 9 deletions src/modules/rlm_couchbase/mod.c
Expand Up @@ -522,7 +522,7 @@ int mod_ensure_start_timestamp(json_object *json, VALUE_PAIR *vps)
* @param docid Document id.
* @return Returns 0 on success, -1 on error.
*/
int _mod_client_map_section(CONF_SECTION *client, CONF_SECTION const *map,
int mod_client_map_section(CONF_SECTION *client, CONF_SECTION const *map,
json_object *json, char const *docid)
{
CONF_ITEM const *ci;
Expand All @@ -545,7 +545,7 @@ int _mod_client_map_section(CONF_SECTION *client, CONF_SECTION const *map,

cf_section_add(client, cc);

if (_mod_client_map_section(cc, cs, json, docid) != 0) {
if (mod_client_map_section(cc, cs, json, docid) != 0) {
return -1;
}
/* continue on to the next item */
Expand Down Expand Up @@ -639,15 +639,9 @@ int mod_load_client_documents(rlm_couchbase_t *inst, CONF_SECTION *cs)
/* init cookie error status */
cookie->jerr = json_tokener_success;

/* setup cookie tokener */
cookie->jtok = json_tokener_new();

/* query view for document */
cb_error = couchbase_query_view(cb_inst, cookie, vpath, NULL);

/* free json token */
json_tokener_free(cookie->jtok);

/* check error */
if (cb_error != LCB_SUCCESS || cookie->jerr != json_tokener_success) {
/* log error */
Expand Down Expand Up @@ -766,7 +760,7 @@ int mod_load_client_documents(rlm_couchbase_t *inst, CONF_SECTION *cs)
/* allocate conf section */
client = cf_section_alloc(NULL, "client", docid);

if (_mod_client_map_section(client, cs, cookie->jobj, docid) != 0) {
if (mod_client_map_section(client, cs, cookie->jobj, docid) != 0) {
/* free config setion */
talloc_free(client);
/* set return */
Expand Down
6 changes: 5 additions & 1 deletion src/modules/rlm_couchbase/mod.h
Expand Up @@ -80,12 +80,16 @@ int mod_build_attribute_element_map(CONF_SECTION *conf, void *instance);

int mod_attribute_to_element(const char *name, json_object *map, void *buf);

void *mod_json_object_to_value_pairs(json_object *json, const char *section, REQUEST *request);
void *mod_json_object_to_value_pairs(json_object *json, const char *section,
REQUEST *request);

json_object *mod_value_pair_to_json_object(REQUEST *request, VALUE_PAIR *vp);

int mod_ensure_start_timestamp(json_object *json, VALUE_PAIR *vps);

int mod_client_map_section(CONF_SECTION *client, CONF_SECTION const *map,
json_object *json, char const *docid);

int mod_load_client_documents(rlm_couchbase_t *inst, CONF_SECTION *cs);

#endif /* _mod_h_ */

0 comments on commit 4e03556

Please sign in to comment.