X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ffreenet%2Fperiodicdbmaintenance.cpp;h=f6f501bd62c6437f14f0cb7304f52c671325265c;hb=3dc3ac3cfe10b7196a7977e9c041c29fa141c35e;hp=0febbbd736f6210ff8a1ecedf89fe8cec2008e12;hpb=f60495a029c54358f82956482fe203fe2b7b5b23;p=fms.git diff --git a/src/freenet/periodicdbmaintenance.cpp b/src/freenet/periodicdbmaintenance.cpp index 0febbbd..f6f501b 100644 --- a/src/freenet/periodicdbmaintenance.cpp +++ b/src/freenet/periodicdbmaintenance.cpp @@ -1,4 +1,6 @@ #include "../../include/freenet/periodicdbmaintenance.h" +#include "../../include/stringfunctions.h" +#include "../../include/option.h" #ifdef XMEM #include @@ -6,6 +8,8 @@ PeriodicDBMaintenance::PeriodicDBMaintenance() { + std::string tempval; + m_check10mins.SetToGMTime(); m_check30mins.SetToGMTime(); m_check1hour.SetToGMTime(); @@ -13,10 +17,15 @@ PeriodicDBMaintenance::PeriodicDBMaintenance() m_check1day.SetToGMTime(); // set back times so they will do their first maintenance soon ( within the next hour ) - stagger so not all run at once - m_check1hour.Add(0,-45); - m_check6hours.Add(0,0,-5); + m_check30mins.Add(0,-5); + m_check1hour.Add(0,-42); + m_check6hours.Add(0,-1,-5); m_check1day.Add(0,0,-23); + tempval="180"; + Option::Instance()->Get("DeleteMessagesOlderThan",tempval); + StringFunctions::Convert(tempval,m_deletemessagesolderthan); + } void PeriodicDBMaintenance::Do10MinuteMaintenance() @@ -34,8 +43,46 @@ void PeriodicDBMaintenance::Do30MinuteMaintenance() void PeriodicDBMaintenance::Do1HourMaintenance() { // recalculate all trust levels - this is CPU instensive - // TODO - will probably have to change this to do 1 identity at a time as this locks that database for the duration - m_db->Execute("UPDATE tblIdentity SET PeerMessageTrust=(SELECT PeerMessageTrust FROM vwCalculatedPeerTrust WHERE TargetIdentityID=IdentityID), PeerTrustListTrust=(SELECT PeerTrustListTrust FROM vwCalculatedPeerTrust WHERE TargetIdentityID=IdentityID);"); + // 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;"); + SQLite3DB::Statement upd=m_db->Prepare("UPDATE tblIdentity SET PeerMessageTrust=?, PeerTrustListTrust=? WHERE IdentityID=?"); + st.Step(); + while(st.RowReturned()) + { + int identityid=0; + int trust=0; + + st.ResultInt(0,identityid); + + upd.Bind(0,identityid); + if(st.ResultNull(1)==false) + { + trust=0; + st.ResultInt(1,trust); + upd.Bind(0,trust); + } + else + { + upd.Bind(0); + } + if(st.ResultNull(2)==false) + { + trust=0; + st.ResultInt(2,trust); + upd.Bind(1,trust); + } + else + { + upd.Bind(1); + } + upd.Bind(2,identityid); + upd.Step(); + upd.Reset(); + + st.Step(); + } + + m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do1HourMaintenance"); } @@ -83,7 +130,74 @@ void PeriodicDBMaintenance::Do1DayMaintenance() date.SetToGMTime(); date.Add(0,0,0,-2); m_db->Execute("DELETE FROM tblTrustListInserts WHERE Day<'"+date.Format("%Y-%m-%d")+"';"); - m_db->Execute("DELETE FROM tblTrustListRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';"); + m_db->Execute("DELETE FROM tblTrustListRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';"); + + // delete trust lists from identities we aren't trusting anymore + m_db->Execute("DELETE FROM tblPeerTrust WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity WHERE (LocalTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinLocalTrustListTrust')) AND (PeerTrustListTrust IS NULL OR PeerTrustListTrust>=(SELECT OptionValue FROM tblOption WHERE Option='MinPeerTrustListTrust')));"); + + // remove identityid from messages where the identity has been deleted + m_db->Execute("UPDATE tblMessage SET IdentityID=NULL WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity);"); + + // try to re-attach messages from identities that were previously deleted, but have been since re-added + // first get the names from messages that have a NULL IdentityID + SQLite3DB::Statement st=m_db->Prepare("SELECT FromName FROM tblMessage WHERE IdentityID IS NULL GROUP BY FromName;"); + st.Step(); + while(st.RowReturned()) + { + std::string name=""; + std::string publickey=""; + int identityid=0; + st.ResultText(0,name); + + std::vector parts; + StringFunctions::Split(name,"@",parts); + + // find identities with this name + SQLite3DB::Statement st2=m_db->Prepare("SELECT IdentityID,PublicKey FROM tblIdentity WHERE Name=?;"); + st2.Bind(0,name); + st2.Step(); + while(st2.RowReturned()) + { + publickey=""; + identityid=0; + st2.ResultText(1,publickey); + // check if public key matches 2nd part + if(parts.size()>1 && publickey.find(parts[1])==4) + { + // we have the identity - so update the messages table with the identityid + st2.ResultInt(0,identityid); + + SQLite3DB::Statement st3=m_db->Prepare("UPDATE tblMessage SET IdentityID=? WHERE Name=? AND IdentityID IS NULL;"); + st3.Bind(0,identityid); + st3.Bind(1,name); + st3.Step(); + } + st2.Step(); + } + + st.Step(); + } + + // delete single use identities that are older than 7 days + date.SetToGMTime(); + date.Add(0,0,0,-7); + st=m_db->Prepare("DELETE FROM tblIdentity WHERE SingleUse='true' AND DateAddedPrepare("DELETE FROM tblLocalIdentity WHERE SingleUse='true' AND DateCreatedPrepare("DELETE FROM tblMessage WHERE MessageDate