Fix DiffState
Change-Id: Id4d48a263e60a1d7416a8181e19e1db0f5d0d67c
diff --git a/src/diff-state-container.cpp b/src/diff-state-container.cpp
new file mode 100644
index 0000000..38a67f3
--- /dev/null
+++ b/src/diff-state-container.cpp
@@ -0,0 +1,25 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2014 University of California, Los Angeles
+ *
+ * This file is part of ChronoSync, synchronization library for distributed realtime
+ * applications for NDN.
+ *
+ * ChronoSync is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
+ * @author Chaoyi Bian <bcy@pku.edu.cn>
+ * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
+ * @author Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#include "diff-state-container.hpp"
diff --git a/src/sync-diff-state-container.h b/src/diff-state-container.hpp
similarity index 70%
rename from src/sync-diff-state-container.h
rename to src/diff-state-container.hpp
index 428877e..d20fbee 100644
--- a/src/sync-diff-state-container.h
+++ b/src/diff-state-container.hpp
@@ -19,35 +19,47 @@
* @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
* @author Chaoyi Bian <bcy@pku.edu.cn>
* @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
+ * @author Yingdi Yu <yingdi@cs.ucla.edu>
*/
-#ifndef SYNC_DIFF_STATE_CONTAINER_H
-#define SYNC_DIFF_STATE_CONTAINER_H
+#ifndef CHRONOSYNC_DIFF_STATE_CONTAINER_HPP
+#define CHRONOSYNC_DIFF_STATE_CONTAINER_HPP
-#include "sync-diff-state.h"
-#include "sync-digest.h"
+#include "mi-tag.hpp"
+#include "diff-state.hpp"
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/tag.hpp>
-// #include <boost/multi_index/ordered_index.hpp>
-// #include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
-// #include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
+namespace chronosync {
+
namespace mi = boost::multi_index;
-namespace Sync {
+struct DigestPtrHash
+{
+ std::size_t
+ operator()(ndn::ConstBufferPtr digest) const
+ {
+ BOOST_ASSERT(digest->size() > sizeof(std::size_t));
-/// @cond include_hidden
-struct sequenced { };
-struct timed { };
-/// @endcond
+ return *reinterpret_cast<const std::size_t*>(digest->buf());
+ }
+};
+
+struct DigestPtrEqual
+{
+ bool
+ operator()(ndn::ConstBufferPtr digest1, ndn::ConstBufferPtr digest2) const
+ {
+ return *digest1 == *digest2;
+ }
+};
/**
- * \ingroup sync
* @brief Container for differential states
*/
struct DiffStateContainer : public mi::multi_index_container<
@@ -56,11 +68,11 @@
// For fast access to elements using DiffState hashes
mi::hashed_unique<
mi::tag<hashed>,
- mi::const_mem_fun<DiffState, DigestConstPtr, &DiffState::getDigest>,
+ mi::const_mem_fun<DiffState, ndn::ConstBufferPtr, &DiffState::getRootDigest>,
DigestPtrHash,
DigestPtrEqual
- >
- ,
+ >,
+
// sequenced index to access older/newer element (like in list)
mi::sequenced<mi::tag<sequenced> >
>
@@ -68,6 +80,6 @@
{
};
-} // Sync
+} // namespace chronosync
-#endif // SYNC_DIFF_STATE_CONTAINER_H
+#endif // CHRONOSYNC_DIFF_STATE_CONTAINER_HPP
diff --git a/src/diff-state.cpp b/src/diff-state.cpp
new file mode 100644
index 0000000..f5813bf
--- /dev/null
+++ b/src/diff-state.cpp
@@ -0,0 +1,43 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2014 University of California, Los Angeles
+ *
+ * This file is part of ChronoSync, synchronization library for distributed realtime
+ * applications for NDN.
+ *
+ * ChronoSync is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
+ * @author Chaoyi Bian <bcy@pku.edu.cn>
+ * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
+ * @author Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#include "diff-state.hpp"
+
+namespace chronosync {
+
+ConstStatePtr
+DiffState::diff() const
+{
+ StatePtr result = make_shared<State>();
+
+ ConstDiffStatePtr state = m_next;
+ while (static_cast<bool>(state)) {
+ *result += *state;
+ state = state->m_next;
+ }
+
+ return result;
+}
+
+} // namespace chronosync
diff --git a/src/diff-state.hpp b/src/diff-state.hpp
new file mode 100644
index 0000000..46ed412
--- /dev/null
+++ b/src/diff-state.hpp
@@ -0,0 +1,104 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012-2014 University of California, Los Angeles
+ *
+ * This file is part of ChronoSync, synchronization library for distributed realtime
+ * applications for NDN.
+ *
+ * ChronoSync is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
+ * @author Chaoyi Bian <bcy@pku.edu.cn>
+ * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
+ * @author Yingdi Yu <yingdi@cs.ucla.edu>
+ */
+
+#ifndef CHRONOSYNC_DIFF_STATE_HPP
+#define CHRONOSYNC_DIFF_STATE_HPP
+
+#include "state.hpp"
+
+namespace chronosync {
+
+class DiffState;
+typedef shared_ptr<DiffState> DiffStatePtr;
+typedef shared_ptr<const DiffState> ConstDiffStatePtr;
+
+/**
+ * @brief Contains the diff info between two states.
+ *
+ * DiffState is used to construct DiffLog. It serves as
+ * a log entry. Each log entry contains the updates between
+ * two states, and is indexed by the digest of the second state
+ * which is the result when the updates have been applied.
+ *
+ * DiffLog is a chain of DiffStates. Each DiffState connects to
+ * the next DiffState (a newer diff) through member m_next. The
+ * m_next of the last DiffState in a log should be empty. And the
+ * root digest of the last DiffState in the log should be the most
+ * current state.
+ */
+class DiffState : public State
+{
+public:
+ /**
+ * @brief Set successor for the diff state
+ *
+ * @param next successor state
+ */
+ void
+ setNext(ConstDiffStatePtr next)
+ {
+ m_next = next;
+ }
+
+ /**
+ * @brief Set digest for the diff state (obtained from a corresponding full state)
+ *
+ * @param digest root digest of the full state
+ */
+ void
+ setRootDigest(ndn::ConstBufferPtr digest)
+ {
+ m_digest = digest;
+ }
+
+ /**
+ * @brief Get root digest of the full state after applying the diff state
+ */
+ ndn::ConstBufferPtr
+ getRootDigest() const
+ {
+ return m_digest;
+ }
+
+ /**
+ * @brief Accumulate differences from this state to the most current state
+ *
+ * This method assumes that the DiffState is in a log. It will iterate the all
+ * the DiffState between its m_next DiffState and the last DiffState in the log,
+ * and aggregate all the differences into one diff, which is represented as a
+ * State object.
+ *
+ * @returns Accumulated differences from this state to the most current state
+ */
+ ConstStatePtr
+ diff() const;
+
+private:
+ ConstDiffStatePtr m_next;
+ ndn::ConstBufferPtr m_digest;
+};
+
+} // chronosync
+
+#endif // CHRONOSYNC_DIFF_STATE_HPP
diff --git a/src/sync-diff-state.cc b/src/sync-diff-state.cc
deleted file mode 100644
index 5ddaace..0000000
--- a/src/sync-diff-state.cc
+++ /dev/null
@@ -1,102 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2014 University of California, Los Angeles
- *
- * This file is part of ChronoSync, synchronization library for distributed realtime
- * applications for NDN.
- *
- * ChronoSync is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- *
- * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
- * @author Chaoyi Bian <bcy@pku.edu.cn>
- * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
- */
-
-#include "sync-diff-state.h"
-#include "sync-diff-leaf.h"
-
-#include <boost/make_shared.hpp>
-#include <boost/foreach.hpp>
-#include <boost/assert.hpp>
-
-using namespace boost;
-
-namespace Sync {
-
-DiffState::DiffState ()
-{
-}
-
-DiffState::~DiffState ()
-{
-}
-
-DiffStatePtr
-DiffState::diff () const
-{
- DiffStatePtr ret = make_shared<DiffState> ();
-
- DiffStatePtr state = m_next;
- while (state != 0)
- {
- *ret += *state;
- state = state->m_next;
- }
-
- return ret;
-}
-
-DiffState &
-DiffState::operator += (const DiffState &state)
-{
- BOOST_FOREACH (LeafConstPtr _leaf, state.getLeaves ())
- {
- DiffLeafConstPtr leaf = dynamic_pointer_cast<const DiffLeaf> (_leaf);
- BOOST_ASSERT (leaf != 0);
-
- if (leaf->getOperation () == UPDATE)
- update (leaf->getInfo (), leaf->getSeq ());
- else if (leaf->getOperation () == REMOVE)
- remove (leaf->getInfo ());
- else
- {
- BOOST_ASSERT (false);
- }
- }
-
- return *this;
-}
-
-// from State
-boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
-DiffState::update (NameInfoConstPtr info, const SeqNo &seq)
-{
- m_leaves.erase (info);
-
- DiffLeafPtr leaf = make_shared<DiffLeaf> (info, cref (seq));
- m_leaves.insert (leaf);
-
- return make_tuple (true, false, SeqNo ());
-}
-
-bool
-DiffState::remove (NameInfoConstPtr info)
-{
- m_leaves.erase (info);
-
- DiffLeafPtr leaf = make_shared<DiffLeaf> (info);
- m_leaves.insert (leaf);
-
- return true;
-}
-
-} // ns3
diff --git a/src/sync-diff-state.h b/src/sync-diff-state.h
deleted file mode 100644
index da0d649..0000000
--- a/src/sync-diff-state.h
+++ /dev/null
@@ -1,103 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012-2014 University of California, Los Angeles
- *
- * This file is part of ChronoSync, synchronization library for distributed realtime
- * applications for NDN.
- *
- * ChronoSync is free software: you can redistribute it and/or modify it under the terms
- * of the GNU General Public License as published by the Free Software Foundation, either
- * version 3 of the License, or (at your option) any later version.
- *
- * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
- *
- * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
- * @author Chaoyi Bian <bcy@pku.edu.cn>
- * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
- */
-
-#ifndef SYNC_DIFF_STATE_H
-#define SYNC_DIFF_STATE_H
-
-#include "sync-state.h"
-#include <iostream>
-
-namespace Sync {
-
-class DiffState;
-typedef boost::shared_ptr<DiffState> DiffStatePtr;
-typedef boost::shared_ptr<DiffState> DiffStateConstPtr;
-
-/**
- * @ingroup ccnx
- * @brief Differential SYNC state
- */
-class DiffState : public State
-{
-public:
- /**
- * @see Default constructor
- */
- DiffState ();
- virtual ~DiffState ();
-
- /**
- * @brief Set successor for the diff state
- * @param next successor state
- */
- void
- setNext (DiffStatePtr next)
- {
- m_next = next;
- }
-
- /**
- * @brief Set digest for the diff state (obtained from a corresponding full state)
- * @param digest A read only smart pointer to a digest object (that should be unmodified anywhere else)
- */
- void
- setDigest (DigestConstPtr digest) { m_digest = digest; }
-
- /**
- * @brief Get digest for the diff state
- */
- DigestConstPtr
- getDigest () const { return m_digest; }
-
- /**
- * @brief Accumulate differences from `this' state to the most current state
- * @returns Accumulated differences from `this' state to the most current state
- */
- DiffStatePtr
- diff () const;
-
- /**
- * @brief Combine differences from `this' and `state'
- * @param state Differential state to combine with
- * @return Combined differences
- *
- * In case of collisions, `this' leaf will be replaced with the leaf of `state'
- */
- DiffState&
- operator += (const DiffState &state);
-
- // from State
- virtual boost::tuple<bool/*inserted*/, bool/*updated*/, SeqNo/*oldSeqNo*/>
- update (NameInfoConstPtr info, const SeqNo &seq);
-
- virtual bool
- remove (NameInfoConstPtr info);
-
-private:
- DiffStatePtr m_next;
- DigestConstPtr m_digest;
-};
-
-} // Sync
-
-#endif // SYNC_DIFF_STATE_H