并行加载权息信息

This commit is contained in:
fasiondog 2021-01-26 22:58:19 +08:00
parent 14b94befe5
commit 139e451ba7
5 changed files with 27 additions and 10 deletions

View File

@ -41,11 +41,13 @@ void StockManager::quit() {
}
StockManager::StockManager() {
m_stockDict_mutex = new std::mutex;
m_marketInfoDict_mutex = new std::mutex;
m_stockTypeInfo_mutex = new std::mutex;
}
StockManager::~StockManager() {
delete m_stockDict_mutex;
delete m_marketInfoDict_mutex;
delete m_stockTypeInfo_mutex;
fmt::print("Quit Hikyuu system!\n\n");
@ -252,6 +254,7 @@ Stock StockManager::getStock(const string& querystr) const {
Stock result;
string query_str = querystr;
to_upper(query_str);
std::lock_guard<std::mutex> lock(*m_stockDict_mutex);
auto iter = m_stockDict.find(query_str);
return (iter != m_stockDict.end()) ? iter->second : result;
}
@ -354,6 +357,7 @@ void StockManager::removeTempCsvStock(const string& code) {
bool StockManager::loadStock(const Stock& stock) {
string market_code(stock.market_code());
to_upper(market_code);
std::lock_guard<std::mutex> lock(*m_stockDict_mutex);
HKU_ERROR_IF_RETURN(m_stockDict.find(market_code) != m_stockDict.end(), false,
"The stock had exist! {}", market_code);
m_stockDict[market_code] = stock;
@ -361,6 +365,7 @@ bool StockManager::loadStock(const Stock& stock) {
}
bool StockManager::loadMarketInfo(const MarketInfo& marketInfo) {
std::lock_guard<std::mutex> lock(*m_marketInfoDict_mutex);
string market = marketInfo.market();
to_upper(market);
HKU_ERROR_IF_RETURN(m_marketInfoDict.find(market) != m_marketInfoDict.end(), false,
@ -370,6 +375,7 @@ bool StockManager::loadMarketInfo(const MarketInfo& marketInfo) {
}
bool StockManager::loadStockTypeInfo(const StockTypeInfo& stkTypeInfo) {
std::lock_guard<std::mutex> lock(*m_stockTypeInfo_mutex);
HKU_ERROR_IF_RETURN(m_stockTypeInfo.find(stkTypeInfo.type()) != m_stockTypeInfo.end(), false,
"The stockTypeInfo had exist! {}", stkTypeInfo.type());
m_stockTypeInfo[stkTypeInfo.type()] = stkTypeInfo;

View File

@ -197,6 +197,7 @@ private:
BlockInfoDriverPtr m_blockDriver;
StockMapIterator::stock_map_t m_stockDict; // SH000001 -> stock
std::mutex* m_stockDict_mutex;
typedef unordered_map<string, MarketInfo> MarketInfoMap;
mutable MarketInfoMap m_marketInfoDict;

View File

@ -6,6 +6,8 @@
*/
#include <boost/algorithm/string.hpp>
#include "../StockManager.h"
#include "../base/GlobalTaskGroup.h"
#include "BaseInfoDriver.h"
namespace hku {
@ -68,6 +70,22 @@ bool BaseInfoDriver::loadBaseInfo() {
HKU_INFO("Loading stock information...");
HKU_FATAL_IF_RETURN(!_loadStock(), false, "Can't load Stock");
HKU_INFO("Loading stock weight...");
auto* tg = getGlobalTaskGroup();
auto& sm = StockManager::instance();
std::vector<std::future<void>> task_list;
for (auto stock : sm) {
task_list.push_back(tg->submit([=]() mutable {
StockWeightList weightList = this->getStockWeightList(
stock.market(), stock.code(), Datetime::min(), Null<Datetime>());
stock.setWeightList(weightList);
}));
}
// 权息信息如果不等待加载完毕,在数据加载期间进行计算可能导致复权错误,所以这里需要等待
for (auto& task : task_list) {
task.get();
}
return true;
}

View File

@ -201,11 +201,7 @@ bool MySQLBaseInfoDriver::_loadStock() {
Stock(marketDict[r.marketid], r.code, r.name, r.type, r.valid, startDate, endDate);
}
if (sm.loadStock(stock)) {
StockWeightList weightList =
getStockWeightList(marketDict[r.marketid], r.code, Datetime::min(), Null<Datetime>());
stock.setWeightList(weightList);
}
sm.loadStock(stock);
}
return true;

View File

@ -148,11 +148,7 @@ bool SQLiteBaseInfoDriver::_loadStock() {
Stock(marketDict[r.marketid], r.code, r.name, r.type, r.valid, startDate, endDate);
}
if (sm.loadStock(stock)) {
StockWeightList weightList =
getStockWeightList(marketDict[r.marketid], r.code, Datetime::min(), Null<Datetime>());
stock.setWeightList(weightList);
}
sm.loadStock(stock);
}
return true;