blob: 56fd66d32c8c84352f44b0462e0d1411eda0ee7c [file] [log] [blame]
Yingdi Yu0c3e5912015-03-17 14:22:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07003 * Copyright (c) 2014-2017, Regents of the University of California
Yingdi Yu0c3e5912015-03-17 14:22:38 -07004 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07005 * 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 Yu0c3e5912015-03-17 14:22:38 -07008 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07009 * 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 Yu0c3e5912015-03-17 14:22:38 -070013 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070014 * 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 Yu0c3e5912015-03-17 14:22:38 -070017 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070018 * 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 Yu0c3e5912015-03-17 14:22:38 -070020 */
21
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070022#ifndef NDN_DELOREAN_CORE_SUB_TREE_HPP
23#define NDN_DELOREAN_CORE_SUB_TREE_HPP
Yingdi Yu0c3e5912015-03-17 14:22:38 -070024
25#include "node.hpp"
26
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070027namespace ndn {
28namespace delorean {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070029
30typedef std::function<void(const Node::Index&)> CompleteCallback;
31typedef std::function<void(const Node::Index&,
32 const NonNegativeInteger&,
33 ndn::ConstBufferPtr)> RootUpdateCallback;
34
35class SubTreeBinary
36{
37public:
38 class Error : public std::runtime_error
39 {
40 public:
41 explicit
42 Error(const std::string& what)
43 : std::runtime_error(what)
44 {
45 }
46 };
47
48public:
49 /**
50 * @brief Constructor
51 *
52 * Create an empty subtree.
53 *
54 * @param loggerName The name of logger
55 * @param completeCallback Callback when the subtree is complete
56 * @param rootUpdateCallback Callback when the subtree root is updated
57 */
58 SubTreeBinary(const Name& loggerName,
59 const CompleteCallback& completeCallback,
60 const RootUpdateCallback& rootUpdateCallback);
61 /**
62 * @brief Constructor
63 *
64 * Create a subtree with its first leaf node hash.
65 *
66 * @param loggerName The name of logger
67 * @param rootIndex The index of sub tree root when it is full
68 * @param completeCallback Callback when the subtree is complete
69 * @param rootUpdateCallback Callback when the subtree root is updated
70 */
71 SubTreeBinary(const Name& loggerName,
72 const Node::Index& rootIndex,
73 const CompleteCallback& completeCallback,
74 const RootUpdateCallback& rootUpdateCallback);
75
76 const Node::Index&
77 getPeakIndex() const
78 {
79 return m_peakIndex;
80 }
81
82 const NonNegativeInteger&
83 getMinSeqNo() const
84 {
85 return m_minSeqNo;
86 }
87
88 const NonNegativeInteger&
89 getMaxSeqNo() const
90 {
91 return m_maxSeqNo;
92 }
93
94 size_t
95 getLeafLevel() const
96 {
97 return m_leafLevel;
98 }
99
100 const NonNegativeInteger&
101 getNextLeafSeqNo() const;
102
103 /**
104 * @brief get the root of the subtree
105 *
106 * @return pointer to the root, nullptr if no leaf added
107 */
108 ConstNodePtr
109 getRoot() const
110 {
111 return m_actualRoot;
112 }
113
114 ndn::ConstBufferPtr
115 getRootHash() const;
116
117 ConstNodePtr
118 getNode(const Node::Index& index) const;
119
120 bool
121 addLeaf(NodePtr leaf);
122
123 bool
124 updateLeaf(const NonNegativeInteger& nextSeqNo, ndn::ConstBufferPtr hash);
125
126 bool
127 isFull() const;
128
129 shared_ptr<Data>
130 encode() const;
131
132 void
133 decode(const Data& data);
134
135public:
136 static Node::Index
137 toSubTreePeakIndex(const Node::Index& index, bool notRoot = true);
138
139private:
140 void
141 initialize(const Node::Index& peakIndex);
142
143 void
144 updateActualRoot(NodePtr node);
145
146 void
147 updateParentNode(NodePtr node);
148
149public:
150 static const size_t SUB_TREE_DEPTH;
151
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -0700152NDN_DELOREAN_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700153 static const time::milliseconds INCOMPLETE_FRESHNESS_PERIOD;
154 static const std::string COMPONENT_COMPLETE;
155 static const ssize_t OFFSET_ROOTHASH;
156 static const ssize_t OFFSET_COMPLETE;
157 static const ssize_t OFFSET_SEQNO;
158 static const ssize_t OFFSET_LEVEL;
159 static const size_t N_LOGGER_SUFFIX;
160
161private:
162 Name m_loggerName;
163 Node::Index m_peakIndex;
164 NonNegativeInteger m_minSeqNo;
165 NonNegativeInteger m_maxSeqNo;
166 size_t m_leafLevel;
167
168 CompleteCallback m_completeCallback;
169 RootUpdateCallback m_rootUpdateCallback;
170
171 NodePtr m_actualRoot;
172 bool m_isPendingLeafEmpty;
173 NonNegativeInteger m_pendingLeafSeqNo;
174
175 std::map<Node::Index, NodePtr> m_nodes;
176};
177
178typedef shared_ptr<SubTreeBinary> SubTreeBinaryPtr;
179typedef shared_ptr<const SubTreeBinary> ConstSubTreeBinaryPtr;
180
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700181} // namespace delorean
182} // namespace ndn
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700183
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -0700184#endif // NDN_DELOREAN_CORE_SUB_TREE_HPP