X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fdbmaintenancethread.cpp;h=e9f03e0dd68f53cbae1f8a7b2d0b361843fc0439;hb=59a5414ec47a2932a7802fcd1d98c4d80166564f;hp=b6137150e09204be68442bced6d37910b90adbbb;hpb=76805933f794915a72b7f0a21b12af6654759f4f;p=fms.git diff --git a/src/dbmaintenancethread.cpp b/src/dbmaintenancethread.cpp index b613715..e9f03e0 100644 --- a/src/dbmaintenancethread.cpp +++ b/src/dbmaintenancethread.cpp @@ -1,6 +1,8 @@ #include "../include/dbmaintenancethread.h" #include "../include/stringfunctions.h" #include "../include/option.h" +#include "../include/threadbuilder.h" +#include "../include/dbsetup.h" #include #include @@ -20,27 +22,104 @@ DBMaintenanceThread::DBMaintenanceThread() m_last1day=Poco::Timestamp(); m_last1day-=Poco::Timespan(0,23,51,0,0); - std::string tempval="180"; - Option::Instance()->Get("DeleteMessagesOlderThan",tempval); - StringFunctions::Convert(tempval,m_deletemessagesolderthan); + } void DBMaintenanceThread::Do10MinuteMaintenance() { + Option option(m_db); + std::string ll(""); + option.Get("LogLevel",ll); + + // TODO - remove after corruption issue fixed + if(ll=="8") + { + std::string dbres=TestDBIntegrity(m_db); + m_log->trace("DBMaintenanceThread::Do10MinuteMaintenance() start TestDBIntegrity returned "+dbres); + } + + ThreadBuilder tb(m_db); + SQLite3DB::Statement boardst=m_db->Prepare("SELECT BoardID FROM tblBoard WHERE Forum='true';"); + // select messages for a board that aren't in a thread + SQLite3DB::Statement selectst=m_db->Prepare("SELECT tblMessage.MessageID FROM tblMessage \ + INNER JOIN tblMessageBoard ON tblMessage.MessageID=tblMessageBoard.MessageID\ + LEFT JOIN tblThreadPost ON tblMessage.MessageID=tblThreadPost.MessageID \ + LEFT JOIN tblThread ON tblThreadPost.ThreadID=tblThread.ThreadID\ + WHERE tblMessageBoard.BoardID=? AND tblThread.BoardID IS NULL;"); + + boardst.Step(); + while(boardst.RowReturned()) + { + int boardid=-1; + + boardst.ResultInt(0,boardid); + + selectst.Bind(0,boardid); + selectst.Step(); + + while(selectst.RowReturned()) + { + int messageid=-1; + + selectst.ResultInt(0,messageid); + + tb.Build(messageid,boardid,true); + + selectst.Step(); + } + selectst.Reset(); + + boardst.Step(); + } + + // TODO - remove after corruption issue fixed + if(ll=="8") + { + std::string dbres=TestDBIntegrity(m_db); + m_log->trace("DBMaintenanceThread::Do10MinuteMaintenance() middle TestDBIntegrity returned "+dbres); + } + + // now rebuild threads where the message has been deleted + SQLite3DB::Statement st=m_db->Prepare("SELECT tblThreadPost.MessageID, tblThread.BoardID FROM tblThreadPost INNER JOIN tblThread ON tblThreadPost.ThreadID=tblThread.ThreadID LEFT JOIN tblMessage ON tblThreadPost.MessageID=tblMessage.MessageID WHERE tblMessage.MessageID IS NULL;"); + st.Step(); + while(st.RowReturned()) + { + int messageid=-1; + int boardid=-1; + + st.ResultInt(0,messageid); + st.ResultInt(1,boardid); + + tb.Build(messageid,boardid,true); + + st.Step(); + } + + // delete threads that have no messages + m_db->Execute("DELETE FROM tblThread WHERE ThreadID IN (SELECT tblThread.ThreadID FROM tblThread LEFT JOIN tblThreadPost ON tblThread.ThreadID=tblThreadPost.ThreadID WHERE tblThreadPost.ThreadID IS NULL);"); + + // TODO - remove after corruption issue fixed + if(ll=="8") + { + std::string dbres=TestDBIntegrity(m_db); + m_log->trace("DBMaintenanceThread::Do10MinuteMaintenance() end TestDBIntegrity returned "+dbres); + } m_log->debug("PeriodicDBMaintenance::Do10MinuteMaintenance"); } void DBMaintenanceThread::Do30MinuteMaintenance() { - + // UNCOMMENT method in run when code is placed here m_log->debug("PeriodicDBMaintenance::Do30MinuteMaintenance"); } void DBMaintenanceThread::Do1HourMaintenance() { + + m_db->Execute("BEGIN;"); // recalculate all trust levels - this is CPU instensive // do 1 identity at a time as doing it with 1 UPDATE statement locks that database for the duration SQLite3DB::Statement st=m_db->Prepare("SELECT TargetIdentityID,PeerMessageTrust,PeerTrustListTrust FROM vwCalculatedPeerTrust;"); @@ -98,12 +177,16 @@ void DBMaintenanceThread::Do1HourMaintenance() // insert all identities not in trust list already m_db->Execute("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) SELECT LocalIdentityID,IdentityID FROM tblLocalIdentity,tblIdentity WHERE LocalIdentityID || '_' || IdentityID NOT IN (SELECT LocalIdentityID || '_' || IdentityID FROM tblIdentityTrust);"); + m_db->Execute("COMMIT;"); + m_log->debug("PeriodicDBMaintenance::Do1HourMaintenance"); } void DBMaintenanceThread::Do6HourMaintenance() { + m_db->Execute("BEGIN;"); + // if we remove a board and the reply boardid is still set to it, we need to replace it with a boardid that does exist SQLite3DB::Statement st=m_db->Prepare("SELECT MessageID FROM tblMessage WHERE ReplyBoardID NOT IN (SELECT BoardID FROM tblBoard);"); SQLite3DB::Statement st2=m_db->Prepare("SELECT BoardID FROM tblMessageBoard WHERE MessageID=?;"); @@ -132,6 +215,8 @@ void DBMaintenanceThread::Do6HourMaintenance() st.Step(); } + m_db->Execute("COMMIT;"); + m_log->debug("PeriodicDBMaintenance::Do6HourMaintenance"); } @@ -139,6 +224,8 @@ void DBMaintenanceThread::Do1DayMaintenance() { Poco::DateTime date; + m_db->Execute("BEGIN;"); + // delete all puzzles 2 or more days old date=Poco::Timestamp(); date-=Poco::Timespan(2,0,0,0,0); @@ -252,14 +339,24 @@ void DBMaintenanceThread::Do1DayMaintenance() // delete old messages date=Poco::Timestamp(); date-=Poco::Timespan(m_deletemessagesolderthan,0,0,0,0); + m_log->trace("PeriodicDBMaintenance::Do1DayMaintenance deleting messages prior to "+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")); st=m_db->Prepare("DELETE FROM tblMessage WHERE MessageDatePrepare("DELETE FROM tblMessageRequests WHERE DayExecute("DELETE FROM tblIdentityTrust WHERE LocalIdentityID NOT IN (SELECT LocalIdentityID FROM tblLocalIdentity);"); m_db->Execute("DELETE FROM tblIdentityTrust WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity);"); + m_db->Execute("COMMIT;"); + m_log->debug("PeriodicDBMaintenance::Do1DayMaintenance"); } @@ -268,7 +365,23 @@ void DBMaintenanceThread::run() { m_log->debug("DBMaintenanceThread::run thread started."); + LoadDatabase(); + Option option(m_db); + std::string tempval(""); + + m_deletemessagesolderthan=180; + tempval="180"; + option.Get("DeleteMessagesOlderThan",tempval); + StringFunctions::Convert(tempval,m_deletemessagesolderthan); + + m_messagedownloadmaxdaysbackward=5; + tempval="5"; + option.Get("MessageDownloadMaxDaysBackward",tempval); + StringFunctions::Convert(tempval,m_messagedownloadmaxdaysbackward); + + Poco::DateTime now; + int i=0; do { @@ -279,11 +392,13 @@ void DBMaintenanceThread::run() Do10MinuteMaintenance(); m_last10minute=Poco::Timestamp(); } + /* if((m_last30minute+Poco::Timespan(0,0,30,0,0))<=now) { Do30MinuteMaintenance(); m_last30minute=Poco::Timestamp(); } + */ if((m_last1hour+Poco::Timespan(0,1,0,0,0))<=now) { Do1HourMaintenance(); @@ -300,7 +415,12 @@ void DBMaintenanceThread::run() m_last1day=Poco::Timestamp(); } - Poco::Thread::sleep(1000); + i=0; + while(i++<5 && !IsCancelled()) + { + Poco::Thread::sleep(1000); + } + }while(!IsCancelled()); m_log->debug("DBMaintenanceThread::run thread exiting.");