1 #include "../../include/freenet/periodicdbmaintenance.h"
\r
2 #include "../../include/stringfunctions.h"
\r
3 #include "../../include/option.h"
\r
9 PeriodicDBMaintenance::PeriodicDBMaintenance()
\r
11 std::string tempval;
\r
13 m_check10mins.SetToGMTime();
\r
14 m_check30mins.SetToGMTime();
\r
15 m_check1hour.SetToGMTime();
\r
16 m_check6hours.SetToGMTime();
\r
17 m_check1day.SetToGMTime();
\r
19 // set back times so they will do their first maintenance soon ( within the next hour ) - stagger so not all run at once
\r
20 m_check30mins.Add(0,-5);
\r
21 m_check1hour.Add(0,-42);
\r
22 m_check6hours.Add(0,-1,-5);
\r
23 m_check1day.Add(0,0,-23);
\r
26 Option::Instance()->Get("DeleteMessagesOlderThan",tempval);
\r
27 StringFunctions::Convert(tempval,m_deletemessagesolderthan);
\r
31 void PeriodicDBMaintenance::Do10MinuteMaintenance()
\r
34 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do10MinuteMaintenance");
\r
37 void PeriodicDBMaintenance::Do30MinuteMaintenance()
\r
40 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do30MinuteMaintenance");
\r
43 void PeriodicDBMaintenance::Do1HourMaintenance()
\r
45 // recalculate all trust levels - this is CPU instensive
\r
46 // do 1 identity at a time as doing it with 1 UPDATE statement locks that database for the duration
\r
47 SQLite3DB::Statement st=m_db->Prepare("SELECT TargetIdentityID,PeerMessageTrust,PeerTrustListTrust FROM vwCalculatedPeerTrust;");
\r
48 SQLite3DB::Statement upd=m_db->Prepare("UPDATE tblIdentity SET PeerMessageTrust=?, PeerTrustListTrust=? WHERE IdentityID=?");
\r
50 while(st.RowReturned())
\r
55 st.ResultInt(0,identityid);
\r
57 upd.Bind(0,identityid);
\r
58 if(st.ResultNull(1)==false)
\r
61 st.ResultInt(1,trust);
\r
68 if(st.ResultNull(2)==false)
\r
71 st.ResultInt(2,trust);
\r
78 upd.Bind(2,identityid);
\r
87 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do1HourMaintenance");
\r
90 void PeriodicDBMaintenance::Do6HourMaintenance()
\r
93 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do6HourMaintenance");
\r
96 void PeriodicDBMaintenance::Do1DayMaintenance()
\r
100 // delete all puzzles 2 or more days old
\r
101 date.SetToGMTime();
\r
102 date.Add(0,0,0,-2);
\r
103 m_db->Execute("DELETE FROM tblIntroductionPuzzleInserts WHERE Day<='"+date.Format("%Y-%m-%d")+"';");
\r
104 m_db->Execute("DELETE FROM tblIntroductionPuzzleRequests WHERE Day<='"+date.Format("%Y-%m-%d")+"';");
\r
106 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do1DayMaintenance");
\r
108 // delete all identities we've never seen and were added more than 20 days ago
\r
109 date.SetToGMTime();
\r
110 date.Add(0,0,0,-20);
\r
111 m_db->Execute("DELETE FROM tblIdentity WHERE LastSeen IS NULL AND DateAdded<'"+date.Format("%Y-%m-%d")+"';");
\r
113 // delete old identity requests - we don't need them anymore
\r
114 date.SetToGMTime();
\r
115 date.Add(0,0,0,-2);
\r
116 m_db->Execute("DELETE FROM tblIdentityRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
118 // delete old local identity inserts - we don't need them anymore
\r
119 date.SetToGMTime();
\r
120 date.Add(0,0,0,-2);
\r
121 m_db->Execute("DELETE FROM tblLocalIdentityInserts WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
123 // delete old message list inserts/requests - we don't need them anymore
\r
124 date.SetToGMTime();
\r
125 date.Add(0,0,0,-2);
\r
126 m_db->Execute("DELETE FROM tblMessageListInserts WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
127 m_db->Execute("DELETE FROM tblMessageListRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
129 // delete old trust list inserts/requests - we don't need them anymore
\r
130 date.SetToGMTime();
\r
131 date.Add(0,0,0,-2);
\r
132 m_db->Execute("DELETE FROM tblTrustListInserts WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
133 m_db->Execute("DELETE FROM tblTrustListRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
135 // delete trust lists from identities we aren't trusting anymore
\r
136 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')));");
\r
138 // remove identityid from messages where the identity has been deleted
\r
139 m_db->Execute("UPDATE tblMessage SET IdentityID=NULL WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity);");
\r
141 // try to re-attach messages from identities that were previously deleted, but have been since re-added
\r
142 // first get the names from messages that have a NULL IdentityID
\r
143 SQLite3DB::Statement st=m_db->Prepare("SELECT FromName FROM tblMessage WHERE IdentityID IS NULL GROUP BY FromName;");
\r
145 while(st.RowReturned())
\r
147 std::string name="";
\r
148 std::string publickey="";
\r
150 st.ResultText(0,name);
\r
152 std::vector<std::string> parts;
\r
153 StringFunctions::Split(name,"@",parts);
\r
155 // find identities with this name
\r
156 SQLite3DB::Statement st2=m_db->Prepare("SELECT IdentityID,PublicKey FROM tblIdentity WHERE Name=?;");
\r
159 while(st2.RowReturned())
\r
163 st2.ResultText(1,publickey);
\r
164 // check if public key matches 2nd part
\r
165 if(parts.size()>1 && publickey.find(parts[1])==4)
\r
167 // we have the identity - so update the messages table with the identityid
\r
168 st2.ResultInt(0,identityid);
\r
170 SQLite3DB::Statement st3=m_db->Prepare("UPDATE tblMessage SET IdentityID=? WHERE Name=? AND IdentityID IS NULL;");
\r
171 st3.Bind(0,identityid);
\r
181 // delete single use identities that are older than 7 days
\r
182 date.SetToGMTime();
\r
183 date.Add(0,0,0,-7);
\r
184 st=m_db->Prepare("DELETE FROM tblIdentity WHERE SingleUse='true' AND DateAdded<?;");
\r
185 st.Bind(0,date.Format("%Y-%m-%d %H:%M:%S"));
\r
188 // delete local single use identities that are older than 7 days
\r
189 date.SetToGMTime();
\r
190 date.Add(0,0,0,-7);
\r
191 st=m_db->Prepare("DELETE FROM tblLocalIdentity WHERE SingleUse='true' AND DateCreated<?;");
\r
192 st.Bind(0,date.Format("%Y-%m-%d %H:%M:%S"));
\r
195 // delete old messages
\r
196 date.SetToGMTime();
\r
197 date.Add(0,0,0,-m_deletemessagesolderthan);
\r
198 st=m_db->Prepare("DELETE FROM tblMessage WHERE MessageDate<?;");
\r
199 st.Bind(0,date.Format("%Y-%m-%d"));
\r
204 void PeriodicDBMaintenance::Process()
\r
209 if(m_check10mins<=(now-((1.0/1440.0)*10.0)))
\r
211 Do10MinuteMaintenance();
\r
214 if(m_check30mins<=(now-((1.0/1440.0)*30.0)))
\r
216 Do30MinuteMaintenance();
\r
219 if(m_check1hour<=(now-(1.0/24.0)))
\r
221 Do1HourMaintenance();
\r
224 if(m_check6hours<=(now-(1.0/4.0)))
\r
226 Do6HourMaintenance();
\r
229 if(m_check1day<=(now-(1.0)))
\r
231 Do1DayMaintenance();
\r
237 void PeriodicDBMaintenance::RegisterWithThread(FreenetMasterThread *thread)
\r
239 thread->RegisterPeriodicProcessor(this);
\r