blob: 1ab25e906f30441aa01810c14900c5a162817dc2 [file] [log] [blame]
Alexander Afanasyev71b43e72012-12-27 01:03:43 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyeva199f972013-01-02 19:37:26 -08003 * Copyright (c) 2013 University of California, Los Angeles
Alexander Afanasyev71b43e72012-12-27 01:03:43 -08004 *
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 Afanasyeva199f972013-01-02 19:37:26 -080025#include "db-helper.h"
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080026#include <sync-state.pb.h>
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080027#include <ccnx-name.h>
Zhenkai Zhue851b952013-01-13 22:29:57 -080028#include <map>
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080029#include <boost/thread/shared_mutex.hpp>
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080030
31typedef boost::shared_ptr<SyncStateMsg> SyncStateMsgPtr;
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080032
Alexander Afanasyeva199f972013-01-02 19:37:26 -080033class SyncLog : public DbHelper
34{
35public:
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080036 SyncLog (const boost::filesystem::path &path, const std::string &localName);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080037
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080038 /**
39 * @brief Get local username
40 */
Alexander Afanasyev7326a252013-01-20 23:43:25 -080041 inline const Ccnx::Name &
42 GetLocalName () const;
43
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080044 /**
45 * @brief Get database ID of the local sync node (make sense only for the local database)
46 */
47 inline const sqlite3_int64
48 GetLocalSyncNodeId () const;
49
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080050 sqlite3_int64
51 GetNextLocalSeqNo (); // side effect: local seq_no will be increased
52
Alexander Afanasyeva199f972013-01-02 19:37:26 -080053 // done
54 void
Zhenkai Zhue851b952013-01-13 22:29:57 -080055 UpdateDeviceSeqNo (const Ccnx::Name &name, sqlite3_int64 seqNo);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080056
Alexander Afanasyev7326a252013-01-20 23:43:25 -080057 void
58 UpdateLocalSeqNo (sqlite3_int64 seqNo);
59
Zhenkai Zhue851b952013-01-13 22:29:57 -080060 Ccnx::Name
61 LookupLocator (const Ccnx::Name &deviceName);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080062
Zhenkai Zhue851b952013-01-13 22:29:57 -080063 void
64 UpdateLocator (const Ccnx::Name &deviceName, const Ccnx::Name &locator);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080065
Alexander Afanasyevdac84922013-01-20 23:32:17 -080066 void
67 UpdateLocalLocator (const Ccnx::Name &locator);
68
Alexander Afanasyeva199f972013-01-02 19:37:26 -080069 // done
70 /**
71 * Create an entry in SyncLog and SyncStateNodes corresponding to the current state of SyncNodes
72 */
73 HashPtr
74 RememberStateInStateLog ();
75
76 // done
77 sqlite3_int64
78 LookupSyncLog (const std::string &stateHash);
79
80 // done
81 sqlite3_int64
82 LookupSyncLog (const Hash &stateHash);
83
84 // How difference is exposed will be determined later by the actual protocol
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080085 SyncStateMsgPtr
Zhenkai Zhu085aae72013-01-17 21:09:01 -080086 FindStateDifferences (const std::string &oldHash, const std::string &newHash, bool includeOldSeq = false);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080087
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080088 SyncStateMsgPtr
Alexander Afanasyevdac84922013-01-20 23:32:17 -080089 FindStateDifferences (const Hash &oldHash, const Hash &newHash, bool includeOldSeq = false);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080090
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080091 //-------- only used in test -----------------
92 sqlite3_int64
93 SeqNo(const Ccnx::Name &name);
94
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080095protected:
Zhenkai Zhue851b952013-01-13 22:29:57 -080096 void
97 UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080098
Zhenkai Zhue851b952013-01-13 22:29:57 -080099
100protected:
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800101 Ccnx::Name m_localName;
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800102
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800103 sqlite3_int64 m_localDeviceId;
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -0800104
105 typedef boost::mutex Mutex;
106 typedef boost::unique_lock<Mutex> WriteLock;
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800107
108 Mutex m_stateUpdateMutex;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800109};
110
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800111typedef boost::shared_ptr<SyncLog> SyncLogPtr;
112
Alexander Afanasyev7326a252013-01-20 23:43:25 -0800113const Ccnx::Name &
114SyncLog::GetLocalName () const
115{
116 return m_localName;
117}
118
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800119const sqlite3_int64
120SyncLog::GetLocalSyncNodeId () const
121{
122 return m_localDeviceId;
123}
124
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800125
126#endif // SYNC_LOG_H