ActionLog preliminary works, but still has a lot of pieces missing
diff --git a/daemon/daemon.cc b/daemon/daemon.cc
index 97fee4f..5b953d3 100644
--- a/daemon/daemon.cc
+++ b/daemon/daemon.cc
@@ -25,6 +25,7 @@
#include <Ice/Identity.h>
#include "notify-i.h"
+#include <boost/make_shared.hpp>
using namespace std;
using namespace boost;
@@ -34,6 +35,12 @@
class MyService : public Ice::Service
{
+public:
+ MyService (ActionLogPtr actionLog)
+ : m_actionLog (actionLog)
+ {
+ }
+
protected:
virtual bool start (int, char*[], int&)
{
@@ -41,7 +48,7 @@
Ice::Identity identity;
identity.name="NotifyDaemon";
- NotifyPtr servant=new NotifyI;
+ NotifyPtr servant=new NotifyI (m_actionLog);
_adapter->add (servant, identity);
@@ -51,7 +58,8 @@
}
private:
- Ice::ObjectAdapterPtr _adapter;
+ Ice::ObjectAdapterPtr _adapter;
+ ActionLogPtr m_actionLog;
};
int
@@ -62,9 +70,9 @@
try
{
// DbHelper db ("./", "/ndn/ucla.edu/alex");
- ActionLog bla ("./", "/ndn/ucla.edu/alex");
+ ActionLogPtr actionLog = make_shared<ActionLog> ("./", "/ndn/ucla.edu/alex");
- MyService svc;
+ MyService svc (actionLog);
status = svc.main (argc, argv);
// HashPtr hash = db.RememberStateInStateLog ();
diff --git a/daemon/notify-i.cc b/daemon/notify-i.cc
index 43aa32e..0994aa3 100644
--- a/daemon/notify-i.cc
+++ b/daemon/notify-i.cc
@@ -23,11 +23,12 @@
#include <hash-helper.h>
using namespace std;
+using namespace boost;
-// NotifyI::NotifyI (DbHelperPtr &db)
-// : m_db (db)
-// {
-// }
+NotifyI::NotifyI (ActionLogPtr &actionLog)
+ : m_actionLog (actionLog)
+{
+}
void
NotifyI::updateFile (const ::std::string &filename,
@@ -40,9 +41,17 @@
{
Hash hash (hashRaw.first, hashRaw.second-hashRaw.first);
- // m_db->AddActionUpdate (filename, hash, atime, mtime, ctime, mode);
-
- // cout << "updateFile " << filename << " with hash " << hash << endl;
+ cout << "updateFile " << filename << " with hash " << hash << endl;
+ try
+ {
+ m_actionLog->AddActionUpdate (filename, hash, atime, mtime, ctime, mode);
+
+ m_actionLog->RememberStateInStateLog ();
+ }
+ catch (const boost::exception &e)
+ {
+ cout << "ERRORR: " << *get_error_info<errmsg_info_str> (e) << endl;
+ }
}
void
@@ -50,16 +59,16 @@
const ::std::string &newFilename,
const ::Ice::Current&)
{
- // cout << "moveFile from " << oldFilename << " to " << newFilename << endl;
- // m_db->AddActionMove (filename, oldFilename);
+ cout << "moveFile from " << oldFilename << " to " << newFilename << endl;
+ // m_actionLog->AddActionMove (oldFilename, newFilename);
}
void
NotifyI::deleteFile (const ::std::string &filename,
const ::Ice::Current&)
{
- // m_db->AddActionDelete (filename, oldFilename);
-
- // cout << "deleteFile " << filename << endl;
+ cout << "deleteFile " << filename << endl;
+ m_actionLog->AddActionDelete (filename);
+ m_actionLog->RememberStateInStateLog ();
}
diff --git a/daemon/notify-i.h b/daemon/notify-i.h
index 42d4d7e..9fd7044 100644
--- a/daemon/notify-i.h
+++ b/daemon/notify-i.h
@@ -22,12 +22,13 @@
#ifndef NOTIFY_I_H
#define NOTIFY_I_H
+#include <action-log.h>
#include <chronoshare-client.ice.h>
class NotifyI : public ChronoshareClient::Notify
{
public:
- // NotifyI (DbHelperPtr &db);
+ NotifyI (ActionLogPtr &actionLog);
virtual void
updateFile (const ::std::string &filename,
@@ -48,7 +49,7 @@
const ::Ice::Current& = ::Ice::Current());
private:
- // DbHelperPtr m_db;
+ ActionLogPtr m_actionLog;
};
#endif // NOTIFY_I_H
diff --git a/src/action-log.cc b/src/action-log.cc
index 6d0c48b..41166e7 100644
--- a/src/action-log.cc
+++ b/src/action-log.cc
@@ -30,33 +30,42 @@
{
}
-tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64>
+tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string>
ActionLog::GetExistingRecord (const std::string &filename)
{
// check if something already exists
sqlite3_stmt *stmt;
- int res = sqlite3_prepare_v2 (m_db, "SELECT version,device_id,seq_no FROM ActionLog WHERE filename=? ORDER BY version DESC,device_id DESC LIMIT 1", -1, &stmt, 0);
+ int res = sqlite3_prepare_v2 (m_db, "SELECT a.version,a.device_id,a.seq_no,a.action,s.device_name "
+ "FROM ActionLog a JOIN SyncNodes s ON s.device_id = a.device_id "
+ "WHERE filename=? ORDER BY a.version DESC,a.device_id DESC LIMIT 1", -1, &stmt, 0);
if (res != SQLITE_OK)
{
BOOST_THROW_EXCEPTION (Error::Db ()
- << errmsg_info_str ("Some error with AddActionUpdate"));
+ << errmsg_info_str ("Some error with GetExistingRecord"));
}
+ // with parent with version number + 1
+ sqlite3_int64 version = 0;
+ sqlite3_int64 parent_device_id = -1;
+ sqlite3_int64 parent_seq_no = -1;
+ string parent_device_name;
+
sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
- if (sqlite3_step (stmt) == SQLITE_ROW)
+ if (sqlite3_step (stmt) == SQLITE_ROW)
{
- // with parent with version number + 1
- sqlite3_int64 version = sqlite3_column_int64 (stmt, 0) + 1;
- sqlite3_int64 parent_device_id = sqlite3_column_int64 (stmt, 1);
- sqlite3_int64 parent_seq_no = sqlite3_column_int64 (stmt, 0);
+ version = sqlite3_column_int64 (stmt, 0) + 1;
- sqlite3_finalize (stmt);
- return make_tuple (version, parent_device_id, parent_seq_no);
+ if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
+ {
+ parent_device_id = sqlite3_column_int64 (stmt, 1);
+ parent_seq_no = sqlite3_column_int64 (stmt, 2);
+ parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_text (stmt, 4)));
+ }
}
-
+
sqlite3_finalize (stmt);
- return make_tuple ((sqlite3_int64)0, (sqlite3_int64)-1, (sqlite3_int64)-1);
+ return make_tuple (version, parent_device_id, parent_seq_no, parent_device_name);
}
// local add action. remote action is extracted from content object
@@ -66,48 +75,67 @@
time_t atime, time_t mtime, time_t ctime,
int mode)
{
- int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
+ sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
sqlite3_int64 seq_no = GetNextLocalSeqNo ();
sqlite3_int64 version = 0;
sqlite3_int64 parent_device_id = -1;
+ string parent_device_name;
sqlite3_int64 parent_seq_no = -1;
sqlite3_int64 action_time = time (0);
- tie (version, parent_device_id, parent_seq_no) = GetExistingRecord (filename);
+ tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
sqlite3_stmt *stmt;
- sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
- "(device_id, seq_no, action, filename, version, action_timestamp, "
- "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
- "parent_device_id, parent_seq_no) "
- "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
- " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
- " ?, ?)", -1, &stmt, 0);
+ int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
+ "(device_id, seq_no, action, filename, version, action_timestamp, "
+ "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
+ "parent_device_id, parent_seq_no) "
+ "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
+ " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
+ " ?, ?);", -1, &stmt, 0);
+ // cout << "INSERT INTO ActionLog "
+ // "(device_id, seq_no, action, filename, version, action_timestamp, "
+ // "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
+ // "parent_device_id, parent_seq_no) "
+ // "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
+ // " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
+ // " ?, ?)" << endl;
+
+ if (res != SQLITE_OK)
+ {
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str (sqlite3_errmsg (m_db))
+ );
+ // << errmsg_info_str ("Some error with prepare AddActionUpdate"));
+ }
+
+
sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
sqlite3_bind_int64 (stmt, 2, seq_no);
sqlite3_bind_int (stmt, 3, 0);
sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
- sqlite3_bind_blob (stmt, 5, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
-
+ sqlite3_bind_int64 (stmt, 5, version);
sqlite3_bind_int64 (stmt, 6, action_time);
- sqlite3_bind_int64 (stmt, 7, atime);
- sqlite3_bind_int64 (stmt, 8, mtime);
- sqlite3_bind_int64 (stmt, 9, ctime);
- sqlite3_bind_int (stmt, 10, mode);
+ sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
+
+ sqlite3_bind_int64 (stmt, 8, atime);
+ sqlite3_bind_int64 (stmt, 9, mtime);
+ sqlite3_bind_int64 (stmt, 10, ctime);
+ sqlite3_bind_int (stmt, 11, mode);
if (parent_device_id > 0 && parent_seq_no > 0)
{
- sqlite3_bind_int64 (stmt, 11, parent_device_id);
- sqlite3_bind_int64 (stmt, 12, parent_seq_no);
+ sqlite3_bind_int64 (stmt, 12, parent_device_id);
+ sqlite3_bind_int64 (stmt, 13, parent_seq_no);
}
else
{
- sqlite3_bind_null (stmt, 11);
sqlite3_bind_null (stmt, 12);
+ sqlite3_bind_null (stmt, 13);
}
sqlite3_step (stmt);
@@ -120,19 +148,85 @@
item.set_version (version);
item.set_timestamp (action_time);
item.set_file_hash (hash.GetHash (), hash.GetHashBytes ());
- // item
+ item.set_atime (atime);
+ item.set_mtime (mtime);
+ item.set_ctime (ctime);
+ item.set_mode (mode);
+
+ if (parent_device_id > 0 && parent_seq_no > 0)
+ {
+ item.set_parent_device_name (parent_device_name);
+ item.set_parent_seq_no (parent_seq_no);
+ }
+
+ // assign name to the action, serialize action, and create content object
sqlite3_finalize (stmt);
- res += sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
+ sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
}
void
ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
{
+ // not supported yet
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("Move operation is not yet supported"));
}
void
ActionLog::AddActionDelete (const std::string &filename)
{
+ sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
+
+ sqlite3_int64 version = 0;
+ sqlite3_int64 parent_device_id = -1;
+ string parent_device_name;
+ sqlite3_int64 parent_seq_no = -1;
+
+ sqlite3_int64 action_time = time (0);
+
+ tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
+ if (parent_device_id < 0) // no records exist or file was already deleted
+ {
+ sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
+ return;
+ }
+
+ sqlite3_int64 seq_no = GetNextLocalSeqNo ();
+
+ sqlite3_stmt *stmt;
+ sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
+ "(device_id, seq_no, action, filename, version, action_timestamp, "
+ "parent_device_id, parent_seq_no) "
+ "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
+ " ?, ?)", -1, &stmt, 0);
+
+ sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
+ sqlite3_bind_int64 (stmt, 2, seq_no);
+ sqlite3_bind_int (stmt, 3, 1);
+ sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
+ sqlite3_bind_int64 (stmt, 5, version);
+ sqlite3_bind_int64 (stmt, 6, action_time);
+
+ sqlite3_bind_int64 (stmt, 7, parent_device_id);
+ sqlite3_bind_int64 (stmt, 8, parent_seq_no);
+
+ sqlite3_step (stmt);
+
+ // missing part: creating ContentObject for the action !!!
+
+ ActionItem item;
+ item.set_action (ActionItem::UPDATE);
+ item.set_filename (filename);
+ item.set_version (version);
+ item.set_timestamp (action_time);
+ item.set_parent_device_name (parent_device_name);
+ item.set_parent_seq_no (parent_seq_no);
+
+ // assign name to the action, serialize action, and create content object
+
+ sqlite3_finalize (stmt);
+
+ sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
}
diff --git a/src/action-log.h b/src/action-log.h
index 0ffd64f..1d63fb8 100644
--- a/src/action-log.h
+++ b/src/action-log.h
@@ -25,6 +25,9 @@
#include "sync-log.h"
#include <boost/tuple/tuple.hpp>
+class ActionLog;
+typedef boost::shared_ptr<ActionLog> ActionLogPtr;
+
class ActionLog : public SyncLog
{
public:
@@ -43,7 +46,7 @@
AddActionDelete (const std::string &filename);
private:
- boost::tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64>
+ boost::tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, std::string>
GetExistingRecord (const std::string &filename);
protected:
diff --git a/src/db-helper.cc b/src/db-helper.cc
index 9bd2d61..f0e78d0 100644
--- a/src/db-helper.cc
+++ b/src/db-helper.cc
@@ -106,13 +106,13 @@
parent_device_id INTEGER, \n\
parent_seq_no INTEGER, \n\
\n\
- action_name TEXT NOT NULL, \n\
- action_content_object BLOB NOT NULL, \n\
+ action_name TEXT, \n\
+ action_content_object BLOB, \n\
\n\
PRIMARY KEY (device_id, seq_no), \n\
\n\
FOREIGN KEY (parent_device_id, parent_seq_no) \n\
- REFERENCES ActionLog (device_id, parent_seq_no) \n\
+ REFERENCES ActionLog (device_id, seq_no) \n\
ON UPDATE RESTRICT \n\
ON DELETE SET NULL \n\
); \n\
diff --git a/src/sync-log.cc b/src/sync-log.cc
index 93d4a3e..677904a 100644
--- a/src/sync-log.cc
+++ b/src/sync-log.cc
@@ -30,6 +30,8 @@
: DbHelper (path)
, m_localName (localName)
{
+ SyncLog::RememberStateInStateLog ();
+
UpdateDeviceSeqno (localName, 0);
sqlite3_stmt *stmt;
@@ -199,11 +201,11 @@
{
sqlite3_stmt *stmt;
// update is performed using trigger
- int res = sqlite3_prepare (m_db, "INSERT INTO SyncNodes (device_id, seq_no) VALUES (?,?);",
+ int res = sqlite3_prepare (m_db, "UPDATE SyncNodes SET seq_no=MAX(seq_no,?) WHERE device_id=?;",
-1, &stmt, 0);
- res += sqlite3_bind_int64 (stmt, 1, deviceId);
- res += sqlite3_bind_int64 (stmt, 2, seqNo);
+ res += sqlite3_bind_int64 (stmt, 1, seqNo);
+ res += sqlite3_bind_int64 (stmt, 2, deviceId);
sqlite3_step (stmt);
if (res != SQLITE_OK)