blob: 74716e6684a53c122a0609f13ec7a77efa4cb1d2 [file] [log] [blame]
peizhen guocf4df2d2014-08-12 13:22:32 -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
peizhen guocf4df2d2014-08-12 13:22:32 -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.
peizhen guocf4df2d2014-08-12 13:22:32 -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.
peizhen guocf4df2d2014-08-12 13:22:32 -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.
peizhen guocf4df2d2014-08-12 13:22:32 -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/>.
peizhen guocf4df2d2014-08-12 13:22:32 -070020 */
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070021
peizhen guocf4df2d2014-08-12 13:22:32 -070022#include "node.hpp"
23
Yingdi Yu0c3e5912015-03-17 14:22:38 -070024#include <ndn-cxx/util/digest.hpp>
25#include <boost/lexical_cast.hpp>
26
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070027namespace ndn {
28namespace delorean {
peizhen guocf4df2d2014-08-12 13:22:32 -070029
Yingdi Yu0c3e5912015-03-17 14:22:38 -070030ndn::ConstBufferPtr Node::EMPTY_HASH;
31
32Node::Index::Index(const NonNegativeInteger& nodeSeq, size_t nodeLevel)
33 : seqNo(nodeSeq)
34 , level(nodeLevel)
35 , range(1 << nodeLevel)
peizhen guocf4df2d2014-08-12 13:22:32 -070036{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070037 if (seqNo % range != 0)
38 throw Error("Index: index level and seqNo do not match: (" +
39 boost::lexical_cast<std::string>(seqNo) + ", " +
40 boost::lexical_cast<std::string>(level) + ")");
peizhen guocf4df2d2014-08-12 13:22:32 -070041}
42
Yingdi Yu0c3e5912015-03-17 14:22:38 -070043bool
44Node::Index::operator<(const Index& other) const
peizhen guocf4df2d2014-08-12 13:22:32 -070045{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070046 if (seqNo < other.seqNo) {
47 return true;
48 }
49 else if (seqNo == other.seqNo) {
50 return level < other.level;
51 }
52 else {
53 return false;
54 }
peizhen guocf4df2d2014-08-12 13:22:32 -070055}
56
Yingdi Yu0c3e5912015-03-17 14:22:38 -070057bool
58Node::Index::operator==(const Index& other) const
peizhen guocf4df2d2014-08-12 13:22:32 -070059{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070060 return equals(other);
peizhen guocf4df2d2014-08-12 13:22:32 -070061}
62
Yingdi Yu0c3e5912015-03-17 14:22:38 -070063bool
64Node::Index::operator!=(const Index& other) const
65{
66 return !equals(other);
67}
peizhen guocf4df2d2014-08-12 13:22:32 -070068
Yingdi Yu0c3e5912015-03-17 14:22:38 -070069bool
70Node::Index::equals(const Index& other) const
71{
72 if (seqNo == other.seqNo && level == other.level) {
73 return true;
74 }
75 else {
76 return false;
77 }
78}
79
80Node::Node(const NonNegativeInteger& nodeSeqNo,
81 size_t nodeLevel,
82 const NonNegativeInteger& leafSeqNo,
83 ndn::ConstBufferPtr hash)
84 : m_index(nodeSeqNo, nodeLevel)
85 , m_hash(hash)
86{
87 if (leafSeqNo == 0 && m_index.seqNo > leafSeqNo)
88 m_leafSeqNo = m_index.seqNo;
89 else
90 setLeafSeqNo(leafSeqNo);
91}
peizhen guocf4df2d2014-08-12 13:22:32 -070092
93void
Yingdi Yu0c3e5912015-03-17 14:22:38 -070094Node::setLeafSeqNo(const NonNegativeInteger& leafSeqNo)
peizhen guocf4df2d2014-08-12 13:22:32 -070095{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070096 if (leafSeqNo > m_index.seqNo + m_index.range || leafSeqNo < m_index.seqNo)
97 throw Error("Node: leaf seqNo is out of range");
98
99 m_leafSeqNo = leafSeqNo;
peizhen guocf4df2d2014-08-12 13:22:32 -0700100}
101
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700102void
103Node::setHash(ndn::ConstBufferPtr hash)
104{
105 m_hash = hash;
106}
peizhen guocf4df2d2014-08-12 13:22:32 -0700107
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700108bool
109Node::isFull() const
110{
111 return m_index.seqNo + m_index.range == m_leafSeqNo;
112}
peizhen guocf4df2d2014-08-12 13:22:32 -0700113
114ndn::ConstBufferPtr
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700115Node::getEmptyHash()
peizhen guocf4df2d2014-08-12 13:22:32 -0700116{
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700117 if (EMPTY_HASH == nullptr) {
118 ndn::util::Sha256 sha256;
119 EMPTY_HASH = sha256.computeDigest();
120 }
121
122 return EMPTY_HASH;
peizhen guocf4df2d2014-08-12 13:22:32 -0700123}
124
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700125} // namespace delorean
126} // namespace ndn