Skip to content

Commit

Permalink
add class SimpleListResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
lzydmxy committed Oct 23, 2023
1 parent afe6c51 commit 40b9b5b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
17 changes: 11 additions & 6 deletions src/Service/KeeperStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,29 +786,34 @@ struct SvsKeeperStorageListRequest final : public StoreRequest
process(KeeperStore & store, int64_t /*zxid*/, int64_t /*session_id*/, int64_t /* time */) const override
{
Coordination::ZooKeeperResponsePtr response_ptr = zk_request->makeResponse();
Coordination::ZooKeeperListResponse & response = dynamic_cast<Coordination::ZooKeeperListResponse &>(*response_ptr);
Coordination::ZooKeeperListRequest & request = dynamic_cast<Coordination::ZooKeeperListRequest &>(*zk_request);

auto node = store.container.get(request.path);
if (node == nullptr)
{
response.error = Coordination::Error::ZNONODE;
response_ptr->error = Coordination::Error::ZNONODE;
return {response_ptr, {}};
}

auto path_prefix = request.path;
if (path_prefix.empty())
throw RK::Exception("Logical error: path cannot be empty", ErrorCodes::LOGICAL_ERROR);

if (response_ptr->getOpNum() == Coordination::OpNum::List)
{
Coordination::ZooKeeperListResponse & response = dynamic_cast<Coordination::ZooKeeperListResponse &>(*response_ptr);
std::shared_lock r_lock(node->mutex);
response.names.insert(response.names.end(), node->children.begin(), node->children.end());
response.stat = node->statForResponse();
}
else
{
Coordination::ZooKeeperSimpleListResponse & response = dynamic_cast<Coordination::ZooKeeperSimpleListResponse &>(*response_ptr);
std::shared_lock r_lock(node->mutex);
response.names.insert(response.names.end(), node->children.begin(), node->children.end());
if (response.getOpNum() == Coordination::OpNum::List)
response.stat = node->statForResponse();
}

std::sort(response.names.begin(), response.names.end());
response.error = Coordination::Error::ZOK;
response_ptr->error = Coordination::Error::ZOK;

return {response_ptr, {}};
}
Expand Down
5 changes: 5 additions & 0 deletions src/ZooKeeper/IKeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ struct ListResponse : virtual Response
Stat stat;
};

struct SimpleListResponse : virtual Response
{
std::vector<String> names;
};

struct CheckRequest : virtual Request
{
String path;
Expand Down
8 changes: 4 additions & 4 deletions src/ZooKeeper/ZooKeeperCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ struct ZooKeeperSimpleListRequest final : ZooKeeperListRequest
}
};

struct ZooKeeperListResponse : ListResponse, ZooKeeperResponse
struct ZooKeeperListResponse final : ListResponse, ZooKeeperResponse
{
void readImpl(ReadBuffer & in) override;
void writeImpl(WriteBuffer & out) const override;
Expand Down Expand Up @@ -477,7 +477,7 @@ struct ZooKeeperListResponse : ListResponse, ZooKeeperResponse
}
};

struct ZooKeeperSimpleListResponse final : ZooKeeperListResponse
struct ZooKeeperSimpleListResponse final : SimpleListResponse, ZooKeeperResponse
{
void readImpl(ReadBuffer & in) override;
void writeImpl(WriteBuffer & out) const override;
Expand All @@ -491,14 +491,14 @@ struct ZooKeeperSimpleListResponse final : ZooKeeperListResponse
std::vector<String> copy_nodes(names);
std::sort(copy_other_nodes.begin(), copy_other_nodes.end());
std::sort(copy_nodes.begin(), copy_nodes.end());
return ZooKeeperResponse::operator==(response) && copy_other_nodes == copy_nodes;
return ZooKeeperResponse::operator==(response) && copy_other_nodes == copy_nodes;
}
return false;
}

String toString() const override
{
String base = "ListResponse " + ZooKeeperResponse::toString() + ", stat " + stat.toString() + ", names ";
String base = "SimpleListResponse " + ZooKeeperResponse::toString() + ", names ";
auto func = [&](const String & value){ base += ", " + value; };
std::for_each(names.begin(), names.end(), func);
return base;
Expand Down

0 comments on commit 40b9b5b

Please sign in to comment.