forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgossip_digest.hh
129 lines (111 loc) · 3.97 KB
/
gossip_digest.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Modified by Cloudius Systems.
* Copyright 2015 Cloudius Systems.
*/
#pragma once
#include "types.hh"
#include "core/sstring.hh"
#include "util/serialization.hh"
#include "gms/inet_address.hh"
namespace gms {
/**
* Contains information about a specified list of Endpoints and the largest version
* of the state they have generated as known by the local endpoint.
*/
class gossip_digest { // implements Comparable<GossipDigest>
private:
using inet_address = gms::inet_address;
inet_address _endpoint;
int32_t _generation;
int32_t _max_version;
public:
gossip_digest()
: _endpoint(0)
, _generation(0)
, _max_version(0) {
}
gossip_digest(inet_address ep, int32_t gen, int32_t version)
: _endpoint(ep)
, _generation(gen)
, _max_version(version) {
}
inet_address get_endpoint() {
return _endpoint;
}
int32_t get_generation() {
return _generation;
}
int32_t get_max_version() {
return _max_version;
}
int32_t compare_to(gossip_digest d) {
if (_generation != d.get_generation()) {
return (_generation - d.get_generation());
}
return (_max_version - d.get_max_version());
}
friend bool operator<(const gossip_digest& x, const gossip_digest& y) {
if (x._generation != y._generation) {
return x._generation < y._generation;
}
return x._max_version < y._max_version;
}
friend inline std::ostream& operator<<(std::ostream& os, const gossip_digest& d) {
return os << d._endpoint << ":" << d._generation << ":" << d._max_version;
}
// The following replaces GossipDigestSerializer from the Java code
void serialize(bytes::iterator& out) const {
_endpoint.serialize(out);
serialize_int32(out, _generation);
serialize_int32(out, _max_version);
}
static gossip_digest deserialize(bytes_view& v) {
auto endpoint = inet_address::deserialize(v);
auto generation = read_simple<int32_t>(v);
auto max_version = read_simple<int32_t>(v);
return gossip_digest(endpoint, generation, max_version);
}
size_t serialized_size() const {
return _endpoint.serialized_size() + serialize_int32_size + serialize_int32_size;
}
}; // class gossip_digest
// serialization helper for std::vector<gossip_digest>
class gossip_digest_serialization_helper {
public:
static void serialize(bytes::iterator& out, const std::vector<gossip_digest>& digests) {
serialize_int32(out, int32_t(digests.size()));
for (auto& digest : digests) {
digest.serialize(out);
}
}
static std::vector<gossip_digest> deserialize(bytes_view& v) {
int32_t size = read_simple<int32_t>(v);
std::vector<gossip_digest> digests;
for (int32_t i = 0; i < size; ++i)
digests.push_back(gossip_digest::deserialize(v));
return digests;
}
static size_t serialized_size(const std::vector<gossip_digest>& digests) {
size_t size = serialize_int32_size;
for (auto& digest : digests)
size += digest.serialized_size();
return size;
}
};
} // namespace gms