예제 #1
0
void DbService::getTick(const BfGetTickReq* request, ::grpc::ServerWriter<BfTickData>* writer)
{
    g_sm->checkCurrentOn(ServiceMgr::EXTERNAL);

    if (request->symbol().length() == 0 || request->exchange().length() == 0 || request->todate().length() == 0 || request->totime().length() == 0) {
        BfDebug("invalid parm,ignore");
        return;
    }

    leveldb::ReadOptions options;
    options.fill_cache = false;
    leveldb::Iterator* it = db_->NewIterator(options);
    if (!it) {
        qFatal("NewIterator == nullptr");
    }

    // key: tick-symbol-exchange-actiondate-ticktime
    std::string key = QString().sprintf("tick-%s-%s-%s-%s", request->symbol().c_str(), request->exchange().c_str(), request->todate().c_str(), request->totime().c_str()).toStdString();
    it->Seek(leveldb::Slice(key));
    int count = 0;
    for (; it->Valid() && count < request->count(); it->Prev()) {
        //遇到了前后两个结束item
        const char* buf = it->value().data();
        int len = it->value().size();
        BfTickData bfItem;
        if (!bfItem.ParseFromArray(buf, len)) {
            qFatal("ParseFromArray error");
            break;
        }
        if (bfItem.symbol().length() == 0) {
            std::string lastestKey = QString().sprintf("tick-%s-%s=", request->symbol().c_str(), request->exchange().c_str()).toStdString();
            std::string itKey = it->key().ToString();
            if (itKey == lastestKey) {
                continue;
            } else {
                break;
            }
        }

        count++;
        writer->Write(bfItem);
    }
    delete it;
}
예제 #2
0
파일: statform.cpp 프로젝트: flatM/bftrader
void StatForm::statTick(QString symbol, QString exchange, QString name)
{
    QString startDate, startTime, endDate, endTime;

    if (1) {
        leveldb::DB* db = g_sm->dbService()->getDb();
        leveldb::ReadOptions options;
        options.fill_cache = false;
        leveldb::Iterator* it = db->NewIterator(options);
        if (!it) {
            qFatal("NewIterator == nullptr");
        }

        //第一个是tick-symbol-exchange+
        //最后一个是tick-symbol-exchange=
        QString key = QString().sprintf("tick-%s-%s+", qPrintable(symbol), qPrintable(exchange));
        it->Seek(leveldb::Slice(key.toStdString()));
        if (it->Valid()) {
            it->Next();
        }
        if (it->Valid()) {
            //遇到了前后两个结束item
            const char* buf = it->value().data();
            int len = it->value().size();
            BfTickData bfItem;
            if (bfItem.ParseFromArray(buf, len) && bfItem.symbol().length() != 0) {
                startDate = bfItem.actiondate().c_str();
                startTime = bfItem.ticktime().c_str();
            }
        }
        delete it;
    }

    if (startDate.length() != 0) {
        leveldb::DB* db = g_sm->dbService()->getDb();
        leveldb::ReadOptions options;
        options.fill_cache = false;
        leveldb::Iterator* it = db->NewIterator(options);
        if (!it) {
            qFatal("NewIterator == nullptr");
        }

        //第一个是tick-symbol-exchange+
        //最后一个是tick-symbol-exchange=
        QString key = QString().sprintf("tick-%s-%s=", qPrintable(symbol), qPrintable(exchange));
        it->Seek(leveldb::Slice(key.toStdString()));
        if (it->Valid()) {
            it->Prev();
        }
        if (it->Valid()) {
            //遇到了前后两个结束item
            const char* buf = it->value().data();
            int len = it->value().size();
            BfTickData bfItem;
            if (bfItem.ParseFromArray(buf, len) && bfItem.symbol().length() != 0) {
                endDate = bfItem.actiondate().c_str();
                endTime = bfItem.ticktime().c_str();
            }
        }
        delete it;
    }

    if (startDate.length() != 0 && endDate.length() != 0) {
        onGotData(symbol, exchange, name, "tick", startDate, startTime, endDate, endTime);
    }
}