Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 5 | * This file is part of NDN DeLorean, An Authentication System for Data Archives in |
| 6 | * Named Data Networking. See AUTHORS.md for complete list of NDN DeLorean authors |
| 7 | * and contributors. |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 8 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 9 | * NDN DeLorean is free software: you can redistribute it and/or modify it under |
| 10 | * the terms of the GNU General Public License as published by the Free Software |
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
| 12 | * version. |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 13 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 14 | * NDN DeLorean is distributed in the hope that it will be useful, but WITHOUT ANY |
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 16 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 17 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 18 | * You should have received a copy of the GNU General Public License along with NDN |
| 19 | * DeLorean, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 22 | #ifndef NDN_DELOREAN_CORE_SUB_TREE_HPP |
| 23 | #define NDN_DELOREAN_CORE_SUB_TREE_HPP |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 24 | |
| 25 | #include "node.hpp" |
| 26 | |
| 27 | namespace nsl { |
| 28 | |
| 29 | typedef std::function<void(const Node::Index&)> CompleteCallback; |
| 30 | typedef std::function<void(const Node::Index&, |
| 31 | const NonNegativeInteger&, |
| 32 | ndn::ConstBufferPtr)> RootUpdateCallback; |
| 33 | |
| 34 | class SubTreeBinary |
| 35 | { |
| 36 | public: |
| 37 | class Error : public std::runtime_error |
| 38 | { |
| 39 | public: |
| 40 | explicit |
| 41 | Error(const std::string& what) |
| 42 | : std::runtime_error(what) |
| 43 | { |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | public: |
| 48 | /** |
| 49 | * @brief Constructor |
| 50 | * |
| 51 | * Create an empty subtree. |
| 52 | * |
| 53 | * @param loggerName The name of logger |
| 54 | * @param completeCallback Callback when the subtree is complete |
| 55 | * @param rootUpdateCallback Callback when the subtree root is updated |
| 56 | */ |
| 57 | SubTreeBinary(const Name& loggerName, |
| 58 | const CompleteCallback& completeCallback, |
| 59 | const RootUpdateCallback& rootUpdateCallback); |
| 60 | /** |
| 61 | * @brief Constructor |
| 62 | * |
| 63 | * Create a subtree with its first leaf node hash. |
| 64 | * |
| 65 | * @param loggerName The name of logger |
| 66 | * @param rootIndex The index of sub tree root when it is full |
| 67 | * @param completeCallback Callback when the subtree is complete |
| 68 | * @param rootUpdateCallback Callback when the subtree root is updated |
| 69 | */ |
| 70 | SubTreeBinary(const Name& loggerName, |
| 71 | const Node::Index& rootIndex, |
| 72 | const CompleteCallback& completeCallback, |
| 73 | const RootUpdateCallback& rootUpdateCallback); |
| 74 | |
| 75 | const Node::Index& |
| 76 | getPeakIndex() const |
| 77 | { |
| 78 | return m_peakIndex; |
| 79 | } |
| 80 | |
| 81 | const NonNegativeInteger& |
| 82 | getMinSeqNo() const |
| 83 | { |
| 84 | return m_minSeqNo; |
| 85 | } |
| 86 | |
| 87 | const NonNegativeInteger& |
| 88 | getMaxSeqNo() const |
| 89 | { |
| 90 | return m_maxSeqNo; |
| 91 | } |
| 92 | |
| 93 | size_t |
| 94 | getLeafLevel() const |
| 95 | { |
| 96 | return m_leafLevel; |
| 97 | } |
| 98 | |
| 99 | const NonNegativeInteger& |
| 100 | getNextLeafSeqNo() const; |
| 101 | |
| 102 | /** |
| 103 | * @brief get the root of the subtree |
| 104 | * |
| 105 | * @return pointer to the root, nullptr if no leaf added |
| 106 | */ |
| 107 | ConstNodePtr |
| 108 | getRoot() const |
| 109 | { |
| 110 | return m_actualRoot; |
| 111 | } |
| 112 | |
| 113 | ndn::ConstBufferPtr |
| 114 | getRootHash() const; |
| 115 | |
| 116 | ConstNodePtr |
| 117 | getNode(const Node::Index& index) const; |
| 118 | |
| 119 | bool |
| 120 | addLeaf(NodePtr leaf); |
| 121 | |
| 122 | bool |
| 123 | updateLeaf(const NonNegativeInteger& nextSeqNo, ndn::ConstBufferPtr hash); |
| 124 | |
| 125 | bool |
| 126 | isFull() const; |
| 127 | |
| 128 | shared_ptr<Data> |
| 129 | encode() const; |
| 130 | |
| 131 | void |
| 132 | decode(const Data& data); |
| 133 | |
| 134 | public: |
| 135 | static Node::Index |
| 136 | toSubTreePeakIndex(const Node::Index& index, bool notRoot = true); |
| 137 | |
| 138 | private: |
| 139 | void |
| 140 | initialize(const Node::Index& peakIndex); |
| 141 | |
| 142 | void |
| 143 | updateActualRoot(NodePtr node); |
| 144 | |
| 145 | void |
| 146 | updateParentNode(NodePtr node); |
| 147 | |
| 148 | public: |
| 149 | static const size_t SUB_TREE_DEPTH; |
| 150 | |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 151 | NDN_DELOREAN_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 152 | static const time::milliseconds INCOMPLETE_FRESHNESS_PERIOD; |
| 153 | static const std::string COMPONENT_COMPLETE; |
| 154 | static const ssize_t OFFSET_ROOTHASH; |
| 155 | static const ssize_t OFFSET_COMPLETE; |
| 156 | static const ssize_t OFFSET_SEQNO; |
| 157 | static const ssize_t OFFSET_LEVEL; |
| 158 | static const size_t N_LOGGER_SUFFIX; |
| 159 | |
| 160 | private: |
| 161 | Name m_loggerName; |
| 162 | Node::Index m_peakIndex; |
| 163 | NonNegativeInteger m_minSeqNo; |
| 164 | NonNegativeInteger m_maxSeqNo; |
| 165 | size_t m_leafLevel; |
| 166 | |
| 167 | CompleteCallback m_completeCallback; |
| 168 | RootUpdateCallback m_rootUpdateCallback; |
| 169 | |
| 170 | NodePtr m_actualRoot; |
| 171 | bool m_isPendingLeafEmpty; |
| 172 | NonNegativeInteger m_pendingLeafSeqNo; |
| 173 | |
| 174 | std::map<Node::Index, NodePtr> m_nodes; |
| 175 | }; |
| 176 | |
| 177 | typedef shared_ptr<SubTreeBinary> SubTreeBinaryPtr; |
| 178 | typedef shared_ptr<const SubTreeBinary> ConstSubTreeBinaryPtr; |
| 179 | |
| 180 | } // namespace nsl |
| 181 | |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 182 | #endif // NDN_DELOREAN_CORE_SUB_TREE_HPP |