levelDB:条目没有保存在磁盘上

时间:2014-07-18 13:11:22

标签: c++ leveldb

当我在levelDB中添加一个新条目时,我可以在应用程序运行时检索它,但是当我关闭应用程序并再次启动它时,一切都消失了。

在磁盘上正确创建数据库,以下是一些操作后的样子

-rw-r--r-- 1 jan jan     0 Jul 18 08:56 000011.log
-rw-r--r-- 1 jan jan    16 Jul 18 08:56 CURRENT
-rw-r--r-- 1 jan jan     0 Jul 18 08:55 LOCK
-rw-r--r-- 1 jan jan   172 Jul 18 08:56 LOG
-rw-r--r-- 1 jan jan   172 Jul 18 08:56 LOG.old
-rw-r--r-- 1 jan jan 65536 Jul 18 08:56 MANIFEST-000010

这是c ++代码

#include "varietedatabaseinterface.h"
#include "leveldb/db.h"
#include "varietesananas.h"
#include <sstream>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <QDebug>


VarieteDatabaseInterface::VarieteDatabaseInterface()
{
}

void VarieteDatabaseInterface::setStoragePaths(QString appStoragePath, QString imageStoragePath)
{
    this->appStoragePath = appStoragePath;
    this->imageStoragePath = imageStoragePath;

    QString dbPath = appStoragePath + "/varietesAnanas.db";

    this->options.create_if_missing = true;
    this->writeOptions.sync = true;
    this->status = leveldb::DB::Open(this->options, dbPath.toStdString(), &this->db);

    if (false == this->status.ok())
    {
         qDebug() << "Unable to open/create test database : "  << appStoragePath;
         qDebug() << "Error msg : " << QString::fromStdString(status.ToString());
    }
}

VarietesAnanas* VarieteDatabaseInterface::getVarieteWithKey(QString key)
{
    std::string value;

    this->db->Get(this->readOptions, key.toStdString(), &value);

    qDebug() << "LevelDb value : " << QString::fromStdString(value);
    qDebug() << "for key : " << key;

    VarietesAnanas* variete = new VarietesAnanas(appStoragePath, imageStoragePath);

    if(value.length() > 1)
    {
        std::stringstream ifStream;
        ifStream << value;

        boost::archive::xml_iarchive iarchive(ifStream);
        iarchive >> BOOST_SERIALIZATION_NVP(variete);
    }
    else
    {
        qDebug() << "something went wrong while recovering data from DB : ";
    }

    return variete;
}

void VarieteDatabaseInterface::saveVariete(VarietesAnanas* variete)
{
    std::ostringstream sStream;

    boost::archive::xml_oarchive oarchive(sStream);
    oarchive << BOOST_SERIALIZATION_NVP(variete);
    qDebug() << "sStream : " << QString::fromStdString(sStream.str());

    //this->db->Delete(this->writeOptions, variete->getKey());
    this->db->Put(this->writeOptions, variete->getKey(), sStream.str());
}

QList<VarietesAnanas*> VarieteDatabaseInterface::getAllvarietes()
{
    QList<VarietesAnanas*> varieteList;

    leveldb::Iterator* it = this->db->NewIterator(this->readOptions);
    for (it->SeekToFirst(); it->Valid(); it->Next())
    {
        leveldb::Slice key = it->key();
        leveldb::Slice value = it->value();

        VarietesAnanas* variete = new VarietesAnanas(this->appStoragePath, this->imageStoragePath);

        std::stringstream ifStream;
        ifStream << value.ToString();

        boost::archive::xml_iarchive iarchive(ifStream);

        iarchive >> BOOST_SERIALIZATION_NVP(variete);

        varieteList.append(variete);
    }
    return varieteList;
}

0 个答案:

没有答案