Add "limit" support on "ls" command.

Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
This commit is contained in:
Tao He 2020-09-07 10:58:47 +08:00
parent b3f6b826cd
commit fa30281a19
5 changed files with 24 additions and 1 deletions

View File

@ -157,6 +157,15 @@ namespace etcd
pplx::task<Response> ls(std::string const & key); pplx::task<Response> 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<Response> 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. * 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 * @param key is the directory to be created to be listed

View File

@ -32,6 +32,7 @@ namespace etcdv3
int old_revision; int old_revision;
int64_t lease_id; int64_t lease_id;
int ttl; int ttl;
int limit;
std::string key; std::string key;
std::string value; std::string value;
std::string old_value; std::string old_value;

View File

@ -339,6 +339,18 @@ pplx::task<etcd::Response> etcd::Client::ls(std::string const & key)
etcdv3::ActionParameters params; etcdv3::ActionParameters params;
params.key.assign(key); params.key.assign(key);
params.withPrefix = true; params.withPrefix = true;
params.limit = 0; // default no limit.
params.kv_stub = stub_.get();
std::shared_ptr<etcdv3::AsyncGetAction> call(new etcdv3::AsyncGetAction(params));
return Response::create(call);
}
pplx::task<etcd::Response> 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(); params.kv_stub = stub_.get();
std::shared_ptr<etcdv3::AsyncGetAction> call(new etcdv3::AsyncGetAction(params)); std::shared_ptr<etcdv3::AsyncGetAction> call(new etcdv3::AsyncGetAction(params));
return Response::create(call); return Response::create(call);

View File

@ -8,6 +8,7 @@ etcdv3::AsyncGetAction::AsyncGetAction(etcdv3::ActionParameters param)
{ {
RangeRequest get_request; RangeRequest get_request;
get_request.set_key(parameters.key); get_request.set_key(parameters.key);
get_request.set_limit(param.limit);
if(parameters.withPrefix) if(parameters.withPrefix)
{ {
std::string range_end(parameters.key); std::string range_end(parameters.key);

View File

@ -6,7 +6,7 @@ void etcdv3::AsyncRangeResponse::ParseResponse(RangeResponse& resp, bool prefix)
{ {
action = etcdv3::GET_ACTION; action = etcdv3::GET_ACTION;
index = resp.header().revision(); index = resp.header().revision();
if(resp.kvs_size() == 0) if(resp.kvs_size() == 0 && !prefix)
{ {
error_code=100; error_code=100;
error_message="Key not found"; error_message="Key not found";