f6f501bd62c6437f14f0cb7304f52c671325265c
[fms.git] / src / freenet / periodicdbmaintenance.cpp
1 #include "../../include/freenet/periodicdbmaintenance.h"\r
2 #include "../../include/stringfunctions.h"\r
3 #include "../../include/option.h"\r
4 \r
5 #ifdef XMEM\r
6         #include <xmem.h>\r
7 #endif\r
8 \r
9 PeriodicDBMaintenance::PeriodicDBMaintenance()\r
10 {\r
11         std::string tempval;\r
12 \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
18 \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
24 \r
25         tempval="180";\r
26         Option::Instance()->Get("DeleteMessagesOlderThan",tempval);\r
27         StringFunctions::Convert(tempval,m_deletemessagesolderthan);\r
28 \r
29 }\r
30 \r
31 void PeriodicDBMaintenance::Do10MinuteMaintenance()\r
32 {\r
33 \r
34         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do10MinuteMaintenance");\r
35 }\r
36 \r
37 void PeriodicDBMaintenance::Do30MinuteMaintenance()\r
38 {\r
39 \r
40         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do30MinuteMaintenance");\r
41 }\r
42 \r
43 void PeriodicDBMaintenance::Do1HourMaintenance()\r
44 {\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
49         st.Step();\r
50         while(st.RowReturned())\r
51         {\r
52                 int identityid=0;\r
53                 int trust=0;\r
54                 \r
55                 st.ResultInt(0,identityid);\r
56 \r
57                 upd.Bind(0,identityid);\r
58                 if(st.ResultNull(1)==false)\r
59                 {\r
60                         trust=0;\r
61                         st.ResultInt(1,trust);\r
62                         upd.Bind(0,trust);\r
63                 }\r
64                 else\r
65                 {\r
66                         upd.Bind(0);\r
67                 }\r
68                 if(st.ResultNull(2)==false)\r
69                 {\r
70                         trust=0;\r
71                         st.ResultInt(2,trust);\r
72                         upd.Bind(1,trust);\r
73                 }\r
74                 else\r
75                 {\r
76                         upd.Bind(1);\r
77                 }\r
78                 upd.Bind(2,identityid);\r
79                 upd.Step();\r
80                 upd.Reset();\r
81 \r
82                 st.Step();\r
83         }\r
84 \r
85 \r
86 \r
87         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do1HourMaintenance");\r
88 }\r
89 \r
90 void PeriodicDBMaintenance::Do6HourMaintenance()\r
91 {\r
92 \r
93         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do6HourMaintenance");\r
94 }\r
95 \r
96 void PeriodicDBMaintenance::Do1DayMaintenance()\r
97 {\r
98         DateTime date;\r
99 \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
105 \r
106         m_log->WriteLog(LogFile::LOGLEVEL_DEBUG,"PeriodicDBMaintenance::Do1DayMaintenance");\r
107 \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
112 \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
117 \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
122 \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
128 \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
134 \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
137 \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
140 \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
144         st.Step();\r
145         while(st.RowReturned())\r
146         {\r
147                 std::string name="";\r
148                 std::string publickey="";\r
149                 int identityid=0;\r
150                 st.ResultText(0,name);\r
151 \r
152                 std::vector<std::string> parts;\r
153                 StringFunctions::Split(name,"@",parts);\r
154 \r
155                 // find identities with this name\r
156                 SQLite3DB::Statement st2=m_db->Prepare("SELECT IdentityID,PublicKey FROM tblIdentity WHERE Name=?;");\r
157                 st2.Bind(0,name);\r
158                 st2.Step();\r
159                 while(st2.RowReturned())\r
160                 {\r
161                         publickey="";\r
162                         identityid=0;\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
166                         {\r
167                                 // we have the identity - so update the messages table with the identityid\r
168                                 st2.ResultInt(0,identityid);\r
169 \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
172                                 st3.Bind(1,name);\r
173                                 st3.Step();\r
174                         }\r
175                         st2.Step();\r
176                 }\r
177 \r
178                 st.Step();\r
179         }\r
180 \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
186         st.Step();\r
187 \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
193         st.Step();\r
194 \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
200         st.Step();\r
201 \r
202 }\r
203 \r
204 void PeriodicDBMaintenance::Process()\r
205 {\r
206         DateTime now;\r
207         now.SetToGMTime();\r
208         \r
209         if(m_check10mins<=(now-((1.0/1440.0)*10.0)))\r
210         {\r
211                 Do10MinuteMaintenance();\r
212                 m_check10mins=now;\r
213         }\r
214         if(m_check30mins<=(now-((1.0/1440.0)*30.0)))\r
215         {\r
216                 Do30MinuteMaintenance();\r
217                 m_check30mins=now;\r
218         }\r
219         if(m_check1hour<=(now-(1.0/24.0)))\r
220         {\r
221                 Do1HourMaintenance();\r
222                 m_check1hour=now;\r
223         }\r
224         if(m_check6hours<=(now-(1.0/4.0)))\r
225         {\r
226                 Do6HourMaintenance();\r
227                 m_check6hours=now;\r
228         }\r
229         if(m_check1day<=(now-(1.0)))\r
230         {\r
231                 Do1DayMaintenance();\r
232                 m_check1day=now;\r
233         }\r
234 \r
235 }\r
236 \r
237 void PeriodicDBMaintenance::RegisterWithThread(FreenetMasterThread *thread)\r
238 {\r
239         thread->RegisterPeriodicProcessor(this);\r
240 }\r