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
int exec(const std::string &query);
+ void transactionBegin();
+ void transactionEnd();
+
inline long long getLastInsertRowId() const noexcept
{
return sqlite3_last_insert_rowid(m_handle);
stmt2.exec();
}
+void Manager::transactionBegin()
+{
+ this->m_conn.transactionBegin();
+}
+
+void Manager::transactionEnd()
+{
+ this->m_conn.transactionEnd();
+}
+
} // namespace Db
} // namespace Csr
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);
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);
#include <iostream>
#include <fstream>
#include <string>
+#include <chrono>
#include <boost/test/unit_test.hpp>
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)
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()