Bladeren bron

Implemented cache DB versioning

Also regenerates the DB is versions don't match
Stefan Schmidt 6 jaren geleden
bovenliggende
commit
28301447a7

+ 1 - 1
code/ID2TLib/Statistics.py

@@ -54,7 +54,7 @@ class Statistics:
             print("Flag -r/--recalculate found. Recalculating statistics.")
 
         # Recalculate statistics if database does not exist OR param -r/--recalculate is provided
-        if (not self.stats_db.get_db_exists()) or flag_recalculate_stats:
+        if (not self.stats_db.get_db_exists()) or flag_recalculate_stats or self.stats_db.get_db_outdated():
             self.pcap_proc = pr.pcap_processor(self.pcap_filepath, str(self.do_extra_tests))
             self.pcap_proc.collect_statistics()
             self.pcap_proc.write_to_database(self.path_db)

+ 14 - 1
code/ID2TLib/StatsDatabase.py

@@ -3,6 +3,7 @@ import re
 import sqlite3
 import sys
 from random import randint
+import ID2TLib.libpcapreader as pr
 
 
 def dict_gen(curs: sqlite3.Cursor):
@@ -32,7 +33,10 @@ class StatsDatabase:
 
         # If DB not existing, create a new DB scheme
         if self.existing_db:
-            print('Located statistics database at: ', db_path)
+            if self.get_db_outdated():
+                print('Statistics database outdated. Recreating database at: ', db_path)
+            else:
+                print('Located statistics database at: ', db_path)
         else:
             print('Statistics database not found. Creating new database at: ', db_path)
 
@@ -61,6 +65,15 @@ class StatsDatabase:
         """
         return self.existing_db
 
+    def get_db_outdated(self):
+        """
+        Retrieves the database version from the database and compares it to the version
+        it should have to check whether the database is outdated and needs to be recreated.
+        :return: True if the versions match, otherwise False
+        """
+        self.cursor.execute('PRAGMA user_version;')
+        return self.cursor.fetchall()[0][0] != pr.pcap_processor.get_db_version()
+
     @staticmethod
     def _get_selector_keywords():
         """

+ 2 - 1
code_boost/src/cxx/pcap_processor.cpp

@@ -347,5 +347,6 @@ BOOST_PYTHON_MODULE (libpcapreader) {
             .def("merge_pcaps", &pcap_processor::merge_pcaps)
             .def("collect_statistics", &pcap_processor::collect_statistics)
             .def("get_timestamp_mu_sec", &pcap_processor::get_timestamp_mu_sec)
-            .def("write_to_database", &pcap_processor::write_to_database);
+            .def("write_to_database", &pcap_processor::write_to_database)
+            .def("get_db_version", &pcap_processor::get_db_version).staticmethod("get_db_version");
 }

+ 3 - 0
code_boost/src/cxx/pcap_processor.h

@@ -13,6 +13,7 @@
 #include <sys/stat.h>
 #include <unordered_map>
 #include "statistics.h"
+#include "statistics_db.h"
 
 using namespace Tins;
 
@@ -44,6 +45,8 @@ public:
     void collect_statistics();
 
     void write_to_database(std::string database_path);
+
+    static int get_db_version() { return statistics_db::DB_VERSION; };
 };
 
 

+ 1 - 0
code_boost/src/cxx/statistics.cpp

@@ -602,6 +602,7 @@ void statistics::writeToDatabase(std::string database_path) {
         db.writeStatisticsWin(win_distribution);
         db.writeStatisticsConv(conv_statistics);
         db.writeStatisticsInterval(interval_statistics);
+        db.writeDbVersion();
     }
     else {
         // Tinslib failed to recognize the types of the packets in the input PCAP

+ 11 - 0
code_boost/src/cxx/statistics_db.cpp

@@ -438,3 +438,14 @@ void statistics_db::writeStatisticsInterval(std::unordered_map<std::string, entr
     }
 }
 
+void statistics_db::writeDbVersion(){
+	try {
+		SQLite::Transaction transaction(*db);
+		SQLite::Statement query(*db, std::string("PRAGMA user_version = ") + std::to_string(DB_VERSION) + ";");
+		query.exec();
+		transaction.commit();
+	}
+	catch (std::exception &e) {
+        std::cout << "Exception in statistics_db: " << e.what() << std::endl;
+    }
+}

+ 7 - 0
code_boost/src/cxx/statistics_db.h

@@ -18,6 +18,11 @@ public:
      */
     statistics_db(std::string database_path);
 
+    /*
+     * Database version: Increment number on every change in the C++ code!
+     */
+    static const int DB_VERSION = 1;
+
     /*
      * Methods for writing values into database
      */
@@ -45,6 +50,8 @@ public:
 
     void writeStatisticsInterval(std::unordered_map<std::string, entry_intervalStat> intervalStatistics);
 
+    void writeDbVersion();
+
 private:
     // Pointer to the SQLite database
     std::unique_ptr<SQLite::Database> db;