#ifndef LIBDGRID_H
|
#define LIBDGRID_H
|
|
#include <string>
|
#include <vector>
|
#include <map>
|
#include <memory>
|
#include <vector>
|
#include <algorithm>
|
#include <functional>
|
#include <cstdint>
|
|
namespace DGrid {
|
|
typedef std::map<std::string, std::string> stringmap;
|
|
#define SELECT_TIMEOUT_SEC 1
|
#define SELECT_TIMEOUT_USEC 0
|
|
uint32_t maxPayloadSize();
|
|
typedef std::vector<char> NodeID;
|
typedef std::vector<char> TableID;
|
typedef uint64_t RecordID;
|
|
inline bool operator!(const TableID& id) { return id.empty(); }
|
|
enum AuthType {
|
AUTH_UNKNOWN = 0,
|
AUTH_NONE = 1,
|
AUTH_KEY = 2,
|
AUTH_USER = 3
|
};
|
AuthType int2authType(const uint8_t);
|
const std::string authType2string(const uint8_t);
|
|
enum AuthState {
|
AUTH_STATE_NONE = 0,
|
AUTH_STATE_START = 1,
|
AUTH_STATE_FAILED = 2,
|
AUTH_STATE_OK = 3
|
};
|
|
enum TableScope {
|
TABLE_GLOBAL = 0,
|
TABLE_LOCAL = 1
|
};
|
TableScope int2tableScope(const uint8_t);
|
const std::string tableScope2string(const uint8_t);
|
|
enum TableType {
|
TABLE_TYPE_INVALID = 0,
|
TABLE_STANDARD = 1,
|
TABLE_HASHED = 2,
|
TABLE_SORTED = 3,
|
TABLE_SORTED_UNIQ = 4
|
};
|
TableType int2tableType(const uint8_t);
|
TableType string2tableType(const std::string&);
|
const std::string tableType2string(const uint8_t);
|
|
enum KeyFlags {
|
KEY_FLAG_UNIQUE = 0x0001,
|
KEY_FLAG_HASHED = 0x0002
|
};
|
|
enum FieldType {
|
FIELD_TYPE_RAW = 0,
|
FIELD_TYPE_INTEGER = 1,
|
FIELD_TYPE_FLOAT = 2,
|
FIELD_TYPE_NUMERIC = 3,
|
FIELD_TYPE_PACKED = 4
|
};
|
FieldType int2fieldType(const uint8_t);
|
const std::string fieldType2string(const uint8_t);
|
|
enum SortFlag {
|
SORT_FLAG_DESC = 0x01,
|
SORT_FLAG_STABLE = 0x02,
|
SORT_FLAG_ASTEXT = 0x04
|
};
|
|
enum SortOrder {
|
SORT_ASC = 0,
|
SORT_DESC = SORT_FLAG_DESC
|
};
|
|
SortOrder int2sortOrder(const uint8_t);
|
const std::string sortOrder2string(const uint8_t);
|
|
struct SortOrderItem {
|
std::string field;
|
uint16_t flags;
|
};
|
using SortOrderList = std::vector<SortOrderItem>;
|
|
#define DEFAULT_DGRIDD_LOCAL_SOCKET "/var/run/dgridd.sock"
|
#define DEFAULT_DGRID_SERVER_PORT 4030
|
#define DEFAULT_DGRID_NODE_PORT 4040
|
#define DEFAULT_DGRID_TIMEOUT 60000 // in msecs
|
#define MAX_SOCKET_PATH_LEN 108
|
//#define DEBUG_TRANSPORT
|
|
enum DGridEventType {
|
ConnectionEstablished = 1,
|
ConnectionFailed = 2,
|
ConnectionClosed = 3,
|
ConnectionBroken = 4
|
};
|
std::string eventType2string(DGridEventType);
|
|
class DGridEvent {
|
public:
|
DGridEvent() = delete;
|
DGridEvent(const DGridEvent&) = delete;
|
DGridEvent(void* sender, DGridEventType type, std::string message);
|
~DGridEvent();
|
|
void* sender;
|
DGridEventType type;
|
std::string message;
|
};
|
typedef std::function<void(std::shared_ptr<DGridEvent>)> DGridEventHandler;
|
|
std::string strtrim(const std::string& str);
|
std::wstring strtrim(const std::wstring& str);
|
|
template<typename T>
|
std::basic_string<T> strtrim(const std::basic_string<T>& str) {
|
size_t first = str.find_first_not_of(' ');
|
if (std::basic_string<T>::npos == first) {
|
return str;
|
}
|
size_t last = str.find_last_not_of(' ');
|
return str.substr(first, (last - first + 1));
|
}
|
|
template<class T>
|
T to_lower_case(const T&v)
|
{
|
T str = v;
|
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
return str;
|
}
|
|
template<class T>
|
T to_upper_case(const T &v)
|
{
|
T str = v;
|
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
|
return str;
|
}
|
|
|
std::string raw_to_hexstring(const std::vector<char> &v);
|
std::vector<char> hexstring_to_raw(const std::string &v);
|
uint64_t getCurrentTime();
|
|
enum DGridResult {
|
DGRID_UNKNOWN = 0,
|
DGRID_OK = 1,
|
DGRID_FAIL = 2,
|
DGRID_NOT_FOUND = 3,
|
DGRID_NOT_IMPLEMENTED = 4,
|
DGRID_NO_CONN = 5,
|
DGRID_TIMEOUT = 6,
|
DGRID_SEND_ERROR = 7,
|
DGRID_INVALID_ARGUMENT = 8,
|
DGRID_TOO_OLD = 9,
|
DGRID_NOT_ENOUGH_NODES = 10,
|
DGRID_ALREADY_EXISTS = 11,
|
DGRID_OUT_OF_RANGE = 12,
|
DGRID_NOT_FOUND_INDEX_SUGGESTED = 13,
|
DGRID_NOT_AUTHORIZED = 14
|
};
|
DGridResult int2resultCode(const uint8_t);
|
std::string DGridResultString(DGridResult r);
|
|
// common packet header structure
|
struct Network_packet_header {
|
uint8_t magic[2]; // uint16_t big-endian
|
uint8_t type;
|
uint32_t dlen; // uint32_t big-endian
|
uint16_t crc; // uint16_t big-endian
|
} __attribute__ (( __packed__ ));
|
static const uint16_t Network_packet_header_size = sizeof(Network_packet_header);
|
|
// common packet structure
|
struct Network_packet {
|
Network_packet_header header;
|
char data[];
|
} __attribute__ (( __packed__ ));
|
|
// echo packet structure
|
struct Network_pkt_echo {
|
uint8_t seq;
|
} __attribute__ (( __packed__ ));
|
|
// ACK packet structure
|
struct Network_pkt_acknowledge {
|
uint32_t req_id; // request ID, uint32_t big-endian
|
uint8_t req_type; // request type
|
uint8_t req_subtype;
|
uint8_t result;
|
char data[];
|
} __attribute__ (( __packed__ ));
|
static const uint16_t Network_pkt_acknowledge_size = sizeof(Network_pkt_acknowledge);
|
|
// common request packet structure
|
struct Network_pkt_request {
|
uint32_t id; // uint32_t big-endian
|
uint8_t type;
|
uint8_t subtype;
|
char data[];
|
} __attribute__ (( __packed__ ));
|
static const uint16_t Network_pkt_request_size = sizeof(Network_pkt_request);
|
|
// packet types
|
enum PacketType {
|
PKT_INVALID = 0,
|
PKT_RESET = 1,
|
PKT_AUTH = 2,
|
PKT_AUTH_RPL = 3,
|
PKT_ECHO = 4,
|
PKT_ECHO_RPL = 5,
|
PKT_REQ = 6,
|
PKT_ACK = 7
|
};
|
PacketType int2packetType(const uint8_t);
|
const std::string packetType2string(const uint8_t);
|
|
// request types
|
enum RequestType {
|
REQ_NONE = 0x00,
|
|
REQ_KEY_VALUE = 0x01,
|
REQ_TABLE_CURSOR = 0x05,
|
REQ_TABLE = 0x06,
|
REQ_TABLE_MULTI = 0x07,
|
REQ_CLEAR_SESSION = 0x08,
|
REQ_MIGRATION = 0x09,
|
REQ_STATS = 0x0A,
|
REQ_STATS_KV = 0x0B,
|
REQ_STATS_TABLE = 0x0C,
|
REQ_TABLE_CREATE = 0x10,
|
REQ_USER = 0x11,
|
|
REQ_NODE_GET_LOAD = 0x82,
|
REQ_NODE_GET_THEME_SIMPLE = 0x83,
|
REQ_NODE_APPEND_THEME_SIMPLE = 0x84,
|
REQ_NODE_SYNC_THEME_SIMPLE = 0x85,
|
REQ_NODE_TABLE_CHECK = 0x86,
|
REQ_NODE_PASSTHRU_TABLE_CREATE = 0x87,
|
REQ_NODE_PASSTHRU_TABLE = 0x88,
|
REQ_NODE_PASSTHRU_MIGRATION = 0x89,
|
REQ_NODE_PASSTHRU_TABLE_CURSOR = 0x8A,
|
REQ_NODE_PASSTHRU_TABLE_MULTI = 0x90,
|
|
REQ_INVALID = 0xFF
|
};
|
RequestType int2requestType(const uint8_t);
|
const std::string requestType2string(const uint8_t);
|
|
// request subtypes
|
enum RequestSubType {
|
REQ_ST_NONE = 0x00,
|
REQ_ST_GET = 0x01,
|
REQ_ST_SET = 0x02,
|
REQ_ST_DEL = 0x03,
|
REQ_ST_CREATE = 0x04,
|
REQ_ST_LIST = 0x05,
|
REQ_ST_COUNT = 0x06,
|
REQ_ST_DROP = 0x07,
|
REQ_ST_APPEND = 0x08,
|
REQ_ST_INSERT = 0x09,
|
REQ_ST_CLEAR = 0x0A,
|
REQ_ST_GET_ALL = 0x0B,
|
REQ_ST_GET_STRUCT = 0x0C,
|
REQ_ST_GET_NEXT = 0x0D,
|
REQ_ST_LIST_SESSIONS = 0x0E,
|
REQ_ST_GET_ID = 0x0F,
|
REQ_ST_DEL_ADJ_DUPES = 0x10,
|
REQ_ST_SORT = 0x11,
|
REQ_ST_CREATE_CURSOR = 0x12,
|
REQ_ST_SECKEY_RESET = 0x13,
|
REQ_ST_SECKEY_REBUILD = 0x14,
|
REQ_ST_INVALID = 0xFF
|
};
|
|
RequestSubType int2requestSubType(const uint8_t);
|
const std::string requestSubType2string(const uint8_t);
|
|
bool isTableModifyRequest(const RequestSubType req_subtype);
|
|
|
enum BooleanOperator {
|
BOOL_AND = 0,
|
BOOL_OR = 1,
|
BOOL_EQUAL = 2,
|
BOOL_NOT = 3
|
};
|
BooleanOperator int2booleanOperator(const uint8_t);
|
const std::string booleanOperator2string(const uint8_t);
|
|
enum ComparisionType {
|
COMPARE_EQ = 0,
|
COMPARE_NE = 1,
|
COMPARE_LT = 2,
|
COMPARE_LE = 3,
|
COMPARE_GT = 4,
|
COMPARE_GE = 5
|
};
|
|
ComparisionType int2comparisionType(const uint8_t);
|
const std::string comparisionType2string(const uint8_t);
|
|
struct ACK_data {
|
ACK_data() : result(DGRID_UNKNOWN), req_id(0) {}
|
ACK_data(uint32_t i) : result(DGRID_UNKNOWN), req_id(i) {}
|
ACK_data(DGridResult t, uint32_t i) : result(t), req_id(i) {}
|
DGridResult result;
|
RequestType req_type;
|
RequestSubType req_subtype;
|
uint32_t req_id;
|
std::vector<char> payload;
|
std::string err_details;
|
};
|
|
// packet send/receive methods
|
int32_t sendPacket(int sockfd, const PacketType type, const std::vector<char>& data);
|
int32_t sendPacket(int sockfd, const PacketType type, const char* data = NULL, const uint32_t dlen = 0);
|
int32_t recvPacket(int sockfd, PacketType& type, std::vector<char>& data, bool& crc_ok);
|
|
bool sendReset(int sockfd);
|
bool sendRequest(int sockfd, const uint32_t req_id, const RequestType type, const RequestSubType subtype, const char* data = NULL, const uint32_t dlen = 0);
|
bool sendAck(int sockfd, const Network_pkt_request* req, const DGridResult type, const char* data = NULL, const uint32_t dlen = 0);
|
|
} // namespace DGrid
|
|
#endif // LIBDGRID_H
|