预加载支持最大数量限制,以便加载分钟线级别数据至内存

This commit is contained in:
fasiondog 2020-12-27 00:23:58 +08:00
parent 1a056139aa
commit 08c575b1df
5 changed files with 69 additions and 16 deletions

View File

@ -276,8 +276,16 @@ void Stock::loadKDataToBuffer(KQuery::KType inkType) {
if (m_kdataDriver) {
m_data->pKData[kType] = new KRecordList;
m_data->pMutex[kType] = new std::shared_mutex();
const auto& param = StockManager::instance().getPreloadParameter();
string preload_type = fmt::format("{}_max", kType);
to_lower(preload_type);
int max_num = param.tryGet<int>(preload_type, 5120);
HKU_ERROR_IF_RETURN(max_num < 0, void(), "Invalid preload {} param: {}", preload_type,
max_num);
size_t total = m_kdataDriver->getCount(m_data->m_market, m_data->m_code, kType);
int start = total <= max_num ? 0 : total - max_num;
*(m_data->pKData[kType]) = m_kdataDriver->getKRecordList(
m_data->m_market, m_data->m_code, KQuery(0, Null<int64_t>(), kType));
m_data->m_market, m_data->m_code, KQuery(start, Null<int64_t>(), kType));
}
}
}

View File

@ -282,8 +282,13 @@ Stock StockManager::addTempCsvStock(const string& code, const string& day_filena
KDataTempCsvDriver* p = new KDataTempCsvDriver(day_filename, min_filename);
result.setKDataDriver(KDataDriverPtr(p));
result.loadKDataToBuffer(KQuery::DAY);
result.loadKDataToBuffer(KQuery::MIN);
const auto& preload_param = getPreloadParameter();
if (preload_param.tryGet<bool>("day", true)) {
result.loadKDataToBuffer(KQuery::DAY);
}
if (preload_param.tryGet<bool>("min", false)) {
result.loadKDataToBuffer(KQuery::MIN);
}
return loadStock(result) ? result : Null<Stock>();
}

View File

@ -9,6 +9,7 @@
#define NOMINMAX
#endif
#include <set>
#include <fmt/format.h>
#include "utilities/IniParser.h"
#include "hikyuu.h"
@ -62,8 +63,14 @@ void hikyuu_init(const string& config_file_name, bool ignore_preload) {
}
option = config.getOptionList("preload");
for (auto iter = option->begin(); iter != option->end(); ++iter) {
preloadParam.set<bool>(*iter, ignore_preload ? false : config.getBool("preload", *iter));
try {
preloadParam.set<bool>(*iter,
ignore_preload ? false : config.getBool("preload", *iter));
} catch (...) {
preloadParam.set<int>(*iter, ignore_preload ? false : config.getInt("preload", *iter));
}
}
StockManager& sm = StockManager::instance();

View File

@ -12,12 +12,29 @@ dir = ./test_data/block
self = self.ini
[preload]
day = 1
week = 0
month = 0
quarter = 0
halfyear = 0
year = 0
day = True
week = False
month = False
quarter = False
halfyear = False
year = False
min = False
min5 = False
min15 = False
min30 = False
min60 = False
day_max = 100000
week_max = 100000
month_max = 100000
quarter_max = 100000
halfyear_max = 100000
year_max = 100000
min_max = 5120
min5_max = 5120
min15_max = 5120
min30_max = 5120
min60_max = 5120
[baseinfo]
type = sqlite3

View File

@ -12,12 +12,28 @@ dir = .\\test_data\\block
self = self.ini
[preload]
day = 1
week = 0
month = 0
quarter = 0
halfyear = 0
year = 0
day = True
week = False
month = False
quarter = False
halfyear = False
year = False
min = False
min5 = False
min15 = False
min30 = False
min60 = False
day_max = 100000
week_max = 100000
month_max = 100000
quarter_max = 100000
halfyear_max = 100000
year_max = 100000
min_max = 5120
min5_max = 5120
min15_max = 5120
min30_max = 5120
min60_max = 5120
[baseinfo]
type = sqlite3