blob: 767a9ffbbab64bab68ed00a1a47885228626dc8b [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 Afanasyevdac84922013-01-20 23:32:17 -080038 // // fill in the map with device-name <-> locator pairs
39 // void
40 // initYP(map<Ccnx::Name, Ccnx::Name> &yp);
Zhenkai Zhue851b952013-01-13 22:29:57 -080041
Alexander Afanasyev7326a252013-01-20 23:43:25 -080042 inline const Ccnx::Name &
43 GetLocalName () const;
44
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080045 sqlite3_int64
46 GetNextLocalSeqNo (); // side effect: local seq_no will be increased
47
Alexander Afanasyeva199f972013-01-02 19:37:26 -080048 // done
49 void
Zhenkai Zhue851b952013-01-13 22:29:57 -080050 UpdateDeviceSeqNo (const Ccnx::Name &name, sqlite3_int64 seqNo);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080051
Alexander Afanasyev7326a252013-01-20 23:43:25 -080052 void
53 UpdateLocalSeqNo (sqlite3_int64 seqNo);
54
Zhenkai Zhue851b952013-01-13 22:29:57 -080055 Ccnx::Name
56 LookupLocator (const Ccnx::Name &deviceName);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080057
Zhenkai Zhue851b952013-01-13 22:29:57 -080058 void
59 UpdateLocator (const Ccnx::Name &deviceName, const Ccnx::Name &locator);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080060
Alexander Afanasyevdac84922013-01-20 23:32:17 -080061 void
62 UpdateLocalLocator (const Ccnx::Name &locator);
63
Alexander Afanasyeva199f972013-01-02 19:37:26 -080064 // 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 Afanasyev6f70a0f2013-01-02 20:44:09 -080080 SyncStateMsgPtr
Zhenkai Zhu085aae72013-01-17 21:09:01 -080081 FindStateDifferences (const std::string &oldHash, const std::string &newHash, bool includeOldSeq = false);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080082
Alexander Afanasyev6f70a0f2013-01-02 20:44:09 -080083 SyncStateMsgPtr
Alexander Afanasyevdac84922013-01-20 23:32:17 -080084 FindStateDifferences (const Hash &oldHash, const Hash &newHash, bool includeOldSeq = false);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080085
Zhenkai Zhu9501b8b2013-01-17 12:37:00 -080086 //-------- only used in test -----------------
87 sqlite3_int64
88 SeqNo(const Ccnx::Name &name);
89
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080090protected:
Zhenkai Zhue851b952013-01-13 22:29:57 -080091 void
92 UpdateDeviceSeqNo (sqlite3_int64 deviceId, sqlite3_int64 seqNo);
Alexander Afanasyevdac84922013-01-20 23:32:17 -080093
Zhenkai Zhue851b952013-01-13 22:29:57 -080094
95protected:
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080096 Ccnx::Name m_localName;
Alexander Afanasyevdac84922013-01-20 23:32:17 -080097
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080098 sqlite3_int64 m_localDeviceId;
Alexander Afanasyevbeee0b42013-01-16 18:25:08 -080099
100 typedef boost::mutex Mutex;
101 typedef boost::unique_lock<Mutex> WriteLock;
Alexander Afanasyevdac84922013-01-20 23:32:17 -0800102
103 Mutex m_stateUpdateMutex;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800104};
105
Zhenkai Zhub330aed2013-01-17 13:29:37 -0800106typedef boost::shared_ptr<SyncLog> SyncLogPtr;
107
Alexander Afanasyev7326a252013-01-20 23:43:25 -0800108const Ccnx::Name &
109SyncLog::GetLocalName () const
110{
111 return m_localName;
112}
113
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800114
115#endif // SYNC_LOG_H