ActionLog preliminary works, but still has a lot of pieces missing
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);
}