From a60d5c812a26fcdcd0aa3af4fbee9f3b09243bfc Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sun, 3 May 2015 11:28:59 +0300 Subject: [PATCH] Fixed RaftCore API --- .../dynamic_node_id_allocation_server.hpp | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp index 7e3f6abed9..c310fb46bf 100644 --- a/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_allocation_server.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include // Types used by the server #include @@ -384,16 +385,31 @@ public: */ void appendLog(const protocol::dynamic_node_id::server::Entry& entry); + /** + * This class is used to perform log searches. + */ + struct LogEntryInfo + { + protocol::dynamic_node_id::server::Entry entry; + bool committed; + + LogEntryInfo(const protocol::dynamic_node_id::server::Entry& arg_entry, bool arg_committed) + : entry(arg_entry) + , committed(arg_committed) + { } + }; + /** * This method is used by the allocator to query existence of certain entries in the Raft log. * Predicate is a callable of the following prototype: - * bool (const protocol::dynamic_node_id::server::Entry&) - * Once the predicate returns true, the loop will be terminated and the method will return a pointer to the last - * visited entry; otherwise nullptr will be returned. + * bool (const LogEntryInfo& entry) + * Once the predicate returns true, the loop will be terminated and the method will return an initialized lazy + * contructor to the last visited entry; otherwise the constructor will not be initialized. In this case, lazy + * constructor is used as boost::optional. * The log is always traversed from HIGH to LOW index values, i.e. entry 0 will be traversed last. */ template - const protocol::dynamic_node_id::server::Entry* traverseLogFromEndUntil(const Predicate& predicate) const + inline LazyConstructor traverseLogFromEndUntil(const Predicate& predicate) const { UAVCAN_ASSERT(try_implicit_cast(predicate, true)); for (int index = static_cast(persistent_state_.getLog().getMaxIndex()); index--; index >= 0) @@ -401,12 +417,15 @@ public: const protocol::dynamic_node_id::server::Entry* const entry = persistent_state_.getLog().getEntryAtIndex(Log::Index(index)); UAVCAN_ASSERT(entry != NULL); - if (predicate(*entry)) + const LogEntryInfo info(*entry, Log::Index(index) <= commit_index_); + if (predicate(info)) { - return entry; + LazyConstructor ret; + ret.template construct(info); + return ret; } } - return NULL; + return LazyConstructor(); } };