blob: 7a59e3acca870d7e94ddbc7e351fb908ce3ffa91 [file] [log] [blame]
Yingdi Yu6f797782014-08-27 12:31:11 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawande08784d42017-09-06 23:40:21 -05003 * Copyright (c) 2012-2017 University of California, Los Angeles
Yingdi Yu6f797782014-08-27 12:31:11 -07004 *
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
30namespace chronosync {
31
32class DiffState;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050033using DiffStatePtr = shared_ptr<DiffState>;
34using ConstDiffStatePtr = shared_ptr<const DiffState>;
Yingdi Yu6f797782014-08-27 12:31:11 -070035
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 */
50class DiffState : public State
51{
52public:
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 Gawande08784d42017-09-06 23:40:21 -050070 setRootDigest(ConstBufferPtr digest)
Yingdi Yu6f797782014-08-27 12:31:11 -070071 {
72 m_digest = digest;
73 }
74
75 /**
76 * @brief Get root digest of the full state after applying the diff state
77 */
Ashlesh Gawande08784d42017-09-06 23:40:21 -050078 ConstBufferPtr
Yingdi Yu6f797782014-08-27 12:31:11 -070079 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
97private:
Ashlesh Gawande08784d42017-09-06 23:40:21 -050098 ConstDiffStatePtr m_next;
99 ConstBufferPtr m_digest;
Yingdi Yu6f797782014-08-27 12:31:11 -0700100};
101
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500102} // namespace chronosync
Yingdi Yu6f797782014-08-27 12:31:11 -0700103
104#endif // CHRONOSYNC_DIFF_STATE_HPP