You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@celix.apache.org by GitBox <gi...@apache.org> on 2020/09/28 20:52:53 UTC

[GitHub] [celix] Oipo commented on a change in pull request #285: Add API call for retrieving db index

Oipo commented on a change in pull request #285:
URL: https://github.com/apache/celix/pull/285#discussion_r496224385



##########
File path: 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);

Review comment:
       Please rename modifiedIndex to currentEtcdIndex

##########
File path: 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) {

Review comment:
       Please rename modifiedIndex to currentEtcdIndex

##########
File path: 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) {

Review comment:
       Although it happens multiple times in the current code, would you mind removing the if statement here? `free()` explicitly handles NULL values properly by ignoring them.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org