blob: d0882e4858828aa8c15025831685e0f98a6a3f46 [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
peizhen guocf4df2d2014-08-12 13:22:32 -070027namespace nsl {
28
Yingdi Yu0c3e5912015-03-17 14:22:38 -070029ndn::ConstBufferPtr Node::EMPTY_HASH;
30
31Node::Index::Index(const NonNegativeInteger& nodeSeq, size_t nodeLevel)
32 : seqNo(nodeSeq)
33 , level(nodeLevel)
34 , range(1 << nodeLevel)
peizhen guocf4df2d2014-08-12 13:22:32 -070035{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070036 if (seqNo % range != 0)
37 throw Error("Index: index level and seqNo do not match: (" +
38 boost::lexical_cast<std::string>(seqNo) + ", " +
39 boost::lexical_cast<std::string>(level) + ")");
peizhen guocf4df2d2014-08-12 13:22:32 -070040}
41
Yingdi Yu0c3e5912015-03-17 14:22:38 -070042bool
43Node::Index::operator<(const Index& other) const
peizhen guocf4df2d2014-08-12 13:22:32 -070044{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070045 if (seqNo < other.seqNo) {
46 return true;
47 }
48 else if (seqNo == other.seqNo) {
49 return level < other.level;
50 }
51 else {
52 return false;
53 }
peizhen guocf4df2d2014-08-12 13:22:32 -070054}
55
Yingdi Yu0c3e5912015-03-17 14:22:38 -070056bool
57Node::Index::operator==(const Index& other) const
peizhen guocf4df2d2014-08-12 13:22:32 -070058{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070059 return equals(other);
peizhen guocf4df2d2014-08-12 13:22:32 -070060}
61
Yingdi Yu0c3e5912015-03-17 14:22:38 -070062bool
63Node::Index::operator!=(const Index& other) const
64{
65 return !equals(other);
66}
peizhen guocf4df2d2014-08-12 13:22:32 -070067
Yingdi Yu0c3e5912015-03-17 14:22:38 -070068bool
69Node::Index::equals(const Index& other) const
70{
71 if (seqNo == other.seqNo && level == other.level) {
72 return true;
73 }
74 else {
75 return false;
76 }
77}
78
79Node::Node(const NonNegativeInteger& nodeSeqNo,
80 size_t nodeLevel,
81 const NonNegativeInteger& leafSeqNo,
82 ndn::ConstBufferPtr hash)
83 : m_index(nodeSeqNo, nodeLevel)
84 , m_hash(hash)
85{
86 if (leafSeqNo == 0 && m_index.seqNo > leafSeqNo)
87 m_leafSeqNo = m_index.seqNo;
88 else
89 setLeafSeqNo(leafSeqNo);
90}
peizhen guocf4df2d2014-08-12 13:22:32 -070091
92void
Yingdi Yu0c3e5912015-03-17 14:22:38 -070093Node::setLeafSeqNo(const NonNegativeInteger& leafSeqNo)
peizhen guocf4df2d2014-08-12 13:22:32 -070094{
Yingdi Yu0c3e5912015-03-17 14:22:38 -070095 if (leafSeqNo > m_index.seqNo + m_index.range || leafSeqNo < m_index.seqNo)
96 throw Error("Node: leaf seqNo is out of range");
97
98 m_leafSeqNo = leafSeqNo;
peizhen guocf4df2d2014-08-12 13:22:32 -070099}
100
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700101void
102Node::setHash(ndn::ConstBufferPtr hash)
103{
104 m_hash = hash;
105}
peizhen guocf4df2d2014-08-12 13:22:32 -0700106
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700107bool
108Node::isFull() const
109{
110 return m_index.seqNo + m_index.range == m_leafSeqNo;
111}
peizhen guocf4df2d2014-08-12 13:22:32 -0700112
113ndn::ConstBufferPtr
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700114Node::getEmptyHash()
peizhen guocf4df2d2014-08-12 13:22:32 -0700115{
Yingdi Yu0c3e5912015-03-17 14:22:38 -0700116 if (EMPTY_HASH == nullptr) {
117 ndn::util::Sha256 sha256;
118 EMPTY_HASH = sha256.computeDigest();
119 }
120
121 return EMPTY_HASH;
peizhen guocf4df2d2014-08-12 13:22:32 -0700122}
123
124} // namespace nsl