ClickHouse/libs/libzkutil/include/zkutil/Increment.h

49 lines
813 B
C
Raw Normal View History

#pragma once
#include <zkutil/ZooKeeper.h>
2015-04-22 17:56:27 +00:00
namespace zkutil
{
class Increment
{
public:
2015-04-22 17:56:27 +00:00
Increment(ZooKeeperPtr zk_, const std::string & path_)
: zk(zk_), path(path_)
{
zk->createAncestors(path);
}
size_t get()
{
LOG_TRACE(log, "Get increment");
size_t result = 0;
std::string result_str;
zkutil::Stat stat;
bool success = false;
do
{
if (zk->tryGet(path, result_str, &stat))
{
result = std::stol(result_str) + 1;
success = zk->trySet(path, std::to_string(result), stat.version) == ZOK;
}
else
{
success = zk->tryCreate(path, std::to_string(result), zkutil::CreateMode::Persistent) == ZOK;
}
2015-04-22 17:56:27 +00:00
}
while (!success);
return result;
}
private:
zkutil::ZooKeeperPtr zk;
std::string path;
Logger * log = &Logger::get("zkutil::Increment");
};
2015-04-22 17:56:27 +00:00
}