コード例 #1
0
ファイル: dbservice.cpp プロジェクト: fakechris/bftrader
void DbService::insertContract(const BfContractData& bfItem)
{
    BfDebug(__FUNCTION__);
    g_sm->checkCurrentOn(ServiceMgr::DB);

    if (bfItem.symbol().length() == 0 || bfItem.exchange().length() == 0 || bfItem.name().length() == 0) {
        BfDebug("invalid contract,ignore");
        return;
    }

    leveldb::WriteOptions options;
    leveldb::WriteBatch batch;
    std::string key, val;

    if (1) {

        // key: contract-symbol-exchange
        key = QString().sprintf("contract-%s-%s", bfItem.symbol().c_str(), bfItem.exchange().c_str()).toStdString();
        bool ok = bfItem.SerializeToString(&val);
        if (!ok) {
            qFatal("SerializeToString fail");
        }
        batch.Put(key, val);

        // key: tick-symbol-exchange+
        // key: tick-symbol-exchange=
        BfTickData bfNullTick;
        key = QString().sprintf("tick-%s-%s+", bfItem.symbol().c_str(), bfItem.exchange().c_str()).toStdString();
        val = bfNullTick.SerializeAsString();
        batch.Put(key, val);
        key = QString().sprintf("tick-%s-%s=", bfItem.symbol().c_str(), bfItem.exchange().c_str()).toStdString();
        val = bfNullTick.SerializeAsString();
        batch.Put(key, val);

        // key: bar-symbol-exchange-period+
        // key: bar-symbol-exchange-period=
        BfBarData bfNullBar;
        for (int i = BfBarPeriod_MIN; i <= BfBarPeriod_MAX; i++) {
            key = QString().sprintf("bar-%s-%s-%s+", bfItem.symbol().c_str(), bfItem.exchange().c_str(), qPrintable(ProtoUtils::formatPeriod((BfBarPeriod)i))).toStdString();
            val = bfNullBar.SerializeAsString();
            batch.Put(key, val);
            key = QString().sprintf("bar-%s-%s-%s=", bfItem.symbol().c_str(), bfItem.exchange().c_str(), qPrintable(ProtoUtils::formatPeriod((BfBarPeriod)i))).toStdString();
            val = bfNullBar.SerializeAsString();
            batch.Put(key, val);
        }
    }

    db_->Write(options, &batch);
}
コード例 #2
0
ファイル: dbservice.cpp プロジェクト: mailtostanley/bftrader
void DbService::batchWriteTicks()
{
    g_sm->checkCurrentOn(ServiceMgr::DB);

    if( tickCount_ == 0 ){
        return;
    }

    leveldb::WriteOptions options;
    leveldb::WriteBatch batch;
    for (int i = 0; i < tickCount_; i++) {
        void* curTick = tickArray[i].curTick;
        void* preTick = tickArray[i].preTick;

        BfTickData bfItem;
        CtpUtils::translateTick(curTick, preTick, &bfItem);

        // tick里面的exchange不一定有=
        QString exchange = bfItem.exchange().c_str();
        if (exchange.trimmed().length() == 0) {
            void* contract = g_sm->ctpMgr()->getContract(bfItem.symbol().c_str());
            exchange = CtpUtils::getExchangeFromContract(contract);
            bfItem.set_exchange(exchange.toStdString());
        }

        // key: tick.exchange.symbol.actiondata.ticktime
        std::string key = QString().sprintf("tick.%s.%s.%s.%s", bfItem.exchange().c_str(), bfItem.symbol().c_str(), bfItem.actiondate().c_str(), bfItem.ticktime().c_str()).toStdString();
        std::string val = bfItem.SerializeAsString();

        batch.Put(key, val);
    }
    db_->Write(options, &batch);

    tickCount_ = 0;
}