Adding test for the database and sync log. Preliminary file state maintenance operations
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..a7496ec
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,33 @@
+#!/usr/bin/make -f
+# Waf Makefile wrapper
+
+all:
+ @./waf build
+
+all-debug:
+ @./waf -v build
+
+all-progress:
+ @./waf -p build
+
+install:
+ ./waf install --yes;
+
+uninstall:
+ ./waf uninstall
+
+clean:
+ @./waf clean
+
+distclean:
+ @./waf distclean
+ @-rm -rf build
+
+check:
+ @./waf check
+
+dist:
+ @./waf dist
+
+.PHONY: clean dist distclean check uninstall install all
+
diff --git a/client/client.cc b/client/client.cc
index df827ba..4d20e98 100644
--- a/client/client.cc
+++ b/client/client.cc
@@ -127,10 +127,15 @@
notify->moveFile (argv[2], argv[3]);
}
+ else
+ {
+ cerr << "ERROR: Unknown command " << cmd << endl;
+ usage ();
+ }
}
else
{
- cerr << "Cannot connect to the daemon\n";
+ cerr << "ERROR: Cannot connect to the daemon\n";
status = 1;
}
}
diff --git a/src/action-log.cc b/src/action-log.cc
index 41166e7..293a79a 100644
--- a/src/action-log.cc
+++ b/src/action-log.cc
@@ -28,6 +28,14 @@
ActionLog::ActionLog (const std::string &path, const std::string &localName)
: SyncLog (path, localName)
{
+ int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
+ ActionLog::apply_action_xFun,
+ 0, 0);
+ if (res != SQLITE_OK)
+ {
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("Cannot create function ``apply_action''"));
+ }
}
tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string>
@@ -37,7 +45,7 @@
sqlite3_stmt *stmt;
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);
+ "WHERE filename=? ORDER BY a.version DESC LIMIT 1", -1, &stmt, 0);
if (res != SQLITE_OK)
{
@@ -230,3 +238,18 @@
sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
}
+
+
+void
+ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
+{
+ ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
+
+ cout << "apply_function called with " << argc << endl;
+
+ cout << "device_name: " << sqlite3_value_text (argv[0]) << endl;
+ cout << "action: " << sqlite3_value_int (argv[1]) << endl;
+ cout << "filename: " << sqlite3_value_text (argv[2]) << endl;
+
+ sqlite3_result_null (context);
+}
diff --git a/src/action-log.h b/src/action-log.h
index 1d63fb8..f6cc0a7 100644
--- a/src/action-log.h
+++ b/src/action-log.h
@@ -48,6 +48,9 @@
private:
boost::tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, std::string>
GetExistingRecord (const std::string &filename);
+
+ static void
+ apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv);
protected:
};
diff --git a/src/database-test.cc b/src/database-test.cc
deleted file mode 100644
index ba1fecc..0000000
--- a/src/database-test.cc
+++ /dev/null
@@ -1,80 +0,0 @@
-/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
-/*
- * Copyright (c) 2012 University of California, Los Angeles
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation;
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
- * Zhenkai Zhu <zhenkai@cs.ucla.edu>
- */
-
-#include "db-helper.h"
-#include <iostream>
-
-using namespace std;
-using namespace boost;
-
-typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
-
-
-int
-main (int argc, char **argv)
-{
- try
- {
- DbHelper db ("./");
-
- HashPtr hash = db.RememberStateInStateLog ();
- // should be empty
- cout << "Hash: [" << *hash << "]" << endl;
-
- //
- db.UpdateDeviceSeqno ("Alex", 1);
- hash = db.RememberStateInStateLog ();
- cout << "Hash: [" << *hash << "]" << endl;
-
- db.UpdateDeviceSeqno ("Alex", 2);
- hash = db.RememberStateInStateLog ();
- cout << "Hash: [" << *hash << "]" << endl;
-
- db.UpdateDeviceSeqno ("Alex", 2);
- hash = db.RememberStateInStateLog ();
- cout << "Hash: [" << *hash << "]" << endl;
-
- db.UpdateDeviceSeqno ("Alex", 1);
- hash = db.RememberStateInStateLog ();
- cout << "Hash: [" << *hash << "]" << endl;
-
- db.FindStateDifferences ("00", "ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52");
- db.FindStateDifferences ("ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52", "00");
- db.FindStateDifferences ("869c38c6dffe8911ced320aecc6d9244904d13d3e8cd21081311f2129b4557ce",
- "ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52");
- db.FindStateDifferences ("ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52",
- "869c38c6dffe8911ced320aecc6d9244904d13d3e8cd21081311f2129b4557ce");
-
- db.UpdateDeviceSeqno ("Bob", 1);
- hash = db.RememberStateInStateLog ();
- cout << "Hash: [" << *hash << "]" << endl;
-
- db.FindStateDifferences ("00", "48f4d95b503b9a79c2d5939fa67722b13fc01db861fc501d09efd0a38dbafab8");
- db.FindStateDifferences ("ec0a9941fa726e1fb8f34ecdbd8e3faa75dc9dba22e6a2ea1d8482aae5fdfb52",
- "48f4d95b503b9a79c2d5939fa67722b13fc01db861fc501d09efd0a38dbafab8");
- }
- catch (const boost::exception &e)
- {
- cout << "ERRORR: " << *get_error_info<errmsg_info_str> (e) << endl;
- }
-
- return 0;
-}
diff --git a/src/db-helper.cc b/src/db-helper.cc
index f0e78d0..7b6c091 100644
--- a/src/db-helper.cc
+++ b/src/db-helper.cc
@@ -120,6 +120,41 @@
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.action,NEW.filename,NEW.file_hash, \
+ NEW.file_atime,NEW.file_mtime,NEW.file_ctime, \
+ NEW.file_chmod); /* 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\
+ 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\
+ \n\
+ PRIMARY KEY (type, filename) \n\
+); \n\
";
DbHelper::DbHelper (const std::string &path)
diff --git a/src/hash-helper.cc b/src/hash-helper.cc
index 04b52b2..33c0e77 100644
--- a/src/hash-helper.cc
+++ b/src/hash-helper.cc
@@ -118,7 +118,7 @@
return retval;
}
-
+ retval->m_buf = new unsigned char [EVP_MAX_MD_SIZE];
unsigned char *end = copy (string_to_binary (hashInTextEncoding.begin ()),
string_to_binary (hashInTextEncoding.end ()),
diff --git a/include/object-db-file.h b/src/object-db-file.h
similarity index 100%
rename from include/object-db-file.h
rename to src/object-db-file.h
diff --git a/include/object-db.h b/src/object-db.h
similarity index 88%
rename from include/object-db.h
rename to src/object-db.h
index 207e0b3..8c31418 100644
--- a/include/object-db.h
+++ b/src/object-db.h
@@ -9,7 +9,7 @@
struct ObjectDBException : virtual boost::exception, virtual exception { };
typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
-void throwException(const string &msg) { boost::throw_exception(ObjectDBException() << error_info_str(msg)); }
+inline void throwException(const string &msg) { boost::throw_exception(ObjectDBException() << error_info_str(msg)); }
typedef unsigned char Byte;
typedef vector<Byte> Bytes;
diff --git a/test/database-test.cc b/test/database-test.cc
new file mode 100644
index 0000000..7432fd0
--- /dev/null
+++ b/test/database-test.cc
@@ -0,0 +1,80 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ */
+
+#include <boost/test/unit_test.hpp>
+#include <boost/lexical_cast.hpp>
+
+#include <unistd.h>
+#include "action-log.h"
+#include <iostream>
+
+using namespace std;
+using namespace boost;
+
+BOOST_AUTO_TEST_SUITE(DatabaseTest)
+
+
+BOOST_AUTO_TEST_CASE (BasicDatabaseTest)
+{
+ char dir_tmpl [] = "/tmp/tmp-chornoshare-XXXXXXXXXXX";
+ string tmp_dir = mkdtemp (dir_tmpl);
+ SyncLog db (tmp_dir, "/alex");
+
+ HashPtr hash = db.RememberStateInStateLog ();
+ // should be empty
+ BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "461f0ed1300b7f947fbe8e38a04186b74938febe7e43fe4ed571551fa3bd6ab9");
+
+ db.UpdateDeviceSeqno ("Alex", 1);
+ hash = db.RememberStateInStateLog ();
+
+ BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "80463c859f23367e1cbabfa80d6de78af334589ec88dc9c56c854c1f7e196c34");
+
+ db.UpdateDeviceSeqno ("Alex", 2);
+ hash = db.RememberStateInStateLog ();
+ BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
+
+ db.UpdateDeviceSeqno ("Alex", 2);
+ hash = db.RememberStateInStateLog ();
+ BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
+
+ db.UpdateDeviceSeqno ("Alex", 1);
+ hash = db.RememberStateInStateLog ();
+ BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
+
+
+ db.FindStateDifferences ("00", "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
+ db.FindStateDifferences ("95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833", "00");
+ db.FindStateDifferences ("869c38c6dffe8911ced320aecc6d9244904d13d3e8cd21081311f2129b4557ce",
+ "95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833");
+ db.FindStateDifferences ("95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833",
+ "869c38c6dffe8911ced320aecc6d9244904d13d3e8cd21081311f2129b4557ce");
+
+ db.UpdateDeviceSeqno ("Bob", 1);
+ hash = db.RememberStateInStateLog ();
+ BOOST_CHECK_EQUAL (lexical_cast<string> (*hash), "d001d4680fd9adcb48e34a795e3cc3d5d36f209fbab34fd57f70f362c2085310");
+
+ db.FindStateDifferences ("00", "d001d4680fd9adcb48e34a795e3cc3d5d36f209fbab34fd57f70f362c2085310");
+ db.FindStateDifferences ("95284d3132a7a88b85c5141ca63efa68b7a7daf37315def69e296a0c24692833",
+ "d001d4680fd9adcb48e34a795e3cc3d5d36f209fbab34fd57f70f362c2085310");
+
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/wscript b/wscript
index 736c5d1..7fdb1ea 100644
--- a/wscript
+++ b/wscript
@@ -72,16 +72,6 @@
includes = ['include', ],
)
- # Unit tests
- if bld.env['TEST']:
- unittests = bld.program (
- target="unit-tests",
- source = bld.path.ant_glob(['test/**/*.cc']),
- features=['cxx', 'cxxprogram'],
- use = 'BOOST_TEST ccnx',
- includes = ['include', ],
- )
-
common = bld.objects (
target = "common",
features = ["cxx"],
@@ -92,6 +82,29 @@
includes = ['include', 'src'],
)
+ database = bld.objects (
+ target = "database",
+ features = ["cxx"],
+ source = [
+ 'src/db-helper.cc',
+ 'src/sync-log.cc',
+ 'src/action-log.cc',
+ 'src/action-item.proto',
+ 'src/sync-state.proto',
+ ],
+ use = "BOOST SQLITE3 SSL common",
+ includes = ['include', 'src'],
+ )
+
+ # Unit tests
+ if bld.env['TEST']:
+ unittests = bld.program (
+ target="unit-tests",
+ source = bld.path.ant_glob(['test/**/*.cc']),
+ features=['cxx', 'cxxprogram'],
+ use = 'BOOST_TEST ccnx database',
+ includes = ['include', 'src'],
+ )
client = bld (
target="cs-client",
@@ -108,12 +121,7 @@
# source = bld.path.ant_glob(['src/**/*.cc']),
source = ['daemon/daemon.cc',
'daemon/notify-i.cc',
- 'src/db-helper.cc',
- 'src/sync-log.cc',
- 'src/action-log.cc',
- 'src/sync-state.proto',
- 'src/action-item.proto',
],
- use = "BOOST CCNX SSL SQLITE3 ICE common",
+ use = "BOOST CCNX SSL SQLITE3 ICE common database",
includes = ['include', 'src'],
)