Revert "Better list requests"

This reverts commit ee98b2a472.
This commit is contained in:
alesapin 2021-02-19 00:09:41 +03:00
parent ee98b2a472
commit 839d6f7072
2 changed files with 21 additions and 24 deletions

View File

@ -25,10 +25,10 @@ static String parentPath(const String & path)
return "/";
}
static std::string_view getBaseNameView(const String & path)
static String baseName(const String & path)
{
size_t basename_start = path.rfind('/');
return std::string_view{&path[basename_start + 1], path.length() - basename_start - 1};
auto rslash_pos = path.rfind('/');
return path.substr(rslash_pos + 1);
}
static NuKeeperStorage::ResponsesForSessions processWatchesImpl(const String & path, NuKeeperStorage::Watches & watches, NuKeeperStorage::Watches & list_watches, Coordination::Event event_type)
@ -167,17 +167,14 @@ struct NuKeeperStorageCreateRequest final : public NuKeeperStorageRequest
/// Increment sequential number even if node is not sequential
++it->second.seq_num;
response.path_created = path_created;
auto [child_itr, created] = container.emplace(path_created, std::move(created_node));
auto child_path_view = getBaseNameView(child_itr->first);
it->second.children.insert(child_path_view);
container.emplace(path_created, std::move(created_node));
if (request.is_ephemeral)
ephemerals[session_id].emplace(path_created);
undo = [&container, &ephemerals, session_id, path_created, is_ephemeral = request.is_ephemeral, parent_path = it->first, child_path_view]
undo = [&container, &ephemerals, session_id, path_created, is_ephemeral = request.is_ephemeral, parent_path = it->first]
{
container.erase(path_created);
if (is_ephemeral)
@ -186,7 +183,6 @@ struct NuKeeperStorageCreateRequest final : public NuKeeperStorageRequest
--undo_parent.stat.cversion;
--undo_parent.stat.numChildren;
--undo_parent.seq_num;
undo_parent.children.erase(child_path_view);
};
++it->second.stat.cversion;
@ -254,25 +250,21 @@ struct NuKeeperStorageRemoveRequest final : public NuKeeperStorageRequest
if (prev_node.is_ephemeral)
ephemerals[session_id].erase(request.path);
auto child_basename_view = getBaseNameView(it->first);
container.erase(it);
auto & parent = container.at(parentPath(request.path));
--parent.stat.numChildren;
++parent.stat.cversion;
parent.children.erase(child_basename_view);
response.error = Coordination::Error::ZOK;
container.erase(it);
undo = [prev_node, &container, &ephemerals, session_id, path = request.path]
{
if (prev_node.is_ephemeral)
ephemerals[session_id].emplace(path);
auto [itr, inserted] = container.emplace(path, prev_node);
container.emplace(path, prev_node);
auto & undo_parent = container.at(parentPath(path));
++undo_parent.stat.numChildren;
--undo_parent.stat.cversion;
undo_parent.children.insert(getBaseNameView(itr->first));
};
}
@ -378,10 +370,17 @@ struct NuKeeperStorageListRequest final : public NuKeeperStorageRequest
if (path_prefix.empty())
throw DB::Exception("Logical error: path cannot be empty", ErrorCodes::LOGICAL_ERROR);
for (const auto & name : it->second.children)
response.names.emplace_back(name);
if (path_prefix.back() != '/')
path_prefix += '/';
std::sort(response.names.begin(), response.names.end());
/// Fairly inefficient.
for (auto child_it = container.upper_bound(path_prefix);
child_it != container.end() && startsWith(child_it->first, path_prefix);
++child_it)
{
if (parentPath(child_it->first) == request.path)
response.names.emplace_back(baseName(child_it->first));
}
response.stat = it->second.stat;
response.error = Coordination::Error::ZOK;

View File

@ -16,7 +16,6 @@ using namespace DB;
struct NuKeeperStorageRequest;
using NuKeeperStorageRequestPtr = std::shared_ptr<NuKeeperStorageRequest>;
using ResponseCallback = std::function<void(const Coordination::ZooKeeperResponsePtr &)>;
using ChildrenRefSet = std::unordered_set<std::string_view>;
class NuKeeperStorage
{
@ -31,7 +30,6 @@ public:
bool is_sequental = false;
Coordination::Stat stat{};
int32_t seq_num = 0;
ChildrenRefSet children;
};
struct ResponseForSession
@ -50,9 +48,9 @@ public:
using RequestsForSessions = std::vector<RequestForSession>;
using Container = std::unordered_map<std::string, Node>;
using Ephemerals = std::unordered_map<int64_t, std::unordered_set<std::string>>;
using SessionAndWatcher = std::unordered_map<int64_t, std::unordered_set<std::string>>;
using Container = std::map<std::string, Node>;
using Ephemerals = std::unordered_map<int64_t, std::unordered_set<String>>;
using SessionAndWatcher = std::unordered_map<int64_t, std::unordered_set<String>>;
using SessionAndTimeout = std::unordered_map<int64_t, long>;
using SessionIDs = std::vector<int64_t>;