2014-03-07 19:18:48 +00:00
|
|
|
#pragma once
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/Common.h>
|
2014-03-07 19:18:48 +00:00
|
|
|
#include <future>
|
2016-05-28 17:31:50 +00:00
|
|
|
#include <memory>
|
2016-12-12 04:57:09 +00:00
|
|
|
#include <vector>
|
2014-06-04 13:48:36 +00:00
|
|
|
#include <zookeeper/zookeeper.h>
|
2014-06-30 11:33:06 +00:00
|
|
|
#include <Poco/Event.h>
|
2014-03-07 19:18:48 +00:00
|
|
|
|
2016-12-12 04:57:09 +00:00
|
|
|
|
2014-03-07 19:18:48 +00:00
|
|
|
namespace zkutil
|
|
|
|
{
|
2016-12-12 04:57:09 +00:00
|
|
|
|
2016-05-28 10:35:44 +00:00
|
|
|
using ACLPtr = const ACL_vector *;
|
|
|
|
using Stat = Stat;
|
2014-06-04 13:48:36 +00:00
|
|
|
|
|
|
|
struct Op
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
Op() : data(new zoo_op_t) {}
|
|
|
|
virtual ~Op() {}
|
2014-06-04 13:48:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
virtual std::string describe() = 0;
|
2016-06-03 10:48:21 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::unique_ptr<zoo_op_t> data;
|
2014-06-04 13:48:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
struct Remove;
|
|
|
|
struct Create;
|
|
|
|
struct SetData;
|
|
|
|
struct Check;
|
2014-06-04 13:48:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Op::Remove : public Op
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Remove(const std::string & path_, int32_t version) :
|
|
|
|
path(path_)
|
|
|
|
{
|
|
|
|
zoo_delete_op_init(data.get(), path.c_str(), version);
|
|
|
|
}
|
2014-06-04 13:48:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string describe() override { return "command: remove, path: " + path; }
|
2016-06-03 16:12:51 +00:00
|
|
|
|
2014-06-04 13:48:36 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string path;
|
2014-06-04 13:48:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Op::Create : public Op
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Create(const std::string & path_, const std::string & value_, ACLPtr acl, int32_t flags);
|
2014-06-04 13:48:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string getPathCreated()
|
|
|
|
{
|
|
|
|
return created_path.data();
|
|
|
|
}
|
2014-06-04 13:48:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string describe() override
|
|
|
|
{
|
|
|
|
return "command: create"
|
|
|
|
", path: " + path +
|
|
|
|
", value: " + value;
|
|
|
|
}
|
2016-06-03 10:48:21 +00:00
|
|
|
|
2014-06-04 13:48:36 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string path;
|
|
|
|
std::string value;
|
|
|
|
std::vector<char> created_path;
|
2014-06-04 13:48:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Op::SetData : public Op
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
SetData(const std::string & path_, const std::string & value_, int32_t version) :
|
|
|
|
path(path_), value(value_)
|
|
|
|
{
|
|
|
|
zoo_set_op_init(data.get(), path.c_str(), value.c_str(), value.size(), version, &stat);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string describe() override
|
|
|
|
{
|
|
|
|
return
|
|
|
|
"command: set"
|
|
|
|
", path: " + path +
|
|
|
|
", value: " + value +
|
|
|
|
", version: " + std::to_string(data->set_op.version);
|
|
|
|
}
|
2016-06-03 10:48:21 +00:00
|
|
|
|
2014-06-04 13:48:36 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string path;
|
|
|
|
std::string value;
|
|
|
|
Stat stat;
|
2014-06-04 13:48:36 +00:00
|
|
|
};
|
2014-03-07 19:18:48 +00:00
|
|
|
|
2014-06-04 13:48:36 +00:00
|
|
|
struct Op::Check : public Op
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Check(const std::string & path_, int32_t version) :
|
|
|
|
path(path_)
|
|
|
|
{
|
|
|
|
zoo_check_op_init(data.get(), path.c_str(), version);
|
|
|
|
}
|
2016-06-03 10:48:21 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string describe() override { return "command: check, path: " + path; }
|
2016-12-12 03:33:34 +00:00
|
|
|
|
2014-06-04 13:48:36 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string path;
|
2014-06-04 13:48:36 +00:00
|
|
|
};
|
2014-03-07 19:18:48 +00:00
|
|
|
|
2015-01-21 03:56:28 +00:00
|
|
|
struct OpResult : public zoo_op_result_t
|
2014-06-04 13:48:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Указатели в этой структуре указывают на поля в классе Op.
|
|
|
|
/// Поэтому деструктор не нужен
|
2014-06-04 13:48:36 +00:00
|
|
|
};
|
2014-03-07 19:18:48 +00:00
|
|
|
|
2016-12-12 04:57:09 +00:00
|
|
|
using Ops = std::vector<std::unique_ptr<Op>>;
|
2016-05-28 10:35:44 +00:00
|
|
|
using OpResults = std::vector<OpResult>;
|
|
|
|
using OpResultsPtr = std::shared_ptr<OpResults>;
|
|
|
|
using Strings = std::vector<std::string>;
|
2014-03-07 19:18:48 +00:00
|
|
|
|
2014-06-04 13:48:36 +00:00
|
|
|
namespace CreateMode
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int Persistent;
|
|
|
|
extern const int Ephemeral;
|
|
|
|
extern const int EphemeralSequential;
|
|
|
|
extern const int PersistentSequential;
|
2014-06-04 13:48:36 +00:00
|
|
|
}
|
2014-03-07 19:18:48 +00:00
|
|
|
|
2016-05-28 17:31:50 +00:00
|
|
|
using EventPtr = std::shared_ptr<Poco::Event>;
|
2014-03-07 19:18:48 +00:00
|
|
|
|
2017-03-17 00:44:00 +00:00
|
|
|
class ZooKeeper;
|
|
|
|
|
|
|
|
/// Callback to call when the watch fires.
|
2017-03-16 22:39:52 +00:00
|
|
|
/// Because callbacks are called in the single "completion" thread internal to libzookeeper,
|
|
|
|
/// they must execute as quickly as possible (preferably just set some notification).
|
|
|
|
/// Parameters:
|
2017-03-17 00:44:00 +00:00
|
|
|
/// zookeeper - zookeeper session to which the fired watch belongs
|
|
|
|
/// type - event type, one of the *_EVENT constants from zookeeper.h
|
|
|
|
/// state - session connection state, one of the *_STATE constants from zookeeper.h
|
|
|
|
/// path - znode path to which the change happened. if event == ZOO_SESSION_EVENT it is either NULL or empty string.
|
|
|
|
using WatchCallback = std::function<void(ZooKeeper & zookeeper, int type, int state, const char * path)>;
|
|
|
|
|
2014-03-07 19:18:48 +00:00
|
|
|
}
|