Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wsrep api cleanup #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions include/wsrep/client_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ namespace wsrep

/**
* Append a key into transaction write set.
*
* @param key Key to be appended
*
* @return Zero on success, non-zero on failure.
*/
int append_key(const wsrep::key& key)
{
Expand All @@ -315,6 +319,27 @@ namespace wsrep
return transaction_.append_key(key);
}

/**
* Append keys in key_array into transaction write set.
*
* @param keys Array of keys to be appended
*
* @return Zero in case of success, non-zero on failure.
*/
int append_keys(const wsrep::key_array& keys)
{
assert(mode_ == m_local || mode_ == m_toi);
assert(state_ == s_exec);
for (auto i(keys.begin()); i != keys.end(); ++i)
{
if (transaction_.append_key(*i))
{
return 1;
}
}
return 0;
}

/**
* Append data into transaction write set.
*/
Expand Down
7 changes: 6 additions & 1 deletion include/wsrep/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@

/** @file compiler.hpp
*
* Compiler specific options.
* Compiler specific macro definitions.
*
* WSREP_OVERRIDE - Set to "override" if the compiler supports it, otherwise
* left empty.
* WSREP_CONSTEXPR_OR_INLINE - Set to "constexpr" if the compiler supports it,
* otherwise "inline".
*/

#define WSREP_UNUSED __attribute__((unused))
Expand Down
33 changes: 26 additions & 7 deletions include/wsrep/gtid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@

#include "id.hpp"
#include "seqno.hpp"
#include "compiler.hpp"

#include <iosfwd>

/**
* Minimum number of bytes guaratneed to store GTID string representation,
* terminating '\0' not included (36 + 1 + 20).
*/
#define WSREP_LIB_GTID_C_STR_LEN 57

namespace wsrep
{
class gtid
Expand Down Expand Up @@ -61,23 +68,35 @@ namespace wsrep
wsrep::seqno seqno_;
};

/**
* Scan a GTID from C string.
*
* @param buf Buffer containing the string
* @param len Length of buffer
* @param[out] gtid Gtid to be printed to
*
* @return Number of bytes scanned, negative value on error.
*/
ssize_t scan_from_c_str(const char* buf, size_t buf_len,
wsrep::gtid& gtid);

/**
* Print a GTID into character buffer.
* @param buf Pointer to the beginning of the buffer
* @param buf_len Buffer length
*
* @return Number of characters printed or negative value for error
*/
ssize_t gtid_print_to_c_str(const wsrep::gtid&, char* buf, size_t buf_len);
ssize_t print_to_c_str(const wsrep::gtid&, char* buf, size_t buf_len);

/**
* Return minimum number of bytes guaranteed to store GTID string
* representation, terminating '\0' not included (36 + 1 + 20)
* Overload for ostream operator<<.
*/
static inline size_t gtid_c_str_len()
{
return 57;
}
std::ostream& operator<<(std::ostream&, const wsrep::gtid&);

/**
* Overload for istream operator>>.
*/
std::istream& operator>>(std::istream&, wsrep::gtid&);
}

Expand Down
15 changes: 8 additions & 7 deletions include/wsrep/id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ namespace wsrep
class id
{
public:
typedef struct native_type { unsigned char buf[16]; } native_type;
/**
* Default constructor. Constructs an empty identifier.
*/
id() : data_() { std::memset(data_, 0, sizeof(data_)); }
id() : data_() { std::memset(data_.buf, 0, sizeof(data_.buf)); }

/**
* Construct from string. The input string may contain either
Expand All @@ -62,24 +63,24 @@ namespace wsrep
{
throw wsrep::runtime_error("Too long identifier");
}
std::memset(data_, 0, sizeof(data_));
std::memcpy(data_, data, size);
std::memset(data_.buf, 0, sizeof(data_.buf));
std::memcpy(data_.buf, data, size);
}

bool operator<(const id& other) const
{
return (std::memcmp(data_, other.data_, sizeof(data_)) < 0);
return (std::memcmp(data_.buf, other.data_.buf, sizeof(data_.buf)) < 0);
}

bool operator==(const id& other) const
{
return (std::memcmp(data_, other.data_, sizeof(data_)) == 0);
return (std::memcmp(data_.buf, other.data_.buf, sizeof(data_.buf)) == 0);
}
bool operator!=(const id& other) const
{
return !(*this == other);
}
const void* data() const { return data_; }
const void* data() const { return data_.buf; }

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

Expand All @@ -94,7 +95,7 @@ namespace wsrep
}
private:
static const wsrep::id undefined_;
unsigned char data_[16];
native_type data_;
};

std::ostream& operator<<(std::ostream&, const wsrep::id& id);
Expand Down
7 changes: 7 additions & 0 deletions include/wsrep/provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "buffer.hpp"
#include "client_id.hpp"
#include "transaction_id.hpp"
#include "compiler.hpp"

#include <cassert>
#include <cstring>
Expand All @@ -33,6 +34,12 @@
#include <vector>
#include <ostream>

/**
* Empty provider magic. If none provider is passed to make_provider(),
* a dummy provider is loaded.
*/
#define WSREP_LIB_PROVIDER_NONE "none"

namespace wsrep
{
class server_state;
Expand Down
7 changes: 4 additions & 3 deletions include/wsrep/seqno.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
#ifndef WSREP_SEQNO_HPP
#define WSREP_SEQNO_HPP

#include "exception.hpp"

#include <iosfwd>

namespace wsrep
Expand All @@ -33,6 +31,8 @@ namespace wsrep
class seqno
{
public:
typedef long long native_type;

seqno()
: seqno_(-1)
{ }
Expand Down Expand Up @@ -77,8 +77,9 @@ namespace wsrep
return (*this + seqno(other));
}
static seqno undefined() { return seqno(-1); }

private:
long long seqno_;
native_type seqno_;
};

std::ostream& operator<<(std::ostream& os, wsrep::seqno seqno);
Expand Down
8 changes: 7 additions & 1 deletion include/wsrep/server_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
#include <string>
#include <map>

/**
* Magic string to tell provider to engage into trivial (empty)
* state transfer. No data will be passed, but the node shall be
* considered joined.
*/
#define WSREP_LIB_SST_TRIVIAL "trivial"

namespace wsrep
{
// Forward declarations
Expand Down Expand Up @@ -179,7 +186,6 @@ namespace wsrep
rm_sync
};


virtual ~server_state();

wsrep::encryption_service* encryption_service()
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ add_library(wsrep-lib
server_state.cpp
thread.cpp
transaction.cpp
uuid.cpp
wsrep_provider_v26.cpp)
target_link_libraries(wsrep-lib wsrep_api_v26 pthread dl)
40 changes: 38 additions & 2 deletions src/gtid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,47 @@ std::istream& wsrep::operator>>(std::istream& is, wsrep::gtid& gtid)
std::getline(is, id_str, ':');
long long seq;
is >> seq;
gtid = wsrep::gtid(wsrep::id(id_str), wsrep::seqno(seq));
if (!is)
{
is.clear(std::ios_base::failbit);
return is;
}
try
{
// wsrep::id constructor will throw if it cannot parse the
// id_str.
gtid = wsrep::gtid(wsrep::id(id_str), wsrep::seqno(seq));
}
catch (const wsrep::runtime_error& e)
{
// Formatting or extraction error. Clear the istream state and
// set failibit.
is.clear(std::ios_base::failbit);
}
return is;
}

ssize_t wsrep::gtid_print_to_c_str(
ssize_t wsrep::scan_from_c_str(
const char* buf, size_t buf_len, wsrep::gtid& gtid)
{
std::istringstream is(std::string(buf, buf_len));
is >> gtid;
// Whole string was consumed without failures
if (is && is.eof())
{
return buf_len;
}
// Some failure occurred
if (!is)
{
return -EINVAL;
}
// The string was not consumed completely, return current position
// of the istream.
return is.tellg();
}

ssize_t wsrep::print_to_c_str(
const wsrep::gtid& gtid, char* buf, size_t buf_len)
{
std::ostringstream os;
Expand Down
22 changes: 12 additions & 10 deletions src/id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

#include "wsrep/id.hpp"
#include <wsrep_api.h>
#include "uuid.hpp"

#include <cctype>
#include <sstream>
Expand All @@ -29,15 +29,17 @@ const wsrep::id wsrep::id::undefined_ = wsrep::id();
wsrep::id::id(const std::string& str)
: data_()
{
wsrep_uuid_t wsrep_uuid;
if (wsrep_uuid_scan(str.c_str(), str.size(), &wsrep_uuid) ==
WSREP_UUID_STR_LEN)
wsrep::uuid_t wsrep_uuid;

if (str.size() == WSREP_LIB_UUID_STR_LEN &&
wsrep::uuid_scan(str.c_str(), str.size(), &wsrep_uuid) ==
WSREP_LIB_UUID_STR_LEN)
{
std::memcpy(data_, wsrep_uuid.data, sizeof(data_));
std::memcpy(data_.buf, wsrep_uuid.data, sizeof(data_.buf));
}
else if (str.size() <= 16)
{
std::memcpy(data_, str.c_str(), str.size());
std::memcpy(data_.buf, str.c_str(), str.size());
}
else
{
Expand All @@ -58,14 +60,14 @@ std::ostream& wsrep::operator<<(std::ostream& os, const wsrep::id& id)
}
else
{
char uuid_str[WSREP_UUID_STR_LEN + 1];
wsrep_uuid_t uuid;
char uuid_str[WSREP_LIB_UUID_STR_LEN + 1];
wsrep::uuid_t uuid;
std::memcpy(uuid.data, ptr, sizeof(uuid.data));
if (wsrep_uuid_print(&uuid, uuid_str, sizeof(uuid_str)) < 0)
if (wsrep::uuid_print(&uuid, uuid_str, sizeof(uuid_str)) < 0)
{
throw wsrep::runtime_error("Could not print uuid");
}
uuid_str[WSREP_UUID_STR_LEN] = '\0';
uuid_str[WSREP_LIB_UUID_STR_LEN] = '\0';
return (os << uuid_str);
}
}
Expand Down
Loading