blob: 9833b926a323e5070cf965f2d2a76e7e4d93079b [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 Afanasyeva199f972013-01-02 19:37:26 -080026#include "sync-log.h"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080027#include "action-item.pb.h"
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -080028#include "file-item.pb.h"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080029#include "ccnx-wrapper.h"
30#include "ccnx-pco.h"
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 Afanasyevf2c16e02013-01-23 18:08:04 -080038typedef std::list<FileItem> FileItems;
39typedef boost::shared_ptr<FileItem> FileItemPtr;
40typedef boost::shared_ptr<FileItems> FileItemsPtr;
41
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080042class ActionLog : public DbHelper
Alexander Afanasyeva199f972013-01-02 19:37:26 -080043{
44public:
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080045 typedef boost::function<void (std::string /*filename*/, Ccnx::Name /*device_name*/, sqlite3_int64 /*seq_no*/,
46 HashPtr /*hash*/, time_t /*m_time*/, int /*mode*/, int /*seg_num*/)> OnFileAddedOrChangedCallback;
47
48 typedef boost::function<void (std::string /*filename*/)> OnFileRemovedCallback;
49
50public:
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080051 ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080052 SyncLogPtr syncLog,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080053 const std::string &sharedFolder,
54 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080055
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080056 //////////////////////////
57 // Local operations //
58 //////////////////////////
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080059 ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080060 AddLocalActionUpdate (const std::string &filename,
61 const Hash &hash,
62 time_t wtime,
63 int mode,
64 int seg_num);
65
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 Afanasyeva35756b2013-01-22 16:59:11 -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 Afanasyev053e5ac2013-01-22 20:59:13 -080077 AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco);
78
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 Afanasyev053e5ac2013-01-22 20:59:13 -080085 AddRemoteAction (Ccnx::PcoPtr actionPco);
86
87 ///////////////////////////
88 // General operations //
89 ///////////////////////////
90
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080091 Ccnx::PcoPtr
92 LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno);
93
94 Ccnx::PcoPtr
95 LookupActionPco (const Ccnx::Name &actionName);
96
97 ActionItemPtr
98 LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno);
99
100 ActionItemPtr
101 LookupAction (const Ccnx::Name &actionName);
102
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800103 ///////////////////////////
104 // File state operations //
105 ///////////////////////////
106 FileItemPtr
107 LookupFile (const std::string &filename);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800108
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800109 FileItemsPtr
110 LookupFilesForHash (const Hash &hash);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800111
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800112public:
113 // for test purposes
114 sqlite3_int64
115 LogSize ();
116
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800117private:
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800118 boost::tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
119 GetLatestActionForFile (const std::string &filename);
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800120
121 static void
122 apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800123
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800124private:
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800125 SyncLogPtr m_syncLog;
126
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800127 Ccnx::CcnxWrapperPtr m_ccnx;
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800128 std::string m_sharedFolderName;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800129
130 OnFileAddedOrChangedCallback m_onFileAddedOrChanged;
131 OnFileRemovedCallback m_onFileRemoved;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800132};
133
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800134namespace Error {
135struct ActionLog : virtual boost::exception, virtual std::exception { };
136}
137
138
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800139#endif // ACTION_LOG_H