Update doc.
3 * see AUTHORS for the list of contributors
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package org.sonews.storage.impl;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.sql.PreparedStatement;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32 import java.util.regex.PatternSyntaxException;
33 import javax.mail.Header;
34 import javax.mail.internet.InternetAddress;
35 import javax.mail.internet.MimeUtility;
36 import org.sonews.config.Config;
37 import org.sonews.util.Log;
38 import org.sonews.feed.Subscription;
39 import org.sonews.storage.Article;
40 import org.sonews.storage.ArticleHead;
41 import org.sonews.storage.Channel;
42 import org.sonews.storage.Group;
43 import org.sonews.storage.Storage;
44 import org.sonews.storage.StorageBackendException;
45 import org.sonews.util.Pair;
48 * JDBCDatabase facade class.
49 * @author Christian Lins
52 // TODO: Refactor this class to reduce size (e.g. ArticleDatabase GroupDatabase)
53 public class JDBCDatabase implements Storage
56 public static final int MAX_RESTARTS = 3;
58 private Connection conn = null;
59 private PreparedStatement pstmtAddArticle1 = null;
60 private PreparedStatement pstmtAddArticle2 = null;
61 private PreparedStatement pstmtAddArticle3 = null;
62 private PreparedStatement pstmtAddArticle4 = null;
63 private PreparedStatement pstmtAddGroup0 = null;
64 private PreparedStatement pstmtAddEvent = null;
65 private PreparedStatement pstmtCountArticles = null;
66 private PreparedStatement pstmtCountGroups = null;
67 private PreparedStatement pstmtDeleteArticle0 = null;
68 private PreparedStatement pstmtDeleteArticle1 = null;
69 private PreparedStatement pstmtDeleteArticle2 = null;
70 private PreparedStatement pstmtDeleteArticle3 = null;
71 private PreparedStatement pstmtGetArticle0 = null;
72 private PreparedStatement pstmtGetArticle1 = null;
73 private PreparedStatement pstmtGetArticleHeaders0 = null;
74 private PreparedStatement pstmtGetArticleHeaders1 = null;
75 private PreparedStatement pstmtGetArticleHeads = null;
76 private PreparedStatement pstmtGetArticleIDs = null;
77 private PreparedStatement pstmtGetArticleIndex = null;
78 private PreparedStatement pstmtGetConfigValue = null;
79 private PreparedStatement pstmtGetEventsCount0 = null;
80 private PreparedStatement pstmtGetEventsCount1 = null;
81 private PreparedStatement pstmtGetGroupForList = null;
82 private PreparedStatement pstmtGetGroup0 = null;
83 private PreparedStatement pstmtGetGroup1 = null;
84 private PreparedStatement pstmtGetFirstArticleNumber = null;
85 private PreparedStatement pstmtGetListForGroup = null;
86 private PreparedStatement pstmtGetLastArticleNumber = null;
87 private PreparedStatement pstmtGetMaxArticleID = null;
88 private PreparedStatement pstmtGetMaxArticleIndex = null;
89 private PreparedStatement pstmtGetOldestArticle = null;
90 private PreparedStatement pstmtGetPostingsCount = null;
91 private PreparedStatement pstmtGetSubscriptions = null;
92 private PreparedStatement pstmtIsArticleExisting = null;
93 private PreparedStatement pstmtIsGroupExisting = null;
94 private PreparedStatement pstmtPurgeGroup0 = null;
95 private PreparedStatement pstmtPurgeGroup1 = null;
96 private PreparedStatement pstmtSetConfigValue0 = null;
97 private PreparedStatement pstmtSetConfigValue1 = null;
98 private PreparedStatement pstmtUpdateGroup = null;
100 /** How many times the database connection was reinitialized */
101 private int restarts = 0;
104 * Rises the database: reconnect and recreate all prepared statements.
105 * @throws java.lang.SQLException
107 protected void arise()
112 // Load database driver
114 Config.inst().get(Config.STORAGE_DBMSDRIVER, "java.lang.Object"));
116 // Establish database connection
117 this.conn = DriverManager.getConnection(
118 Config.inst().get(Config.STORAGE_DATABASE, "<not specified>"),
119 Config.inst().get(Config.STORAGE_USER, "root"),
120 Config.inst().get(Config.STORAGE_PASSWORD, ""));
122 this.conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
123 if(this.conn.getTransactionIsolation() != Connection.TRANSACTION_SERIALIZABLE)
125 Log.msg("Warning: Database is NOT fully serializable!", false);
128 // Prepare statements for method addArticle()
129 this.pstmtAddArticle1 = conn.prepareStatement(
130 "INSERT INTO articles (article_id, body) VALUES(?, ?)");
131 this.pstmtAddArticle2 = conn.prepareStatement(
132 "INSERT INTO headers (article_id, header_key, header_value, header_index) " +
133 "VALUES (?, ?, ?, ?)");
134 this.pstmtAddArticle3 = conn.prepareStatement(
135 "INSERT INTO postings (group_id, article_id, article_index)" +
137 this.pstmtAddArticle4 = conn.prepareStatement(
138 "INSERT INTO article_ids (article_id, message_id) VALUES (?, ?)");
140 // Prepare statement for method addStatValue()
141 this.pstmtAddEvent = conn.prepareStatement(
142 "INSERT INTO events VALUES (?, ?, ?)");
144 // Prepare statement for method addGroup()
145 this.pstmtAddGroup0 = conn.prepareStatement(
146 "INSERT INTO groups (name, flags) VALUES (?, ?)");
148 // Prepare statement for method countArticles()
149 this.pstmtCountArticles = conn.prepareStatement(
150 "SELECT Count(article_id) FROM article_ids");
152 // Prepare statement for method countGroups()
153 this.pstmtCountGroups = conn.prepareStatement(
154 "SELECT Count(group_id) FROM groups WHERE " +
155 "flags & " + Channel.DELETED + " = 0");
157 // Prepare statements for method delete(article)
158 this.pstmtDeleteArticle0 = conn.prepareStatement(
159 "DELETE FROM articles WHERE article_id = " +
160 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
161 this.pstmtDeleteArticle1 = conn.prepareStatement(
162 "DELETE FROM headers WHERE article_id = " +
163 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
164 this.pstmtDeleteArticle2 = conn.prepareStatement(
165 "DELETE FROM postings WHERE article_id = " +
166 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
167 this.pstmtDeleteArticle3 = conn.prepareStatement(
168 "DELETE FROM article_ids WHERE message_id = ?");
170 // Prepare statements for methods getArticle()
171 this.pstmtGetArticle0 = conn.prepareStatement(
172 "SELECT * FROM articles WHERE article_id = " +
173 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
174 this.pstmtGetArticle1 = conn.prepareStatement(
175 "SELECT * FROM articles WHERE article_id = " +
176 "(SELECT article_id FROM postings WHERE " +
177 "article_index = ? AND group_id = ?)");
179 // Prepare statement for method getArticleHeaders()
180 this.pstmtGetArticleHeaders0 = conn.prepareStatement(
181 "SELECT header_key, header_value FROM headers WHERE article_id = ? " +
182 "ORDER BY header_index ASC");
184 // Prepare statement for method getArticleHeaders(regular expr pattern)
185 this.pstmtGetArticleHeaders1 = conn.prepareStatement(
186 "SELECT p.article_index, h.header_value FROM headers h " +
187 "INNER JOIN postings p ON h.article_id = p.article_id " +
188 "INNER JOIN groups g ON p.group_id = g.group_id " +
189 "WHERE g.name = ? AND " +
190 "h.header_key = ? AND " +
191 "p.article_index >= ? " +
192 "ORDER BY p.article_index ASC");
194 this.pstmtGetArticleIDs = conn.prepareStatement(
195 "SELECT article_index FROM postings WHERE group_id = ?");
197 // Prepare statement for method getArticleIndex
198 this.pstmtGetArticleIndex = conn.prepareStatement(
199 "SELECT article_index FROM postings WHERE " +
200 "article_id = (SELECT article_id FROM article_ids " +
201 "WHERE message_id = ?) " +
202 " AND group_id = ?");
204 // Prepare statements for method getArticleHeads()
205 this.pstmtGetArticleHeads = conn.prepareStatement(
206 "SELECT article_id, article_index FROM postings WHERE " +
207 "postings.group_id = ? AND article_index >= ? AND " +
208 "article_index <= ?");
210 // Prepare statements for method getConfigValue()
211 this.pstmtGetConfigValue = conn.prepareStatement(
212 "SELECT config_value FROM config WHERE config_key = ?");
214 // Prepare statements for method getEventsCount()
215 this.pstmtGetEventsCount0 = conn.prepareStatement(
216 "SELECT Count(*) FROM events WHERE event_key = ? AND " +
217 "event_time >= ? AND event_time < ?");
219 this.pstmtGetEventsCount1 = conn.prepareStatement(
220 "SELECT Count(*) FROM events WHERE event_key = ? AND " +
221 "event_time >= ? AND event_time < ? AND group_id = ?");
223 // Prepare statement for method getGroupForList()
224 this.pstmtGetGroupForList = conn.prepareStatement(
225 "SELECT name FROM groups INNER JOIN groups2list " +
226 "ON groups.group_id = groups2list.group_id " +
227 "WHERE groups2list.listaddress = ?");
229 // Prepare statement for method getGroup()
230 this.pstmtGetGroup0 = conn.prepareStatement(
231 "SELECT group_id, flags FROM groups WHERE Name = ?");
232 this.pstmtGetGroup1 = conn.prepareStatement(
233 "SELECT name FROM groups WHERE group_id = ?");
235 // Prepare statement for method getLastArticleNumber()
236 this.pstmtGetLastArticleNumber = conn.prepareStatement(
237 "SELECT Max(article_index) FROM postings WHERE group_id = ?");
239 // Prepare statement for method getListForGroup()
240 this.pstmtGetListForGroup = conn.prepareStatement(
241 "SELECT listaddress FROM groups2list INNER JOIN groups " +
242 "ON groups.group_id = groups2list.group_id WHERE name = ?");
244 // Prepare statement for method getMaxArticleID()
245 this.pstmtGetMaxArticleID = conn.prepareStatement(
246 "SELECT Max(article_id) FROM articles");
248 // Prepare statement for method getMaxArticleIndex()
249 this.pstmtGetMaxArticleIndex = conn.prepareStatement(
250 "SELECT Max(article_index) FROM postings WHERE group_id = ?");
252 // Prepare statement for method getOldestArticle()
253 this.pstmtGetOldestArticle = conn.prepareStatement(
254 "SELECT message_id FROM article_ids WHERE article_id = " +
255 "(SELECT Min(article_id) FROM article_ids)");
257 // Prepare statement for method getFirstArticleNumber()
258 this.pstmtGetFirstArticleNumber = conn.prepareStatement(
259 "SELECT Min(article_index) FROM postings WHERE group_id = ?");
261 // Prepare statement for method getPostingsCount()
262 this.pstmtGetPostingsCount = conn.prepareStatement(
263 "SELECT Count(*) FROM postings NATURAL JOIN groups " +
264 "WHERE groups.name = ?");
266 // Prepare statement for method getSubscriptions()
267 this.pstmtGetSubscriptions = conn.prepareStatement(
268 "SELECT host, port, name FROM peers NATURAL JOIN " +
269 "peer_subscriptions NATURAL JOIN groups WHERE feedtype = ?");
271 // Prepare statement for method isArticleExisting()
272 this.pstmtIsArticleExisting = conn.prepareStatement(
273 "SELECT Count(article_id) FROM article_ids WHERE message_id = ?");
275 // Prepare statement for method isGroupExisting()
276 this.pstmtIsGroupExisting = conn.prepareStatement(
277 "SELECT * FROM groups WHERE name = ?");
279 // Prepare statement for method setConfigValue()
280 this.pstmtSetConfigValue0 = conn.prepareStatement(
281 "DELETE FROM config WHERE config_key = ?");
282 this.pstmtSetConfigValue1 = conn.prepareStatement(
283 "INSERT INTO config VALUES(?, ?)");
285 // Prepare statements for method purgeGroup()
286 this.pstmtPurgeGroup0 = conn.prepareStatement(
287 "DELETE FROM peer_subscriptions WHERE group_id = ?");
288 this.pstmtPurgeGroup1 = conn.prepareStatement(
289 "DELETE FROM groups WHERE group_id = ?");
291 // Prepare statement for method update(Group)
292 this.pstmtUpdateGroup = conn.prepareStatement(
293 "UPDATE groups SET flags = ?, name = ? WHERE group_id = ?");
295 catch(ClassNotFoundException ex)
297 throw new Error("JDBC Driver not found!", ex);
302 * Adds an article to the database.
305 * @throws java.sql.SQLException
308 public void addArticle(final Article article)
309 throws StorageBackendException
313 this.conn.setAutoCommit(false);
315 int newArticleID = getMaxArticleID() + 1;
317 // Fill prepared statement with values;
318 // writes body to article table
319 pstmtAddArticle1.setInt(1, newArticleID);
320 pstmtAddArticle1.setBytes(2, article.getBody());
321 pstmtAddArticle1.execute();
324 Enumeration headers = article.getAllHeaders();
325 for(int n = 0; headers.hasMoreElements(); n++)
327 Header header = (Header)headers.nextElement();
328 pstmtAddArticle2.setInt(1, newArticleID);
329 pstmtAddArticle2.setString(2, header.getName().toLowerCase());
330 pstmtAddArticle2.setString(3,
331 header.getValue().replaceAll("[\r\n]", ""));
332 pstmtAddArticle2.setInt(4, n);
333 pstmtAddArticle2.execute();
336 // For each newsgroup add a reference
337 List<Group> groups = article.getGroups();
338 for(Group group : groups)
340 pstmtAddArticle3.setLong(1, group.getInternalID());
341 pstmtAddArticle3.setInt(2, newArticleID);
342 pstmtAddArticle3.setLong(3, getMaxArticleIndex(group.getInternalID()) + 1);
343 pstmtAddArticle3.execute();
346 // Write message-id to article_ids table
347 this.pstmtAddArticle4.setInt(1, newArticleID);
348 this.pstmtAddArticle4.setString(2, article.getMessageID());
349 this.pstmtAddArticle4.execute();
352 this.conn.setAutoCommit(true);
354 this.restarts = 0; // Reset error count
356 catch(SQLException ex)
360 this.conn.rollback(); // Rollback changes
362 catch(SQLException ex2)
364 Log.msg("Rollback of addArticle() failed: " + ex2, false);
369 this.conn.setAutoCommit(true); // and release locks
371 catch(SQLException ex2)
373 Log.msg("setAutoCommit(true) of addArticle() failed: " + ex2, false);
376 restartConnection(ex);
382 * Adds a group to the JDBCDatabase. This method is not accessible via NNTP.
384 * @throws java.sql.SQLException
387 public void addGroup(String name, int flags)
388 throws StorageBackendException
392 this.conn.setAutoCommit(false);
393 pstmtAddGroup0.setString(1, name);
394 pstmtAddGroup0.setInt(2, flags);
396 pstmtAddGroup0.executeUpdate();
398 this.conn.setAutoCommit(true);
399 this.restarts = 0; // Reset error count
401 catch(SQLException ex)
405 this.conn.rollback();
406 this.conn.setAutoCommit(true);
408 catch(SQLException ex2)
410 ex2.printStackTrace();
413 restartConnection(ex);
414 addGroup(name, flags);
419 public void addEvent(long time, int type, long gid)
420 throws StorageBackendException
424 this.conn.setAutoCommit(false);
425 this.pstmtAddEvent.setLong(1, time);
426 this.pstmtAddEvent.setInt(2, type);
427 this.pstmtAddEvent.setLong(3, gid);
428 this.pstmtAddEvent.executeUpdate();
430 this.conn.setAutoCommit(true);
433 catch(SQLException ex)
437 this.conn.rollback();
438 this.conn.setAutoCommit(true);
440 catch(SQLException ex2)
442 ex2.printStackTrace();
445 restartConnection(ex);
446 addEvent(time, type, gid);
451 public int countArticles()
452 throws StorageBackendException
458 rs = this.pstmtCountArticles.executeQuery();
468 catch(SQLException ex)
470 restartConnection(ex);
471 return countArticles();
481 catch(SQLException ex)
483 ex.printStackTrace();
491 public int countGroups()
492 throws StorageBackendException
498 rs = this.pstmtCountGroups.executeQuery();
508 catch(SQLException ex)
510 restartConnection(ex);
511 return countGroups();
521 catch(SQLException ex)
523 ex.printStackTrace();
531 public void delete(final String messageID)
532 throws StorageBackendException
536 this.conn.setAutoCommit(false);
538 this.pstmtDeleteArticle0.setString(1, messageID);
539 int rs = this.pstmtDeleteArticle0.executeUpdate();
541 // We do not trust the ON DELETE CASCADE functionality to delete
542 // orphaned references...
543 this.pstmtDeleteArticle1.setString(1, messageID);
544 rs = this.pstmtDeleteArticle1.executeUpdate();
546 this.pstmtDeleteArticle2.setString(1, messageID);
547 rs = this.pstmtDeleteArticle2.executeUpdate();
549 this.pstmtDeleteArticle3.setString(1, messageID);
550 rs = this.pstmtDeleteArticle3.executeUpdate();
553 this.conn.setAutoCommit(true);
555 catch(SQLException ex)
557 throw new StorageBackendException(ex);
562 public Article getArticle(String messageID)
563 throws StorageBackendException
568 pstmtGetArticle0.setString(1, messageID);
569 rs = pstmtGetArticle0.executeQuery();
577 byte[] body = rs.getBytes("body");
578 String headers = getArticleHeaders(rs.getInt("article_id"));
579 return new Article(headers, body);
582 catch(SQLException ex)
584 restartConnection(ex);
585 return getArticle(messageID);
595 catch(SQLException ex)
597 ex.printStackTrace();
599 restarts = 0; // Reset error count
605 * Retrieves an article by its ID.
608 * @throws StorageBackendException
611 public Article getArticle(long articleIndex, long gid)
612 throws StorageBackendException
618 this.pstmtGetArticle1.setLong(1, articleIndex);
619 this.pstmtGetArticle1.setLong(2, gid);
621 rs = this.pstmtGetArticle1.executeQuery();
625 byte[] body = rs.getBytes("body");
626 String headers = getArticleHeaders(rs.getInt("article_id"));
627 return new Article(headers, body);
634 catch(SQLException ex)
636 restartConnection(ex);
637 return getArticle(articleIndex, gid);
647 catch(SQLException ex)
649 ex.printStackTrace();
657 * Searches for fitting header values using the given regular expression.
664 * @throws StorageBackendException
667 public List<Pair<Long, String>> getArticleHeaders(Channel group, long start,
668 long end, String headerKey, String patStr)
669 throws StorageBackendException, PatternSyntaxException
672 List<Pair<Long, String>> heads = new ArrayList<Pair<Long, String>>();
676 this.pstmtGetArticleHeaders1.setString(1, group.getName());
677 this.pstmtGetArticleHeaders1.setString(2, headerKey);
678 this.pstmtGetArticleHeaders1.setLong(3, start);
680 rs = this.pstmtGetArticleHeaders1.executeQuery();
682 // Convert the "NNTP" regex to Java regex
683 patStr = patStr.replace("*", ".*");
684 Pattern pattern = Pattern.compile(patStr);
688 Long articleIndex = rs.getLong(1);
689 if(end < 0 || articleIndex <= end) // Match start is done via SQL
691 String headerValue = rs.getString(2);
692 Matcher matcher = pattern.matcher(headerValue);
693 if(matcher.matches())
695 heads.add(new Pair<Long, String>(articleIndex, headerValue));
700 catch(SQLException ex)
702 restartConnection(ex);
703 return getArticleHeaders(group, start, end, headerKey, patStr);
713 catch(SQLException ex)
715 ex.printStackTrace();
723 private String getArticleHeaders(long articleID)
724 throws StorageBackendException
730 this.pstmtGetArticleHeaders0.setLong(1, articleID);
731 rs = this.pstmtGetArticleHeaders0.executeQuery();
733 StringBuilder buf = new StringBuilder();
738 buf.append(rs.getString(1)); // key
740 String foldedValue = MimeUtility.fold(0, rs.getString(2));
741 buf.append(foldedValue); // value
753 return buf.toString();
755 catch(SQLException ex)
757 restartConnection(ex);
758 return getArticleHeaders(articleID);
768 catch(SQLException ex)
770 ex.printStackTrace();
777 public long getArticleIndex(Article article, Group group)
778 throws StorageBackendException
784 this.pstmtGetArticleIndex.setString(1, article.getMessageID());
785 this.pstmtGetArticleIndex.setLong(2, group.getInternalID());
787 rs = this.pstmtGetArticleIndex.executeQuery();
790 return rs.getLong(1);
797 catch(SQLException ex)
799 restartConnection(ex);
800 return getArticleIndex(article, group);
810 catch(SQLException ex)
812 ex.printStackTrace();
819 * Returns a list of Long/Article Pairs.
820 * @throws java.sql.SQLException
823 public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, long first,
825 throws StorageBackendException
831 this.pstmtGetArticleHeads.setLong(1, group.getInternalID());
832 this.pstmtGetArticleHeads.setLong(2, first);
833 this.pstmtGetArticleHeads.setLong(3, last);
834 rs = pstmtGetArticleHeads.executeQuery();
836 List<Pair<Long, ArticleHead>> articles
837 = new ArrayList<Pair<Long, ArticleHead>>();
841 long aid = rs.getLong("article_id");
842 long aidx = rs.getLong("article_index");
843 String headers = getArticleHeaders(aid);
844 articles.add(new Pair<Long, ArticleHead>(aidx,
845 new ArticleHead(headers)));
850 catch(SQLException ex)
852 restartConnection(ex);
853 return getArticleHeads(group, first, last);
863 catch(SQLException ex)
865 ex.printStackTrace();
872 public List<Long> getArticleNumbers(long gid)
873 throws StorageBackendException
878 List<Long> ids = new ArrayList<Long>();
879 this.pstmtGetArticleIDs.setLong(1, gid);
880 rs = this.pstmtGetArticleIDs.executeQuery();
883 ids.add(rs.getLong(1));
887 catch(SQLException ex)
889 restartConnection(ex);
890 return getArticleNumbers(gid);
899 restarts = 0; // Clear the restart count after successful request
901 catch(SQLException ex)
903 ex.printStackTrace();
910 public String getConfigValue(String key)
911 throws StorageBackendException
916 this.pstmtGetConfigValue.setString(1, key);
918 rs = this.pstmtGetConfigValue.executeQuery();
921 return rs.getString(1); // First data on index 1 not 0
928 catch(SQLException ex)
930 restartConnection(ex);
931 return getConfigValue(key);
941 catch(SQLException ex)
943 ex.printStackTrace();
945 restarts = 0; // Clear the restart count after successful request
951 public int getEventsCount(int type, long start, long end, Channel channel)
952 throws StorageBackendException
960 this.pstmtGetEventsCount0.setInt(1, type);
961 this.pstmtGetEventsCount0.setLong(2, start);
962 this.pstmtGetEventsCount0.setLong(3, end);
963 rs = this.pstmtGetEventsCount0.executeQuery();
967 this.pstmtGetEventsCount1.setInt(1, type);
968 this.pstmtGetEventsCount1.setLong(2, start);
969 this.pstmtGetEventsCount1.setLong(3, end);
970 this.pstmtGetEventsCount1.setLong(4, channel.getInternalID());
971 rs = this.pstmtGetEventsCount1.executeQuery();
983 catch(SQLException ex)
985 restartConnection(ex);
986 return getEventsCount(type, start, end, channel);
996 catch(SQLException ex)
998 ex.printStackTrace();
1005 * Reads all Groups from the JDBCDatabase.
1007 * @throws StorageBackendException
1010 public List<Channel> getGroups()
1011 throws StorageBackendException
1014 List<Channel> buffer = new ArrayList<Channel>();
1015 Statement stmt = null;
1019 stmt = conn.createStatement();
1020 rs = stmt.executeQuery("SELECT * FROM groups ORDER BY name");
1024 String name = rs.getString("name");
1025 long id = rs.getLong("group_id");
1026 int flags = rs.getInt("flags");
1028 Group group = new Group(name, id, flags);
1034 catch(SQLException ex)
1036 restartConnection(ex);
1045 stmt.close(); // Implicitely closes ResultSets
1047 catch(SQLException ex)
1049 ex.printStackTrace();
1056 public List<String> getGroupsForList(InternetAddress listAddress)
1057 throws StorageBackendException
1059 ResultSet rs = null;
1063 this.pstmtGetGroupForList.setString(1, listAddress.getAddress());
1065 rs = this.pstmtGetGroupForList.executeQuery();
1066 List<String> groups = new ArrayList<String>();
1069 String group = rs.getString(1);
1074 catch(SQLException ex)
1076 restartConnection(ex);
1077 return getGroupsForList(listAddress);
1087 catch(SQLException ex)
1089 ex.printStackTrace();
1096 * Returns the Group that is identified by the name.
1099 * @throws StorageBackendException
1102 public Group getGroup(String name)
1103 throws StorageBackendException
1105 ResultSet rs = null;
1109 this.pstmtGetGroup0.setString(1, name);
1110 rs = this.pstmtGetGroup0.executeQuery();
1118 long id = rs.getLong("group_id");
1119 int flags = rs.getInt("flags");
1120 return new Group(name, id, flags);
1123 catch(SQLException ex)
1125 restartConnection(ex);
1126 return getGroup(name);
1136 catch(SQLException ex)
1138 ex.printStackTrace();
1145 public String getListForGroup(String group)
1146 throws StorageBackendException
1148 ResultSet rs = null;
1152 this.pstmtGetListForGroup.setString(1, group);
1153 rs = this.pstmtGetListForGroup.executeQuery();
1156 return rs.getString(1);
1163 catch(SQLException ex)
1165 restartConnection(ex);
1166 return getListForGroup(group);
1176 catch(SQLException ex)
1178 ex.printStackTrace();
1184 private int getMaxArticleIndex(long groupID)
1185 throws StorageBackendException
1187 ResultSet rs = null;
1191 this.pstmtGetMaxArticleIndex.setLong(1, groupID);
1192 rs = this.pstmtGetMaxArticleIndex.executeQuery();
1197 maxIndex = rs.getInt(1);
1202 catch(SQLException ex)
1204 restartConnection(ex);
1205 return getMaxArticleIndex(groupID);
1215 catch(SQLException ex)
1217 ex.printStackTrace();
1223 private int getMaxArticleID()
1224 throws StorageBackendException
1226 ResultSet rs = null;
1230 rs = this.pstmtGetMaxArticleID.executeQuery();
1235 maxIndex = rs.getInt(1);
1240 catch(SQLException ex)
1242 restartConnection(ex);
1243 return getMaxArticleID();
1253 catch(SQLException ex)
1255 ex.printStackTrace();
1262 public int getLastArticleNumber(Group group)
1263 throws StorageBackendException
1265 ResultSet rs = null;
1269 this.pstmtGetLastArticleNumber.setLong(1, group.getInternalID());
1270 rs = this.pstmtGetLastArticleNumber.executeQuery();
1273 return rs.getInt(1);
1280 catch(SQLException ex)
1282 restartConnection(ex);
1283 return getLastArticleNumber(group);
1293 catch(SQLException ex)
1295 ex.printStackTrace();
1302 public int getFirstArticleNumber(Group group)
1303 throws StorageBackendException
1305 ResultSet rs = null;
1308 this.pstmtGetFirstArticleNumber.setLong(1, group.getInternalID());
1309 rs = this.pstmtGetFirstArticleNumber.executeQuery();
1312 return rs.getInt(1);
1319 catch(SQLException ex)
1321 restartConnection(ex);
1322 return getFirstArticleNumber(group);
1332 catch(SQLException ex)
1334 ex.printStackTrace();
1341 * Returns a group name identified by the given id.
1344 * @throws StorageBackendException
1346 public String getGroup(int id)
1347 throws StorageBackendException
1349 ResultSet rs = null;
1353 this.pstmtGetGroup1.setInt(1, id);
1354 rs = this.pstmtGetGroup1.executeQuery();
1358 return rs.getString(1);
1365 catch(SQLException ex)
1367 restartConnection(ex);
1368 return getGroup(id);
1378 catch(SQLException ex)
1380 ex.printStackTrace();
1387 public double getEventsPerHour(int key, long gid)
1388 throws StorageBackendException
1390 String gidquery = "";
1393 gidquery = " AND group_id = " + gid;
1396 Statement stmt = null;
1397 ResultSet rs = null;
1401 stmt = this.conn.createStatement();
1402 rs = stmt.executeQuery("SELECT Count(*) / (Max(event_time) - Min(event_time))" +
1403 " * 1000 * 60 * 60 FROM events WHERE event_key = " + key + gidquery);
1407 restarts = 0; // reset error count
1408 return rs.getDouble(1);
1415 catch(SQLException ex)
1417 restartConnection(ex);
1418 return getEventsPerHour(key, gid);
1426 stmt.close(); // Implicitely closes the result sets
1429 catch(SQLException ex)
1431 ex.printStackTrace();
1437 public String getOldestArticle()
1438 throws StorageBackendException
1440 ResultSet rs = null;
1444 rs = this.pstmtGetOldestArticle.executeQuery();
1447 return rs.getString(1);
1454 catch(SQLException ex)
1456 restartConnection(ex);
1457 return getOldestArticle();
1467 catch(SQLException ex)
1469 ex.printStackTrace();
1476 public int getPostingsCount(String groupname)
1477 throws StorageBackendException
1479 ResultSet rs = null;
1483 this.pstmtGetPostingsCount.setString(1, groupname);
1484 rs = this.pstmtGetPostingsCount.executeQuery();
1487 return rs.getInt(1);
1491 Log.msg("Warning: Count on postings return nothing!", true);
1495 catch(SQLException ex)
1497 restartConnection(ex);
1498 return getPostingsCount(groupname);
1508 catch(SQLException ex)
1510 ex.printStackTrace();
1517 public List<Subscription> getSubscriptions(int feedtype)
1518 throws StorageBackendException
1520 ResultSet rs = null;
1524 List<Subscription> subs = new ArrayList<Subscription>();
1525 this.pstmtGetSubscriptions.setInt(1, feedtype);
1526 rs = this.pstmtGetSubscriptions.executeQuery();
1530 String host = rs.getString("host");
1531 String group = rs.getString("name");
1532 int port = rs.getInt("port");
1533 subs.add(new Subscription(host, port, feedtype, group));
1538 catch(SQLException ex)
1540 restartConnection(ex);
1541 return getSubscriptions(feedtype);
1551 catch(SQLException ex)
1553 ex.printStackTrace();
1560 * Checks if there is an article with the given messageid in the JDBCDatabase.
1563 * @throws StorageBackendException
1566 public boolean isArticleExisting(String messageID)
1567 throws StorageBackendException
1569 ResultSet rs = null;
1573 this.pstmtIsArticleExisting.setString(1, messageID);
1574 rs = this.pstmtIsArticleExisting.executeQuery();
1575 return rs.next() && rs.getInt(1) == 1;
1577 catch(SQLException ex)
1579 restartConnection(ex);
1580 return isArticleExisting(messageID);
1590 catch(SQLException ex)
1592 ex.printStackTrace();
1599 * Checks if there is a group with the given name in the JDBCDatabase.
1602 * @throws StorageBackendException
1605 public boolean isGroupExisting(String name)
1606 throws StorageBackendException
1608 ResultSet rs = null;
1612 this.pstmtIsGroupExisting.setString(1, name);
1613 rs = this.pstmtIsGroupExisting.executeQuery();
1616 catch(SQLException ex)
1618 restartConnection(ex);
1619 return isGroupExisting(name);
1629 catch(SQLException ex)
1631 ex.printStackTrace();
1638 public void setConfigValue(String key, String value)
1639 throws StorageBackendException
1643 conn.setAutoCommit(false);
1644 this.pstmtSetConfigValue0.setString(1, key);
1645 this.pstmtSetConfigValue0.execute();
1646 this.pstmtSetConfigValue1.setString(1, key);
1647 this.pstmtSetConfigValue1.setString(2, value);
1648 this.pstmtSetConfigValue1.execute();
1650 conn.setAutoCommit(true);
1652 catch(SQLException ex)
1654 restartConnection(ex);
1655 setConfigValue(key, value);
1660 * Closes the JDBCDatabase connection.
1662 public void shutdown()
1663 throws StorageBackendException
1667 if(this.conn != null)
1672 catch(SQLException ex)
1674 throw new StorageBackendException(ex);
1679 public void purgeGroup(Group group)
1680 throws StorageBackendException
1684 this.pstmtPurgeGroup0.setLong(1, group.getInternalID());
1685 this.pstmtPurgeGroup0.executeUpdate();
1687 this.pstmtPurgeGroup1.setLong(1, group.getInternalID());
1688 this.pstmtPurgeGroup1.executeUpdate();
1690 catch(SQLException ex)
1692 restartConnection(ex);
1697 private void restartConnection(SQLException cause)
1698 throws StorageBackendException
1701 Log.msg(Thread.currentThread()
1702 + ": Database connection was closed (restart " + restarts + ").", false);
1704 if(restarts >= MAX_RESTARTS)
1706 // Delete the current, probably broken JDBCDatabase instance.
1707 // So no one can use the instance any more.
1708 JDBCDatabaseProvider.instances.remove(Thread.currentThread());
1710 // Throw the exception upwards
1711 throw new StorageBackendException(cause);
1716 Thread.sleep(1500L * restarts);
1718 catch(InterruptedException ex)
1720 Log.msg("Interrupted: " + ex.getMessage(), false);
1723 // Try to properly close the old database connection
1726 if(this.conn != null)
1731 catch(SQLException ex)
1733 Log.msg(ex.getMessage(), true);
1738 // Try to reinitialize database connection
1741 catch(SQLException ex)
1743 Log.msg(ex.getMessage(), true);
1744 restartConnection(ex);
1749 * Writes the flags and the name of the given group to the database.
1751 * @throws StorageBackendException
1754 public boolean update(Group group)
1755 throws StorageBackendException
1759 this.pstmtUpdateGroup.setInt(1, group.getFlags());
1760 this.pstmtUpdateGroup.setString(2, group.getName());
1761 this.pstmtUpdateGroup.setLong(3, group.getInternalID());
1762 int rs = this.pstmtUpdateGroup.executeUpdate();
1765 catch(SQLException ex)
1767 restartConnection(ex);
1768 return update(group);