1 #include "../include/dbmaintenancethread.h"
\r
2 #include "../include/stringfunctions.h"
\r
3 #include "../include/option.h"
\r
5 #include <Poco/Timestamp.h>
\r
6 #include <Poco/Timespan.h>
\r
7 #include <Poco/DateTimeFormatter.h>
\r
8 #include <Poco/Thread.h>
\r
10 DBMaintenanceThread::DBMaintenanceThread()
\r
12 // move last maintenance times back so they will all run soon
\r
13 m_last10minute=Poco::Timestamp();
\r
14 m_last30minute=Poco::Timestamp();
\r
15 m_last30minute-=Poco::Timespan(0,0,11,0,0);
\r
16 m_last1hour=Poco::Timestamp();
\r
17 m_last1hour-=Poco::Timespan(0,0,49,0,0);
\r
18 m_last6hour=Poco::Timestamp();
\r
19 m_last6hour-=Poco::Timespan(0,5,42,0,0);
\r
20 m_last1day=Poco::Timestamp();
\r
21 m_last1day-=Poco::Timespan(0,23,51,0,0);
\r
23 std::string tempval="180";
\r
24 Option::Instance()->Get("DeleteMessagesOlderThan",tempval);
\r
25 StringFunctions::Convert(tempval,m_deletemessagesolderthan);
\r
30 void DBMaintenanceThread::Do10MinuteMaintenance()
\r
33 m_log->debug("PeriodicDBMaintenance::Do10MinuteMaintenance");
\r
36 void DBMaintenanceThread::Do30MinuteMaintenance()
\r
39 m_log->debug("PeriodicDBMaintenance::Do30MinuteMaintenance");
\r
42 void DBMaintenanceThread::Do1HourMaintenance()
\r
44 // recalculate all trust levels - this is CPU instensive
\r
45 // do 1 identity at a time as doing it with 1 UPDATE statement locks that database for the duration
\r
46 SQLite3DB::Statement st=m_db->Prepare("SELECT TargetIdentityID,PeerMessageTrust,PeerTrustListTrust FROM vwCalculatedPeerTrust;");
\r
47 SQLite3DB::Statement upd=m_db->Prepare("UPDATE tblIdentity SET PeerMessageTrust=?, PeerTrustListTrust=? WHERE IdentityID=?");
\r
49 while(st.RowReturned())
\r
54 st.ResultInt(0,identityid);
\r
56 upd.Bind(0,identityid);
\r
57 if(st.ResultNull(1)==false)
\r
60 st.ResultInt(1,trust);
\r
67 if(st.ResultNull(2)==false)
\r
70 st.ResultInt(2,trust);
\r
77 upd.Bind(2,identityid);
\r
84 // set null peer trust for identities without a calculated trust
\r
85 st=m_db->Prepare("SELECT IdentityID FROM tblIdentity WHERE IdentityID NOT IN (SELECT TargetIdentityID FROM vwCalculatedPeerTrust);");
\r
86 upd=m_db->Prepare("UPDATE tblIdentity SET PeerMessageTrust=NULL, PeerTrustListTrust=NULL WHERE IdentityID=?;");
\r
88 while(st.RowReturned())
\r
91 st.ResultInt(0,identityid);
\r
92 upd.Bind(0,identityid);
\r
98 // insert all identities not in trust list already
\r
99 m_db->Execute("INSERT INTO tblIdentityTrust(LocalIdentityID,IdentityID) SELECT LocalIdentityID,IdentityID FROM tblLocalIdentity,tblIdentity WHERE LocalIdentityID || '_' || IdentityID NOT IN (SELECT LocalIdentityID || '_' || IdentityID FROM tblIdentityTrust);");
\r
101 m_log->debug("PeriodicDBMaintenance::Do1HourMaintenance");
\r
104 void DBMaintenanceThread::Do6HourMaintenance()
\r
107 // 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
108 SQLite3DB::Statement st=m_db->Prepare("SELECT MessageID FROM tblMessage WHERE ReplyBoardID NOT IN (SELECT BoardID FROM tblBoard);");
\r
109 SQLite3DB::Statement st2=m_db->Prepare("SELECT BoardID FROM tblMessageBoard WHERE MessageID=?;");
\r
110 SQLite3DB::Statement upd=m_db->Prepare("UPDATE tblMessage SET ReplyBoardID=? WHERE MessageID=?;");
\r
112 while(st.RowReturned())
\r
114 // find a valid boardid for the message
\r
118 st.ResultInt(0,messageid);
\r
120 st2.Bind(0,messageid);
\r
122 if(st2.RowReturned())
\r
124 st2.ResultInt(0,boardid);
\r
125 upd.Bind(0,boardid);
\r
126 upd.Bind(1,messageid);
\r
135 m_log->debug("PeriodicDBMaintenance::Do6HourMaintenance");
\r
138 void DBMaintenanceThread::Do1DayMaintenance()
\r
140 Poco::DateTime date;
\r
142 // delete all puzzles 2 or more days old
\r
143 date=Poco::Timestamp();
\r
144 date-=Poco::Timespan(2,0,0,0,0);
\r
145 m_db->Execute("DELETE FROM tblIntroductionPuzzleInserts WHERE Day<='"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
146 m_db->Execute("DELETE FROM tblIntroductionPuzzleRequests WHERE Day<='"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
148 // delete all identities we've never seen and were added more than 20 days ago
\r
149 date=Poco::Timestamp();
\r
150 date-=Poco::Timespan(20,0,0,0,0);
\r
151 m_db->Execute("DELETE FROM tblIdentity WHERE LastSeen IS NULL AND DateAdded<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
153 // delete old identity requests - we don't need them anymore
\r
154 date=Poco::Timestamp();
\r
155 date-=Poco::Timespan(2,0,0,0,0);
\r
156 m_db->Execute("DELETE FROM tblIdentityRequests WHERE Day<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
158 // delete old board list inserts/requests - we don't need them anymore
\r
159 date=Poco::Timestamp();
\r
160 date-=Poco::Timespan(2,0,0,0,0);
\r
161 m_db->Execute("DELETE FROM tblBoardListInserts WHERE Day<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
162 m_db->Execute("DELETE FROM tblBoardListRequests WHERE Day<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
164 // delete old local identity inserts - we don't need them anymore
\r
165 date=Poco::Timestamp();
\r
166 date-=Poco::Timespan(2,0,0,0,0);
\r
167 m_db->Execute("DELETE FROM tblLocalIdentityInserts WHERE Day<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
169 // delete old message list inserts/requests - we don't need them anymore
\r
170 date=Poco::Timestamp();
\r
171 date-=Poco::Timespan(2,0,0,0,0);
\r
172 m_db->Execute("DELETE FROM tblMessageListInserts WHERE Day<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
173 m_db->Execute("DELETE FROM tblMessageListRequests WHERE Day<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
175 // delete old trust list inserts/requests - we don't need them anymore
\r
176 date=Poco::Timestamp();
\r
177 date-=Poco::Timespan(2,0,0,0,0);
\r
178 m_db->Execute("DELETE FROM tblTrustListInserts WHERE Day<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
179 m_db->Execute("DELETE FROM tblTrustListRequests WHERE Day<'"+Poco::DateTimeFormatter::format(date,"%Y-%m-%d")+"';");
\r
181 // delete trust lists from identities we aren't trusting anymore
\r
182 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
184 // remove identityid from messages where the identity has been deleted
\r
185 m_db->Execute("UPDATE tblMessage SET IdentityID=NULL WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity);");
\r
187 // try to re-attach messages from identities that were previously deleted, but have been since re-added
\r
188 // first get the names from messages that have a NULL IdentityID
\r
189 SQLite3DB::Statement st=m_db->Prepare("SELECT FromName FROM tblMessage WHERE IdentityID IS NULL GROUP BY FromName;");
\r
191 while(st.RowReturned())
\r
193 std::string name="";
\r
194 std::string namepart="";
\r
195 std::string publickey="";
\r
197 st.ResultText(0,name);
\r
199 std::vector<std::string> parts;
\r
200 StringFunctions::Split(name,"@",parts);
\r
202 // name can have a @ in it - so reattach all parts except the last which is the key
\r
203 for(long i=0; i<parts.size()-1; i++)
\r
209 namepart+=parts[i];
\r
212 // find identities with this name
\r
213 SQLite3DB::Statement st2=m_db->Prepare("SELECT IdentityID,PublicKey FROM tblIdentity WHERE Name=?;");
\r
214 st2.Bind(0,namepart);
\r
216 while(st2.RowReturned())
\r
220 st2.ResultText(1,publickey);
\r
221 // check if public key matches 2nd part
\r
222 if(parts.size()>1 && publickey.find(parts[1])==4)
\r
224 // we have the identity - so update the messages table with the identityid
\r
225 st2.ResultInt(0,identityid);
\r
227 SQLite3DB::Statement st3=m_db->Prepare("UPDATE tblMessage SET IdentityID=? WHERE FromName=? AND IdentityID IS NULL;");
\r
228 st3.Bind(0,identityid);
\r
238 // delete single use identities that are older than 7 days
\r
239 date=Poco::Timestamp();
\r
240 date-=Poco::Timespan(7,0,0,0,0);
\r
241 st=m_db->Prepare("DELETE FROM tblIdentity WHERE SingleUse='true' AND DateAdded<?;");
\r
242 st.Bind(0,Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S"));
\r
245 // delete local single use identities that are older than 7 days
\r
246 date=Poco::Timestamp();
\r
247 date-=Poco::Timespan(7,0,0,0,0);
\r
248 st=m_db->Prepare("DELETE FROM tblLocalIdentity WHERE SingleUse='true' AND DateCreated<?;");
\r
249 st.Bind(0,Poco::DateTimeFormatter::format(date,"%Y-%m-%d %H:%M:%S"));
\r
252 // delete old messages
\r
253 date=Poco::Timestamp();
\r
254 date-=Poco::Timespan(m_deletemessagesolderthan,0,0,0,0);
\r
255 st=m_db->Prepare("DELETE FROM tblMessage WHERE MessageDate<?;");
\r
256 st.Bind(0,Poco::DateTimeFormatter::format(date,"%Y-%m-%d"));
\r
259 // delete tblIdentityTrust for local identities and identities that have been deleted
\r
260 m_db->Execute("DELETE FROM tblIdentityTrust WHERE LocalIdentityID NOT IN (SELECT LocalIdentityID FROM tblLocalIdentity);");
\r
261 m_db->Execute("DELETE FROM tblIdentityTrust WHERE IdentityID NOT IN (SELECT IdentityID FROM tblIdentity);");
\r
263 m_log->debug("PeriodicDBMaintenance::Do1DayMaintenance");
\r
267 void DBMaintenanceThread::run()
\r
269 m_log->debug("DBMaintenanceThread::run thread started.");
\r
271 Poco::DateTime now;
\r
275 now=Poco::Timestamp();
\r
277 if((m_last10minute+Poco::Timespan(0,0,10,0,0))<=now)
\r
279 Do10MinuteMaintenance();
\r
280 m_last10minute=Poco::Timestamp();
\r
282 if((m_last30minute+Poco::Timespan(0,0,30,0,0))<=now)
\r
284 Do30MinuteMaintenance();
\r
285 m_last30minute=Poco::Timestamp();
\r
287 if((m_last1hour+Poco::Timespan(0,1,0,0,0))<=now)
\r
289 Do1HourMaintenance();
\r
290 m_last1hour=Poco::Timestamp();
\r
292 if((m_last6hour+Poco::Timespan(0,6,0,0,0))<=now)
\r
294 Do6HourMaintenance();
\r
295 m_last6hour=Poco::Timestamp();
\r
297 if((m_last1day+Poco::Timespan(1,0,0,0,0))<=now)
\r
299 Do1DayMaintenance();
\r
300 m_last1day=Poco::Timestamp();
\r
303 Poco::Thread::sleep(1000);
\r
304 }while(!IsCancelled());
\r
306 m_log->debug("DBMaintenanceThread::run thread exiting.");
\r