Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 3 | * Copyright (c) 2012-2017 University of California, Los Angeles |
Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
| 7 | * |
| 8 | * ChronoSync 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, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ChronoSync 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 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 20 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 22 | * @author Yingdi Yu <yingdi@cs.ucla.edu> |
| 23 | */ |
| 24 | |
| 25 | #ifndef CHRONOSYNC_DIFF_STATE_HPP |
| 26 | #define CHRONOSYNC_DIFF_STATE_HPP |
| 27 | |
| 28 | #include "state.hpp" |
| 29 | |
| 30 | namespace chronosync { |
| 31 | |
| 32 | class DiffState; |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 33 | using DiffStatePtr = shared_ptr<DiffState>; |
| 34 | using ConstDiffStatePtr = shared_ptr<const DiffState>; |
Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @brief Contains the diff info between two states. |
| 38 | * |
| 39 | * DiffState is used to construct DiffLog. It serves as |
| 40 | * a log entry. Each log entry contains the updates between |
| 41 | * two states, and is indexed by the digest of the second state |
| 42 | * which is the result when the updates have been applied. |
| 43 | * |
| 44 | * DiffLog is a chain of DiffStates. Each DiffState connects to |
| 45 | * the next DiffState (a newer diff) through member m_next. The |
| 46 | * m_next of the last DiffState in a log should be empty. And the |
| 47 | * root digest of the last DiffState in the log should be the most |
| 48 | * current state. |
| 49 | */ |
| 50 | class DiffState : public State |
| 51 | { |
| 52 | public: |
| 53 | /** |
| 54 | * @brief Set successor for the diff state |
| 55 | * |
| 56 | * @param next successor state |
| 57 | */ |
| 58 | void |
| 59 | setNext(ConstDiffStatePtr next) |
| 60 | { |
| 61 | m_next = next; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @brief Set digest for the diff state (obtained from a corresponding full state) |
| 66 | * |
| 67 | * @param digest root digest of the full state |
| 68 | */ |
| 69 | void |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 70 | setRootDigest(ConstBufferPtr digest) |
Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 71 | { |
| 72 | m_digest = digest; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @brief Get root digest of the full state after applying the diff state |
| 77 | */ |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 78 | ConstBufferPtr |
Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 79 | getRootDigest() const |
| 80 | { |
| 81 | return m_digest; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @brief Accumulate differences from this state to the most current state |
| 86 | * |
| 87 | * This method assumes that the DiffState is in a log. It will iterate the all |
| 88 | * the DiffState between its m_next DiffState and the last DiffState in the log, |
| 89 | * and aggregate all the differences into one diff, which is represented as a |
| 90 | * State object. |
| 91 | * |
| 92 | * @returns Accumulated differences from this state to the most current state |
| 93 | */ |
| 94 | ConstStatePtr |
| 95 | diff() const; |
| 96 | |
| 97 | private: |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 98 | ConstDiffStatePtr m_next; |
| 99 | ConstBufferPtr m_digest; |
Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Ashlesh Gawande | 08784d4 | 2017-09-06 23:40:21 -0500 | [diff] [blame] | 102 | } // namespace chronosync |
Yingdi Yu | 6f79778 | 2014-08-27 12:31:11 -0700 | [diff] [blame] | 103 | |
| 104 | #endif // CHRONOSYNC_DIFF_STATE_HPP |