Initial separating action-log and sync-log
diff --git a/src/db-helper.cc b/src/db-helper.cc
index 82feb89..95ccc7a 100644
--- a/src/db-helper.cc
+++ b/src/db-helper.cc
@@ -20,163 +20,33 @@
*/
#include "db-helper.h"
-// #include "sync-log.h"
+#include "logging.h"
+
#include <boost/make_shared.hpp>
#include <boost/ref.hpp>
#include <boost/throw_exception.hpp>
+INIT_LOGGER ("DbHelper");
+
using namespace boost;
namespace fs = boost::filesystem;
const std::string INIT_DATABASE = "\
PRAGMA foreign_keys = ON; \n\
- \n\
-CREATE TABLE \n\
- SyncNodes( \n\
- device_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
- device_name BLOB NOT NULL, \n\
- description TEXT, \n\
- seq_no INTEGER NOT NULL, \n\
- last_known_locator BLOB, \n\
- last_update TIMESTAMP \n\
- ); \n\
- \n\
-CREATE TRIGGER SyncNodesUpdater_trigger \n\
- BEFORE INSERT ON SyncNodes \n\
- FOR EACH ROW \n\
- WHEN (SELECT device_id \n\
- FROM SyncNodes \n\
- WHERE device_name=NEW.device_name) \n\
- IS NOT NULL \n\
- BEGIN \n\
- UPDATE SyncNodes \n\
- SET seq_no=max(seq_no,NEW.seq_no) \n\
- WHERE device_name=NEW.device_name; \n\
- SELECT RAISE(IGNORE); \n\
- END; \n\
- \n\
-CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \n\
- \n\
-CREATE TABLE SyncLog( \n\
- state_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
- state_hash BLOB NOT NULL UNIQUE, \n\
- last_update TIMESTAMP NOT NULL \n\
- ); \n\
- \n\
-CREATE TABLE \n\
- SyncStateNodes( \n\
- id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
- state_id INTEGER NOT NULL \n\
- REFERENCES SyncLog (state_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
- device_id INTEGER NOT NULL \n\
- REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
- seq_no INTEGER NOT NULL \n\
- ); \n\
- \n\
-CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \n\
-CREATE INDEX SyncStateNodes_state_id ON SyncStateNodes (state_id); \n\
-CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \n\
- \n\
-CREATE TRIGGER SyncLogGuard_trigger \n\
- BEFORE INSERT ON SyncLog \n\
- FOR EACH ROW \n\
- WHEN (SELECT state_hash \n\
- FROM SyncLog \n\
- WHERE state_hash=NEW.state_hash) \n\
- IS NOT NULL \n\
- BEGIN \n\
- DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \n\
- END; \n\
- \n\
-CREATE TABLE ActionLog ( \n\
- device_id INTEGER NOT NULL, \n\
- seq_no INTEGER NOT NULL, \n\
- \n\
- action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\
- filename TEXT NOT NULL, \n\
- \n\
- version INTEGER NOT NULL, \n\
- action_timestamp TIMESTAMP NOT NULL, \n\
- \n\
- file_hash BLOB, /* NULL if action is \"delete\" */ \n\
- file_atime TIMESTAMP, \n\
- file_mtime TIMESTAMP, \n\
- file_ctime TIMESTAMP, \n\
- file_chmod INTEGER, \n\
- file_seg_num INTEGER, /* NULL if action is \"delete\" */ \n\
- \n\
- parent_device_id INTEGER, \n\
- parent_seq_no INTEGER, \n\
- \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, seq_no) \n\
- ON UPDATE RESTRICT \n\
- ON DELETE SET NULL \n\
-); \n\
- \n\
-CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
-CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\
-CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
- \n\
-CREATE TRIGGER ActionLogInsert_trigger \n\
- AFTER INSERT ON ActionLog \n\
- FOR EACH ROW \n\
- WHEN (SELECT device_id \n\
- FROM ActionLog \n\
- WHERE filename=NEW.filename AND \n\
- version > NEW.version) IS NULL AND \n\
- (SELECT a.device_id \n\
- FROM ActionLog a \n\
- LEFT JOIN SyncNodes s ON s.device_id=a.device_id \n\
- WHERE filename=NEW.filename AND \n\
- version = NEW.version AND \n\
- a.device_id != NEW.device_id AND \n\
- s.device_name > (SELECT device_name \n\
- FROM SyncNodes \n\
- WHERE device_id=NEW.device_id)) IS NULL \n\
- BEGIN \n\
- SELECT apply_action ((SELECT device_name FROM SyncNodes where device_id=NEW.device_id), \
- NEW.device_id, NEW.seq_no, \
- NEW.action,NEW.filename,NEW.file_hash, \
- strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
- NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \
- END; \n\
- \n\
-CREATE TABLE FileState ( \n\
- type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\
- filename TEXT NOT NULL, \n\
- device_id INTEGER NOT NULL, \n\
- seq_no INTEGER NOT NULL, \n\
- file_hash BLOB, /* NULL if action is \"delete\" */ \n\
- file_atime TIMESTAMP, \n\
- file_mtime TIMESTAMP, \n\
- file_ctime TIMESTAMP, \n\
- file_chmod INTEGER, \n\
- file_seg_num INTEGER, \n\
- \n\
- PRIMARY KEY (type, filename) \n\
-); \n\
- \n\
-CREATE INDEX FileState_device_id_seq_no ON FileState (device_id, seq_no); \n\
";
DbHelper::DbHelper (const fs::path &path)
{
fs::path chronoshareDirectory = path / ".chronoshare";
fs::create_directories (chronoshareDirectory);
-
+
int res = sqlite3_open((chronoshareDirectory / "state.db").c_str (), &m_db);
if (res != SQLITE_OK)
{
BOOST_THROW_EXCEPTION (Error::Db ()
<< errmsg_info_str ("Cannot open/create dabatabase: [" + (chronoshareDirectory / "state.db").string () + "]"));
}
-
+
res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0,
DbHelper::hash_xStep, DbHelper::hash_xFinal);
if (res != SQLITE_OK)
@@ -187,14 +57,8 @@
// Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
// for now, just attempt to create everything
-
- char *errmsg = 0;
- res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
- if (res != SQLITE_OK && errmsg != 0)
- {
- std::cerr << "DEBUG: " << errmsg << std::endl;
- sqlite3_free (errmsg);
- }
+ sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
+ _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
}
DbHelper::~DbHelper ()
@@ -222,7 +86,7 @@
sqlite3_result_error (context, "Hash expects (blob,integer) parameters", -1);
return;
}
-
+
EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
if (hash_context == 0)
@@ -236,7 +100,7 @@
*hash_context = EVP_MD_CTX_create ();
EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0);
}
-
+
int nameBytes = sqlite3_value_bytes (argv[0]);
const void *name = sqlite3_value_blob (argv[0]);
sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]);
@@ -262,7 +126,7 @@
sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
return;
}
-
+
unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE];
unsigned int hashLength = 0;
@@ -271,7 +135,7 @@
sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
delete [] hash;
-
+
EVP_MD_CTX_destroy (*hash_context);
}