1#pragma once
2
3#include <set>
4#include <Core/Types.h>
5#include <IO/ReadBuffer.h>
6#include <IO/ReadBufferFromString.h>
7#include <IO/WriteBuffer.h>
8#include <IO/WriteBufferFromString.h>
9#include <IO/Operators.h>
10#include <Common/ZooKeeper/ZooKeeper.h>
11
12
13namespace DB
14{
15
16/** To implement the functionality of the "quorum write".
17 * Information about which replicas the inserted part of data appeared on,
18 * and on how many replicas it should be.
19 */
20struct ReplicatedMergeTreeQuorumEntry
21{
22 String part_name;
23 size_t required_number_of_replicas{};
24 std::set<String> replicas;
25
26 ReplicatedMergeTreeQuorumEntry() {}
27 ReplicatedMergeTreeQuorumEntry(const String & str)
28 {
29 fromString(str);
30 }
31
32 void writeText(WriteBuffer & out) const
33 {
34 out << "version: 1\n"
35 << "part_name: " << part_name << "\n"
36 << "required_number_of_replicas: " << required_number_of_replicas << "\n"
37 << "actual_number_of_replicas: " << replicas.size() << "\n"
38 << "replicas:\n";
39
40 for (const auto & replica : replicas)
41 out << escape << replica << "\n";
42 }
43
44 void readText(ReadBuffer & in)
45 {
46 size_t actual_number_of_replicas = 0;
47
48 in >> "version: 1\n"
49 >> "part_name: " >> part_name >> "\n"
50 >> "required_number_of_replicas: " >> required_number_of_replicas >> "\n"
51 >> "actual_number_of_replicas: " >> actual_number_of_replicas >> "\n"
52 >> "replicas:\n";
53
54 for (size_t i = 0; i < actual_number_of_replicas; ++i)
55 {
56 String replica;
57 in >> escape >> replica >> "\n";
58 replicas.insert(replica);
59 }
60 }
61
62 String toString() const
63 {
64 WriteBufferFromOwnString out;
65 writeText(out);
66 return out.str();
67 }
68
69 void fromString(const String & str)
70 {
71 ReadBufferFromString in(str);
72 readText(in);
73 }
74};
75
76}
77