blob: ba52e1bdd3895bfa17cf94e94e8f8bdb95678eb6 [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
27namespace nsl {
28
29typedef std::function<void(const Node::Index&)> CompleteCallback;
30typedef std::function<void(const Node::Index&,
31 const NonNegativeInteger&,
32 ndn::ConstBufferPtr)> RootUpdateCallback;
33
34class SubTreeBinary
35{
36public:
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
47public:
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
134public:
135 static Node::Index
136 toSubTreePeakIndex(const Node::Index& index, bool notRoot = true);
137
138private:
139 void
140 initialize(const Node::Index& peakIndex);
141
142 void
143 updateActualRoot(NodePtr node);
144
145 void
146 updateParentNode(NodePtr node);
147
148public:
149 static const size_t SUB_TREE_DEPTH;
150
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -0700151NDN_DELOREAN_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700152 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
160private:
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
177typedef shared_ptr<SubTreeBinary> SubTreeBinaryPtr;
178typedef shared_ptr<const SubTreeBinary> ConstSubTreeBinaryPtr;
179
180} // namespace nsl
181
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -0700182#endif // NDN_DELOREAN_CORE_SUB_TREE_HPP