1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2010 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <platform/socket.h>
21 #include <string>
22 
23 typedef enum : int {
24     vbucket_state_active = 1, /**< Actively servicing a vbucket. */
25     vbucket_state_replica, /**< Servicing a vbucket as a replica only. */
26     vbucket_state_pending, /**< Pending active. */
27     vbucket_state_dead /**< Not in use, pending deletion. */
28 } vbucket_state_t;
29 
30 /**
31  * Enumeration used by GET_ALL_VBSEQNOS for the client to specify which
32  * vBucket(s) they are interested in. Allows both specific states, plus some
33  * groups.
34  */
35 enum class RequestedVBState : int {
36     Alive = 0, /**< A value indicating the vBucket is not dead*/
37     Active = vbucket_state_active,
38     Replica = vbucket_state_replica,
39     Pending = vbucket_state_pending,
40     Dead = vbucket_state_dead,
41 };
42 
43 #define is_valid_vbucket_state_t(state) \
44     (state == vbucket_state_active || \
45      state == vbucket_state_replica || \
46      state == vbucket_state_pending || \
47      state == vbucket_state_dead)
48 
49 typedef struct {
50     uint64_t uuid;
51     uint64_t seqno;
52 } vbucket_failover_t;
53 
54 /**
55  * Vbid - a custom type class to control the use of vBucket ID's and their
56  * output formatting, wrapping it with "vb:"
57  */
58 class Vbid {
59 public:
60     typedef uint16_t id_type;
61 
62     Vbid() = default;
63 
Vbid(id_type vbidParam)64     explicit Vbid(id_type vbidParam) : vbid(vbidParam){};
65 
66     // Retrieve the vBucket ID in the form of uint16_t
get() const67     const id_type get() const {
68         return vbid;
69     }
70 
71     // Retrieve the vBucket ID in a printable/loggable form
to_string() const72     std::string to_string() const {
73         return "vb:" + std::to_string(vbid);
74     }
75 
76     // Converting the byte order of Vbid's between host and network order
77     // has been simplified with these functions
ntoh() const78     Vbid ntoh() const {
79         return Vbid(ntohs(vbid));
80     }
81 
hton() const82     Vbid hton() const {
83         return Vbid(htons(vbid));
84     }
85 
operator <(const Vbid& other) const86     bool operator<(const Vbid& other) const {
87         return (vbid < other.get());
88     }
89 
operator <=(const Vbid& other) const90     bool operator<=(const Vbid& other) const {
91         return (vbid <= other.get());
92     }
93 
operator >(const Vbid& other) const94     bool operator>(const Vbid& other) const {
95         return (vbid > other.get());
96     }
97 
operator >=(const Vbid& other) const98     bool operator>=(const Vbid& other) const {
99         return (vbid >= other.get());
100     }
101 
operator ==(const Vbid& other) const102     bool operator==(const Vbid& other) const {
103         return (vbid == other.get());
104     }
105 
operator !=(const Vbid& other) const106     bool operator!=(const Vbid& other) const {
107         return (vbid != other.get());
108     }
109 
operator ++()110     Vbid operator++() {
111         return Vbid(++vbid);
112     }
113 
operator ++(int)114     Vbid operator++(int) {
115         return Vbid(vbid++);
116     }
117 
118 protected:
119     id_type vbid;
120 };
121 
122 std::ostream& operator<<(std::ostream& os, const Vbid& d);
123 std::string to_string(const Vbid& vbucket);
124 
125 namespace std {
126 template <>
127 struct hash<Vbid> {
128 public:
operator ()std::hash129     size_t operator()(const Vbid& d) const {
130         return static_cast<size_t>(d.get());
131     }
132 };
133 } // namespace std
134