Avoid including protobuf & grpc headers in our interface files.

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2021-05-27 22:10:24 +08:00
parent 0b9a4f36ce
commit e80709418b
8 changed files with 124 additions and 18 deletions

View File

@ -88,12 +88,9 @@ endif()
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
enable_testing()
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(tst)
if (BUILD_ETCD_TESTS)
enable_testing()
add_subdirectory(tst)
endif ()
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/etcd/Client.hpp install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/etcd/Client.hpp
${CMAKE_CURRENT_SOURCE_DIR}/etcd/KeepAlive.hpp ${CMAKE_CURRENT_SOURCE_DIR}/etcd/KeepAlive.hpp

View File

@ -5,7 +5,6 @@
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <grpc++/grpc++.h>
#include "pplx/pplxtasks.h" #include "pplx/pplxtasks.h"
#include "etcd/Response.hpp" #include "etcd/Response.hpp"
@ -18,6 +17,10 @@ namespace etcdv3 {
} }
} }
namespace grpc {
class Channel;
}
namespace etcd namespace etcd
{ {
class KeepAlive; class KeepAlive;

View File

@ -8,7 +8,6 @@
#include "pplx/pplxtasks.h" #include "pplx/pplxtasks.h"
#include "etcd/Value.hpp" #include "etcd/Value.hpp"
#include "kv.pb.h"
namespace etcdv3 { namespace etcdv3 {
class AsyncWatchAction; class AsyncWatchAction;
@ -138,7 +137,7 @@ namespace etcd
/** /**
* Returns the watched events. * Returns the watched events.
*/ */
std::vector<mvccpb::Event> const & events() const; std::vector<Event> const & events() const;
/** /**
* Returns the duration of request execution in microseconds. * Returns the duration of request execution in microseconds.
@ -158,7 +157,7 @@ namespace etcd
Values _values; Values _values;
Keys _keys; Keys _keys;
std::string _lock_key; // for lock std::string _lock_key; // for lock
std::vector<mvccpb::Event> _events; // for watch std::vector<Event> _events; // for watch
std::chrono::microseconds _duration; // execute duration (in microseconds), during the action created and response parsed std::chrono::microseconds _duration; // execute duration (in microseconds), during the action created and response parsed
friend class SyncClient; friend class SyncClient;
friend class etcdv3::AsyncWatchAction; friend class etcdv3::AsyncWatchAction;

View File

@ -8,8 +8,17 @@ namespace etcdv3 {
class KeyValue; class KeyValue;
} }
namespace mvccpb {
class KeyValue;
class Event;
}
namespace etcd namespace etcd
{ {
class Value;
class Event;
class Response;
/** /**
* Represents a value object received from the etcd server * Represents a value object received from the etcd server
*/ */
@ -54,8 +63,12 @@ namespace etcd
friend class BaseResponse; //deliberately done since Value class will be removed during full V3 friend class BaseResponse; //deliberately done since Value class will be removed during full V3
friend class DeleteRpcResponse; friend class DeleteRpcResponse;
friend class AsyncDeleteResponse; friend class AsyncDeleteResponse;
friend class Event;
Value(); Value();
Value(etcdv3::KeyValue const & kvs); Value(etcdv3::KeyValue const & kvs);
Value(mvccpb::KeyValue const & kvs);
std::string _key; std::string _key;
bool dir; bool dir;
std::string value; std::string value;
@ -66,6 +79,38 @@ namespace etcd
}; };
typedef std::vector<Value> Values; typedef std::vector<Value> Values;
class Event
{
public:
enum class EventType {
PUT,
DELETE_,
INVALID,
};
enum EventType event_type() const;
bool has_kv() const;
bool has_prev_kv() const;
const Value &kv() const;
const Value &prev_kv() const;
protected:
friend class Response;
Event(mvccpb::Event const & event);
private:
enum EventType event_type_;
Value _kv, _prev_kv;
bool _has_kv, _has_prev_kv;
};
typedef std::vector<Event> Events;
} }
#endif #endif

View File

@ -3,7 +3,6 @@
#include <iostream> #include <iostream>
etcd::Response::Response(const etcdv3::V3Response& reply, std::chrono::microseconds const& duration) etcd::Response::Response(const etcdv3::V3Response& reply, std::chrono::microseconds const& duration)
{ {
_index = reply.get_index(); _index = reply.get_index();
@ -27,7 +26,9 @@ etcd::Response::Response(const etcdv3::V3Response& reply, std::chrono::microseco
_prev_value = Value(reply.get_prev_value()); _prev_value = Value(reply.get_prev_value());
_lock_key = reply.get_lock_key(); _lock_key = reply.get_lock_key();
_events = reply.get_events(); for (auto const &ev: reply.get_events()) {
_events.emplace_back(etcd::Event(ev));
}
// duration // duration
_duration = duration; _duration = duration;
@ -111,7 +112,7 @@ std::string const & etcd::Response::lock_key() const {
return _lock_key; return _lock_key;
} }
std::vector<mvccpb::Event> const & etcd::Response::events() const { std::vector<etcd::Event> const & etcd::Response::events() const {
return this->_events; return this->_events;
} }

View File

@ -15,15 +15,26 @@ etcd::Value::Value()
etcd::Value::Value(etcdv3::KeyValue const & kv) etcd::Value::Value(etcdv3::KeyValue const & kv)
{ {
dir=false; dir = false;
_key=kv.kvs.key(); _key = kv.kvs.key();
value=kv.kvs.value(); value = kv.kvs.value();
created=kv.kvs.create_revision(); created = kv.kvs.create_revision();
modified=kv.kvs.mod_revision(); modified = kv.kvs.mod_revision();
leaseId = kv.kvs.lease(); leaseId = kv.kvs.lease();
_ttl = kv.get_ttl(); _ttl = kv.get_ttl();
} }
etcd::Value::Value(mvccpb::KeyValue const & kv)
{
dir = false;
_key = kv.key();
value = kv.value();
created = kv.create_revision();
modified = kv.mod_revision();
leaseId = kv.lease();
_ttl = -1;
}
std::string const & etcd::Value::key() const std::string const & etcd::Value::key() const
{ {
return _key; return _key;
@ -58,3 +69,41 @@ int64_t etcd::Value::lease() const
{ {
return leaseId; return leaseId;
} }
etcd::Event::Event(mvccpb::Event const & event) {
_has_kv = event.has_kv();
_has_prev_kv = event.has_prev_kv();
if (_has_kv) {
_kv = Value(event.kv());
}
if (_has_prev_kv) {
_prev_kv = Value(event.prev_kv());
}
if (event.type() == mvccpb::Event::PUT) {
event_type_ = EventType::PUT;
} else if (event.type() == mvccpb::Event::DELETE_) {
event_type_ = EventType::DELETE_;
} else {
event_type_ = EventType::INVALID;
}
}
enum etcd::Event::EventType etcd::Event::event_type() const {
return event_type_;
}
bool etcd::Event::has_kv() const {
return _has_kv;
}
bool etcd::Event::has_prev_kv() const {
return _has_prev_kv;
}
const etcd::Value &etcd::Event::kv() const {
return _kv;
}
const etcd::Value &etcd::Event::prev_kv() const {
return _prev_kv;
}

View File

@ -10,7 +10,11 @@ foreach(testfile ${TEST_FILES})
string(REGEX MATCH "^(.*)\\.[^.]*$" dummy ${testfile}) string(REGEX MATCH "^(.*)\\.[^.]*$" dummy ${testfile})
set(test_name ${CMAKE_MATCH_1}) set(test_name ${CMAKE_MATCH_1})
message(STATUS "Found unit_test - " ${test_name}) message(STATUS "Found unit_test - " ${test_name})
add_executable(${test_name} ${CMAKE_CURRENT_SOURCE_DIR}/${testfile}) if(BUILD_ETCD_TESTS)
add_executable(${test_name} ${CMAKE_CURRENT_SOURCE_DIR}/${testfile})
else()
add_executable(${test_name} EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/${testfile})
endif()
add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>) add_test(NAME ${test_name} COMMAND $<TARGET_FILE:${test_name}>)
set_property(TARGET ${test_name} PROPERTY CXX_STANDARD 11) set_property(TARGET ${test_name} PROPERTY CXX_STANDARD 11)

View File

@ -21,6 +21,14 @@ void printResponse(etcd::Response const & resp)
{ {
std::cout << resp.action() << " " << resp.value().as_string() << std::endl; std::cout << resp.action() << " " << resp.value().as_string() << std::endl;
std::cout << "Previous value: " << resp.prev_value().as_string() << std::endl; std::cout << "Previous value: " << resp.prev_value().as_string() << std::endl;
std::cout << "Events size: " << resp.events().size() << std::endl;
for (auto const &ev: resp.events()) {
std::cout << "Value change in events: " << static_cast<int>(ev.event_type())
<< ", prev kv = " << ev.prev_kv().key() << " -> " << ev.prev_kv().as_string()
<< ", kv = " << ev.kv().key() << " -> " << ev.kv().as_string()
<< std::endl;
}
} }
} }