示例#1
0
 MemoryBackend (size_t keyBytes, Parameters const& keyValues,
     Scheduler& scheduler, beast::Journal journal)
     : name_ (keyValues ["path"].toStdString ())
     , journal_ (journal)
 {
     if (name_.empty())
         throw std::runtime_error ("Missing path in Memory backend");
     db_ = &memoryFactory.open(name_);
 }
示例#2
0
 MemoryBackend (size_t keyBytes, Section const& keyValues,
     Scheduler& scheduler, beast::Journal journal)
     : name_ (get<std::string>(keyValues, "path"))
     , journal_ (journal)
 {
     if (name_.empty())
         throw std::runtime_error ("Missing path in Memory backend");
     db_ = &memoryFactory.open(name_);
 }
示例#3
0
namespace NodeStore {

struct MemoryDB
{
    std::mutex mutex;
    bool open = false;
    std::map <uint256 const, NodeObject::Ptr> table;
};

class MemoryFactory : public Factory
{
private:
    std::mutex mutex_;
    std::map <std::string, MemoryDB, beast::ci_less> map_;

public:
    MemoryFactory();
    ~MemoryFactory();

    std::string
    getName() const;

    std::unique_ptr <Backend>
    createInstance (
        size_t keyBytes,
        Parameters const& keyValues,
        Scheduler& scheduler,
        beast::Journal journal);

    MemoryDB&
    open (std::string const& path)
    {
        std::lock_guard<std::mutex> _(mutex_);
        auto const result = map_.emplace (std::piecewise_construct,
            std::make_tuple(path), std::make_tuple());
        MemoryDB& db = result.first->second;
        if (db.open)
            throw std::runtime_error("already open");
        return db;
    }
};

static MemoryFactory memoryFactory;

//------------------------------------------------------------------------------

class MemoryBackend : public Backend
{
private:
    using Map = std::map <uint256 const, NodeObject::Ptr>;

    std::string name_;
    beast::Journal journal_;
    MemoryDB* db_;

public:
    MemoryBackend (size_t keyBytes, Parameters const& keyValues,
        Scheduler& scheduler, beast::Journal journal)
        : name_ (keyValues ["path"].toStdString ())
        , journal_ (journal)
    {
        if (name_.empty())
            throw std::runtime_error ("Missing path in Memory backend");
        db_ = &memoryFactory.open(name_);
    }

    ~MemoryBackend ()
    {
        close();
    }

    std::string
    getName ()
    {
        return name_;
    }

    void
    close() override
    {
        db_ = nullptr;
    }

    //--------------------------------------------------------------------------

    Status
    fetch (void const* key, NodeObject::Ptr* pObject)
    {
        uint256 const hash (uint256::fromVoid (key));

        std::lock_guard<std::mutex> _(db_->mutex);

        Map::iterator iter = db_->table.find (hash);
        if (iter == db_->table.end())
        {
            pObject->reset();
            return notFound;
        }
        *pObject = iter->second;
        return ok;
    }

    void
    store (NodeObject::ref object)
    {
        std::lock_guard<std::mutex> _(db_->mutex);
        db_->table.emplace (object->getHash(), object);
    }

    void
    storeBatch (Batch const& batch)
    {
        for (auto const& e : batch)
            store (e);
    }

    void
    for_each (std::function <void(NodeObject::Ptr)> f)
    {
        for (auto const& e : db_->table)
            f (e.second);
    }

    int
    getWriteLoad()
    {
        return 0;
    }

    void
    setDeletePath() override
    {
    }

    void
    verify() override
    {
    }
};

//------------------------------------------------------------------------------

MemoryFactory::MemoryFactory()
{
    Manager::instance().insert(*this);
}

MemoryFactory::~MemoryFactory()
{
    Manager::instance().erase(*this);
}

std::string
MemoryFactory::getName() const
{
    return "Memory";
}

std::unique_ptr <Backend>
MemoryFactory::createInstance (
    size_t keyBytes,
    Parameters const& keyValues,
    Scheduler& scheduler,
    beast::Journal journal)
{
    return std::make_unique <MemoryBackend> (
        keyBytes, keyValues, scheduler, journal);
}

}