예제 #1
0
파일: Stock.cpp 프로젝트: zklvyy/hikyuu
bool
Stock::getIndexRange(const KQuery& query, size_t& out_start, size_t& out_end)  const{
    out_start = 0;
    out_end = 0;
    if (!m_data || !m_kdataDriver)
        return false;

    if (KQuery::INDEX == query.queryType())
        return _getIndexRangeByIndex(query, out_start, out_end);

    if ((KQuery::DATE != query.queryType())
            || (query.kType() >= KQuery::INVALID_KTYPE)
            || query.startDatetime() >= query.endDatetime())
        return false;

    if (m_data->pKData[query.kType()]) {
        return _getIndexRangeByDateFromBuffer(query, out_start, out_end);
    }

    if (!m_kdataDriver->getIndexRangeByDate(m_data->m_market, m_data->m_code,
            query, out_start, out_end)) {
        out_start = 0;
        out_end = 0;
        return false;
    }

    return true;
}
예제 #2
0
파일: Stock.cpp 프로젝트: zklvyy/hikyuu
bool Stock::
_getIndexRangeByDateFromBuffer(const KQuery& query,
        size_t& out_start, size_t& out_end) const {
    out_start = 0;
    out_end = 0;

    //总数为0,视为失败
    size_t total = getCount(query.kType());
    if( 0 == total ){
        return false;
    }

    KRecordList& kdata = *(m_data->pKData[query.kType()]);
    size_t mid, low = 0, high = total - 1;
    size_t startpos, endpos;
    while(low <= high) {
        if(query.startDatetime() > kdata[high].datetime) {
            mid = high + 1;
            break;
        }

        if(kdata[low].datetime >= query.startDatetime()) {
            mid = low;
            break;
        }

        mid = (low + high) / 2;
        if(query.startDatetime() > kdata[mid].datetime) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }

    if(mid >= total) {
        return false;
    }

    startpos = mid;

    low = mid;
    high = total - 1;
    while(low <= high) {
        if(query.endDatetime() > kdata[high].datetime) {
            mid = high + 1;
            break;
        }

        if(kdata[low].datetime >= query.endDatetime()) {
            mid = low;
            break;
        }

        mid = (low + high) / 2;
        if(query.endDatetime() > kdata[mid].datetime) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }

    endpos = (mid >= total) ? total : mid;
    if(startpos >= endpos) {
        return false;
    }

    out_start = startpos;
    out_end = endpos;

    return true;
}
예제 #3
0
bool MySQLKDataDriver::
getIndexRangeByDate(const string& market, const string& code,
        const KQuery& query, size_t& out_start, size_t& out_end) {
    string func_name(" [MySQLKDataDriver::getIndexRangeByDate]");
    out_start = 0;
    out_end = 0;
    if (query.queryType() != KQuery::DATE) {
        HKU_ERROR("queryType must be KQuery::DATE" << func_name);
        return false;
    }

    if(query.startDatetime() >= query.endDatetime()
    || query.startDatetime() > (Datetime::max)()) {
        return false;
    }

    MYSQL_RES *mysql_result;
    MYSQL_ROW row;

    string table(_getTableName(market, code, query.kType()));
    std::stringstream buf (std::stringstream::out);
    buf << "select count(1) from " << table
        << " where date<" << query.startDatetime().number();

    if (!_query(buf.str())) {
        HKU_ERROR("mysql_query error! " << func_name);
        return false;
    }

    mysql_result = mysql_store_result(m_mysql.get());
    if (!mysql_result) {
        HKU_ERROR("mysql_store_result error!" << func_name);
        return false;
    }

    while ((row = mysql_fetch_row(mysql_result))) {
        try {
            out_start = boost::lexical_cast<size_t>(row[0]);
        } catch (...) {
            HKU_INFO("Error boost::lexical_cast<size_t>" << table << func_name);
            out_start = 0;
            mysql_free_result(mysql_result);
            return false;
        }
    }

    mysql_free_result(mysql_result);

    buf.str("");
    buf << "select count(1) from " << table
        << " where date<=" << query.endDatetime().number();
    if (!_query(buf.str())) {
        HKU_ERROR("mysql_query error! " << func_name);
        return false;
    }

    mysql_result = mysql_store_result(m_mysql.get());
    if (!mysql_result) {
        HKU_ERROR("mysql_store_result error!" << func_name);
        return false;
    }

    while ((row = mysql_fetch_row(mysql_result))) {
        try {
            out_end = boost::lexical_cast<size_t>(row[0]) - 1;
        } catch (...) {
            HKU_INFO("Error boost::lexical_cast<size_t>" << table << func_name);
            out_end = 0;
            mysql_free_result(mysql_result);
            return false;
        }
    }

    mysql_free_result(mysql_result);
    return true;
}