blob: 80f354c455da723e604358c1528bc4227a2e88e3 [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyeva199f972013-01-02 19:37:26 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyeva199f972013-01-02 19:37:26 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyeva199f972013-01-02 19:37:26 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyeva199f972013-01-02 19:37:26 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyeva199f972013-01-02 19:37:26 -080019 */
20
21#ifndef ACTION_LOG_H
22#define ACTION_LOG_H
23
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include "action-item.pb.hpp"
25#include "ccnx-pco.hpp"
26#include "ccnx-wrapper.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080027#include "db-helper.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080028#include "file-item.pb.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080029#include "file-state.hpp"
30#include "sync-log.hpp"
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080031
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080032#include <boost/tuple/tuple.hpp>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080033
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080034class ActionLog;
35typedef boost::shared_ptr<ActionLog> ActionLogPtr;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080036typedef boost::shared_ptr<ActionItem> ActionItemPtr;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080037
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080038class ActionLog : public DbHelper
Alexander Afanasyeva199f972013-01-02 19:37:26 -080039{
40public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080041 typedef boost::function<void(std::string /*filename*/, Ccnx::Name /*device_name*/, sqlite3_int64 /*seq_no*/,
42 HashPtr /*hash*/, time_t /*m_time*/, int /*mode*/, int /*seg_num*/)>
43 OnFileAddedOrChangedCallback;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080044
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080045 typedef boost::function<void(std::string /*filename*/)> OnFileRemovedCallback;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080046
47public:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048 ActionLog(Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path& path, SyncLogPtr syncLog,
49 const std::string& sharedFolder, const std::string& appName,
50 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080051
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080052 virtual ~ActionLog()
53 {
54 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080055
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080056 //////////////////////////
57 // Local operations //
58 //////////////////////////
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080059 ActionItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080060 AddLocalActionUpdate(const std::string& filename,
61 const Hash& hash,
62 time_t wtime,
63 int mode,
64 int seg_num);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080065
66 // void
67 // AddActionMove (const std::string &oldFile, const std::string &newFile);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080068
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080069 ActionItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080070 AddLocalActionDelete(const std::string& filename);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080071
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080072 //////////////////////////
73 // Remote operations //
74 //////////////////////////
75
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080076 ActionItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080077 AddRemoteAction(const Ccnx::Name& deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080078
79 /**
80 * @brief Add remote action using just action's parsed content object
81 *
82 * This function extracts device name and sequence number from the content object's and calls the overloaded method
83 */
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080084 ActionItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080085 AddRemoteAction(Ccnx::PcoPtr actionPco);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080086
87 ///////////////////////////
88 // General operations //
89 ///////////////////////////
90
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080091 Ccnx::PcoPtr
92 LookupActionPco(const Ccnx::Name& deviceName, sqlite3_int64 seqno);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080093
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080094 Ccnx::PcoPtr
95 LookupActionPco(const Ccnx::Name& actionName);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080096
97 ActionItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080098 LookupAction(const Ccnx::Name& deviceName, sqlite3_int64 seqno);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080099
100 ActionItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101 LookupAction(const Ccnx::Name& actionName);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800102
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800103 FileItemPtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800104 LookupAction(const std::string& filename, sqlite3_int64 version, const Hash& filehash);
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800105
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800106 /**
107 * @brief Lookup up to [limit] actions starting [offset] in decreasing order (by timestamp) and calling visitor(device_name,seqno,action) for each action
108 */
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800109 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 LookupActionsInFolderRecursively(
111 const boost::function<void(const Ccnx::Name& name, sqlite3_int64 seq_no, const ActionItem&)>& visitor,
112 const std::string& folder, int offset = 0, int limit = -1);
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800113
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800114 bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800115 LookupActionsForFile(
116 const boost::function<void(const Ccnx::Name& name, sqlite3_int64 seq_no, const ActionItem&)>& visitor,
117 const std::string& file, int offset = 0, int limit = -1);
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800118
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800119 void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800120 LookupRecentFileActions(const boost::function<void(const std::string&, int, int)>& visitor,
121 int limit = 5);
Zhenkai Zhu25e13582013-02-27 15:33:01 -0800122
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800123 //
124 inline FileStatePtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800125 GetFileState();
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800126
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800127public:
128 // for test purposes
129 sqlite3_int64
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800130 LogSize();
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800131
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800132private:
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800133 boost::tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
134 GetLatestActionForFile(const std::string& filename);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800135
136 static void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800137 apply_action_xFun(sqlite3_context* context, int argc, sqlite3_value** argv);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800138
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800139private:
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800140 SyncLogPtr m_syncLog;
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800141 FileStatePtr m_fileState;
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800142
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -0700143 Ndnx::NdnxWrapperPtr m_ndnx;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800144 std::string m_sharedFolderName;
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800145 std::string m_appName;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800146
147 OnFileAddedOrChangedCallback m_onFileAddedOrChanged;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800148 OnFileRemovedCallback m_onFileRemoved;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800149};
150
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800151namespace Error {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800152struct ActionLog : virtual boost::exception, virtual std::exception
153{
154};
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800155}
156
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800157inline FileStatePtr
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800158ActionLog::GetFileState()
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800159{
160 return m_fileState;
161}
162
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800163
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800164#endif // ACTION_LOG_H