Add transacion on database 06/77206/2 sandbox/kwon/transaction
authorsangwan.kwon <sangwan.kwon@samsung.com>
Fri, 24 Jun 2016 05:17:12 +0000 (14:17 +0900)
committersangwan.kwon <sangwan.kwon@samsung.com>
Fri, 1 Jul 2016 08:03:17 +0000 (17:03 +0900)
Change-Id: Ie752f3cba3dd88d917a4f08b75cfa9b124533dcc
Signed-off-by: sangwan.kwon <sangwan.kwon@samsung.com>
src/framework/db/connection.cpp
src/framework/db/connection.h
src/framework/db/manager.cpp
src/framework/db/manager.h
src/framework/service/cs-logic.cpp
test/internals/test-db.cpp

index cf0a2ce..ab110e5 100644 (file)
@@ -43,5 +43,14 @@ int Connection::exec(const std::string &query)
        return ::sqlite3_changes(m_handle);
 }
 
+void Connection::transactionBegin()
+{
+       ::sqlite3_exec(m_handle, "BEGIN TRANSACTION;", 0, 0, 0);
+}
+
+void Connection::transactionEnd()
+{
+       ::sqlite3_exec(m_handle, "END TRANSACTION;", 0, 0, 0);
+}
 } // namespace Db
 } // namespace Csr
index 795a08f..e9e4391 100644 (file)
@@ -36,6 +36,9 @@ public:
 
        int exec(const std::string &query);
 
+       void transactionBegin();
+       void transactionEnd();
+
        inline long long getLastInsertRowId() const noexcept
        {
                return sqlite3_last_insert_rowid(m_handle);
index 90f26e0..9d4f9e7 100644 (file)
@@ -588,5 +588,15 @@ void Manager::deleteDetectedDeprecated(time_t since)
        stmt2.exec();
 }
 
+void Manager::transactionBegin()
+{
+       this->m_conn.transactionBegin();
+}
+
+void Manager::transactionEnd()
+{
+       this->m_conn.transactionEnd();
+}
+
 } // namespace Db
 } // namespace Csr
index 2ec4bdf..1b7ed51 100644 (file)
@@ -80,6 +80,9 @@ public:
        void deleteDetectedByFilepathOnPath(const std::string &path);
        void deleteDetectedDeprecated(time_t since);
 
+       void transactionBegin();
+       void transactionEnd();
+
 private:
        RowShPtr getDetectedByNameOnPath(const std::string &path, time_t since);
        RowShPtr getDetectedCloudByNameOnPath(const std::string &path, time_t since);
index 22a6172..0505e76 100644 (file)
@@ -277,34 +277,42 @@ RawBuffer CsLogic::scanApp(const CsContext &context, const std::string &pkgPath)
        auto &riskiest = cache.riskiest;
        INFO("start to judge scan stage on pkg[" << pkgPath << "]");
        switch (this->judgeScanStage(history, after, riskiest,
-                               jWorse, jHistory, since)) {
+                       jWorse, jHistory, since)) {
        case CsLogic::ScanStage::NEW_RISKIEST :
+               this->m_db->transactionBegin();
                this->m_db->insertCache(cache);
                this->m_db->insertWorst(pkgId, pkgPath, cache.riskiestPath);
                this->m_db->updateIgnoreFlag(pkgPath, false);
                this->m_db->insertLastScanTime(pkgPath, cache.dataVersion, cache.scanTime);
+               this->m_db->transactionEnd();
                return this->handleAskUser(context, *riskiest);
 
        case CsLogic::ScanStage::NEW_RISKIEST_KEEP_FLAG :
+               this->m_db->transactionBegin();
                this->m_db->insertCache(cache);
                this->m_db->insertWorst(pkgId, pkgPath, cache.riskiestPath);
                this->m_db->insertLastScanTime(pkgPath, cache.dataVersion, cache.scanTime);
+               this->m_db->transactionEnd();
                if (jWorse->isIgnored)
                        return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
 
                return this->handleAskUser(context, *riskiest);
 
        case CsLogic::ScanStage::HISTORY_RISKIEST :
+               this->m_db->transactionBegin();
                this->m_db->insertCache(cache);
                this->m_db->insertLastScanTime(pkgPath, cache.dataVersion, cache.scanTime);
+               this->m_db->transactionEnd();
                if (jHistory->isIgnored)
                        return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
                return this->handleAskUser(context, *jHistory);
 
        case CsLogic::ScanStage::WORSE_RISKIEST :
+               this->m_db->transactionBegin();
                this->m_db->insertCache(cache);
                this->m_db->insertWorst(pkgId, pkgPath, jWorse->fileInAppPath);
                this->m_db->insertLastScanTime(pkgPath, cache.dataVersion, cache.scanTime);
+               this->m_db->transactionEnd();
                if (jWorse->isIgnored)
                        return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
                return this->handleAskUser(context, *jWorse);
index 4ed8387..8862531 100644 (file)
@@ -24,6 +24,7 @@
 #include <iostream>
 #include <fstream>
 #include <string>
+#include <chrono>
 
 #include <boost/test/unit_test.hpp>
 
@@ -45,6 +46,25 @@ void checkSameMalware(const CsDetected &d, const Db::Row &r)
        ASSERT_IF(d.ts,          r.ts);
 }
 
+const char *appendIdxToStr(const char *str, int idx)
+{
+       return std::string(str + std::to_string(idx)).c_str();
+}
+
+using TimePoint = std::chrono::high_resolution_clock::time_point;
+
+TimePoint timeCheckStart()
+{
+       return std::chrono::high_resolution_clock::now();
+}
+
+void timeCheckEnd(TimePoint start)
+{
+       auto end = std::chrono::high_resolution_clock::now();
+       std::chrono::duration<double> diff = end-start;
+       BOOST_MESSAGE("Elapsed time[" << diff.count() << "]. ");
+}
+
 } // namespace anonymous
 
 BOOST_AUTO_TEST_SUITE(DB)
@@ -221,4 +241,49 @@ BOOST_AUTO_TEST_CASE(detected_malware_file)
        EXCEPTION_GUARD_END
 }
 
+BOOST_AUTO_TEST_CASE(transaction_time)
+{
+       EXCEPTION_GUARD_START
+
+       Db::Manager db(TEST_DB_FILE, TEST_DB_SCRIPTS);
+       const int testSize = 500;
+       std::string dataVersion = "1.0.0";
+
+       // select test with vacant data
+       auto detectedList = db.getDetectedAllByNameOnDir("/opt", 0);
+       ASSERT_IF(detectedList.empty(), true);
+
+       BOOST_MESSAGE("Start to time check about insert DB");
+       auto start = timeCheckStart();
+       db.transactionBegin();
+       for(int i = 0; i < testSize; i++) {
+               CsDetected d;
+               d.targetName = appendIdxToStr("/opt/transmalware", i);
+               d.severity = CSR_CS_SEVERITY_LOW;
+               d.malwareName = appendIdxToStr("transmalware", i);
+               d.detailedUrl = appendIdxToStr("http://detailed.transmalware", i);
+               d.ts = 100;
+
+               db.insertDetectedFile(d.targetName, d, dataVersion);
+       }
+       db.transactionEnd();
+       timeCheckEnd(start);
+
+       BOOST_MESSAGE("Start to time check about insert DB");
+       auto start2 = timeCheckStart();
+       for(int i = 0; i < testSize; i++) {
+               CsDetected d;
+               d.targetName = appendIdxToStr("/opt/testmalware", i);
+               d.severity = CSR_CS_SEVERITY_LOW;
+               d.malwareName = appendIdxToStr("testmalware", i);
+               d.detailedUrl = appendIdxToStr("http://detailed.malware", i);
+               d.ts = 100;
+
+               db.insertDetectedFile(d.targetName, d, dataVersion);
+       }
+       timeCheckEnd(start2);
+
+       EXCEPTION_GUARD_END
+}
+
 BOOST_AUTO_TEST_SUITE_END()