⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dsmcc_storage.cpp

📁 数字电视中间件小型库
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                                   profile.object_location.module_id, 
                                   profile.object_location.object_key, 
                                   profile.object_location.object_key_length);

        object_info[key].parent = next_insert_id - 1; // Remember, we incremented this when INSERTing
        object_info[key].name   = MakeString(srg.bindings[i].name.id.begin(), srg.bindings[i].name.id.size() - 1); // -1 to skip the terminating \0

    }

    return true;
}

bool DSM_CC::MythDatabaseStorage::StoreObject(uint32_t download_id, uint16_t module_id, uint8_t module_version, const BIOP::DirectoryMessage& dm) {
    MSqlQuery query(MSqlQuery::InitCon());
    QString key = PlacementKey(download_id, module_id, dm.header);

    // Do we exist in the database?
    query.prepare(QString(
        "SELECT id, module_version FROM dsmcc_objects "
        "WHERE download_id = %1 AND module_id = %2 AND object_key = ?"
        ).arg(download_id).arg(module_id)
    );

    query.bindValue(0, MakeString(dm.header.object_key.begin(), dm.header.object_key.size()));
    query.exec();

    if(!query.isActive()) {
        #ifdef DSM_CC_TRACE_STORAGE
            DSM_CC_TRACE_STR("MythDBStorage", "Bad query", query.executedQuery().ascii());
        #endif
        return false;
    }

    if(!query.size()) {
        // No, we don't exist, so insert the record
        query.prepare(QString(
            "INSERT INTO dsmcc_objects (id, download_id, module_id, module_version, object_key, object_name, object_type, parent_id) "
            "VALUES (%1, %2, %3, %4, ?, ?, 'dir', %5)"
            ).arg(next_insert_id++).arg(download_id).arg(module_id).arg(module_version).arg(object_info[key].parent)
        );
        query.bindValue(0, MakeString(dm.header.object_key.begin(), dm.header.object_key.size()));
        query.bindValue(1, object_info[key].name);
        query.exec();

        if(!query.isActive()) {
            #ifdef DSM_CC_TRACE_STORAGE
                DSM_CC_TRACE_STR("MythDBStorage", "Bad query", query.executedQuery().ascii());
            #endif
            return false;
        }
    }
    else {
        // We exist; if the module version is the same, then do nothing
        // Otherwise, update the record to reflect the new data
        query.next();
        if(query.value(1).toInt() != module_version) {
            // Simply update the version; nothing else required (the object key is the same, see first query)
            QString newquery("UPDATE dsmcc_objects SET module_version = %1 WHERE id = %2");
            query.prepare(newquery.arg(module_version).arg(query.value(0).toInt()));
            query.exec();
            if(!query.isActive()) {
                #ifdef DSM_CC_TRACE_STORAGE
                    DSM_CC_TRACE_STR("MythDBStorage", "Bad query", query.executedQuery().ascii());
                #endif
                return false;
            }
        }
    }

    // Great! Now we have to cache object child relationships in the hashtable
    uint32_t num_bindings = dm.bindings.size();

    for(uint32_t i = 0; i < num_bindings; ++i) {
        if(dm.bindings[i].ior.profiles[0].id_tag != BIOP_PROFILE_ID_BIOP) {
            continue;
        }
        BIOP::BIOPProfile profile = dm.bindings[i].ior.profiles[0].biop;
        QString key = PlacementKey(profile.object_location.carousel_id,
                                   profile.object_location.module_id, 
                                   profile.object_location.object_key, 
                                   profile.object_location.object_key_length);

        object_info[key].parent = next_insert_id - 1; // Remember, we incremented this when INSERTing
        object_info[key].name   = MakeString(dm.bindings[i].name.id.begin(), dm.bindings[i].name.id.size() - 1); // -1 to skip the terminating \0

    }

    return true;
}

bool DSM_CC::MythDatabaseStorage::StoreObject(uint32_t download_id, uint16_t module_id, uint8_t module_version, const BIOP::FileMessage& fm) {
    MSqlQuery query(MSqlQuery::InitCon());
    QString key = PlacementKey(download_id, module_id, fm.header);

    // Do we exist in the database?
    query.prepare(QString(
        "SELECT id, module_version FROM dsmcc_objects "
        "WHERE download_id = %1 AND module_id = %2 AND object_key = ?"
        ).arg(download_id).arg(module_id)
    );

    query.bindValue(0, MakeString(fm.header.object_key.begin(), fm.header.object_key.size()));
    query.exec();

    if(!query.isActive()) {
        #ifdef DSM_CC_TRACE_STORAGE
            DSM_CC_TRACE_STR("MythDBStorage", "Bad query", query.executedQuery().ascii());
        #endif
        return false;
    }

    uint32_t my_id;

    if(!query.size()) {
        // No, we don't exist, so insert the record
        query.prepare(QString(
            "INSERT INTO dsmcc_objects (id, download_id, module_id, module_version, object_key, object_name, object_type, parent_id) "
            "VALUES (%1, %2, %3, %4, ?, ?, 'fil', %5)"
            ).arg(next_insert_id++).arg(download_id).arg(module_id).arg(module_version).arg(object_info[key].parent)
        );
        query.bindValue(0, MakeString(fm.header.object_key.begin(), fm.header.object_key.size()));
        query.bindValue(1, object_info[key].name);
        query.exec();

        if(!query.isActive()) {
            #ifdef DSM_CC_TRACE_STORAGE
                DSM_CC_TRACE_STR("MythDBStorage", "Bad query", query.executedQuery().ascii());
            #endif
            return false;
        }

        my_id = next_insert_id - 1;
    }
    else {

        query.next();

        if(query.value(1).toInt() != module_version) {

            // Simply update the version; nothing else required (the object key is the same, see first query)
            my_id = query.value(0).toInt();

            query.prepare(QString(
                "UPDATE dsmcc_objects SET module_version = %1 WHERE id = %2"
                ).arg(module_version).arg(my_id)
            );
            query.exec();

            if(!query.isActive()) {
                #ifdef DSM_CC_TRACE_STORAGE
                    DSM_CC_TRACE_STR("MythDBStorage", "Bad query", query.executedQuery().ascii());
                #endif
                return false;
            }

            
        }
        else {
            // Just return, the file's contents should already be in the database
            return true;
        }
    }

    // Finally, dump the file contents to the dsmcc_file_contents table
    query.prepare(QString(
        "SELECT id FROM dsmcc_file_contents WHERE id = %1"
        ).arg(my_id)
    );
    query.exec();

    if(!query.isActive()) {
        #ifdef DSM_CC_TRACE_STORAGE
            DSM_CC_TRACE_STR("MythDBStorage", "Bad query", query.executedQuery().ascii());
        #endif
        return false;
    }

    if(!query.size()) {
        // Insert
        query.prepare(QString(
            "INSERT INTO dsmcc_file_contents (id, contents) VALUES (%1, ?)"
            ).arg(my_id)
        );
    }
    else {
        // Update
        query.prepare(QString(
            "UPDATE dsmcc_file_contents SET contents = ? WHERE id = %1"
            ).arg(my_id)
        );
    }

    QByteArray filecontents;
    filecontents.duplicate(reinterpret_cast<char*>(fm.contents), fm.header.message_body_length - 4);
    query.bindValue(0, filecontents);

    query.exec();

    if(!query.isActive()) {
        #ifdef DSM_CC_TRACE_STORAGE
            DSM_CC_TRACE_STR("MythDBStorage", "Bad query", query.executedQuery().ascii());
        #endif
        return false;
    }

    return true;
}

QString DSM_CC::Storage::PlacementKey(uint32_t download_id, uint16_t module_id, const BIOP::MessageHeader& header) {
    char buffer[20]; // More than enough

    sprintf(buffer, "%x-%x", download_id, module_id);
    QString retval(buffer);

    retval += '-';
    for(uint8_t i = 0; i < header.object_key.size(); ++i) {
        retval += header.object_key[i];
    }

    return retval;
}

QString DSM_CC::Storage::PlacementKey(uint32_t download_id, uint16_t module_id, uint32_t object_key, uint8_t object_key_length) {
    char buffer[20]; // More than enough

    sprintf(buffer, "%x-%x", download_id, module_id);
    QString retval(buffer);

    retval += '-';

    retval += (object_key >> 24) & 0xff;
    if(object_key_length > 1) retval += (object_key >> 16) & 0xff;
    if(object_key_length > 2) retval += (object_key >> 8) & 0xff;
    if(object_key_length > 3) retval += object_key & 0xff;

    return retval;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -