Skip to content

Commit

Permalink
FOLD: Use set instead of map
Browse files Browse the repository at this point in the history
  • Loading branch information
thejohnfreeman committed Jul 12, 2019
1 parent b5b1a76 commit 530f3bb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
29 changes: 24 additions & 5 deletions src/ripple/overlay/PeerReservationTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define RIPPLE_OVERLAY_PEER_RESERVATION_TABLE_H_INCLUDED

#include <ripple/beast/hash/uhash.h>
#include <ripple/beast/hash/hash_append.h>
#include <ripple/beast/utility/Journal.h>
#include <ripple/json/json_forwards.h>
#include <ripple/protocol/PublicKey.h>
Expand All @@ -30,7 +31,7 @@
#include <soci/soci.h>

#include <string>
#include <unordered_map>
#include <unordered_set>

namespace ripple {

Expand All @@ -41,17 +42,35 @@ struct PeerReservation
{
public:
PublicKey nodeId;
boost::optional<std::string> description;
mutable boost::optional<std::string> description;

auto
toJson() const -> Json::Value;

template <typename Hasher>
friend void hash_append(Hasher& h, PeerReservation const& x) noexcept
{
using beast::hash_append;
hash_append(h, x.nodeId);
}
};

// TODO: When C++20 arrives, take advantage of "equivalence" instead of
// "equality". Add an overload for `(PublicKey, PeerReservation)`, and just
// pass a `PublicKey` directly to `unordered_set.find`.
struct KeyEqual {
bool operator() (
PeerReservation const& lhs, PeerReservation const& rhs) const
{
return lhs.nodeId == rhs.nodeId;
}
};

class PeerReservationTable
{
public:
using table_type =
std::unordered_map<PublicKey, PeerReservation, beast::uhash<>>;
using table_type = std::unordered_set<
PeerReservation, beast::uhash<>, KeyEqual>;
using const_iterator = table_type::const_iterator;

explicit PeerReservationTable(
Expand Down Expand Up @@ -87,7 +106,7 @@ class PeerReservationTable
bool
contains(PublicKey const& nodeId)
{
return table_.find(nodeId) != table_.end();
return table_.find({nodeId}) != table_.end();
}

// Because `ApplicationImp` has two-phase initialization, so must we.
Expand Down
26 changes: 13 additions & 13 deletions src/ripple/overlay/impl/PeerReservationTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ PeerReservationTable::load(DatabaseCon& connection)
JLOG(journal_.warn()) << "load: not a public key: " << valPubKey;
// TODO: Remove any invalid public keys?
}
auto const& nodeId = *optNodeId;
table_.emplace(nodeId, PeerReservation{nodeId, valDesc});
table_.insert(PeerReservation{*optNodeId, valDesc});
}

return true;
Expand All @@ -90,18 +89,19 @@ PeerReservationTable::upsert(
boost::optional<std::string> const& desc)
-> boost::optional<PeerReservation>
{
PeerReservation const rvn{nodeId, desc};
boost::optional<PeerReservation> previous;

auto emplaced = table_.emplace(nodeId, rvn);
if (!emplaced.second)
// TODO: When C++17 arrives, replace with a structured binding to better
// variable names.
auto inserted = table_.insert(PeerReservation{nodeId, desc});
if (!inserted.second)
{
// The node already has a reservation.
// I think most people just want to overwrite existing reservations.
// TODO: Make a formal team decision that we do not want to raise an
// error upon a collision.
previous = emplaced.first->second;
emplaced.first->second = rvn;
// The node already has a reservation. Overwrite its description.
// `std::unordered_set` does not have an `upsert` method, and sadly
// makes it impossible for us to implement one efficiently:
// https://stackoverflow.com/q/49651835/618906
previous = *inserted.first;
inserted.first->description = desc;
}

auto db = connection_->checkoutDb();
Expand All @@ -120,10 +120,10 @@ PeerReservationTable::erase(PublicKey const& nodeId)
{
boost::optional<PeerReservation> previous;

auto const it = table_.find(nodeId);
auto const it = table_.find({nodeId});
if (it != table_.end())
{
previous = it->second;
previous = *it;
table_.erase(it);
auto db = connection_->checkoutDb();
*db << "DELETE FROM PeerReservations WHERE PublicKey = :nodeId",
Expand Down
3 changes: 1 addition & 2 deletions src/ripple/rpc/handlers/Reservations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ doReservationsList(RPC::Context& context)
// as a Json::Value.
Json::Value result{Json::objectValue};
Json::Value& jaReservations = result[jss::reservations] = Json::arrayValue;
for (auto const& pair : reservations)
for (auto const& reservation : reservations)
{
auto const& reservation = pair.second;
Json::Value jaReservation;
jaReservation[jss::public_key] =
toBase58(TokenType::NodePublic, reservation.nodeId);
Expand Down

0 comments on commit 530f3bb

Please sign in to comment.