From 6eb725de0ebace48b7c7470c32e9888865c7a21c Mon Sep 17 00:00:00 2001 From: Tao He Date: Mon, 7 Sep 2020 10:58:47 +0800 Subject: [PATCH] Add "limit" support on "ls" command. Signed-off-by: Tao He --- etcd/Client.hpp | 9 +++++++++ etcd/v3/Action.hpp | 1 + src/Client.cpp | 12 ++++++++++++ src/v3/AsyncGetAction.cpp | 1 + src/v3/AsyncRangeResponse.cpp | 2 +- 5 files changed, 24 insertions(+), 1 deletion(-) diff --git a/etcd/Client.hpp b/etcd/Client.hpp index 734b060..adc098d 100644 --- a/etcd/Client.hpp +++ b/etcd/Client.hpp @@ -157,6 +157,15 @@ namespace etcd pplx::task ls(std::string const & key); + /** + * Gets a directory listing of the directory identified by the key. + * @param key is the key to be listed + * @param limit is the size limit of results to be listed, we don't use default parameters + * to ensure backwards binary compatibility. + */ + pplx::task ls(std::string const & key, size_t const limit); + + /** * Removes a directory node. Fails if the parent directory dos not exists or not a directory. * @param key is the directory to be created to be listed diff --git a/etcd/v3/Action.hpp b/etcd/v3/Action.hpp index 6b7e14e..5d4543e 100644 --- a/etcd/v3/Action.hpp +++ b/etcd/v3/Action.hpp @@ -32,6 +32,7 @@ namespace etcdv3 int old_revision; int64_t lease_id; int ttl; + int limit; std::string key; std::string value; std::string old_value; diff --git a/src/Client.cpp b/src/Client.cpp index 45bc35e..7ae98ad 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -339,6 +339,18 @@ pplx::task etcd::Client::ls(std::string const & key) etcdv3::ActionParameters params; params.key.assign(key); params.withPrefix = true; + params.limit = 0; // default no limit. + params.kv_stub = stub_.get(); + std::shared_ptr call(new etcdv3::AsyncGetAction(params)); + return Response::create(call); +} + +pplx::task etcd::Client::ls(std::string const & key, size_t const limit) +{ + etcdv3::ActionParameters params; + params.key.assign(key); + params.withPrefix = true; + params.limit = limit; params.kv_stub = stub_.get(); std::shared_ptr call(new etcdv3::AsyncGetAction(params)); return Response::create(call); diff --git a/src/v3/AsyncGetAction.cpp b/src/v3/AsyncGetAction.cpp index 5d76e06..c89c812 100644 --- a/src/v3/AsyncGetAction.cpp +++ b/src/v3/AsyncGetAction.cpp @@ -8,6 +8,7 @@ etcdv3::AsyncGetAction::AsyncGetAction(etcdv3::ActionParameters param) { RangeRequest get_request; get_request.set_key(parameters.key); + get_request.set_limit(param.limit); if(parameters.withPrefix) { std::string range_end(parameters.key); diff --git a/src/v3/AsyncRangeResponse.cpp b/src/v3/AsyncRangeResponse.cpp index 9b96010..606e5a4 100644 --- a/src/v3/AsyncRangeResponse.cpp +++ b/src/v3/AsyncRangeResponse.cpp @@ -6,7 +6,7 @@ void etcdv3::AsyncRangeResponse::ParseResponse(RangeResponse& resp, bool prefix) { action = etcdv3::GET_ACTION; index = resp.header().revision(); - if(resp.kvs_size() == 0) + if(resp.kvs_size() == 0 && !prefix) { error_code=100; error_message="Key not found";