//------------------------------------------------------------------------------ /* 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 #include #include #include #include // #include #include namespace ripple { namespace NodeStore { struct MemoryDB { std::mutex mutex; bool open = false; std::map > table; }; class MemoryFactory : public Factory { private: std::mutex mutex_; std::map map_; public: MemoryFactory(); ~MemoryFactory(); std::string getName() const; std::unique_ptr createInstance ( size_t keyBytes, Section const& keyValues, Scheduler& scheduler, beast::Journal journal); MemoryDB& open (std::string const& path) { std::lock_guard _(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 >; std::string name_; beast::Journal journal_; MemoryDB* db_; public: MemoryBackend (size_t keyBytes, Section const& keyValues, Scheduler& scheduler, beast::Journal journal) : name_ (get(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* pObject) { uint256 const hash (uint256::fromVoid (key)); std::lock_guard _(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> fetchBatch (std::size_t n, void const* const* keys) override { throw std::runtime_error("pure virtual called"); return {}; } void store (std::shared_ptr const& object) { std::lock_guard _(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 )> 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 MemoryFactory::createInstance ( size_t keyBytes, Section const& keyValues, Scheduler& scheduler, beast::Journal journal) { return std::make_unique ( keyBytes, keyValues, scheduler, journal); } } }