Ejemplo n.º 1
0
void TdbDatabase::storePackage(const Package& p) {
    TdbKeyValue kv;

    // check if this package is already indexed
    kv.setKey(p.name() + "-version");
    kv.setValue(tdb_fetch(m_tdbFile, kv.key()));

    if (kv.value_str() == p.version()) {
        kv.setKey(p.name() + "-release");
        kv.setValue(tdb_fetch(m_tdbFile, kv.key()));
        if (kv.value_str() == p.release()) {
            return;
        }
    }

    // OK, we have something new

    int res = 0;

    kv.setKey(p.name() + "-version");
    kv.setValue(p.version());
    res = tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_MODIFY);
    if (res != 0 ) tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_INSERT);

    kv.setKey(p.name() + "-release");
    kv.setValue(p.release());
    res = tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_MODIFY);
    if (res != 0 ) tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_INSERT);

    kv.setKey(p.name() + "-architecture");
    kv.setValue(p.architecture());
    res = tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_MODIFY);
    if (res != 0 ) tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_INSERT);

    kv.setKey(p.name() + "-compression");
    kv.setValue(p.compression());
    res = tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_MODIFY);
    if (res != 0 ) tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_INSERT);

    string filesString;
    bool first = true;

    for (const auto& elem : p.files()) {

        TdbKeyValue fkv(elem, p.name());

        const int ret = tdb_store(m_tdbFile, fkv.key(), fkv.value(),
                                   TDB_INSERT);

        if (ret != 0) {
            fkv.setValue(tdb_fetch(m_tdbFile, fkv.key()));
            vector<string> others;
            istringstream iss(fkv.value_str());
            copy(istream_iterator<string>(iss), istream_iterator<string>(),
                 back_inserter<vector<string> >(others));
            others.push_back(p.name());

            auto uIter = unique(others.begin(), others.end());
            others.resize(uIter - others.begin());

            string newValue;
            bool isthefirst = true;
            for (auto& other : others) {
                if (isthefirst) {
                    isthefirst = false;
                } else {
                    newValue += " ";
                }
                newValue += other;
            }

            fkv.setValue(newValue);

            tdb_store(m_tdbFile, fkv.key(), fkv.value(), TDB_MODIFY);
        }

        if (first) {
            filesString.append(elem);
            first = false;
        } else {
            filesString.append(" " + elem);
        }
    }

    kv.setKey(p.name() + "-files");
    kv.setValue(filesString);
    res = tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_MODIFY);
    if (res != 0 ) tdb_store(m_tdbFile, kv.key(), kv.value(), TDB_INSERT);
}