peizhen guo | cf4df2d | 2014-08-12 13:22:32 -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 |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -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. |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -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. |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -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. |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -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/>. |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 20 | */ |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 21 | |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 22 | #include "node.hpp" |
| 23 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/util/digest.hpp> |
| 25 | #include <boost/lexical_cast.hpp> |
| 26 | |
Alexander Afanasyev | 49e2e4c | 2017-05-06 13:42:57 -0700 | [diff] [blame^] | 27 | namespace ndn { |
| 28 | namespace delorean { |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 29 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 30 | ndn::ConstBufferPtr Node::EMPTY_HASH; |
| 31 | |
| 32 | Node::Index::Index(const NonNegativeInteger& nodeSeq, size_t nodeLevel) |
| 33 | : seqNo(nodeSeq) |
| 34 | , level(nodeLevel) |
| 35 | , range(1 << nodeLevel) |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 36 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 37 | 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 guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 43 | bool |
| 44 | Node::Index::operator<(const Index& other) const |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 45 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 46 | 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 guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 57 | bool |
| 58 | Node::Index::operator==(const Index& other) const |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 59 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 60 | return equals(other); |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 63 | bool |
| 64 | Node::Index::operator!=(const Index& other) const |
| 65 | { |
| 66 | return !equals(other); |
| 67 | } |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 68 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 69 | bool |
| 70 | Node::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 | |
| 80 | Node::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 guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 92 | |
| 93 | void |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 94 | Node::setLeafSeqNo(const NonNegativeInteger& leafSeqNo) |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 95 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 96 | 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 guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 102 | void |
| 103 | Node::setHash(ndn::ConstBufferPtr hash) |
| 104 | { |
| 105 | m_hash = hash; |
| 106 | } |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 107 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 108 | bool |
| 109 | Node::isFull() const |
| 110 | { |
| 111 | return m_index.seqNo + m_index.range == m_leafSeqNo; |
| 112 | } |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 113 | |
| 114 | ndn::ConstBufferPtr |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 115 | Node::getEmptyHash() |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 116 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 117 | if (EMPTY_HASH == nullptr) { |
| 118 | ndn::util::Sha256 sha256; |
| 119 | EMPTY_HASH = sha256.computeDigest(); |
| 120 | } |
| 121 | |
| 122 | return EMPTY_HASH; |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Alexander Afanasyev | 49e2e4c | 2017-05-06 13:42:57 -0700 | [diff] [blame^] | 125 | } // namespace delorean |
| 126 | } // namespace ndn |