Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013 University of California, Los Angeles |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #ifndef SYNC_LOG_H |
| 23 | #define SYNC_LOG_H |
| 24 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 25 | #include "db-helper.h" |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 26 | #include <sync-state.pb.h> |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 27 | #include <ccnx-name.h> |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 28 | #include <map> |
Alexander Afanasyev | beee0b4 | 2013-01-16 18:25:08 -0800 | [diff] [blame] | 29 | #include <boost/thread/shared_mutex.hpp> |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 30 | |
| 31 | typedef boost::shared_ptr<SyncStateMsg> SyncStateMsgPtr; |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 33 | class SyncLog : public DbHelper |
| 34 | { |
| 35 | public: |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 36 | SyncLog (const boost::filesystem::path &path, const std::string &localName); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 38 | // // fill in the map with device-name <-> locator pairs |
| 39 | // void |
| 40 | // initYP(map<Ccnx::Name, Ccnx::Name> &yp); |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame^] | 42 | inline const Ccnx::Name & |
| 43 | GetLocalName () const; |
| 44 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 45 | sqlite3_int64 |
| 46 | GetNextLocalSeqNo (); // side effect: local seq_no will be increased |
| 47 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 48 | // done |
| 49 | void |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 50 | UpdateDeviceSeqNo (const Ccnx::Name &name, sqlite3_int64 seqNo); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame^] | 52 | void |
| 53 | UpdateLocalSeqNo (sqlite3_int64 seqNo); |
| 54 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 55 | Ccnx::Name |
| 56 | LookupLocator (const Ccnx::Name &deviceName); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 57 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 58 | void |
| 59 | UpdateLocator (const Ccnx::Name &deviceName, const Ccnx::Name &locator); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 61 | void |
| 62 | UpdateLocalLocator (const Ccnx::Name &locator); |
| 63 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 64 | // done |
| 65 | /** |
| 66 | * Create an entry in SyncLog and SyncStateNodes corresponding to the current state of SyncNodes |
| 67 | */ |
| 68 | HashPtr |
| 69 | RememberStateInStateLog (); |
| 70 | |
| 71 | // done |
| 72 | sqlite3_int64 |
| 73 | LookupSyncLog (const std::string &stateHash); |
| 74 | |
| 75 | // done |
| 76 | sqlite3_int64 |
| 77 | LookupSyncLog (const Hash &stateHash); |
| 78 | |
| 79 | // How difference is exposed will be determined later by the actual protocol |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 80 | SyncStateMsgPtr |
Zhenkai Zhu | 085aae7 | 2013-01-17 21:09:01 -0800 | [diff] [blame] | 81 | FindStateDifferences (const std::string &oldHash, const std::string &newHash, bool includeOldSeq = false); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | 6f70a0f | 2013-01-02 20:44:09 -0800 | [diff] [blame] | 83 | SyncStateMsgPtr |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 84 | FindStateDifferences (const Hash &oldHash, const Hash &newHash, bool includeOldSeq = false); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 85 | |
Zhenkai Zhu | 9501b8b | 2013-01-17 12:37:00 -0800 | [diff] [blame] | 86 | //-------- only used in test ----------------- |
| 87 | sqlite3_int64 |
| 88 | SeqNo(const Ccnx::Name &name); |
| 89 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 90 | protected: |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 91 | void |
| 92 | UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo); |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 93 | |
Zhenkai Zhu | e851b95 | 2013-01-13 22:29:57 -0800 | [diff] [blame] | 94 | |
| 95 | protected: |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 96 | Ccnx::Name m_localName; |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 98 | sqlite3_int64 m_localDeviceId; |
Alexander Afanasyev | beee0b4 | 2013-01-16 18:25:08 -0800 | [diff] [blame] | 99 | |
| 100 | typedef boost::mutex Mutex; |
| 101 | typedef boost::unique_lock<Mutex> WriteLock; |
Alexander Afanasyev | dac8492 | 2013-01-20 23:32:17 -0800 | [diff] [blame] | 102 | |
| 103 | Mutex m_stateUpdateMutex; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
Zhenkai Zhu | b330aed | 2013-01-17 13:29:37 -0800 | [diff] [blame] | 106 | typedef boost::shared_ptr<SyncLog> SyncLogPtr; |
| 107 | |
Alexander Afanasyev | 7326a25 | 2013-01-20 23:43:25 -0800 | [diff] [blame^] | 108 | const Ccnx::Name & |
| 109 | SyncLog::GetLocalName () const |
| 110 | { |
| 111 | return m_localName; |
| 112 | } |
| 113 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 114 | |
| 115 | #endif // SYNC_LOG_H |