Skip to content

Commit

Permalink
Expose key version in etcd::Value (etcd-cpp-apiv3#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateofio authored Oct 27, 2021
1 parent 1118222 commit bfb56be
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions etcd/Value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ namespace etcd
*/
int64_t modified_index() const;

/**
* Returns the version of this value.
*/
int64_t version() const;

/**
* Returns the ttl of this value or 0 if ttl is not set
*/
Expand All @@ -78,6 +83,7 @@ namespace etcd
std::string value;
int64_t created;
int64_t modified;
int64_t _version;
int _ttl;
int64_t leaseId;
};
Expand Down
8 changes: 8 additions & 0 deletions src/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ etcd::Value::Value()
: dir(false),
created(0),
modified(0),
_version(0),
_ttl(0),
leaseId(0)
{
Expand All @@ -20,6 +21,7 @@ etcd::Value::Value(etcdv3::KeyValue const & kv)
value = kv.kvs.value();
created = kv.kvs.create_revision();
modified = kv.kvs.mod_revision();
_version = kv.kvs.version();
leaseId = kv.kvs.lease();
_ttl = kv.get_ttl();
}
Expand All @@ -31,6 +33,7 @@ etcd::Value::Value(mvccpb::KeyValue const & kv)
value = kv.value();
created = kv.create_revision();
modified = kv.mod_revision();
_version = kv.version();
leaseId = kv.lease();
_ttl = -1;
}
Expand Down Expand Up @@ -60,6 +63,11 @@ int64_t etcd::Value::modified_index() const
return modified;
}

int64_t etcd::Value::version() const
{
return _version;
}

int etcd::Value::ttl() const
{
return _ttl;
Expand Down
1 change: 1 addition & 0 deletions tst/AuthTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ TEST_CASE("add a new key after authenticate")
CHECK(!val.is_dir());
CHECK(0 < val.created_index());
CHECK(0 < val.modified_index());
CHECK(1 == val.version());
CHECK(0 < resp.index());
CHECK(etcd::ERROR_KEY_ALREADY_EXISTS == etcd->add("/test/key1", "43").get().error_code()); // Key already exists
CHECK(etcd::ERROR_KEY_ALREADY_EXISTS == etcd->add("/test/key1", "42").get().error_code()); // Key already exists
Expand Down
4 changes: 4 additions & 0 deletions tst/EtcdTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ TEST_CASE("add a new key")
CHECK(!val.is_dir());
CHECK(0 < val.created_index());
CHECK(0 < val.modified_index());
CHECK(1 == val.version());
CHECK(0 < resp.index());
CHECK(etcd::ERROR_KEY_ALREADY_EXISTS == etcd.add("/test/key1", "43").get().error_code()); // Key already exists
CHECK(etcd::ERROR_KEY_ALREADY_EXISTS == etcd.add("/test/key1", "42").get().error_code()); // Key already exists
Expand Down Expand Up @@ -117,6 +118,7 @@ TEST_CASE("delete a value")
int index = etcd.get("/test/key1").get().index();
int create_index = etcd.get("/test/key1").get().value().created_index();
int modify_index = etcd.get("/test/key1").get().value().modified_index();
int version = etcd.get("/test/key1").get().value().version();

std::cerr << "index = " << index
<< ", create index = " << create_index
Expand All @@ -131,9 +133,11 @@ TEST_CASE("delete a value")
CHECK( "/test/key1" == resp.prev_value().key());
CHECK( create_index == resp.prev_value().created_index());
CHECK( modify_index == resp.prev_value().modified_index());
CHECK( version == resp.prev_value().version());
CHECK("delete" == resp.action());
CHECK( modify_index == resp.value().modified_index());
CHECK( create_index == resp.value().created_index());
CHECK( version == resp.value().version());
CHECK("" == resp.value().as_string());
CHECK( "/test/key1" == resp.value().key());
}
Expand Down
1 change: 1 addition & 0 deletions tst/SecurityChannelTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TEST_CASE("add a new key after authenticate")
CHECK(!val.is_dir());
CHECK(0 < val.created_index());
CHECK(0 < val.modified_index());
CHECK(1 == val.version());
CHECK(0 < resp.index());
CHECK(etcd::ERROR_KEY_ALREADY_EXISTS == etcd->add("/test/key1", "43").get().error_code()); // Key already exists
CHECK(etcd::ERROR_KEY_ALREADY_EXISTS == etcd->add("/test/key1", "42").get().error_code()); // Key already exists
Expand Down

0 comments on commit bfb56be

Please sign in to comment.