DictionaryLifetime::DictionaryLifetime(const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix)
{
	const auto & lifetime_min_key = config_prefix + ".min";
	const auto has_min = config.has(lifetime_min_key);

	this->min_sec = has_min ? config.getInt(lifetime_min_key) : config.getInt(config_prefix);
	this->max_sec = has_min ? config.getInt(config_prefix + ".max") : this->min_sec;
}
Exemple #2
0
void QuotaForIntervals::initFromConfig(const String & config_elem, Poco::Util::AbstractConfiguration & config, pcg64 & rng)
{
    Poco::Util::AbstractConfiguration::Keys config_keys;
    config.keys(config_elem, config_keys);

    for (Poco::Util::AbstractConfiguration::Keys::const_iterator it = config_keys.begin(); it != config_keys.end(); ++it)
    {
        if (!startsWith(*it, "interval"))
            continue;

        String interval_config_elem = config_elem + "." + *it;
        time_t duration = config.getInt(interval_config_elem + ".duration", 0);
        time_t offset = 0;

        if (!duration) /// Skip quotas with zero duration
            continue;

        bool randomize = config.getBool(interval_config_elem + ".randomize", false);
        if (randomize)
            offset = std::uniform_int_distribution<decltype(duration)>(0, duration - 1)(rng);

        cont[duration].initFromConfig(interval_config_elem, duration, randomize, offset, config);
    }
}
Exemple #3
0
Pool::Pool(const Poco::Util::AbstractConfiguration & cfg, const std::string & config_name,
     unsigned default_connections_, unsigned max_connections_,
     const char * parent_config_name_)
    : default_connections(default_connections_), max_connections(max_connections_)
{
    server = cfg.getString(config_name + ".host");

    if (parent_config_name_)
    {
        const std::string parent_config_name(parent_config_name_);
        db = cfg.getString(config_name + ".db", cfg.getString(parent_config_name + ".db", ""));
        user = cfg.has(config_name + ".user")
            ? cfg.getString(config_name + ".user")
            : cfg.getString(parent_config_name + ".user");
        password = cfg.has(config_name + ".password")
            ? cfg.getString(config_name + ".password")
            : cfg.getString(parent_config_name + ".password");

        if (!cfg.has(config_name + ".port") && !cfg.has(config_name + ".socket")
            && !cfg.has(parent_config_name + ".port") && !cfg.has(parent_config_name + ".socket"))
            throw Poco::Exception("mysqlxx::Pool configuration: expected port or socket");

        port = cfg.has(config_name + ".port")
            ? cfg.getInt(config_name + ".port")
            : cfg.getInt(parent_config_name + ".port", 0);
        socket = cfg.has(config_name + ".socket")
            ? cfg.getString(config_name + ".socket")
            : cfg.getString(parent_config_name + ".socket", "");
        ssl_ca = cfg.has(config_name + ".ssl_ca")
            ? cfg.getString(config_name + ".ssl_ca")
            : cfg.getString(parent_config_name + ".ssl_ca", "");
        ssl_cert = cfg.has(config_name + ".ssl_cert")
            ? cfg.getString(config_name + ".ssl_cert")
            : cfg.getString(parent_config_name + ".ssl_cert", "");
        ssl_key = cfg.has(config_name + ".ssl_key")
            ? cfg.getString(config_name + ".ssl_key")
            : cfg.getString(parent_config_name + ".ssl_key", "");
    }
    else
    {
        db = cfg.getString(config_name + ".db", "");
        user = cfg.getString(config_name + ".user");
        password = cfg.getString(config_name + ".password");

        if (!cfg.has(config_name + ".port") && !cfg.has(config_name + ".socket"))
            throw Poco::Exception("mysqlxx::Pool configuration: expected port or socket");

        port = cfg.getInt(config_name + ".port", 0);
        socket = cfg.getString(config_name + ".socket", "");
        ssl_ca = cfg.getString(config_name + ".ssl_ca", "");
        ssl_cert = cfg.getString(config_name + ".ssl_cert", "");
        ssl_key = cfg.getString(config_name + ".ssl_key", "");
    }

    connect_timeout = cfg.getInt(config_name + ".connect_timeout",
        cfg.getInt("mysql_connect_timeout",
            MYSQLXX_DEFAULT_TIMEOUT));

    rw_timeout =
        cfg.getInt(config_name + ".rw_timeout",
            cfg.getInt("mysql_rw_timeout",
                MYSQLXX_DEFAULT_RW_TIMEOUT));
}
DictionaryPtr DictionaryFactory::create(const std::string & name, Poco::Util::AbstractConfiguration & config,
	const std::string & config_prefix, Context & context) const
{
	Poco::Util::AbstractConfiguration::Keys keys;
	const auto & layout_prefix = config_prefix + ".layout";
	config.keys(layout_prefix, keys);
	if (keys.size() != 1)
		throw Exception{name + ": element dictionary.layout should have exactly one child element",
			ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG};

	const DictionaryStructure dict_struct{config, config_prefix + ".structure"};

	auto source_ptr = DictionarySourceFactory::instance().create(
		name, config, config_prefix + ".source", dict_struct, context);

	const DictionaryLifetime dict_lifetime{config, config_prefix + ".lifetime"};

	const bool require_nonempty = config.getBool(config_prefix + ".require_nonempty", false);

	const auto & layout_type = keys.front();

	if ("range_hashed" == layout_type)
	{
		if (dict_struct.key)
			throw Exception{"'key' is not supported for dictionary of layout 'range_hashed'",
				ErrorCodes::UNSUPPORTED_METHOD};

		if (!dict_struct.range_min || !dict_struct.range_max)
			throw Exception{name + ": dictionary of layout 'range_hashed' requires .structure.range_min and .structure.range_max",
				ErrorCodes::BAD_ARGUMENTS};

		return std::make_unique<RangeHashedDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, require_nonempty);
	}
	else if ("complex_key_hashed" == layout_type)
	{
		if (!dict_struct.key)
			throw Exception{"'key' is required for dictionary of layout 'complex_key_hashed'",
				ErrorCodes::BAD_ARGUMENTS};

		return std::make_unique<ComplexKeyHashedDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, require_nonempty);
	}
	else if ("complex_key_cache" == layout_type)
	{
		if (!dict_struct.key)
			throw Exception{"'key' is required for dictionary of layout 'complex_key_hashed'",
				ErrorCodes::BAD_ARGUMENTS};

		const auto size = config.getInt(layout_prefix + ".complex_key_cache.size_in_cells");
		if (size == 0)
			throw Exception{name + ": dictionary of layout 'cache' cannot have 0 cells",
				ErrorCodes::TOO_SMALL_BUFFER_SIZE};

		if (require_nonempty)
			throw Exception{name + ": dictionary of layout 'cache' cannot have 'require_nonempty' attribute set",
				ErrorCodes::BAD_ARGUMENTS};

		return std::make_unique<ComplexKeyCacheDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, size);
	}
	else
	{
		if (dict_struct.key)
			throw Exception{"'key' is not supported for dictionary of layout '" + layout_type + "'",
				ErrorCodes::UNSUPPORTED_METHOD};

		if (dict_struct.range_min || dict_struct.range_max)
			throw Exception{name + ": elements .structure.range_min and .structure.range_max should be defined only "
					"for a dictionary of layout 'range_hashed'",
				ErrorCodes::BAD_ARGUMENTS};

		if ("flat" == layout_type)
		{
			return std::make_unique<FlatDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, require_nonempty);
		}
		else if ("hashed" == layout_type)
		{
			return std::make_unique<HashedDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, require_nonempty);
		}
		else if ("cache" == layout_type)
		{
			const auto size = config.getInt(layout_prefix + ".cache.size_in_cells");
			if (size == 0)
				throw Exception{name + ": dictionary of layout 'cache' cannot have 0 cells",
					ErrorCodes::TOO_SMALL_BUFFER_SIZE};

			if (require_nonempty)
				throw Exception{name + ": dictionary of layout 'cache' cannot have 'require_nonempty' attribute set",
					ErrorCodes::BAD_ARGUMENTS};

			return std::make_unique<CacheDictionary>(name, dict_struct, std::move(source_ptr), dict_lifetime, size);
		}
	}

	throw Exception{name + ": unknown dictionary layout type: " + layout_type,
		ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG};
};