1 /*
2  * Copyright (C) 2018 Codership Oy <info@codership.com>
3  *
4  * This file is part of wsrep-lib.
5  *
6  * Wsrep-lib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Wsrep-lib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef WSREP_GTID_HPP
21 #define WSREP_GTID_HPP
22 
23 #include "id.hpp"
24 #include "seqno.hpp"
25 #include "compiler.hpp"
26 
27 #include <iosfwd>
28 
29 /**
30  * Minimum number of bytes guaratneed to store GTID string representation,
31  * terminating '\0' not included (36 + 1 + 20).
32  */
33 #define WSREP_LIB_GTID_C_STR_LEN 57
34 
35 namespace wsrep
36 {
37     class gtid
38     {
39     public:
gtid()40         gtid()
41             : id_()
42             , seqno_()
43         { }
gtid(const wsrep::id & id,wsrep::seqno seqno)44         gtid(const wsrep::id& id, wsrep::seqno seqno)
45             : id_(id)
46             , seqno_(seqno)
47         { }
id() const48         const wsrep::id& id() const { return id_; }
seqno() const49         wsrep::seqno seqno() const { return seqno_ ; }
is_undefined() const50         bool is_undefined() const
51         {
52             return (seqno_.is_undefined() && id_.is_undefined());
53         }
undefined()54         static const wsrep::gtid& undefined()
55         {
56             return undefined_;
57         }
operator ==(const gtid & other) const58         bool operator==(const gtid& other) const
59         {
60             return (
61                 seqno_ == other.seqno_ &&
62                 id_ == other.id_
63             );
64         }
65     private:
66         static const wsrep::gtid undefined_;
67         wsrep::id id_;
68         wsrep::seqno seqno_;
69     };
70 
71     /**
72      * Scan a GTID from C string.
73      *
74      * @param buf Buffer containing the string
75      * @param len Length of buffer
76      * @param[out] gtid Gtid to be printed to
77      *
78      * @return Number of bytes scanned, negative value on error.
79      */
80     ssize_t scan_from_c_str(const char* buf, size_t buf_len,
81                             wsrep::gtid& gtid);
82 
83     /*
84      * Deprecated version of the above for backwards compatibility.
85      * Will be removed when all the superprojects have been updated.
86      */
gtid_scan_from_c_str(const char * buf,size_t buf_len,wsrep::gtid & gtid)87     static inline ssize_t gtid_scan_from_c_str(const char* buf, size_t buf_len,
88                                                wsrep::gtid& gtid)
89     {
90         return scan_from_c_str(buf, buf_len, gtid);
91     }
92 
93     /**
94      * Print a GTID into character buffer.
95      *
96      * @param gtid GTID to be printed.
97      * @param buf Pointer to the beginning of the buffer
98      * @param buf_len Buffer length
99      *
100      * @return Number of characters printed or negative value for error
101      */
102     ssize_t print_to_c_str(const wsrep::gtid& gtid, char* buf, size_t buf_len);
103 
104     /*
105      * Deprecated version of the above for backwards compatibility.
106      * Will be removed when all the superprojects have been updated.
107      */
gtid_print_to_c_str(const wsrep::gtid & gtid,char * buf,size_t buf_len)108     static inline ssize_t gtid_print_to_c_str(const wsrep::gtid& gtid,
109                                               char* buf, size_t buf_len)
110     {
111         return print_to_c_str(gtid, buf, buf_len);
112     }
113 
114     /**
115      * Return minimum number of chars required to store any GTID.
116      */
gtid_c_str_len()117     static inline size_t gtid_c_str_len() { return WSREP_LIB_GTID_C_STR_LEN; }
118 
119     /**
120      * Overload for ostream operator<<.
121      */
122     std::ostream& operator<<(std::ostream&, const wsrep::gtid&);
123 
124     /**
125      * Overload for istream operator>>.
126      */
127     std::istream& operator>>(std::istream&, wsrep::gtid&);
128 }
129 
130 #endif // WSREP_GTID_HPP
131