From 28c7827f144fb36a6da68245f0ec9cf477f90c4b Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Fri, 28 Feb 2014 09:19:37 -0800 Subject: [PATCH] Calculate program file directories: * Determine location of database files in Config * Inject database directory or file path in PeerFinder and Validators * PeerFinder and Validators will share the same sqlite file --- src/ripple/peerfinder/api/Manager.h | 1 + src/ripple/peerfinder/impl/Manager.cpp | 16 ++++++++++------ src/ripple/validators/api/Manager.h | 6 +++++- src/ripple/validators/impl/Manager.cpp | 24 +++++++++++++++++------- src/ripple_app/main/Application.cpp | 8 +++++--- src/ripple_core/functional/Config.cpp | 12 ++++++++++++ src/ripple_core/functional/Config.h | 13 +++++++++++++ src/ripple_overlay/api/Peers.h | 12 +++++++----- src/ripple_overlay/impl/Peers.cpp | 24 ++++++++++++++---------- 9 files changed, 84 insertions(+), 32 deletions(-) diff --git a/src/ripple/peerfinder/api/Manager.h b/src/ripple/peerfinder/api/Manager.h index 9cf00402a..7039bc409 100644 --- a/src/ripple/peerfinder/api/Manager.h +++ b/src/ripple/peerfinder/api/Manager.h @@ -39,6 +39,7 @@ public: static Manager* New ( Stoppable& parent, SiteFiles::Manager& siteFiles, + File const& pathToDbFileOrDirectory, Callback& callback, clock_type& clock, Journal journal); diff --git a/src/ripple/peerfinder/impl/Manager.cpp b/src/ripple/peerfinder/impl/Manager.cpp index a03d7655c..d3a3d0471 100644 --- a/src/ripple/peerfinder/impl/Manager.cpp +++ b/src/ripple/peerfinder/impl/Manager.cpp @@ -30,6 +30,7 @@ class ManagerImp public: ServiceQueue m_queue; SiteFiles::Manager& m_siteFiles; + File m_databaseFile; clock_type& m_clock; Journal m_journal; StoreSqdb m_store; @@ -43,12 +44,14 @@ public: ManagerImp ( Stoppable& stoppable, SiteFiles::Manager& siteFiles, + File const& pathToDbFileOrDirectory, Callback& callback, clock_type& clock, Journal journal) : Manager (stoppable) , Thread ("PeerFinder") , m_siteFiles (siteFiles) + , m_databaseFile (pathToDbFileOrDirectory) , m_clock (clock) , m_journal (journal) , m_store (journal) @@ -56,6 +59,8 @@ public: , m_logic (clock, callback, m_store, m_checker, journal) , m_secondsTimer (this) { + if (m_databaseFile.isDirectory ()) + m_databaseFile = m_databaseFile.getChildFile("peerfinder.sqlite"); } ~ManagerImp () @@ -262,15 +267,12 @@ public: { m_journal.debug << "Initializing"; - File const file (File::getSpecialLocation ( - File::userDocumentsDirectory).getChildFile ("PeerFinder.sqlite")); - - Error error (m_store.open (file)); + Error error (m_store.open (m_databaseFile)); if (error) { m_journal.fatal << - "Failed to open '" << file.getFullPathName() << "'"; + "Failed to open '" << m_databaseFile.getFullPathName() << "'"; } if (! error) @@ -311,11 +313,13 @@ Manager::Manager (Stoppable& parent) Manager* Manager::New ( Stoppable& parent, SiteFiles::Manager& siteFiles, + File const& databaseFile, Callback& callback, clock_type& clock, Journal journal) { - return new ManagerImp (parent, siteFiles, callback, clock, journal); + return new ManagerImp (parent, siteFiles, databaseFile, + callback, clock, journal); } } diff --git a/src/ripple/validators/api/Manager.h b/src/ripple/validators/api/Manager.h index 970865ac5..759afc663 100644 --- a/src/ripple/validators/api/Manager.h +++ b/src/ripple/validators/api/Manager.h @@ -36,9 +36,13 @@ protected: public: /** Create a new Manager object. @param parent The parent Stoppable. + @param pathToDbFileOrDirectory The directory where our database is stored @param journal Where to send log output. */ - static Manager* New (Stoppable& stoppableParent, Journal journal); + static Manager* New ( + Stoppable& stoppableParent, + File const& pathToDbFileOrDirectory, + Journal journal); /** Destroy the object. Any pending source fetch operations are aborted. This will block diff --git a/src/ripple/validators/impl/Manager.cpp b/src/ripple/validators/impl/Manager.cpp index 1f40a6a70..49aabcb51 100644 --- a/src/ripple/validators/impl/Manager.cpp +++ b/src/ripple/validators/impl/Manager.cpp @@ -134,6 +134,7 @@ class ManagerImp { public: Journal m_journal; + File m_databaseFile; StoreSqdb m_store; Logic m_logic; DeadlineTimer m_checkTimer; @@ -149,10 +150,14 @@ public: // bool m_checkSources; - ManagerImp (Stoppable& parent, Journal journal) + ManagerImp ( + Stoppable& parent, + File const& pathToDbFileOrDirectory, + Journal journal) : Stoppable ("Validators::Manager", parent) , Thread ("Validators") , m_journal (journal) + , m_databaseFile (pathToDbFileOrDirectory) , m_store (m_journal) , m_logic (m_store, m_journal) , m_checkTimer (this) @@ -164,6 +169,11 @@ public: "Validators constructed (debug)"; m_journal.info << "Validators constructed (info)"; + + if (m_databaseFile.isDirectory ()) + m_databaseFile = m_databaseFile.getChildFile("validators.sqlite"); + + } ~ManagerImp () @@ -306,11 +316,8 @@ public: void init () { - File const file (File::getSpecialLocation ( - File::userDocumentsDirectory).getChildFile ("validators.sqlite")); + Error error (m_store.open (m_databaseFile)); - Error error (m_store.open (file)); - if (! error) { m_logic.load (); @@ -375,9 +382,12 @@ Manager::Manager () { } -Validators::Manager* Validators::Manager::New (Stoppable& parent, Journal journal) +Validators::Manager* Validators::Manager::New ( + Stoppable& parent, + File const& pathToDbFileOrDirectory, + Journal journal) { - return new Validators::ManagerImp (parent, journal); + return new Validators::ManagerImp (parent, pathToDbFileOrDirectory, journal); } } diff --git a/src/ripple_app/main/Application.cpp b/src/ripple_app/main/Application.cpp index 915cf865d..9584b4049 100644 --- a/src/ripple_app/main/Application.cpp +++ b/src/ripple_app/main/Application.cpp @@ -288,7 +288,9 @@ public: , m_txQueue (TxQueue::New ()) , m_validators (add (Validators::Manager::New ( - *this, LogPartition::getJournal ()))) + *this, + getConfig ().getModuleDatabasePath (), + LogPartition::getJournal ()))) , mFeatures (IFeatures::New (2 * 7 * 24 * 60 * 60, 200)) // two weeks, 200/256 @@ -685,8 +687,8 @@ public: // // if (!getConfig ().RUN_STANDALONE) m_peers.reset (add (Peers::New (m_mainIoPool, *m_resourceManager, - *m_siteFiles, *m_resolver, m_mainIoPool, - m_peerSSLContext->get ()))); + *m_siteFiles, getConfig ().getModuleDatabasePath (), + *m_resolver, m_mainIoPool, m_peerSSLContext->get ()))); // SSL context used for WebSocket connections. if (getConfig ().WEBSOCKET_SECURE) diff --git a/src/ripple_core/functional/Config.cpp b/src/ripple_core/functional/Config.cpp index 93f2451ba..3c6f48f57 100644 --- a/src/ripple_core/functional/Config.cpp +++ b/src/ripple_core/functional/Config.cpp @@ -239,6 +239,12 @@ void Config::setup (const std::string& strConf, bool bQuiet) if (ec) throw std::runtime_error (boost::str (boost::format ("Can not create %s") % DATA_DIR)); + + // Create the new unified database + m_moduleDbPath = getDatabaseDir(); + + if (m_moduleDbPath.isDirectory ()) + m_moduleDbPath = m_moduleDbPath.getChildFile("rippled.sqlite"); } void Config::load () @@ -759,6 +765,12 @@ Config::Role Config::getAdminRole (Json::Value const& params, beast::IP::Endpoin return role; } +//------------------------------------------------------------------------------ +File const& Config::getModuleDatabasePath () +{ + return m_moduleDbPath; +} + // // //------------------------------------------------------------------------------ diff --git a/src/ripple_core/functional/Config.h b/src/ripple_core/functional/Config.h index 64bf537d9..da21c0e58 100644 --- a/src/ripple_core/functional/Config.h +++ b/src/ripple_core/functional/Config.h @@ -282,7 +282,20 @@ public: private: std::string m_rpcIP; int m_rpcPort; // VFALCO TODO This should be a short. + +private: + /** The folder where new module databases should be located */ + File m_moduleDbPath; + public: + //-------------------------------------------------------------------------- + /** Returns the location were databases should be located + The location may be a file, in which case databases should be placed in + the file, or it may be a directory, in which cases databases should be + stored in a file named after the module (e.g. "peerfinder.sqlite") that + is inside that directory. + */ + File const& getModuleDatabasePath (); //-------------------------------------------------------------------------- diff --git a/src/ripple_overlay/api/Peers.h b/src/ripple_overlay/api/Peers.h index 65d806e39..4207bd850 100644 --- a/src/ripple_overlay/api/Peers.h +++ b/src/ripple_overlay/api/Peers.h @@ -66,12 +66,14 @@ protected: public: typedef std::vector PeerSequence; - static Peers* New (Stoppable& parent, + static Peers* New ( + Stoppable& parent, Resource::Manager& resourceManager, - SiteFiles::Manager& siteFiles, - Resolver& resolver, - boost::asio::io_service& io_service, - boost::asio::ssl::context& context); + SiteFiles::Manager& siteFiles, + File const& pathToDbFileOrDirectory, + Resolver& resolver, + boost::asio::io_service& io_service, + boost::asio::ssl::context& context); virtual ~Peers () = 0; diff --git a/src/ripple_overlay/impl/Peers.cpp b/src/ripple_overlay/impl/Peers.cpp index 97a905dc6..fb7a1ecea 100644 --- a/src/ripple_overlay/impl/Peers.cpp +++ b/src/ripple_overlay/impl/Peers.cpp @@ -142,10 +142,11 @@ public: PeersImp (Stoppable& parent, Resource::Manager& resourceManager, - SiteFiles::Manager& siteFiles, - Resolver& resolver, - boost::asio::io_service& io_service, - boost::asio::ssl::context& ssl_context) + SiteFiles::Manager& siteFiles, + File const& pathToDbFileOrDirectory, + Resolver& resolver, + boost::asio::io_service& io_service, + boost::asio::ssl::context& ssl_context) : Peers (parent) , m_child_count (1) , m_journal (LogPartition::getJournal ()) @@ -153,6 +154,7 @@ public: , m_peerFinder (add (PeerFinder::Manager::New ( *this, siteFiles, + pathToDbFileOrDirectory, *this, get_seconds_clock (), LogPartition::getJournal ()))) @@ -600,15 +602,17 @@ Peers::~Peers () { } -Peers* Peers::New (Stoppable& parent, +Peers* Peers::New ( + Stoppable& parent, Resource::Manager& resourceManager, - SiteFiles::Manager& siteFiles, - Resolver& resolver, - boost::asio::io_service& io_service, - boost::asio::ssl::context& ssl_context) + SiteFiles::Manager& siteFiles, + File const& pathToDbFileOrDirectory, + Resolver& resolver, + boost::asio::io_service& io_service, + boost::asio::ssl::context& ssl_context) { return new PeersImp (parent, resourceManager, siteFiles, - resolver, io_service, ssl_context); + pathToDbFileOrDirectory, resolver, io_service, ssl_context); } }