2021-01-21 11:07:55 +00:00
|
|
|
#include <Coordination/TestKeeperStorageSerializer.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <Common/ZooKeeper/ZooKeeperIO.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2021-01-21 15:09:48 +00:00
|
|
|
void writeNode(const TestKeeperStorage::Node & node, WriteBuffer & out)
|
2021-01-21 11:07:55 +00:00
|
|
|
{
|
|
|
|
Coordination::write(node.data, out);
|
|
|
|
Coordination::write(node.acls, out);
|
|
|
|
Coordination::write(node.is_ephemeral, out);
|
|
|
|
Coordination::write(node.is_sequental, out);
|
|
|
|
Coordination::write(node.stat, out);
|
|
|
|
Coordination::write(node.seq_num, out);
|
|
|
|
}
|
|
|
|
|
2021-01-21 15:09:48 +00:00
|
|
|
void readNode(TestKeeperStorage::Node & node, ReadBuffer & in)
|
2021-01-21 11:07:55 +00:00
|
|
|
{
|
|
|
|
Coordination::read(node.data, in);
|
|
|
|
Coordination::read(node.acls, in);
|
|
|
|
Coordination::read(node.is_ephemeral, in);
|
|
|
|
Coordination::read(node.is_sequental, in);
|
|
|
|
Coordination::read(node.stat, in);
|
|
|
|
Coordination::read(node.seq_num, in);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 08:17:19 +00:00
|
|
|
void TestKeeperStorageSerializer::serialize(const TestKeeperStorage & storage, WriteBuffer & out)
|
2021-01-21 11:07:55 +00:00
|
|
|
{
|
|
|
|
Coordination::write(storage.zxid, out);
|
|
|
|
Coordination::write(storage.session_id_counter, out);
|
|
|
|
Coordination::write(storage.container.size(), out);
|
|
|
|
for (const auto & [path, node] : storage.container)
|
|
|
|
{
|
|
|
|
Coordination::write(path, out);
|
|
|
|
writeNode(node, out);
|
|
|
|
}
|
|
|
|
Coordination::write(storage.ephemerals.size(), out);
|
|
|
|
for (const auto & [session_id, paths] : storage.ephemerals)
|
|
|
|
{
|
|
|
|
Coordination::write(session_id, out);
|
|
|
|
Coordination::write(paths.size(), out);
|
|
|
|
for (const auto & path : paths)
|
|
|
|
Coordination::write(path, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 08:17:19 +00:00
|
|
|
void TestKeeperStorageSerializer::deserialize(TestKeeperStorage & storage, ReadBuffer & in)
|
2021-01-21 11:07:55 +00:00
|
|
|
{
|
|
|
|
int64_t session_id_counter, zxid;
|
|
|
|
Coordination::read(zxid, in);
|
|
|
|
Coordination::read(session_id_counter, in);
|
|
|
|
storage.zxid = zxid;
|
|
|
|
storage.session_id_counter = session_id_counter;
|
|
|
|
|
|
|
|
size_t container_size;
|
|
|
|
Coordination::read(container_size, in);
|
|
|
|
while (storage.container.size() < container_size)
|
|
|
|
{
|
|
|
|
std::string path;
|
|
|
|
Coordination::read(path, in);
|
2021-01-21 15:09:48 +00:00
|
|
|
TestKeeperStorage::Node node;
|
2021-01-21 11:07:55 +00:00
|
|
|
readNode(node, in);
|
|
|
|
storage.container[path] = node;
|
|
|
|
}
|
|
|
|
size_t ephemerals_size;
|
|
|
|
Coordination::read(ephemerals_size, in);
|
|
|
|
while (storage.ephemerals.size() < ephemerals_size)
|
|
|
|
{
|
|
|
|
int64_t session_id;
|
|
|
|
size_t ephemerals_for_session;
|
|
|
|
Coordination::read(session_id, in);
|
|
|
|
Coordination::read(ephemerals_for_session, in);
|
|
|
|
while (storage.ephemerals[session_id].size() < ephemerals_for_session)
|
|
|
|
{
|
|
|
|
std::string ephemeral_path;
|
|
|
|
Coordination::read(ephemeral_path, in);
|
|
|
|
storage.ephemerals[session_id].emplace(ephemeral_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|