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_KEY_HPP
21 #define WSREP_KEY_HPP
22 
23 #include "exception.hpp"
24 #include "buffer.hpp"
25 
26 #include <iosfwd>
27 
28 namespace wsrep
29 {
30     /** @class key
31      *
32      * Certification key type.
33      */
34     class key
35     {
36     public:
37         enum type
38         {
39             shared,
40             reference,
41             update,
42             exclusive
43         };
44 
key(enum type type)45         key(enum type type)
46             : type_(type)
47             , key_parts_()
48             , key_parts_len_()
49         { }
50 
51         /**
52          * Append key part to key.
53          *
54          * @param ptr Pointer to key part data. The caller is supposed to take
55          *            care that the pointer remains valid over the lifetime
56          *            if the key object.
57          * @param len Length of the key part data.
58          */
append_key_part(const void * ptr,size_t len)59         void append_key_part(const void* ptr, size_t len)
60         {
61             if (key_parts_len_ == 3)
62             {
63                 throw wsrep::runtime_error("key parts exceed maximum of 3");
64             }
65             key_parts_[key_parts_len_] = wsrep::const_buffer(ptr, len);
66             ++key_parts_len_;
67         }
68 
type() const69         enum type type() const
70         {
71             return type_;
72         }
73 
size() const74         size_t size() const
75         {
76             return key_parts_len_;
77         }
78 
key_parts() const79         const wsrep::const_buffer* key_parts() const
80         {
81             return key_parts_;
82         }
83     private:
84 
85         enum type type_;
86         wsrep::const_buffer key_parts_[3];
87         size_t key_parts_len_;
88     };
89 
90     typedef std::vector<wsrep::key> key_array;
91 
92     std::ostream& operator<<(std::ostream&, enum wsrep::key::type);
93     std::ostream& operator<<(std::ostream&, const wsrep::key&);
94 }
95 
96 #endif // WSREP_KEY_HPP
97