peizhen guo | 410e0e1 | 2014-08-12 13:24:14 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of NSL (NDN Signature Logger). |
| 6 | * See AUTHORS.md for complete list of NSL authors and contributors. |
| 7 | * |
| 8 | * NSL is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Peizhen Guo <patrick.guopz@gmail.com> |
| 20 | */ |
| 21 | #ifndef NLS_CORE_MERKLE_TREE_CACHE_HPP |
| 22 | #define NLS_CORE_MERKLE_TREE_CACHE_HPP |
| 23 | |
| 24 | #include <map> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include "sub-tree.hpp" |
| 28 | #include "leaf.hpp" |
| 29 | #include "intermediate-node.hpp" |
| 30 | #include "merkle-tree-sqlite3.hpp" |
| 31 | |
| 32 | |
| 33 | namespace nsl { |
| 34 | |
| 35 | |
| 36 | typedef ndn::shared_ptr<const SubTree> ConstSubTreePtr; |
| 37 | typedef ndn::shared_ptr<SubTree> SubTreePtr; |
| 38 | typedef ndn::shared_ptr<const Node> ConstNodePtr; |
| 39 | typedef ndn::shared_ptr<Node> NodePtr; |
| 40 | |
| 41 | class MerkleTreeCache |
| 42 | { |
| 43 | public: |
| 44 | |
| 45 | MerkleTreeCache() |
| 46 | :m_nLevels(0), |
| 47 | m_nLeaves(0) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | ~MerkleTreeCache() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | uint8_t |
| 56 | getLevel() |
| 57 | { |
| 58 | return m_nLevels; |
| 59 | } |
| 60 | |
| 61 | uint64_t |
| 62 | getLeaves() |
| 63 | { |
| 64 | return m_nLeaves; |
| 65 | } |
| 66 | |
| 67 | SubTreePtr |
| 68 | getSubTree(Index rootIndex) |
| 69 | { |
| 70 | if (m_cachedTree.count(rootIndex) > 0) |
| 71 | { |
| 72 | //std::cout<<"I'm here!"<<int(m_cachedTree[rootIndex]->getRemainPosition())<<std::endl; |
| 73 | return m_cachedTree[rootIndex]; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | std::string treeString = m_database.getSubTree(rootIndex); |
| 78 | SubTreePtr newtree = decoding(treeString); |
| 79 | return newtree; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Do the update when a subtree is full |
| 84 | // Iteratively: find parent --> check full --> ... --> if not full --> |
| 85 | // create subtree --> create subsubtree --> ... --> until down to the leaf |
| 86 | // invoke a subtree.updateHash(), and decide whether to add new subtrees according to the callback |
| 87 | // remainPosition value. |
| 88 | void |
| 89 | update(Index subRootIndex, uint8_t subRemainNum, ndn::ConstBufferPtr subRootHash); |
| 90 | |
| 91 | void |
| 92 | addLeaf(Leaf newLeaf); |
| 93 | |
| 94 | NodePtr |
| 95 | queryNode(Index nodeIndex); |
| 96 | |
| 97 | bool |
| 98 | doesNodeExist(Index nodeIndex); |
| 99 | |
| 100 | void |
| 101 | loadSubTreeFromDatabase(Index rootIndex); |
| 102 | |
| 103 | void |
| 104 | loadLeafFromDatabase(uint64_t sequence); |
| 105 | |
| 106 | |
| 107 | |
| 108 | SubTreePtr |
| 109 | decoding(std::string subTreeInfo); |
| 110 | |
| 111 | // remove the comment to test sqlite3 function |
| 112 | // MerkleTreeSqlite3 m_database; |
| 113 | |
| 114 | // To show the exact size of the cache |
| 115 | // std::map<Index, SubTreePtr> m_cachedTree; |
| 116 | // std::map<uint64_t, ndn::ConstBufferPtr> m_leavesData; |
| 117 | |
| 118 | |
| 119 | private: |
| 120 | |
| 121 | // find which subTree the node belongs to (not include root node) |
| 122 | Index |
| 123 | findSubTree(Index nodeIndex); |
| 124 | |
| 125 | // find a subTree's parent root index |
| 126 | Index |
| 127 | getParentRootIndex(Index thisRootIndex); |
| 128 | |
| 129 | |
| 130 | // To be finished |
| 131 | uint8_t |
| 132 | doesCacheFull() |
| 133 | { |
| 134 | if (m_cachedTree.size() >= 3 && m_leavesData.size() >= 64) |
| 135 | { |
| 136 | return 3; |
| 137 | } |
| 138 | else if (m_cachedTree.size() >= 3 && m_leavesData.size() < 64) |
| 139 | { |
| 140 | return 2; |
| 141 | } |
| 142 | else if (m_cachedTree.size() < 3 && m_leavesData.size() >= 64) |
| 143 | { |
| 144 | return 1; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | return 0; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // choose a cache item to remove, |
| 153 | // exactly remove items from cache only when: 1)have full subtrees 2)cache is full |
| 154 | void |
| 155 | removeSubTree(); |
| 156 | |
| 157 | void |
| 158 | removeLeaf(); |
| 159 | |
| 160 | private: |
| 161 | // partly in cache, completely in db |
| 162 | std::map<Index, SubTreePtr> m_cachedTree; |
| 163 | std::map<uint64_t, ndn::ConstBufferPtr> m_leavesData; //starts from 0(same as leaf) |
| 164 | |
| 165 | // completely in memory, add & delete with time |
| 166 | std::map<Index, int> m_timeFromLastUse; |
| 167 | uint8_t m_nLevels; // levels counted based on subtree unit |
| 168 | uint64_t m_nLeaves; // sequence number + 1 |
| 169 | |
| 170 | // sqlite3 database |
| 171 | MerkleTreeSqlite3 m_database; |
| 172 | }; |
| 173 | |
| 174 | } // namespace nsl |
| 175 | |
| 176 | #endif // NLS_CORE_MERKLE_TREE_CACHE_HPP |