blob: cef0a65f06766d4495f7910102c243b2302fcae4 [file] [log] [blame]
Alexander Afanasyeva199f972013-01-02 19:37:26 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2013 University of California, Los Angeles
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 ACTION_LOG_H
23#define ACTION_LOG_H
24
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080025#include "db-helper.h"
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080026#include "file-state.h"
Alexander Afanasyeva199f972013-01-02 19:37:26 -080027#include "sync-log.h"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080028#include "action-item.pb.h"
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -080029#include "file-item.pb.h"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080030#include "ccnx-wrapper.h"
31#include "ccnx-pco.h"
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080032
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080033#include <boost/tuple/tuple.hpp>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080034
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080035class ActionLog;
36typedef boost::shared_ptr<ActionLog> ActionLogPtr;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080037typedef boost::shared_ptr<ActionItem> ActionItemPtr;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080038
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080039class ActionLog : public DbHelper
Alexander Afanasyeva199f972013-01-02 19:37:26 -080040{
41public:
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080042 typedef boost::function<void (std::string /*filename*/, Ccnx::Name /*device_name*/, sqlite3_int64 /*seq_no*/,
43 HashPtr /*hash*/, time_t /*m_time*/, int /*mode*/, int /*seg_num*/)> OnFileAddedOrChangedCallback;
44
45 typedef boost::function<void (std::string /*filename*/)> OnFileRemovedCallback;
46
47public:
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080048 ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080049 SyncLogPtr syncLog,
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080050 const std::string &sharedFolder, const std::string &appName,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080051 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080052
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080053 virtual ~ActionLog () { }
54
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080055 //////////////////////////
56 // Local operations //
57 //////////////////////////
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080058 ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080059 AddLocalActionUpdate (const std::string &filename,
60 const Hash &hash,
61 time_t wtime,
62 int mode,
63 int seg_num);
64
65 // void
66 // AddActionMove (const std::string &oldFile, const std::string &newFile);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080067
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080068 ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080069 AddLocalActionDelete (const std::string &filename);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080070
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080071 //////////////////////////
72 // Remote operations //
73 //////////////////////////
74
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080075 ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080076 AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco);
77
78 /**
79 * @brief Add remote action using just action's parsed content object
80 *
81 * This function extracts device name and sequence number from the content object's and calls the overloaded method
82 */
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080083 ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080084 AddRemoteAction (Ccnx::PcoPtr actionPco);
85
86 ///////////////////////////
87 // General operations //
88 ///////////////////////////
89
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080090 Ccnx::PcoPtr
91 LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno);
92
93 Ccnx::PcoPtr
94 LookupActionPco (const Ccnx::Name &actionName);
95
96 ActionItemPtr
97 LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno);
98
99 ActionItemPtr
100 LookupAction (const Ccnx::Name &actionName);
101
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800102 FileItemPtr
103 LookupAction (const std::string &filename, sqlite3_int64 version, const Hash &filehash);
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800104
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800105 /**
106 * @brief Lookup up to [limit] actions starting [offset] in decreasing order (by timestamp) and calling visitor(device_name,seqno,action) for each action
107 */
Alexander Afanasyeve1c95042013-02-27 01:02:36 -0800108 bool
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800109 LookupActionsInFolderRecursively (const boost::function<void (const Ccnx::Name &name, sqlite3_int64 seq_no, const ActionItem &)> &visitor,
110 const std::string &folder, int offset=0, int limit=-1);
111
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800112 //
113 inline FileStatePtr
114 GetFileState ();
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800115
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800116public:
117 // for test purposes
118 sqlite3_int64
119 LogSize ();
120
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800121private:
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800122 boost::tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
123 GetLatestActionForFile (const std::string &filename);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800124
125 static void
126 apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800127
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800128private:
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800129 SyncLogPtr m_syncLog;
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800130 FileStatePtr m_fileState;
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800131
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800132 Ccnx::CcnxWrapperPtr m_ccnx;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800133 std::string m_sharedFolderName;
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -0800134 std::string m_appName;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800135
136 OnFileAddedOrChangedCallback m_onFileAddedOrChanged;
137 OnFileRemovedCallback m_onFileRemoved;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800138};
139
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800140namespace Error {
141struct ActionLog : virtual boost::exception, virtual std::exception { };
142}
143
Alexander Afanasyevd6364ef2013-02-06 13:13:07 -0800144inline FileStatePtr
145ActionLog::GetFileState ()
146{
147 return m_fileState;
148}
149
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800150
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800151#endif // ACTION_LOG_H