mysql block driver

This commit is contained in:
fasiondog 2024-01-03 19:12:30 +08:00
parent 30a9511123
commit 336e7436d1
5 changed files with 84 additions and 15 deletions

View File

@ -29,13 +29,15 @@ datadir = {dir}
quotation_server = {quotation_server}
[block]
type = qianlong
dir = {dir}/block
= zsbk.ini
= hybk.ini
= dybk.ini
= gnbk.ini
self = self.ini
type = sqlite3
db = {dir}/stock.db
;type = qianlong
;dir = {dir}/block
; = zsbk.ini
; = hybk.ini
; = dybk.ini
; = gnbk.ini
;self = self.ini
[preload]
day = {day}
@ -95,13 +97,11 @@ datadir = {dir}
quotation_server = {quotation_server}
[block]
type = qianlong
dir = {dir}/block
= zsbk.ini
= hybk.ini
= dybk.ini
= gnbk.ini
self = self.ini
type = mysql
host = {host}
port = {port}
usr = {usr}
pwd = {pwd}
[preload]
day = {day}

View File

@ -268,6 +268,8 @@ void StockManager::reload() {
loadAllStocks();
loadAllStockWeights();
m_blockDriver->load();
HKU_INFO("start reload kdata to buffer");
std::vector<Stock> can_not_parallel_stk_list; // 记录不支持并行加载的Stock
{

View File

@ -23,6 +23,7 @@
#if HKU_ENABLE_MYSQL_KDATA
#include "base_info/mysql/MySQLBaseInfoDriver.h"
#include "block_info/mysql/MySQLBlockInfoDriver.h"
#include "kdata/mysql/MySQLKDataDriver.h"
#endif
@ -54,6 +55,7 @@ void DataDriverFactory::init() {
#if HKU_ENABLE_MYSQL_KDATA
DataDriverFactory::regBaseInfoDriver(make_shared<MySQLBaseInfoDriver>());
DataDriverFactory::regBlockDriver(make_shared<MySQLBlockInfoDriver>());
#endif
m_kdataPrototypeDrivers = new map<string, KDataDriverPtr>();

View File

@ -5,28 +5,89 @@
* 1. 20240102 added by fasiondog
*/
#include <nlohmann/json.hpp>
#include "hikyuu/utilities/db_connect/mysql/MySQLConnect.h"
#include "hikyuu/utilities/db_connect/TableMacro.h"
#include "MySQLBlockInfoDriver.h"
using json = nlohmann::json;
namespace hku {
struct MySQLBlockTable {
TABLE_BIND2(MySQLBlockTable, block, category, content)
string category;
string content;
};
MySQLBlockInfoDriver::~MySQLBlockInfoDriver() {}
bool MySQLBlockInfoDriver::_init() {
return true;
}
Block MySQLBlockInfoDriver::getBlock(const string&, const string&) {
void MySQLBlockInfoDriver::load() {
Parameter connect_param;
connect_param.set<string>("host", getParamFromOther<string>(m_params, "host", "127.0.0.1"));
connect_param.set<string>("usr", getParamFromOther<string>(m_params, "usr", "root"));
connect_param.set<string>("pwd", getParamFromOther<string>(m_params, "pwd", ""));
connect_param.set<string>("db", getParamFromOther<string>(m_params, "db", "hku_base"));
string port_str = getParamFromOther<string>(m_params, "port", "3306");
unsigned int port = boost::lexical_cast<unsigned int>(port_str);
connect_param.set<int>("port", port);
MySQLConnect connect(connect_param);
vector<MySQLBlockTable> records;
connect.batchLoad(records);
unordered_map<string, Block> tmp;
for (const auto& record : records) {
json blks = json::parse(record.content);
for (json::iterator it = blks.begin(); it != blks.end(); ++it) {
Block blk(record.category, it.key());
for (const auto& codes : it.value()) {
blk.add(codes);
}
tmp[it.key()] = blk;
}
m_buffer[record.category] = std::move(tmp);
tmp.clear();
}
}
Block MySQLBlockInfoDriver::getBlock(const string& category, const string& name) {
Block ret;
auto category_iter = m_buffer.find(category);
HKU_IF_RETURN(category_iter == m_buffer.end(), ret);
auto block_iter = category_iter->second.find(name);
HKU_IF_RETURN(block_iter == category_iter->second.end(), ret);
ret = block_iter->second;
return ret;
}
BlockList MySQLBlockInfoDriver::getBlockList(const string& category) {
BlockList ret;
auto category_iter = m_buffer.find(category);
HKU_IF_RETURN(category_iter == m_buffer.end(), ret);
const auto& category_blocks = category_iter->second;
for (auto iter = category_blocks.begin(); iter != category_blocks.end(); ++iter) {
ret.emplace_back(iter->second);
}
return ret;
}
BlockList MySQLBlockInfoDriver::getBlockList() {
BlockList ret;
for (auto category_iter = m_buffer.begin(); category_iter != m_buffer.end(); ++category_iter) {
const auto& category_blocks = category_iter->second;
for (auto iter = category_blocks.begin(); iter != category_blocks.end(); ++iter) {
ret.emplace_back(iter->second);
}
}
return ret;
}

View File

@ -16,10 +16,14 @@ public:
MySQLBlockInfoDriver() : BlockInfoDriver("mysql"){};
virtual ~MySQLBlockInfoDriver();
virtual void load() override;
virtual bool _init() override;
virtual Block getBlock(const string&, const string&) override;
virtual BlockList getBlockList(const string& category) override;
virtual BlockList getBlockList() override;
private:
unordered_map<string, unordered_map<string, Block>> m_buffer;
};
} // namespace hku