Skip to content

Commit 29835fb

Browse files
committed
Cleaning wsrep-API dependencies from library code
Replace all wsrep_api.h dependencies with similar code in library implementation. Also add gcc/g++ 4.4 to travis automation to get better compiler coverage.
1 parent 58aa3e8 commit 29835fb

File tree

14 files changed

+354
-30
lines changed

14 files changed

+354
-30
lines changed

include/wsrep/client_state.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ namespace wsrep
307307

308308
/**
309309
* Append a key into transaction write set.
310+
*
311+
* @param key Key to be appended
312+
*
313+
* @return Zero on success, non-zero on failure.
310314
*/
311315
int append_key(const wsrep::key& key)
312316
{
@@ -315,6 +319,27 @@ namespace wsrep
315319
return transaction_.append_key(key);
316320
}
317321

322+
/**
323+
* Append keys in key_array into transaction write set.
324+
*
325+
* @param keys Array of keys to be appended
326+
*
327+
* @return Zero in case of success, non-zero on failure.
328+
*/
329+
int append_keys(const wsrep::key_array& keys)
330+
{
331+
assert(mode_ == m_local || mode_ == m_toi);
332+
assert(state_ == s_exec);
333+
for (auto i(keys.begin()); i != keys.end(); ++i)
334+
{
335+
if (transaction_.append_key(*i))
336+
{
337+
return 1;
338+
}
339+
}
340+
return 0;
341+
}
342+
318343
/**
319344
* Append data into transaction write set.
320345
*/

include/wsrep/compiler.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@
2020

2121
/** @file compiler.hpp
2222
*
23-
* Compiler specific options.
23+
* Compiler specific macro definitions.
24+
*
25+
* WSREP_OVERRIDE - Set to "override" if the compiler supports it, otherwise
26+
* left empty.
27+
* WSREP_CONSTEXPR_OR_INLINE - Set to "constexpr" if the compiler supports it,
28+
* otherwise "inline".
2429
*/
2530

2631
#define WSREP_UNUSED __attribute__((unused))
2732
#if __cplusplus >= 201103L
2833
#define WSREP_OVERRIDE override
34+
#define WSREP_CONSTEXPR_OR_INLINE constexpr
2935
#else
3036
#define WSREP_OVERRIDE
37+
#define WSREP_CONSTEXPR_OR_INLINE inline
3138
#endif // __cplusplus >= 201103L

include/wsrep/gtid.hpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@
2222

2323
#include "id.hpp"
2424
#include "seqno.hpp"
25+
#include "compiler.hpp"
2526

2627
#include <iosfwd>
2728

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+
2835
namespace wsrep
2936
{
3037
class gtid
@@ -61,23 +68,35 @@ namespace wsrep
6168
wsrep::seqno seqno_;
6269
};
6370

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+
6483
/**
6584
* Print a GTID into character buffer.
6685
* @param buf Pointer to the beginning of the buffer
6786
* @param buf_len Buffer length
6887
*
6988
* @return Number of characters printed or negative value for error
7089
*/
71-
ssize_t gtid_print_to_c_str(const wsrep::gtid&, char* buf, size_t buf_len);
90+
ssize_t print_to_c_str(const wsrep::gtid&, char* buf, size_t buf_len);
91+
7292
/**
73-
* Return minimum number of bytes guaranteed to store GTID string
74-
* representation, terminating '\0' not included (36 + 1 + 20)
93+
* Overload for ostream operator<<.
7594
*/
76-
static inline size_t gtid_c_str_len()
77-
{
78-
return 57;
79-
}
8095
std::ostream& operator<<(std::ostream&, const wsrep::gtid&);
96+
97+
/**
98+
* Overload for istream operator>>.
99+
*/
81100
std::istream& operator>>(std::istream&, wsrep::gtid&);
82101
}
83102

include/wsrep/id.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ namespace wsrep
4242
class id
4343
{
4444
public:
45+
typedef struct native_type { unsigned char buf[16]; } native_type;
4546
/**
4647
* Default constructor. Constructs an empty identifier.
4748
*/
48-
id() : data_() { std::memset(data_, 0, sizeof(data_)); }
49+
id() : data_() { std::memset(data_.buf, 0, sizeof(data_.buf)); }
4950

5051
/**
5152
* Construct from string. The input string may contain either
@@ -62,24 +63,24 @@ namespace wsrep
6263
{
6364
throw wsrep::runtime_error("Too long identifier");
6465
}
65-
std::memset(data_, 0, sizeof(data_));
66-
std::memcpy(data_, data, size);
66+
std::memset(data_.buf, 0, sizeof(data_.buf));
67+
std::memcpy(data_.buf, data, size);
6768
}
6869

6970
bool operator<(const id& other) const
7071
{
71-
return (std::memcmp(data_, other.data_, sizeof(data_)) < 0);
72+
return (std::memcmp(data_.buf, other.data_.buf, sizeof(data_.buf)) < 0);
7273
}
7374

7475
bool operator==(const id& other) const
7576
{
76-
return (std::memcmp(data_, other.data_, sizeof(data_)) == 0);
77+
return (std::memcmp(data_.buf, other.data_.buf, sizeof(data_.buf)) == 0);
7778
}
7879
bool operator!=(const id& other) const
7980
{
8081
return !(*this == other);
8182
}
82-
const void* data() const { return data_; }
83+
const void* data() const { return data_.buf; }
8384

8485
size_t size() const { return sizeof(data_); }
8586

@@ -94,7 +95,7 @@ namespace wsrep
9495
}
9596
private:
9697
static const wsrep::id undefined_;
97-
unsigned char data_[16];
98+
native_type data_;
9899
};
99100

100101
std::ostream& operator<<(std::ostream&, const wsrep::id& id);

include/wsrep/provider.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "buffer.hpp"
2626
#include "client_id.hpp"
2727
#include "transaction_id.hpp"
28+
#include "compiler.hpp"
2829

2930
#include <cassert>
3031
#include <cstring>
@@ -404,6 +405,14 @@ namespace wsrep
404405
*/
405406
virtual void* native() const = 0;
406407

408+
409+
/**
410+
* Empty provider magic. If none provider is passed to make_provider(),
411+
* a dummy provider is loaded.
412+
*/
413+
static WSREP_CONSTEXPR_OR_INLINE
414+
const char* none() { return "none"; }
415+
407416
/**
408417
* Create a new provider.
409418
*

include/wsrep/seqno.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
#ifndef WSREP_SEQNO_HPP
2121
#define WSREP_SEQNO_HPP
2222

23-
#include "exception.hpp"
24-
2523
#include <iosfwd>
2624

2725
namespace wsrep
@@ -33,6 +31,8 @@ namespace wsrep
3331
class seqno
3432
{
3533
public:
34+
typedef long long native_type;
35+
3636
seqno()
3737
: seqno_(-1)
3838
{ }
@@ -77,8 +77,9 @@ namespace wsrep
7777
return (*this + seqno(other));
7878
}
7979
static seqno undefined() { return seqno(-1); }
80+
8081
private:
81-
long long seqno_;
82+
native_type seqno_;
8283
};
8384

8485
std::ostream& operator<<(std::ostream& os, wsrep::seqno seqno);

include/wsrep/server_state.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ namespace wsrep
179179
rm_sync
180180
};
181181

182+
/**
183+
* Magic string to tell provider to engage into trivial (empty)
184+
* state transfer. No data will be passed, but the node shall be
185+
* considered joined.
186+
*/
187+
static WSREP_CONSTEXPR_OR_INLINE
188+
const char* sst_trivial() { return "trivial"; };
182189

183190
virtual ~server_state();
184191

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ add_library(wsrep-lib
1515
server_state.cpp
1616
thread.cpp
1717
transaction.cpp
18+
uuid.cpp
1819
wsrep_provider_v26.cpp)
1920
target_link_libraries(wsrep-lib wsrep_api_v26 pthread dl)

src/gtid.cpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,47 @@ std::istream& wsrep::operator>>(std::istream& is, wsrep::gtid& gtid)
3636
std::getline(is, id_str, ':');
3737
long long seq;
3838
is >> seq;
39-
gtid = wsrep::gtid(wsrep::id(id_str), wsrep::seqno(seq));
39+
if (!is)
40+
{
41+
is.clear(std::ios_base::failbit);
42+
return is;
43+
}
44+
try
45+
{
46+
// wsrep::id constructor will throw if it cannot parse the
47+
// id_str.
48+
gtid = wsrep::gtid(wsrep::id(id_str), wsrep::seqno(seq));
49+
}
50+
catch (const wsrep::runtime_error& e)
51+
{
52+
// Formatting or extraction error. Clear the istream state and
53+
// set failibit.
54+
is.clear(std::ios_base::failbit);
55+
}
4056
return is;
4157
}
4258

43-
ssize_t wsrep::gtid_print_to_c_str(
59+
ssize_t wsrep::scan_from_c_str(
60+
const char* buf, size_t buf_len, wsrep::gtid& gtid)
61+
{
62+
std::istringstream is(std::string(buf, buf_len));
63+
is >> gtid;
64+
// Whole string was consumed without failures
65+
if (is && is.eof())
66+
{
67+
return buf_len;
68+
}
69+
// Some failure occurred
70+
if (!is)
71+
{
72+
return -EINVAL;
73+
}
74+
// The string was not consumed completely, return current position
75+
// of the istream.
76+
return is.tellg();
77+
}
78+
79+
ssize_t wsrep::print_to_c_str(
4480
const wsrep::gtid& gtid, char* buf, size_t buf_len)
4581
{
4682
std::ostringstream os;

src/id.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
#include "wsrep/id.hpp"
21-
#include <wsrep_api.h>
21+
#include "uuid.hpp"
2222

2323
#include <cctype>
2424
#include <sstream>
@@ -29,15 +29,17 @@ const wsrep::id wsrep::id::undefined_ = wsrep::id();
2929
wsrep::id::id(const std::string& str)
3030
: data_()
3131
{
32-
wsrep_uuid_t wsrep_uuid;
33-
if (wsrep_uuid_scan(str.c_str(), str.size(), &wsrep_uuid) ==
34-
WSREP_UUID_STR_LEN)
32+
wsrep::uuid_t wsrep_uuid;
33+
34+
if (str.size() == WSREP_LIB_UUID_STR_LEN &&
35+
wsrep::uuid_scan(str.c_str(), str.size(), &wsrep_uuid) ==
36+
WSREP_LIB_UUID_STR_LEN)
3537
{
36-
std::memcpy(data_, wsrep_uuid.data, sizeof(data_));
38+
std::memcpy(data_.buf, wsrep_uuid.data, sizeof(data_.buf));
3739
}
3840
else if (str.size() <= 16)
3941
{
40-
std::memcpy(data_, str.c_str(), str.size());
42+
std::memcpy(data_.buf, str.c_str(), str.size());
4143
}
4244
else
4345
{
@@ -58,14 +60,14 @@ std::ostream& wsrep::operator<<(std::ostream& os, const wsrep::id& id)
5860
}
5961
else
6062
{
61-
char uuid_str[WSREP_UUID_STR_LEN + 1];
62-
wsrep_uuid_t uuid;
63+
char uuid_str[WSREP_LIB_UUID_STR_LEN + 1];
64+
wsrep::uuid_t uuid;
6365
std::memcpy(uuid.data, ptr, sizeof(uuid.data));
64-
if (wsrep_uuid_print(&uuid, uuid_str, sizeof(uuid_str)) < 0)
66+
if (wsrep::uuid_print(&uuid, uuid_str, sizeof(uuid_str)) < 0)
6567
{
6668
throw wsrep::runtime_error("Could not print uuid");
6769
}
68-
uuid_str[WSREP_UUID_STR_LEN] = '\0';
70+
uuid_str[WSREP_LIB_UUID_STR_LEN] = '\0';
6971
return (os << uuid_str);
7072
}
7173
}

0 commit comments

Comments
 (0)