2015-09-29 19:21:02 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <common/DateLUTImpl.h>
|
|
|
|
#include <common/singleton.h>
|
2016-11-15 10:11:05 +00:00
|
|
|
#include <DB/Core/Defines.h>
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <memory>
|
|
|
|
|
2017-01-22 09:27:51 +00:00
|
|
|
|
2016-11-13 19:34:31 +00:00
|
|
|
/// This class provides lazy initialization and lookup of singleton DateLUTImpl objects for a given timezone.
|
2015-09-29 19:21:02 +00:00
|
|
|
class DateLUT : public Singleton<DateLUT>
|
|
|
|
{
|
|
|
|
friend class Singleton<DateLUT>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DateLUT(const DateLUT &) = delete;
|
|
|
|
DateLUT & operator=(const DateLUT &) = delete;
|
|
|
|
|
2016-11-13 19:34:31 +00:00
|
|
|
/// Return singleton DateLUTImpl instance for the default time zone.
|
2016-11-15 10:11:05 +00:00
|
|
|
static ALWAYS_INLINE const DateLUTImpl & instance()
|
2015-09-29 19:21:02 +00:00
|
|
|
{
|
|
|
|
const auto & date_lut = Singleton<DateLUT>::instance();
|
2016-11-13 19:34:31 +00:00
|
|
|
return *date_lut.default_impl.load(std::memory_order_acquire);
|
2015-09-29 19:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 19:34:31 +00:00
|
|
|
/// Return singleton DateLUTImpl instance for a given time zone.
|
2016-11-15 10:11:05 +00:00
|
|
|
static ALWAYS_INLINE const DateLUTImpl & instance(const std::string & time_zone)
|
2015-09-29 19:21:02 +00:00
|
|
|
{
|
|
|
|
const auto & date_lut = Singleton<DateLUT>::instance();
|
2016-11-13 19:34:31 +00:00
|
|
|
if (time_zone.empty())
|
|
|
|
return *date_lut.default_impl.load(std::memory_order_acquire);
|
|
|
|
|
|
|
|
return date_lut.getImplementation(time_zone);
|
2015-09-29 19:21:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-13 19:34:31 +00:00
|
|
|
static void setDefaultTimezone(const std::string & time_zone)
|
|
|
|
{
|
|
|
|
auto & date_lut = Singleton<DateLUT>::instance();
|
|
|
|
const auto & impl = date_lut.getImplementation(time_zone);
|
|
|
|
date_lut.default_impl.store(&impl, std::memory_order_release);
|
|
|
|
}
|
2015-09-29 19:21:02 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
DateLUT();
|
|
|
|
|
|
|
|
private:
|
2017-01-22 09:27:51 +00:00
|
|
|
const DateLUTImpl & getImplementation(const std::string & time_zone) const;
|
2016-11-13 19:34:31 +00:00
|
|
|
|
|
|
|
using DateLUTImplPtr = std::unique_ptr<DateLUTImpl>;
|
|
|
|
|
2017-01-22 09:27:51 +00:00
|
|
|
/// Time zone name -> implementation.
|
|
|
|
mutable std::unordered_map<std::string, DateLUTImplPtr> impls;
|
|
|
|
mutable std::mutex mutex;
|
2016-11-13 19:34:31 +00:00
|
|
|
|
|
|
|
std::atomic<const DateLUTImpl *> default_impl;
|
2015-09-29 19:21:02 +00:00
|
|
|
};
|