diff options
-rw-r--r-- | src/persistence-binding.c | 106 |
1 files changed, 53 insertions, 53 deletions
diff --git a/src/persistence-binding.c b/src/persistence-binding.c index def5184..3efb1f0 100644 --- a/src/persistence-binding.c +++ b/src/persistence-binding.c @@ -27,7 +27,7 @@ #include <json-c/json.h> -#define AFB_BINDING_VERSION 2 +#define AFB_BINDING_VERSION 3 #include <afb/afb-binding.h> #if !defined(TO_STRING_FLAGS) @@ -71,51 +71,51 @@ static int xdb_open(const char *path) ret = db_create(&database, NULL, 0); if (ret != 0) { - AFB_ERROR("Failed to create database: %s.", db_strerror(ret)); + AFB_API_ERROR(afbBindingRoot, "Failed to create database: %s.", db_strerror(ret)); return -1; } ret = database->open(database, NULL, path, NULL, DB_BTREE, DB_CREATE, 0600); if (ret != 0) { - AFB_ERROR("Failed to open the '%s' database: %s.", path, db_strerror(ret)); + AFB_API_ERROR(afbBindingRoot, "Failed to open the '%s' database: %s.", path, db_strerror(ret)); database->close(database, 0); return -1; } return 0; } -static void xdb_put(struct afb_req req, DBT *key, DBT *data, int replace) +static void xdb_put(afb_req_t req, DBT *key, DBT *data, int replace) { int ret; ret = database->put(database, NULL, key, data, replace ? 0 : DB_NOOVERWRITE); if (ret == 0) - afb_req_success(req, NULL, NULL); + afb_req_reply(req, NULL, NULL, NULL); else { - AFB_ERROR("can't %s key %s with %s", replace ? "replace" : "insert", DATA_STR(*key), DATA_STR(*data)); - afb_req_fail_f(req, "failed", "%s", db_strerror(ret)); + AFB_API_ERROR(afbBindingRoot, "can't %s key %s with %s", replace ? "replace" : "insert", DATA_STR(*key), DATA_STR(*data)); + afb_req_reply_f(req, NULL, "failed", "%s", db_strerror(ret)); } } -static void xdb_delete(struct afb_req req, DBT *key) +static void xdb_delete(afb_req_t req, DBT *key) { int ret; ret = database->del(database, NULL, key, 0); if (ret == 0) - afb_req_success_f(req, NULL, NULL); + afb_req_reply_f(req, NULL, NULL, NULL); else { - AFB_ERROR("can't delete key %s", DATA_STR(*key)); - afb_req_fail_f(req, "failed", "%s", db_strerror(ret)); + AFB_API_ERROR(afbBindingRoot, "can't delete key %s", DATA_STR(*key)); + afb_req_reply_f(req, NULL, "failed", "%s", db_strerror(ret)); } free(DATA_PTR(key)); } -static void verb_read(struct afb_req req) +static void verb_read(afb_req_t req) { DATA key; DATA data; @@ -130,7 +130,7 @@ static void verb_read(struct afb_req req) if (get_key(req, &key)) return; - AFB_DEBUG("read: key=%s", DATA_STR(key)); + AFB_API_DEBUG(afbBindingRoot, "read: key=%s", DATA_STR(key)); memset(&data, 0, sizeof data); data.data = value; @@ -144,10 +144,10 @@ static void verb_read(struct afb_req req) val = json_tokener_parse(DATA_STR(data)); json_object_object_add(result, "value", val ? val : json_object_new_string(DATA_STR(data))); - afb_req_success_f(req, result, "db success: read %s=%s.", DATA_STR(key), DATA_STR(data)); + afb_req_reply_f(req, result, NULL, "db success: read %s=%s.", DATA_STR(key), DATA_STR(data)); } else - afb_req_fail_f(req, "Failed to read datas.", "db fail: read %s - %s", DATA_STR(key), db_strerror(ret)); + afb_req_reply_f(req, NULL, "Failed to read datas.", "db fail: read %s - %s", DATA_STR(key), db_strerror(ret)); free(DATA_PTR(key)); } @@ -177,7 +177,7 @@ static GDBM_FILE database; static void onfatal(const char *text) { - AFB_ERROR("fatal gdbm message: %s", text); + AFB_API_ERROR(afbBindingRoot, "fatal gdbm message: %s", text); } static int xdb_open(const char *path) @@ -185,7 +185,7 @@ static int xdb_open(const char *path) database = gdbm_open(path, 512, GDBM_WRCREAT|GDBM_SYNC, 0600, onfatal); if (!database) { - AFB_ERROR("Fail to open/create database: %s%s%s", + AFB_API_ERROR(afbBindingRoot, "Fail to open/create database: %s%s%s", gdbm_strerror(gdbm_errno), IFSYS(", ", ""), IFSYS(strerror(errno), "")); @@ -195,45 +195,45 @@ static int xdb_open(const char *path) return 0; } -static void xdb_put(struct afb_req req, datum *key, datum *data, int replace) +static void xdb_put(afb_req_t req, datum *key, datum *data, int replace) { int ret; ret = gdbm_store(database, *key, *data, replace ? GDBM_REPLACE : GDBM_INSERT); if (ret == 0) - afb_req_success(req, NULL, NULL); + afb_req_reply(req, NULL, NULL, NULL); else { - AFB_ERROR("can't %s key %s with %s: %s%s%s", + AFB_API_ERROR(afbBindingRoot, "can't %s key %s with %s: %s%s%s", replace ? "replace" : "insert", DATA_STR(*key), DATA_STR(*data), gdbm_strerror(gdbm_errno), IFSYS(", ", ""), IFSYS(strerror(errno), "")); - afb_req_fail_f(req, "failed", "%s", ret > 0 ? "key already exists" : gdbm_strerror(gdbm_errno)); + afb_req_reply_f(req, NULL, "failed", "%s", ret > 0 ? "key already exists" : gdbm_strerror(gdbm_errno)); } } -static void xdb_delete(struct afb_req req, datum *key) +static void xdb_delete(afb_req_t req, datum *key) { int ret; ret = gdbm_delete(database, *key); if (ret == 0) - afb_req_success_f(req, NULL, NULL); + afb_req_reply_f(req, NULL, NULL, NULL); else { - AFB_ERROR("can't delete key %s: %s%s%s", + AFB_API_ERROR(afbBindingRoot, "can't delete key %s: %s%s%s", DATA_STR(*key), gdbm_strerror(gdbm_errno), IFSYS(", ", ""), IFSYS(strerror(errno), "")); - afb_req_fail_f(req, "failed", "%s", gdbm_strerror(gdbm_errno)); + afb_req_reply_f(req, NULL, "failed", "%s", gdbm_strerror(gdbm_errno)); } } -static void xdb_get(struct afb_req req, datum *key) +static void xdb_get(afb_req_t req, datum *key) { struct json_object* obj; datum result; @@ -243,17 +243,17 @@ static void xdb_get(struct afb_req req, datum *key) { obj = json_object_new_object(); json_object_object_add(obj, "value", json_tokener_parse(result.dptr)); - afb_req_success(req, obj, NULL); + afb_req_reply(req, obj, NULL, NULL); free(result.dptr); } else { - AFB_ERROR("can't get key %s: %s%s%s", + AFB_API_ERROR(afbBindingRoot, "can't get key %s: %s%s%s", DATA_STR(*key), gdbm_strerror(gdbm_errno), IFSYS(", ", ""), IFSYS(strerror(errno), "")); - afb_req_fail_f(req, "failed", "%s", gdbm_strerror(gdbm_errno)); + afb_req_reply_f(req, NULL, "failed", "%s", gdbm_strerror(gdbm_errno)); } } #endif @@ -295,7 +295,7 @@ static int get_database_path(char *buffer, size_t size) * @brief Initialize the binding. * @return Exit code, zero if success. */ -static int ll_database_binding_init() +static int ll_database_binding_init(afb_api_t api) { char path[PATH_MAX]; int ret; @@ -303,18 +303,18 @@ static int ll_database_binding_init() ret = get_database_path(path, sizeof path); if (ret < 0 || (int)ret >= (int)(sizeof path)) { - AFB_ERROR("Can't compute the database filename"); + AFB_API_ERROR(afbBindingRoot, "Can't compute the database filename"); return -1; } - AFB_INFO("opening database %s", path); + AFB_API_INFO(afbBindingRoot, "opening database %s", path); return xdb_open(path); } /** * Returns the database key for the 'req' */ -static int get_key(struct afb_req req, DATA *key) +static int get_key(afb_req_t req, DATA *key) { char *appid, *data; const char *jkey; @@ -328,14 +328,14 @@ static int get_key(struct afb_req req, DATA *key) args = afb_req_json(req); if (!json_object_object_get_ex(args, "key", &item)) { - afb_req_fail(req, "no-key", NULL); + afb_req_reply(req, NULL, "no-key", NULL); return -1; } if (!item || !(jkey = json_object_to_json_string_ext(item, JSON_C_TO_STRING_PLAIN)) || !(ljkey = strlen(jkey))) { - afb_req_fail(req, "bad-key", NULL); + afb_req_reply(req, NULL, "bad-key", NULL); return -1; } @@ -347,7 +347,7 @@ static int get_key(struct afb_req req, DATA *key) #endif if (!appid) { - afb_req_fail(req, "bad-context", NULL); + afb_req_reply(req, NULL, "bad-context", NULL); return -1; } @@ -358,7 +358,7 @@ static int get_key(struct afb_req req, DATA *key) if (!data) { free(appid); - afb_req_fail(req, "out-of-memory", NULL); + afb_req_reply(req, NULL, "out-of-memory", NULL); return -1; } data[lappid] = ':'; @@ -369,7 +369,7 @@ static int get_key(struct afb_req req, DATA *key) return 0; } -static void put(struct afb_req req, int replace) +static void put(afb_req_t req, int replace) { DATA key; DATA data; @@ -383,13 +383,13 @@ static void put(struct afb_req req, int replace) args = afb_req_json(req); if (!json_object_object_get_ex(args, "value", &item)) { - afb_req_fail(req, "no-value", NULL); + afb_req_reply(req, NULL, "no-value", NULL); return; } value = json_object_to_json_string_ext(item, TO_STRING_FLAGS); if (!value) { - afb_req_fail(req, "out-of-memory", NULL); + afb_req_reply(req, NULL, "out-of-memory", NULL); return; } DATA_SET(&data, value, strlen(value) + 1); /* includes the tailing null */ @@ -398,41 +398,41 @@ static void put(struct afb_req req, int replace) if (get_key(req, &key)) return; - AFB_DEBUG("put: key=%s, value=%s", DATA_STR(key), DATA_STR(data)); + AFB_API_DEBUG(afbBindingRoot, "put: key=%s, value=%s", DATA_STR(key), DATA_STR(data)); xdb_put(req, &key, &data, replace); free(DATA_PTR(key)); } -static void verb_insert(struct afb_req req) +static void verb_insert(afb_req_t req) { put(req, 0); } -static void verb_update(struct afb_req req) +static void verb_update(afb_req_t req) { put(req, 1); } -static void verb_delete(struct afb_req req) +static void verb_delete(afb_req_t req) { DATA key; if (get_key(req, &key)) return; - AFB_DEBUG("delete: key=%s", DATA_STR(key)); + AFB_API_DEBUG(afbBindingRoot, "delete: key=%s", DATA_STR(key)); xdb_delete(req, &key); free(DATA_PTR(key)); } -static void verb_read(struct afb_req req) +static void verb_read(afb_req_t req) { DATA key; if (get_key(req, &key)) return; - AFB_DEBUG("read: key=%s", DATA_STR(key)); + AFB_API_DEBUG(afbBindingRoot, "read: key=%s", DATA_STR(key)); xdb_get(req, &key); free(DATA_PTR(key)); } @@ -450,15 +450,15 @@ static const struct afb_auth ll_database_binding_auths[] = { .info = info_, \ .session = sess_ } -static const afb_verb_v2 ll_database_binding_verbs[]= { - VERB(insert, NULL, NULL, AFB_SESSION_NONE_V2), - VERB(update, NULL, NULL, AFB_SESSION_NONE_V2), - VERB(delete, NULL, NULL, AFB_SESSION_NONE_V2), - VERB(read, NULL, NULL, AFB_SESSION_NONE_V2), +static const afb_verb_t ll_database_binding_verbs[]= { + VERB(insert, NULL, NULL, AFB_SESSION_NONE), + VERB(update, NULL, NULL, AFB_SESSION_NONE), + VERB(delete, NULL, NULL, AFB_SESSION_NONE), + VERB(read, NULL, NULL, AFB_SESSION_NONE), { .verb = NULL} }; -const struct afb_binding_v2 afbBindingV2 = { +const afb_binding_t afbBindingExport = { .api = "persistence", .specification = NULL, .verbs = ll_database_binding_verbs, |