From 93f8131e8326a60e0737abfb62473ad096cc204f Mon Sep 17 00:00:00 2001 From: arches Date: Mon, 11 Jul 2016 05:27:09 -0400 Subject: [PATCH] put deleted keys in values() when prefix = true. --- tst/EtcdTest.cpp | 39 +++++++----------- v3/src/AsyncDeleteRangeResponse.cpp | 61 +++-------------------------- 2 files changed, 21 insertions(+), 79 deletions(-) diff --git a/tst/EtcdTest.cpp b/tst/EtcdTest.cpp index 1b66890..ab3bd2b 100644 --- a/tst/EtcdTest.cpp +++ b/tst/EtcdTest.cpp @@ -16,6 +16,7 @@ TEST_CASE("add a new key") { etcd::Client etcd("http://127.0.0.1:2379"); + etcd.rmdir("/test", true).wait(); etcd::Response resp = etcd.add("/test/key1", "42").get(); REQUIRE(0 == resp.error_code()); CHECK("create" == resp.action()); @@ -50,6 +51,7 @@ TEST_CASE("simplified read") etcd::Client etcd("http://127.0.0.1:2379"); CHECK("42" == etcd.get("/test/key1").get().value().as_string()); CHECK(100 == etcd.get("/test/key2").get().error_code()); // Key not found + CHECK("" == etcd.get("/test/key2").get().value().as_string()); // Key not found } @@ -129,7 +131,7 @@ TEST_CASE("delete a value") CHECK( create_index == resp.prev_value().created_index()); CHECK( modify_index == resp.prev_value().modified_index()); CHECK("delete" == resp.action()); - CHECK( resp.index() == resp.value().modified_index()); + CHECK( modify_index == resp.value().modified_index()); CHECK( create_index == resp.value().created_index()); CHECK("" == resp.value().as_string()); CHECK( "/test/key1" == resp.value().key()); @@ -246,41 +248,30 @@ TEST_CASE("list a directory") TEST_CASE("delete a directory") { etcd::Client etcd("http://127.0.0.1:2379"); + //CHECK(108 == etcd.rmdir("/test/new_dir").get().error_code()); // Directory not empty etcd::Response resp = etcd.ls("/test/new_dir").get(); - //get the lowest created index - std::vector myset; - for(unsigned int cnt=0; cnt < resp.values().size(); cnt++) - { - myset.push_back(resp.value(cnt).created_index()); - } - std::sort(myset.begin(),myset.end()); - - //get the latest modified index - std::vector myset1; - for(unsigned int cnt=0; cnt < resp.values().size(); cnt++) - { - myset1.push_back(resp.value(cnt).modified_index()); - } - std::sort(myset1.begin(),myset1.end()); resp = etcd.rmdir("/test/new_dir", true).get(); int index = resp.index(); - - CHECK("" == resp.prev_value().as_string()); - CHECK( myset[0] == resp.prev_value().created_index()); - CHECK( myset1[myset1.size() - 1] == resp.prev_value().modified_index()); - CHECK( "/test/new_dir" == resp.prev_value().key()); CHECK("delete" == resp.action()); - CHECK( index == resp.value().modified_index()); - CHECK( myset[0] == resp.value().created_index()); - CHECK("" == resp.value().as_string()); + REQUIRE(3 == resp.keys().size()); + CHECK("/test/new_dir/key1" == resp.key(0)); + CHECK("/test/new_dir/key2" == resp.key(1)); + CHECK("value1" == resp.value(0).as_string()); + CHECK("value2" == resp.value(1).as_string()); + resp = etcd.rmdir("/test/dirnotfound", true).get(); CHECK(!resp.is_ok()); CHECK(100 == resp.error_code()); CHECK("Key not found" == resp.error_message()); + + resp = etcd.rmdir("/test/new_dir", false).get(); + CHECK(!resp.is_ok()); + CHECK(100 == resp.error_code()); + CHECK("Key not found" == resp.error_message()); } TEST_CASE("wait for a value change") diff --git a/v3/src/AsyncDeleteRangeResponse.cpp b/v3/src/AsyncDeleteRangeResponse.cpp index ee92b08..266c117 100644 --- a/v3/src/AsyncDeleteRangeResponse.cpp +++ b/v3/src/AsyncDeleteRangeResponse.cpp @@ -17,65 +17,16 @@ void etcdv3::AsyncDeleteRangeResponse::ParseResponse(std::string const& key, boo //get all previous values for(int cnt=0; cnt < resp.prev_kvs_size(); cnt++) { - prev_values.push_back(resp.prev_kvs(cnt)); + values.push_back(resp.prev_kvs(cnt)); } - //copy previous vales to values. We will format this later - for(unsigned int cnt=0; cnt < prev_values.size(); cnt++) + if(!prefix) { - auto temp_value = prev_values[cnt]; - temp_value.set_mod_revision(index); - temp_value.clear_value(); - values.push_back(temp_value); + prev_value = values[0]; + value = values[0]; + value.clear_value(); + values.clear(); } - prev_value = prev_values[0]; - value = values[0]; - - //get all mod revisions of previous values - std::vector mod_index; - for(unsigned int cnt = 0; cnt < prev_values.size(); ++cnt) - { - mod_index.push_back(prev_values[cnt].mod_revision()); - } - //sort it ascending - std::sort(mod_index.begin(),mod_index.end()); - // use the latest mod index - prev_value.set_mod_revision(mod_index.back()); - - //get all created revision of previous values - std::vector create_index; - for(unsigned int cnt = 0; cnt < prev_values.size(); ++cnt) - { - create_index.push_back(prev_values[cnt].create_revision()); - } - //sort it ascending - std::sort(create_index.begin(),create_index.end()); - - //use earliest create index - prev_value.set_create_revision(create_index.front()); - - - - //value modified index should be the same as index - value.set_mod_revision(index); - //value created index should be the same as prev value created index - value.set_create_revision(prev_value.create_revision()); - - //set key.When prefix delete is done, we should use the prefix provided by - //client as the key - value.set_key(key); - prev_value.set_key(key); - - //clear the value - value.clear_value(); - - //if withPrefix, clear previous value also - if(prefix) - { - prev_value.clear_value(); - } - prev_values.clear(); - values.clear(); } }