1
0
mirror of https://github.com/codership/wsrep-lib.git synced 2025-07-28 20:02:00 +03:00

Refactored seqno and id classes out of provider.hpp

This commit is contained in:
Teemu Ollakka
2018-06-12 10:20:58 +03:00
parent f506faa360
commit 97d9f93648
14 changed files with 242 additions and 150 deletions

75
include/wsrep/id.hpp Normal file
View File

@ -0,0 +1,75 @@
//
// Copyright (C) 2018 Codership Oy <info@codership.com>
//
/*! \file id.hpp
*
* A generic identifier utility class.
*/
#ifndef WSREP_ID_HPP
#define WSREP_ID_HPP
#include "exception.hpp"
#include <iosfwd>
#include <cstring> // std::memset()
namespace wsrep
{
/*!
* The idientifier class stores identifiers either in UUID
* format or in string format. The storage format is decided
* upon construction. If the given string contains a valid
* UUID, the storage format will be binary. Otherwise the
* string will be copied verbatim. If the string format is used,
* the maximum length of the identifier is limited to 16 bytes.
*/
class id
{
public:
/*!
* Default constructor. Constructs an empty identifier.
*/
id() : data_() { std::memset(data_, 0, sizeof(data_)); }
/*!
* Construct from string. The input string may contain either
* valid UUID or a string with maximum 16 bytes length.
*/
id(const std::string&);
/*!
* Construct from void pointer.
*/
id (const void* data, size_t size) : data_()
{
if (size > 16)
{
throw wsrep::runtime_error("Too long identifier");
}
std::memset(data_, 0, sizeof(data_));
std::memcpy(data_, data, size);
}
bool operator<(const id& other) const
{
return (std::memcmp(data_, other.data_, sizeof(data_)) < 0);
}
bool operator==(const id& other) const
{
return (std::memcmp(data_, other.data_, sizeof(data_)) == 0);
}
const void* data() const { return data_; }
size_t size() const { return sizeof(data_); }
private:
unsigned char data_[16];
};
std::ostream& operator<<(std::ostream&, const wsrep::id& id);
}
#endif // WSREP_ID_HPP

View File

@ -5,6 +5,8 @@
#ifndef WSREP_PROVIDER_HPP
#define WSREP_PROVIDER_HPP
#include "id.hpp"
#include "seqno.hpp"
#include "key.hpp"
#include "data.hpp"
#include "client_id.hpp"
@ -20,102 +22,7 @@
namespace wsrep
{
/*! \class seqno
*
* Sequence number type.
*
* By convention, nil value is zero, negative values are not allowed.
* Relation operators are restricted to < and > on purpose to
* enforce correct use.
*/
class seqno
{
public:
seqno()
: seqno_()
{ }
template <typename I>
seqno(I seqno)
: seqno_(seqno)
{
assert(seqno_ >= 0);
}
long long get() const
{
return seqno_;
}
bool nil() const
{
return (seqno_ == 0);
}
bool operator<(seqno other) const
{
return (seqno_ < other.seqno_);
}
bool operator>(seqno other) const
{
return (seqno_ > other.seqno_);
}
seqno operator+(seqno other) const
{
return (seqno_ + other.seqno_);
}
static seqno undefined() { return 0; }
private:
long long seqno_;
};
static inline std::ostream& operator<<(std::ostream& os, wsrep::seqno seqno)
{
return (os << seqno.get());
}
class id
{
public:
enum type
{
none,
string,
uuid
};
id()
: type_(none)
, data_()
{ }
id(const std::string&);
id (const void* data, size_t data_size)
: type_()
, data_()
{
assert(data_size <= 16);
std::memcpy(data_, data, data_size);
}
bool operator<(const id& other) const
{
return (std::memcmp(data_, other.data_, sizeof(data_)) < 0);
}
bool operator==(const id& other) const
{
return (std::memcmp(data_, other.data_, sizeof(data_)) == 0);
}
const unsigned char* data() const { return data_; }
size_t data_size() const { return 16; }
private:
enum type type_;
unsigned char data_[16];
};
class server_context;
class gtid
{
@ -242,7 +149,6 @@ namespace wsrep
int flags_;
};
// Abstract interface for provider implementations
class provider
{
@ -308,6 +214,9 @@ namespace wsrep
static const int snapshot = (1 << 7);
};
provider(wsrep::server_context& server_context)
: server_context_(server_context)
{ }
virtual ~provider() { }
// Provider state management
virtual int connect(const std::string& cluster_name,
@ -368,6 +277,8 @@ namespace wsrep
// virtual struct wsrep* native() = 0;
// Factory method
static provider* make_provider(const std::string& provider);
private:
wsrep::server_context& server_context_;
};
static inline std::string flags_to_string(int flags)

74
include/wsrep/seqno.hpp Normal file
View File

@ -0,0 +1,74 @@
//
// Copyright (C) 2018 Codership Oy <info@codership.com>
//
#ifndef WSREP_SEQNO_HPP
#define WSREP_SEQNO_HPP
#include "exception.hpp"
#include <ostream>
namespace wsrep
{
/*! \class seqno
*
* Sequence number type.
*
* By convention, nil value is zero, negative values are not allowed.
* Relation operators are restricted to < and > on purpose to
* enforce correct use.
*/
class seqno
{
public:
seqno()
: seqno_()
{ }
explicit seqno(long long seqno)
: seqno_(seqno)
{
if (seqno_ < 0)
{
throw wsrep::runtime_error("Negative seqno given");
}
}
long long get() const
{
return seqno_;
}
bool nil() const
{
return (seqno_ == 0);
}
bool operator<(seqno other) const
{
return (seqno_ < other.seqno_);
}
bool operator>(seqno other) const
{
return (seqno_ > other.seqno_);
}
seqno operator+(seqno other) const
{
return (seqno(seqno_ + other.seqno_));
}
static seqno undefined() { return seqno(0); }
private:
long long seqno_;
};
static inline std::ostream& operator<<(std::ostream& os, wsrep::seqno seqno)
{
return (os << seqno.get());
}
}
#endif // WSREP_SEQNO_HPP