hikyuu2/hikyuu_cpp/hikyuu/data_driver/DataDriverFactory.cpp

151 lines
5.0 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* DatabaseDriverFactory.cpp
*
* Created on: 2012-8-14
* Author: fasiondog
*/
#include "../GlobalInitializer.h"
#include <boost/algorithm/string.hpp>
2019-08-16 01:17:24 +08:00
#include "base_info/sqlite/SQLiteBaseInfoDriver.h"
#include "base_info/mysql/MySQLBaseInfoDriver.h"
2016-04-03 00:08:31 +08:00
#include "block_info/qianlong/QLBlockInfoDriver.h"
2017-10-20 02:11:57 +08:00
#include "kdata/hdf5/H5KDataDriver.h"
#include "kdata/mysql/MySQLKDataDriver.h"
#include "kdata/tdx/TdxKDataDriver.h"
2021-01-20 00:32:01 +08:00
#include "kdata/cvs/KDataTempCsvDriver.h"
2015-01-07 01:26:14 +08:00
#include "DataDriverFactory.h"
#include "KDataDriver.h"
namespace hku {
map<string, BaseInfoDriverPtr>* DataDriverFactory::m_baseInfoDrivers{nullptr};
map<string, BlockInfoDriverPtr>* DataDriverFactory::m_blockDrivers{nullptr};
2021-01-22 23:50:46 +08:00
map<string, KDataDriverPtr>* DataDriverFactory::m_kdataPrototypeDrivers{nullptr};
2015-01-07 01:26:14 +08:00
2021-01-24 20:47:38 +08:00
map<string, KDataDriverConnectPoolPtr>* DataDriverFactory::m_kdataDriverPools{nullptr};
2021-01-20 00:32:01 +08:00
void DataDriverFactory::init() {
m_baseInfoDrivers = new map<string, BaseInfoDriverPtr>();
DataDriverFactory::regBaseInfoDriver(make_shared<SQLiteBaseInfoDriver>());
DataDriverFactory::regBaseInfoDriver(make_shared<MySQLBaseInfoDriver>());
2015-01-07 01:26:14 +08:00
m_blockDrivers = new map<string, BlockInfoDriverPtr>();
DataDriverFactory::regBlockDriver(make_shared<QLBlockInfoDriver>());
2017-10-20 02:11:57 +08:00
2021-01-22 23:50:46 +08:00
m_kdataPrototypeDrivers = new map<string, KDataDriverPtr>();
2021-01-24 20:47:38 +08:00
m_kdataDriverPools = new map<string, KDataDriverConnectPoolPtr>();
2021-01-20 00:32:01 +08:00
DataDriverFactory::regKDataDriver(make_shared<TdxKDataDriver>());
DataDriverFactory::regKDataDriver(make_shared<H5KDataDriver>());
DataDriverFactory::regKDataDriver(make_shared<MySQLKDataDriver>());
2021-01-20 00:32:01 +08:00
DataDriverFactory::regKDataDriver(make_shared<KDataTempCsvDriver>());
}
2015-01-07 01:26:14 +08:00
void DataDriverFactory::release() {
m_baseInfoDrivers->clear();
delete m_baseInfoDrivers;
m_blockDrivers->clear();
delete m_blockDrivers;
2021-01-22 23:50:46 +08:00
m_kdataPrototypeDrivers->clear();
delete m_kdataPrototypeDrivers;
2021-01-20 00:32:01 +08:00
m_kdataDriverPools->clear();
delete m_kdataDriverPools;
}
void DataDriverFactory::regBaseInfoDriver(const BaseInfoDriverPtr& driver) {
HKU_CHECK(driver, "driver is nullptr!");
string new_type(driver->name());
to_upper(new_type);
(*m_baseInfoDrivers)[new_type] = driver;
2016-04-03 00:08:31 +08:00
}
2015-01-07 01:26:14 +08:00
void DataDriverFactory::removeBaseInfoDriver(const string& name) {
string new_type(name);
to_upper(new_type);
m_baseInfoDrivers->erase(new_type);
}
2019-11-10 23:31:41 +08:00
BaseInfoDriverPtr DataDriverFactory ::getBaseInfoDriver(const Parameter& params) {
map<string, BaseInfoDriverPtr>::const_iterator iter;
string type = params.get<string>("type");
to_upper(type);
iter = m_baseInfoDrivers->find(type);
BaseInfoDriverPtr result;
if (iter != m_baseInfoDrivers->end()) {
result = iter->second;
2017-10-20 02:11:57 +08:00
result->init(params);
}
return result;
}
void DataDriverFactory::regBlockDriver(const BlockInfoDriverPtr& driver) {
HKU_CHECK(driver, "driver is nullptr!");
string name(driver->name());
to_upper(name);
(*m_blockDrivers)[name] = driver;
}
void DataDriverFactory::removeBlockDriver(const string& name) {
string new_name(name);
to_upper(new_name);
m_blockDrivers->erase(new_name);
}
BlockInfoDriverPtr DataDriverFactory::getBlockDriver(const Parameter& params) {
BlockInfoDriverPtr result;
map<string, BlockInfoDriverPtr>::const_iterator iter;
string name = params.get<string>("type");
to_upper(name);
iter = m_blockDrivers->find(name);
if (iter != m_blockDrivers->end()) {
result = iter->second;
2017-10-20 02:11:57 +08:00
result->init(params);
}
return result;
}
void DataDriverFactory::regKDataDriver(const KDataDriverPtr& driver) {
string new_type(driver->name());
to_upper(new_type);
2021-01-20 00:32:01 +08:00
HKU_CHECK(m_kdataDriverPools->find(new_type) == m_kdataDriverPools->end(),
"Repeat regKDataDriver!");
2021-01-22 23:50:46 +08:00
(*m_kdataPrototypeDrivers)[new_type] = driver;
// 不在此创建连接池
2021-01-22 00:39:37 +08:00
//(*m_kdataDriverPools)[new_type] = make_shared<KDataDriverPool>();
}
void DataDriverFactory::removeKDataDriver(const string& name) {
string new_name(name);
to_upper(new_name);
2021-01-22 23:50:46 +08:00
m_kdataPrototypeDrivers->erase(new_name);
2021-01-20 00:32:01 +08:00
auto iter = m_kdataDriverPools->find(new_name);
if (iter != m_kdataDriverPools->end()) {
m_kdataDriverPools->erase(iter);
}
}
2021-01-24 20:47:38 +08:00
KDataDriverConnectPoolPtr DataDriverFactory::getKDataDriverPool(const Parameter& params) {
KDataDriverConnectPoolPtr result;
2021-01-20 00:32:01 +08:00
string name = params.get<string>("type");
to_upper(name);
auto iter = m_kdataDriverPools->find(name);
if (iter != m_kdataDriverPools->end()) {
result = iter->second;
2021-01-22 00:39:37 +08:00
} else {
2021-01-22 23:50:46 +08:00
auto prototype_iter = m_kdataPrototypeDrivers->find(name);
HKU_CHECK(prototype_iter != m_kdataPrototypeDrivers->end(), "Unregistered driver{}",
name);
2021-01-24 20:47:38 +08:00
HKU_CHECK(prototype_iter->second->init(params), "Failed init driver: {}", name);
(*m_kdataDriverPools)[name] = make_shared<KDataDriverConnectPool>(prototype_iter->second);
2021-01-22 00:39:37 +08:00
result = (*m_kdataDriverPools)[name];
2021-01-20 00:32:01 +08:00
}
return result;
}
2015-01-07 01:26:14 +08:00
} /* namespace hku */