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
85 // set null peer trust for identities without a calculated trust
\r
86 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE IdentityID NOT IN (SELECT TargetIdentityID FROM vwCalculatedPeerTrust);");
\r
87 upd=m_db->Prepare("UPDATE tblIdentity SET PeerMessageTrust=NULL, PeerTrustListTrust=NULL WHERE IdentityID=?;");
\r
89 while(st.RowReturned())
\r
92 st.ResultInt(0,identityid);
\r
93 upd.Bind(0,identityid);
\r
99 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do1HourMaintenance");
\r
102 void PeriodicDBMaintenance::Do6HourMaintenance()
\r
105 // 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
\r
106 SQLite3DB::Statement st=m_db->Prepare("SELECT MessageID FROM tblMessage WHERE ReplyBoardID NOT IN (SELECT BoardID FROM tblBoard);");
\r
107 SQLite3DB::Statement st2=m_db->Prepare("SELECT BoardID FROM tblMessageBoard WHERE MessageID=?;");
\r
108 SQLite3DB::Statement upd=m_db->Prepare("UPDATE tblMessage SET ReplyBoardID=? WHERE MessageID=?;");
\r
110 while(st.RowReturned())
\r
112 // find a valid boardid for the message
\r
116 st.ResultInt(0,messageid);
\r
118 st2.Bind(0,messageid);
\r
120 if(st2.RowReturned())
\r
122 st2.ResultInt(0,boardid);
\r
123 upd.Bind(0,boardid);
\r
124 upd.Bind(1,messageid);
\r
133 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do6HourMaintenance");
\r
136 void PeriodicDBMaintenance::Do1DayMaintenance()
\r
140 // delete all puzzles 2 or more days old
\r
141 date.SetToGMTime();
\r
142 date.Add(0,0,0,-2);
\r
143 m_db->Execute("DELETE FROM tblIntroductionPuzzleInserts WHERE Day<='"+date.Format("%Y-%m-%d")+"';");
\r
144 m_db->Execute("DELETE FROM tblIntroductionPuzzleRequests WHERE Day<='"+date.Format("%Y-%m-%d")+"';");
\r
146 // delete all identities we've never seen and were added more than 20 days ago
\r
147 date.SetToGMTime();
\r
148 date.Add(0,0,0,-20);
\r
149 m_db->Execute("DELETE FROM tblIdentity WHERE LastSeen IS NULL AND DateAdded<'"+date.Format("%Y-%m-%d")+"';");
\r
151 // delete old identity requests - we don't need them anymore
\r
152 date.SetToGMTime();
\r
153 date.Add(0,0,0,-2);
\r
154 m_db->Execute("DELETE FROM tblIdentityRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
156 // delete old board list inserts/requests - we don't need them anymore
\r
157 date.SetToGMTime();
\r
158 date.Add(0,0,0,-2);
\r
159 m_db->Execute("DELETE FROM tblBoardListInserts WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
160 m_db->Execute("DELETE FROM tblBoardListRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
162 // delete old local identity inserts - we don't need them anymore
\r
163 date.SetToGMTime();
\r
164 date.Add(0,0,0,-2);
\r
165 m_db->Execute("DELETE FROM tblLocalIdentityInserts WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
167 // delete old message list inserts/requests - we don't need them anymore
\r
168 date.SetToGMTime();
\r
169 date.Add(0,0,0,-2);
\r
170 m_db->Execute("DELETE FROM tblMessageListInserts WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
171 m_db->Execute("DELETE FROM tblMessageListRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
173 // delete old trust list inserts/requests - we don't need them anymore
\r
174 date.SetToGMTime();
\r
175 date.Add(0,0,0,-2);
\r
176 m_db->Execute("DELETE FROM tblTrustListInserts WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
177 m_db->Execute("DELETE FROM tblTrustListRequests WHERE Day<'"+date.Format("%Y-%m-%d")+"';");
\r
179 // delete trust lists from identities we aren't trusting anymore
\r
180 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
182 // remove identityid from messages where the identity has been deleted
\r
183 m_db->Execute("UPDATE tblMessage SET IdentityID=NULL WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity);");
\r
185 // try to re-attach messages from identities that were previously deleted, but have been since re-added
\r
186 // first get the names from messages that have a NULL IdentityID
\r
187 SQLite3DB::Statement st=m_db->Prepare("SELECT FromName FROM tblMessage WHERE IdentityID IS NULL GROUP BY FromName;");
\r
189 while(st.RowReturned())
\r
191 std::string name="";
\r
192 std::string publickey="";
\r
194 st.ResultText(0,name);
\r
196 std::vector<std::string> parts;
\r
197 StringFunctions::Split(name,"@",parts);
\r
199 // find identities with this name
\r
200 SQLite3DB::Statement st2=m_db->Prepare("SELECT IdentityID,PublicKey FROM tblIdentity WHERE Name=?;");
\r
203 while(st2.RowReturned())
\r
207 st2.ResultText(1,publickey);
\r
208 // check if public key matches 2nd part
\r
209 if(parts.size()>1 && publickey.find(parts[1])==4)
\r
211 // we have the identity - so update the messages table with the identityid
\r
212 st2.ResultInt(0,identityid);
\r
214 SQLite3DB::Statement st3=m_db->Prepare("UPDATE tblMessage SET IdentityID=? WHERE Name=? AND IdentityID IS NULL;");
\r
215 st3.Bind(0,identityid);
\r
225 // delete single use identities that are older than 7 days
\r
226 date.SetToGMTime();
\r
227 date.Add(0,0,0,-7);
\r
228 st=m_db->Prepare("DELETE FROM tblIdentity WHERE SingleUse='true' AND DateAdded<?;");
\r
229 st.Bind(0,date.Format("%Y-%m-%d %H:%M:%S"));
\r
232 // delete local single use identities that are older than 7 days
\r
233 date.SetToGMTime();
\r
234 date.Add(0,0,0,-7);
\r
235 st=m_db->Prepare("DELETE FROM tblLocalIdentity WHERE SingleUse='true' AND DateCreated<?;");
\r
236 st.Bind(0,date.Format("%Y-%m-%d %H:%M:%S"));
\r
239 // delete old messages
\r
240 date.SetToGMTime();
\r
241 date.Add(0,0,0,-m_deletemessagesolderthan);
\r
242 st=m_db->Prepare("DELETE FROM tblMessage WHERE MessageDate<?;");
\r
243 st.Bind(0,date.Format("%Y-%m-%d"));
\r
246 // delete tblIdentityTrust for local identities and identities that have been deleted
\r
247 m_db->Execute("DELETE FROM tblIdentityTrust WHERE LocalIdentityID NOT IN (SELECT LocalIdentityID FROM tblLocalIdentity);");
\r
248 m_db->Execute("DELETE FROM tblIdentityTrust WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity);");
\r
250 m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do1DayMaintenance");
\r
254 void PeriodicDBMaintenance::Process()
\r
259 if(m_check10mins<=(now-((1.0/1440.0)*10.0)))
\r
261 Do10MinuteMaintenance();
\r
264 if(m_check30mins<=(now-((1.0/1440.0)*30.0)))
\r
266 Do30MinuteMaintenance();
\r
269 if(m_check1hour<=(now-(1.0/24.0)))
\r
271 Do1HourMaintenance();
\r
274 if(m_check6hours<=(now-(1.0/4.0)))
\r
276 Do6HourMaintenance();
\r
279 if(m_check1day<=(now-(1.0)))
\r
281 Do1DayMaintenance();
\r
287 void PeriodicDBMaintenance::RegisterWithThread(FreenetMasterThread *thread)
\r
289 thread->RegisterPeriodicProcessor(this);
\r