Files
xahaud/src/ripple/nodestore/backend/MemoryFactory.cpp
Vinnie Falco 8be4e7e65f Refactor Serializer, SerialIter, SHAMapItem, NodeObject:
* Make LessThan private
* Make NodeObject::isSame private
* Remove hotTRANSACTION
* Remove some Serializer members
* Remove unused SHAMapItem::getRaw
* Remove unused STLedgerEntry::getOwners
* Remove Serializer constructors
* Remove unused Serializer members
* Remove SerialIter ctor
2015-06-02 12:55:07 -07:00

213 lines
4.9 KiB
C++

//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/nodestore/Factory.h>
#include <ripple/nodestore/Manager.h>
#include <beast/utility/ci_char_traits.h>
#include <beast/cxx14/memory.h> // <memory>
#include <map>
#include <mutex>
namespace ripple {
namespace NodeStore {
struct MemoryDB
{
std::mutex mutex;
bool open = false;
std::map <uint256 const, std::shared_ptr<NodeObject>> 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,
Section 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, std::shared_ptr<NodeObject>>;
std::string name_;
beast::Journal journal_;
MemoryDB* db_;
public:
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_);
}
~MemoryBackend ()
{
close();
}
std::string
getName ()
{
return name_;
}
void
close() override
{
db_ = nullptr;
}
//--------------------------------------------------------------------------
Status
fetch (void const* key, std::shared_ptr<NodeObject>* 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;
}
bool
canFetchBatch() override
{
return false;
}
std::vector<std::shared_ptr<NodeObject>>
fetchBatch (std::size_t n, void const* const* keys) override
{
throw std::runtime_error("pure virtual called");
return {};
}
void
store (std::shared_ptr<NodeObject> const& 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(std::shared_ptr<NodeObject>)> 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,
Section const& keyValues,
Scheduler& scheduler,
beast::Journal journal)
{
return std::make_unique <MemoryBackend> (
keyBytes, keyValues, scheduler, journal);
}
}
}