Skip to content

Commit

Permalink
add hooks for visiting netdb
Browse files Browse the repository at this point in the history
  • Loading branch information
majestrate committed Aug 29, 2016
1 parent 28fdd99 commit fec49e5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
9 changes: 8 additions & 1 deletion FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ namespace fs {
}

void HashedStorage::Traverse(std::vector<std::string> & files) {
Iterate([&files] (const std::string & fname) {
files.push_back(fname);
});
}

void HashedStorage::Iterate(FilenameVisitor v)
{
boost::filesystem::path p(root);
boost::filesystem::recursive_directory_iterator it(p);
boost::filesystem::recursive_directory_iterator end;
Expand All @@ -166,7 +173,7 @@ namespace fs {
if (!boost::filesystem::is_regular_file( it->status() ))
continue;
const std::string & t = it->path().string();
files.push_back(t);
v(t);
}
}
} // fs
Expand Down
4 changes: 4 additions & 0 deletions FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string>
#include <iostream>
#include <sstream>
#include <functional>

namespace i2p {
namespace fs {
Expand Down Expand Up @@ -43,6 +44,7 @@ namespace fs {
std::string suffix; /**< suffix of file in storage (extension) */

public:
typedef std::function<void(const std::string &)> FilenameVisitor;
HashedStorage(const char *n, const char *p1, const char *p2, const char *s):
name(n), prefix1(p1), prefix2(p2), suffix(s) {};

Expand All @@ -58,6 +60,8 @@ namespace fs {
void Remove(const std::string & ident);
/** find all files in storage and store list in provided vector */
void Traverse(std::vector<std::string> & files);
/** visit every file in this storage with a visitor */
void Iterate(FilenameVisitor v);
};

/** @brief Returns current application name, default 'i2pd' */
Expand Down
15 changes: 15 additions & 0 deletions NetDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,21 @@ namespace data
for ( auto & entry : m_LeaseSets)
v(entry.first, entry.second);
}

void NetDb::VisitStoredRouterInfos(RouterInfoVisitor v)
{
m_Storage.Iterate([v] (const std::string & filename) {
const i2p::data::RouterInfo ri(filename);
v(ri);
});
}

void NetDb::VisitRouterInfos(RouterInfoVisitor v)
{
std::unique_lock<std::mutex> lock(m_RouterInfosMutex);
for ( const auto & item : m_RouterInfos )
v(*item.second);
}

void NetDb::Load ()
{
Expand Down
10 changes: 8 additions & 2 deletions NetDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ namespace data

/** function for visiting a leaseset stored in a floodfill */
typedef std::function<void(const IdentHash, std::shared_ptr<LeaseSet>)> LeaseSetVisitor;


/** function for visiting a router info we have locally */
typedef std::function<void(const i2p::data::RouterInfo &)> RouterInfoVisitor;

class NetDb
{
public:
Expand Down Expand Up @@ -86,7 +89,10 @@ namespace data

/** visit all lease sets we currently store */
void VisitLeaseSets(LeaseSetVisitor v);

/** visit all router infos we have currently on disk, usually insanely expensive, does not access in memory RI */
void VisitStoredRouterInfos(RouterInfoVisitor v);
/** visit all router infos we have loaded in memory, cheaper than VisitLocalRouterInfos but locks access while visiting */
void VisitRouterInfos(RouterInfoVisitor v);
private:

void Load ();
Expand Down

0 comments on commit fec49e5

Please sign in to comment.