refactor code

Change-Id: Ia2bc49ed8742d79000fd59f7e95fa9b957573c54
diff --git a/core/merkle-tree.hpp b/core/merkle-tree.hpp
index ccf73c5..3e877f1 100644
--- a/core/merkle-tree.hpp
+++ b/core/merkle-tree.hpp
@@ -16,60 +16,93 @@
  * You should have received a copy of the GNU General Public License along with
  * NSL, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  *
- * @author Peizhen Guo <patrick.guopz@gmail.com>
+ * See AUTHORS.md for complete list of nsl authors and contributors.
  */
-#ifndef NLS_CORE_MERKLE_TREE_HPP
-#define NLS_CORE_MERKLE_TREE_HPP
 
-#include <map>
+#ifndef NSL_CORE_MERKLE_TREE_HPP
+#define NSL_CORE_MERKLE_TREE_HPP
+
+#include "common.hpp"
+#include "db.hpp"
+#include "sub-tree-binary.hpp"
 #include <vector>
 
-#include <stddef.h>
-#include <stdint.h>
-#include <time.h>
-
-#include "leaf.hpp"
-#include "intermediate-node.hpp"
-#include "merkle-tree-cache.hpp"
-
 namespace nsl {
 
 class MerkleTree
 {
 public:
-  MerkleTree();
-
-  ~MerkleTree()
+  class Error : public std::runtime_error
   {
+  public:
+    explicit
+    Error(const std::string& what)
+      : std::runtime_error(what)
+    {
+    }
+  };
+
+public:
+  /**
+   * @brief Constructor
+   */
+  MerkleTree(Db& db);
+
+  MerkleTree(const Name& loggerName, Db& db);
+
+  ~MerkleTree();
+
+  void
+  setLoggerName(const Name& loggerName);
+
+  const NonNegativeInteger&
+  getNextLeafSeqNo() const
+  {
+    return m_nextLeafSeqNo;
   }
 
-  ConstNodePtr
-  getNode(const Index& index);
+  const ndn::ConstBufferPtr&
+  getRootHash() const
+  {
+    return m_hash;
+  }
 
-  uint64_t
-  getLeafNum() const;
+  bool
+  addLeaf(const NonNegativeInteger& seqNo, ndn::ConstBufferPtr hash);
 
-  uint64_t
-  getLevel() const;
+  void
+  loadPendingSubTrees();
 
+  void
+  savePendingTree();
 
-  //return root hash value
-  uint64_t
-  addLeaf(ndn::ConstBufferPtr info);
+  shared_ptr<Data>
+  getPendingSubTreeData(size_t level);
 
+  std::vector<ConstSubTreeBinaryPtr>
+  getExistenceProof(const NonNegativeInteger& seqNo);
 
-  std::vector<ConstNodePtr>
-  generateProof(uint64_t version1, uint64_t version2); // version equals to leaf's index number
-
+  std::vector<ConstSubTreeBinaryPtr>
+  getConsistencyProof(const NonNegativeInteger& seqNo);
 
 private:
-  MerkleTreeCache m_cache;
-  uint64_t m_nLevels;
-  uint64_t m_nLeaves;
+  void
+  getNewRoot(const Node::Index& idx);
 
+  void
+  getNewSibling(const Node::Index& idx);
+
+private:
+  Name m_loggerName;
+  Db& m_db;
+
+  shared_ptr<SubTreeBinary> m_rootSubTree;
+  NonNegativeInteger m_nextLeafSeqNo;
+  ndn::ConstBufferPtr m_hash;
+
+  std::map<size_t, shared_ptr<SubTreeBinary>> m_pendingTrees;
 };
 
+}// namespace nsl
 
-} // namespace nsl
-
-#endif // NLS_CORE_MERKLE_TREE_HPP
+#endif // NSL_CORE_MERKLE_TREE_HPP