diff --git a/libs/etcdlib/api/etcdlib.h b/libs/etcdlib/api/etcdlib.h index 809163e6a..19bf1a40c 100644 --- a/libs/etcdlib/api/etcdlib.h +++ b/libs/etcdlib/api/etcdlib.h @@ -149,6 +149,13 @@ int etcdlib_del(etcdlib_t *etcdlib, const char* key); */ int etcdlib_watch(etcdlib_t *etcdlib, const char* key, long long index, char** action, char** prevValue, char** value, char** rkey, long long* modifiedIndex); +/** + * @desc Retrieve the current database index + * @param const etcdlib_t* etcdlib. The ETCD-LIB instance + * @param int* modifiedIndex. The X-Etcd-Index value as retrieved from the etcd server. + */ +int etcdlib_get_db_index(etcdlib_t *etcdlib, int* modifiedIndex); + #ifdef __cplusplus } #endif diff --git a/libs/etcdlib/src/etcd.c b/libs/etcdlib/src/etcd.c index a69061f92..5f843568b 100644 --- a/libs/etcdlib/src/etcd.c +++ b/libs/etcdlib/src/etcd.c @@ -257,6 +257,40 @@ static long long etcd_get_current_index(const char* headerData) { } +int etcdlib_get_db_index(etcdlib_t *etcdlib, int* modifiedIndex) { + + int res = -1; + struct MemoryStruct reply; + + reply.memory = malloc(1); /* will be grown as needed by the realloc above */ + reply.memorySize = 0; /* no data at this point */ + reply.header = malloc(1); /* will be grown as needed by the realloc above */ + reply.headerSize = 0; /* no data at this point */ + + int retVal = ETCDLIB_RC_OK; + char *url; + asprintf(&url, "http://%s:%d/v2/keys", etcdlib->host, etcdlib->port); + res = performRequest(&etcdlib->curl, &etcdlib->mutex, url, GET, NULL, (void *) &reply); + free(url); + + if (res == CURLE_OK) { + long long indexFromHeader = etcd_get_current_index(reply.header); + *modifiedIndex = (int)indexFromHeader; + } else if (res == CURLE_OPERATION_TIMEDOUT) { + retVal = ETCDLIB_RC_TIMEOUT; + } else { + retVal = ETCDLIB_RC_ERROR; + fprintf(stderr, "Error getting etcd value, curl error: '%s'\n", curl_easy_strerror(res)); + } + + if (reply.memory) { + free(reply.memory); + } + free(reply.header); + return retVal; +} + + int etcd_get_directory(const char* directory, etcdlib_key_value_callback callback, void* arg, long long* modifiedIndex) { return etcdlib_get_directory(&g_etcdlib, directory, callback, arg, modifiedIndex); }