Merge heads.
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.MimeUtility;
35 import org.sonews.config.Config;
36 import org.sonews.util.Log;
37 import org.sonews.feed.Subscription;
38 import org.sonews.storage.Article;
39 import org.sonews.storage.ArticleHead;
40 import org.sonews.storage.Channel;
41 import org.sonews.storage.Group;
42 import org.sonews.storage.Storage;
43 import org.sonews.storage.StorageBackendException;
44 import org.sonews.util.Pair;
47 * JDBCDatabase facade class.
48 * @author Christian Lins
51 // TODO: Refactor this class to reduce size (e.g. ArticleDatabase GroupDatabase)
52 public class JDBCDatabase implements Storage
55 public static final int MAX_RESTARTS = 2;
57 private Connection conn = null;
58 private PreparedStatement pstmtAddArticle1 = null;
59 private PreparedStatement pstmtAddArticle2 = null;
60 private PreparedStatement pstmtAddArticle3 = null;
61 private PreparedStatement pstmtAddArticle4 = null;
62 private PreparedStatement pstmtAddGroup0 = null;
63 private PreparedStatement pstmtAddEvent = null;
64 private PreparedStatement pstmtCountArticles = null;
65 private PreparedStatement pstmtCountGroups = null;
66 private PreparedStatement pstmtDeleteArticle0 = null;
67 private PreparedStatement pstmtDeleteArticle1 = null;
68 private PreparedStatement pstmtDeleteArticle2 = null;
69 private PreparedStatement pstmtDeleteArticle3 = null;
70 private PreparedStatement pstmtGetArticle0 = null;
71 private PreparedStatement pstmtGetArticle1 = null;
72 private PreparedStatement pstmtGetArticleHeaders0 = null;
73 private PreparedStatement pstmtGetArticleHeaders1 = null;
74 private PreparedStatement pstmtGetArticleHeads = null;
75 private PreparedStatement pstmtGetArticleIDs = null;
76 private PreparedStatement pstmtGetArticleIndex = null;
77 private PreparedStatement pstmtGetConfigValue = null;
78 private PreparedStatement pstmtGetEventsCount0 = null;
79 private PreparedStatement pstmtGetEventsCount1 = null;
80 private PreparedStatement pstmtGetGroupForList = null;
81 private PreparedStatement pstmtGetGroup0 = null;
82 private PreparedStatement pstmtGetGroup1 = null;
83 private PreparedStatement pstmtGetFirstArticleNumber = null;
84 private PreparedStatement pstmtGetListForGroup = null;
85 private PreparedStatement pstmtGetLastArticleNumber = null;
86 private PreparedStatement pstmtGetMaxArticleID = null;
87 private PreparedStatement pstmtGetMaxArticleIndex = null;
88 private PreparedStatement pstmtGetOldestArticle = null;
89 private PreparedStatement pstmtGetPostingsCount = null;
90 private PreparedStatement pstmtGetSubscriptions = null;
91 private PreparedStatement pstmtIsArticleExisting = null;
92 private PreparedStatement pstmtIsGroupExisting = null;
93 private PreparedStatement pstmtPurgeGroup0 = null;
94 private PreparedStatement pstmtPurgeGroup1 = null;
95 private PreparedStatement pstmtSetConfigValue0 = null;
96 private PreparedStatement pstmtSetConfigValue1 = null;
97 private PreparedStatement pstmtUpdateGroup = null;
99 /** How many times the database connection was reinitialized */
100 private int restarts = 0;
103 * Rises the database: reconnect and recreate all prepared statements.
104 * @throws java.lang.SQLException
106 protected void arise()
111 // Load database driver
113 Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DBMSDRIVER, "java.lang.Object"));
115 // Establish database connection
116 this.conn = DriverManager.getConnection(
117 Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DATABASE, "<not specified>"),
118 Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_USER, "root"),
119 Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_PASSWORD, ""));
121 this.conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
122 if(this.conn.getTransactionIsolation() != Connection.TRANSACTION_SERIALIZABLE)
124 Log.get().warning("Database is NOT fully serializable!");
127 // Prepare statements for method addArticle()
128 this.pstmtAddArticle1 = conn.prepareStatement(
129 "INSERT INTO articles (article_id, body) VALUES(?, ?)");
130 this.pstmtAddArticle2 = conn.prepareStatement(
131 "INSERT INTO headers (article_id, header_key, header_value, header_index) " +
132 "VALUES (?, ?, ?, ?)");
133 this.pstmtAddArticle3 = conn.prepareStatement(
134 "INSERT INTO postings (group_id, article_id, article_index)" +
136 this.pstmtAddArticle4 = conn.prepareStatement(
137 "INSERT INTO article_ids (article_id, message_id) VALUES (?, ?)");
139 // Prepare statement for method addStatValue()
140 this.pstmtAddEvent = conn.prepareStatement(
141 "INSERT INTO events VALUES (?, ?, ?)");
143 // Prepare statement for method addGroup()
144 this.pstmtAddGroup0 = conn.prepareStatement(
145 "INSERT INTO groups (name, flags) VALUES (?, ?)");
147 // Prepare statement for method countArticles()
148 this.pstmtCountArticles = conn.prepareStatement(
149 "SELECT Count(article_id) FROM article_ids");
151 // Prepare statement for method countGroups()
152 this.pstmtCountGroups = conn.prepareStatement(
153 "SELECT Count(group_id) FROM groups WHERE " +
154 "flags & " + Channel.DELETED + " = 0");
156 // Prepare statements for method delete(article)
157 this.pstmtDeleteArticle0 = conn.prepareStatement(
158 "DELETE FROM articles WHERE article_id = " +
159 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
160 this.pstmtDeleteArticle1 = conn.prepareStatement(
161 "DELETE FROM headers WHERE article_id = " +
162 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
163 this.pstmtDeleteArticle2 = conn.prepareStatement(
164 "DELETE FROM postings WHERE article_id = " +
165 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
166 this.pstmtDeleteArticle3 = conn.prepareStatement(
167 "DELETE FROM article_ids WHERE message_id = ?");
169 // Prepare statements for methods getArticle()
170 this.pstmtGetArticle0 = conn.prepareStatement(
171 "SELECT * FROM articles WHERE article_id = " +
172 "(SELECT article_id FROM article_ids WHERE message_id = ?)");
173 this.pstmtGetArticle1 = conn.prepareStatement(
174 "SELECT * FROM articles WHERE article_id = " +
175 "(SELECT article_id FROM postings WHERE " +
176 "article_index = ? AND group_id = ?)");
178 // Prepare statement for method getArticleHeaders()
179 this.pstmtGetArticleHeaders0 = conn.prepareStatement(
180 "SELECT header_key, header_value FROM headers WHERE article_id = ? " +
181 "ORDER BY header_index ASC");
183 // Prepare statement for method getArticleHeaders(regular expr pattern)
184 this.pstmtGetArticleHeaders1 = conn.prepareStatement(
185 "SELECT p.article_index, h.header_value FROM headers h " +
186 "INNER JOIN postings p ON h.article_id = p.article_id " +
187 "INNER JOIN groups g ON p.group_id = g.group_id " +
188 "WHERE g.name = ? AND " +
189 "h.header_key = ? AND " +
190 "p.article_index >= ? " +
191 "ORDER BY p.article_index ASC");
193 this.pstmtGetArticleIDs = conn.prepareStatement(
194 "SELECT article_index FROM postings WHERE group_id = ?");
196 // Prepare statement for method getArticleIndex
197 this.pstmtGetArticleIndex = conn.prepareStatement(
198 "SELECT article_index FROM postings WHERE " +
199 "article_id = (SELECT article_id FROM article_ids " +
200 "WHERE message_id = ?) " +
201 " AND group_id = ?");
203 // Prepare statements for method getArticleHeads()
204 this.pstmtGetArticleHeads = conn.prepareStatement(
205 "SELECT article_id, article_index FROM postings WHERE " +
206 "postings.group_id = ? AND article_index >= ? AND " +
207 "article_index <= ?");
209 // Prepare statements for method getConfigValue()
210 this.pstmtGetConfigValue = conn.prepareStatement(
211 "SELECT config_value FROM config WHERE config_key = ?");
213 // Prepare statements for method getEventsCount()
214 this.pstmtGetEventsCount0 = conn.prepareStatement(
215 "SELECT Count(*) FROM events WHERE event_key = ? AND " +
216 "event_time >= ? AND event_time < ?");
218 this.pstmtGetEventsCount1 = conn.prepareStatement(
219 "SELECT Count(*) FROM events WHERE event_key = ? AND " +
220 "event_time >= ? AND event_time < ? AND group_id = ?");
222 // Prepare statement for method getGroupForList()
223 this.pstmtGetGroupForList = conn.prepareStatement(
224 "SELECT name FROM groups INNER JOIN groups2list " +
225 "ON groups.group_id = groups2list.group_id " +
226 "WHERE groups2list.listaddress = ?");
228 // Prepare statement for method getGroup()
229 this.pstmtGetGroup0 = conn.prepareStatement(
230 "SELECT group_id, flags FROM groups WHERE Name = ?");
231 this.pstmtGetGroup1 = conn.prepareStatement(
232 "SELECT name FROM groups WHERE group_id = ?");
234 // Prepare statement for method getLastArticleNumber()
235 this.pstmtGetLastArticleNumber = conn.prepareStatement(
236 "SELECT Max(article_index) FROM postings WHERE group_id = ?");
238 // Prepare statement for method getListForGroup()
239 this.pstmtGetListForGroup = conn.prepareStatement(
240 "SELECT listaddress FROM groups2list INNER JOIN groups " +
241 "ON groups.group_id = groups2list.group_id WHERE name = ?");
243 // Prepare statement for method getMaxArticleID()
244 this.pstmtGetMaxArticleID = conn.prepareStatement(
245 "SELECT Max(article_id) FROM articles");
247 // Prepare statement for method getMaxArticleIndex()
248 this.pstmtGetMaxArticleIndex = conn.prepareStatement(
249 "SELECT Max(article_index) FROM postings WHERE group_id = ?");
251 // Prepare statement for method getOldestArticle()
252 this.pstmtGetOldestArticle = conn.prepareStatement(
253 "SELECT message_id FROM article_ids WHERE article_id = " +
254 "(SELECT Min(article_id) FROM article_ids)");
256 // Prepare statement for method getFirstArticleNumber()
257 this.pstmtGetFirstArticleNumber = conn.prepareStatement(
258 "SELECT Min(article_index) FROM postings WHERE group_id = ?");
260 // Prepare statement for method getPostingsCount()
261 this.pstmtGetPostingsCount = conn.prepareStatement(
262 "SELECT Count(*) FROM postings NATURAL JOIN groups " +
263 "WHERE groups.name = ?");
265 // Prepare statement for method getSubscriptions()
266 this.pstmtGetSubscriptions = conn.prepareStatement(
267 "SELECT host, port, name FROM peers NATURAL JOIN " +
268 "peer_subscriptions NATURAL JOIN groups WHERE feedtype = ?");
270 // Prepare statement for method isArticleExisting()
271 this.pstmtIsArticleExisting = conn.prepareStatement(
272 "SELECT Count(article_id) FROM article_ids WHERE message_id = ?");
274 // Prepare statement for method isGroupExisting()
275 this.pstmtIsGroupExisting = conn.prepareStatement(
276 "SELECT * FROM groups WHERE name = ?");
278 // Prepare statement for method setConfigValue()
279 this.pstmtSetConfigValue0 = conn.prepareStatement(
280 "DELETE FROM config WHERE config_key = ?");
281 this.pstmtSetConfigValue1 = conn.prepareStatement(
282 "INSERT INTO config VALUES(?, ?)");
284 // Prepare statements for method purgeGroup()
285 this.pstmtPurgeGroup0 = conn.prepareStatement(
286 "DELETE FROM peer_subscriptions WHERE group_id = ?");
287 this.pstmtPurgeGroup1 = conn.prepareStatement(
288 "DELETE FROM groups WHERE group_id = ?");
290 // Prepare statement for method update(Group)
291 this.pstmtUpdateGroup = conn.prepareStatement(
292 "UPDATE groups SET flags = ?, name = ? WHERE group_id = ?");
294 catch(ClassNotFoundException ex)
296 throw new Error("JDBC Driver not found!", ex);
301 * Adds an article to the database.
304 * @throws java.sql.SQLException
307 public void addArticle(final Article article)
308 throws StorageBackendException
312 this.conn.setAutoCommit(false);
314 int newArticleID = getMaxArticleID() + 1;
316 // Fill prepared statement with values;
317 // writes body to article table
318 pstmtAddArticle1.setInt(1, newArticleID);
319 pstmtAddArticle1.setBytes(2, article.getBody());
320 pstmtAddArticle1.execute();
323 Enumeration headers = article.getAllHeaders();
324 for(int n = 0; headers.hasMoreElements(); n++)
326 Header header = (Header)headers.nextElement();
327 pstmtAddArticle2.setInt(1, newArticleID);
328 pstmtAddArticle2.setString(2, header.getName().toLowerCase());
329 pstmtAddArticle2.setString(3,
330 header.getValue().replaceAll("[\r\n]", ""));
331 pstmtAddArticle2.setInt(4, n);
332 pstmtAddArticle2.execute();
335 // For each newsgroup add a reference
336 List<Group> groups = article.getGroups();
337 for(Group group : groups)
339 pstmtAddArticle3.setLong(1, group.getInternalID());
340 pstmtAddArticle3.setInt(2, newArticleID);
341 pstmtAddArticle3.setLong(3, getMaxArticleIndex(group.getInternalID()) + 1);
342 pstmtAddArticle3.execute();
345 // Write message-id to article_ids table
346 this.pstmtAddArticle4.setInt(1, newArticleID);
347 this.pstmtAddArticle4.setString(2, article.getMessageID());
348 this.pstmtAddArticle4.execute();
351 this.conn.setAutoCommit(true);
353 this.restarts = 0; // Reset error count
355 catch(SQLException ex)
359 this.conn.rollback(); // Rollback changes
361 catch(SQLException ex2)
363 Log.get().severe("Rollback of addArticle() failed: " + ex2);
368 this.conn.setAutoCommit(true); // and release locks
370 catch(SQLException ex2)
372 Log.get().severe("setAutoCommit(true) of addArticle() failed: " + ex2);
375 restartConnection(ex);
381 * Adds a group to the JDBCDatabase. This method is not accessible via NNTP.
383 * @throws java.sql.SQLException
386 public void addGroup(String name, int flags)
387 throws StorageBackendException
391 this.conn.setAutoCommit(false);
392 pstmtAddGroup0.setString(1, name);
393 pstmtAddGroup0.setInt(2, flags);
395 pstmtAddGroup0.executeUpdate();
397 this.conn.setAutoCommit(true);
398 this.restarts = 0; // Reset error count
400 catch(SQLException ex)
404 this.conn.rollback();
405 this.conn.setAutoCommit(true);
407 catch(SQLException ex2)
409 ex2.printStackTrace();
412 restartConnection(ex);
413 addGroup(name, flags);
418 public void addEvent(long time, int type, long gid)
419 throws StorageBackendException
423 this.conn.setAutoCommit(false);
424 this.pstmtAddEvent.setLong(1, time);
425 this.pstmtAddEvent.setInt(2, type);
426 this.pstmtAddEvent.setLong(3, gid);
427 this.pstmtAddEvent.executeUpdate();
429 this.conn.setAutoCommit(true);
432 catch(SQLException ex)
436 this.conn.rollback();
437 this.conn.setAutoCommit(true);
439 catch(SQLException ex2)
441 ex2.printStackTrace();
444 restartConnection(ex);
445 addEvent(time, type, gid);
450 public int countArticles()
451 throws StorageBackendException
457 rs = this.pstmtCountArticles.executeQuery();
467 catch(SQLException ex)
469 restartConnection(ex);
470 return countArticles();
480 catch(SQLException ex)
482 ex.printStackTrace();
490 public int countGroups()
491 throws StorageBackendException
497 rs = this.pstmtCountGroups.executeQuery();
507 catch(SQLException ex)
509 restartConnection(ex);
510 return countGroups();
520 catch(SQLException ex)
522 ex.printStackTrace();
530 public void delete(final String messageID)
531 throws StorageBackendException
535 this.conn.setAutoCommit(false);
537 this.pstmtDeleteArticle0.setString(1, messageID);
538 int rs = this.pstmtDeleteArticle0.executeUpdate();
540 // We do not trust the ON DELETE CASCADE functionality to delete
541 // orphaned references...
542 this.pstmtDeleteArticle1.setString(1, messageID);
543 rs = this.pstmtDeleteArticle1.executeUpdate();
545 this.pstmtDeleteArticle2.setString(1, messageID);
546 rs = this.pstmtDeleteArticle2.executeUpdate();
548 this.pstmtDeleteArticle3.setString(1, messageID);
549 rs = this.pstmtDeleteArticle3.executeUpdate();
552 this.conn.setAutoCommit(true);
554 catch(SQLException ex)
556 throw new StorageBackendException(ex);
561 public Article getArticle(String messageID)
562 throws StorageBackendException
567 pstmtGetArticle0.setString(1, messageID);
568 rs = pstmtGetArticle0.executeQuery();
576 byte[] body = rs.getBytes("body");
577 String headers = getArticleHeaders(rs.getInt("article_id"));
578 return new Article(headers, body);
581 catch(SQLException ex)
583 restartConnection(ex);
584 return getArticle(messageID);
594 catch(SQLException ex)
596 ex.printStackTrace();
598 restarts = 0; // Reset error count
604 * Retrieves an article by its ID.
607 * @throws StorageBackendException
610 public Article getArticle(long articleIndex, long gid)
611 throws StorageBackendException
617 this.pstmtGetArticle1.setLong(1, articleIndex);
618 this.pstmtGetArticle1.setLong(2, gid);
620 rs = this.pstmtGetArticle1.executeQuery();
624 byte[] body = rs.getBytes("body");
625 String headers = getArticleHeaders(rs.getInt("article_id"));
626 return new Article(headers, body);
633 catch(SQLException ex)
635 restartConnection(ex);
636 return getArticle(articleIndex, gid);
646 catch(SQLException ex)
648 ex.printStackTrace();
656 * Searches for fitting header values using the given regular expression.
663 * @throws StorageBackendException
666 public List<Pair<Long, String>> getArticleHeaders(Channel group, long start,
667 long end, String headerKey, String patStr)
668 throws StorageBackendException, PatternSyntaxException
671 List<Pair<Long, String>> heads = new ArrayList<Pair<Long, String>>();
675 this.pstmtGetArticleHeaders1.setString(1, group.getName());
676 this.pstmtGetArticleHeaders1.setString(2, headerKey);
677 this.pstmtGetArticleHeaders1.setLong(3, start);
679 rs = this.pstmtGetArticleHeaders1.executeQuery();
681 // Convert the "NNTP" regex to Java regex
682 patStr = patStr.replace("*", ".*");
683 Pattern pattern = Pattern.compile(patStr);
687 Long articleIndex = rs.getLong(1);
688 if(end < 0 || articleIndex <= end) // Match start is done via SQL
690 String headerValue = rs.getString(2);
691 Matcher matcher = pattern.matcher(headerValue);
692 if(matcher.matches())
694 heads.add(new Pair<Long, String>(articleIndex, headerValue));
699 catch(SQLException ex)
701 restartConnection(ex);
702 return getArticleHeaders(group, start, end, headerKey, patStr);
712 catch(SQLException ex)
714 ex.printStackTrace();
722 private String getArticleHeaders(long articleID)
723 throws StorageBackendException
729 this.pstmtGetArticleHeaders0.setLong(1, articleID);
730 rs = this.pstmtGetArticleHeaders0.executeQuery();
732 StringBuilder buf = new StringBuilder();
737 buf.append(rs.getString(1)); // key
739 String foldedValue = MimeUtility.fold(0, rs.getString(2));
740 buf.append(foldedValue); // value
752 return buf.toString();
754 catch(SQLException ex)
756 restartConnection(ex);
757 return getArticleHeaders(articleID);
767 catch(SQLException ex)
769 ex.printStackTrace();
776 public long getArticleIndex(Article article, Group group)
777 throws StorageBackendException
783 this.pstmtGetArticleIndex.setString(1, article.getMessageID());
784 this.pstmtGetArticleIndex.setLong(2, group.getInternalID());
786 rs = this.pstmtGetArticleIndex.executeQuery();
789 return rs.getLong(1);
796 catch(SQLException ex)
798 restartConnection(ex);
799 return getArticleIndex(article, group);
809 catch(SQLException ex)
811 ex.printStackTrace();
818 * Returns a list of Long/Article Pairs.
819 * @throws java.sql.SQLException
822 public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, long first,
824 throws StorageBackendException
830 this.pstmtGetArticleHeads.setLong(1, group.getInternalID());
831 this.pstmtGetArticleHeads.setLong(2, first);
832 this.pstmtGetArticleHeads.setLong(3, last);
833 rs = pstmtGetArticleHeads.executeQuery();
835 List<Pair<Long, ArticleHead>> articles
836 = new ArrayList<Pair<Long, ArticleHead>>();
840 long aid = rs.getLong("article_id");
841 long aidx = rs.getLong("article_index");
842 String headers = getArticleHeaders(aid);
843 articles.add(new Pair<Long, ArticleHead>(aidx,
844 new ArticleHead(headers)));
849 catch(SQLException ex)
851 restartConnection(ex);
852 return getArticleHeads(group, first, last);
862 catch(SQLException ex)
864 ex.printStackTrace();
871 public List<Long> getArticleNumbers(long gid)
872 throws StorageBackendException
877 List<Long> ids = new ArrayList<Long>();
878 this.pstmtGetArticleIDs.setLong(1, gid);
879 rs = this.pstmtGetArticleIDs.executeQuery();
882 ids.add(rs.getLong(1));
886 catch(SQLException ex)
888 restartConnection(ex);
889 return getArticleNumbers(gid);
898 restarts = 0; // Clear the restart count after successful request
900 catch(SQLException ex)
902 ex.printStackTrace();
909 public String getConfigValue(String key)
910 throws StorageBackendException
915 this.pstmtGetConfigValue.setString(1, key);
917 rs = this.pstmtGetConfigValue.executeQuery();
920 return rs.getString(1); // First data on index 1 not 0
927 catch(SQLException ex)
929 restartConnection(ex);
930 return getConfigValue(key);
940 catch(SQLException ex)
942 ex.printStackTrace();
944 restarts = 0; // Clear the restart count after successful request
950 public int getEventsCount(int type, long start, long end, Channel channel)
951 throws StorageBackendException
959 this.pstmtGetEventsCount0.setInt(1, type);
960 this.pstmtGetEventsCount0.setLong(2, start);
961 this.pstmtGetEventsCount0.setLong(3, end);
962 rs = this.pstmtGetEventsCount0.executeQuery();
966 this.pstmtGetEventsCount1.setInt(1, type);
967 this.pstmtGetEventsCount1.setLong(2, start);
968 this.pstmtGetEventsCount1.setLong(3, end);
969 this.pstmtGetEventsCount1.setLong(4, channel.getInternalID());
970 rs = this.pstmtGetEventsCount1.executeQuery();
982 catch(SQLException ex)
984 restartConnection(ex);
985 return getEventsCount(type, start, end, channel);
995 catch(SQLException ex)
997 ex.printStackTrace();
1004 * Reads all Groups from the JDBCDatabase.
1006 * @throws StorageBackendException
1009 public List<Channel> getGroups()
1010 throws StorageBackendException
1013 List<Channel> buffer = new ArrayList<Channel>();
1014 Statement stmt = null;
1018 stmt = conn.createStatement();
1019 rs = stmt.executeQuery("SELECT * FROM groups ORDER BY name");
1023 String name = rs.getString("name");
1024 long id = rs.getLong("group_id");
1025 int flags = rs.getInt("flags");
1027 Group group = new Group(name, id, flags);
1033 catch(SQLException ex)
1035 restartConnection(ex);
1044 stmt.close(); // Implicitely closes ResultSets
1046 catch(SQLException ex)
1048 ex.printStackTrace();
1055 public List<String> getGroupsForList(String listAddress)
1056 throws StorageBackendException
1058 ResultSet rs = null;
1062 this.pstmtGetGroupForList.setString(1, listAddress);
1064 rs = this.pstmtGetGroupForList.executeQuery();
1065 List<String> groups = new ArrayList<String>();
1068 String group = rs.getString(1);
1073 catch(SQLException ex)
1075 restartConnection(ex);
1076 return getGroupsForList(listAddress);
1086 catch(SQLException ex)
1088 ex.printStackTrace();
1095 * Returns the Group that is identified by the name.
1098 * @throws StorageBackendException
1101 public Group getGroup(String name)
1102 throws StorageBackendException
1104 ResultSet rs = null;
1108 this.pstmtGetGroup0.setString(1, name);
1109 rs = this.pstmtGetGroup0.executeQuery();
1117 long id = rs.getLong("group_id");
1118 int flags = rs.getInt("flags");
1119 return new Group(name, id, flags);
1122 catch(SQLException ex)
1124 restartConnection(ex);
1125 return getGroup(name);
1135 catch(SQLException ex)
1137 ex.printStackTrace();
1144 public List<String> getListsForGroup(String group)
1145 throws StorageBackendException
1147 ResultSet rs = null;
1148 List<String> lists = new ArrayList<String>();
1152 this.pstmtGetListForGroup.setString(1, group);
1153 rs = this.pstmtGetListForGroup.executeQuery();
1157 lists.add(rs.getString(1));
1161 catch(SQLException ex)
1163 restartConnection(ex);
1164 return getListsForGroup(group);
1174 catch(SQLException ex)
1176 ex.printStackTrace();
1182 private int getMaxArticleIndex(long groupID)
1183 throws StorageBackendException
1185 ResultSet rs = null;
1189 this.pstmtGetMaxArticleIndex.setLong(1, groupID);
1190 rs = this.pstmtGetMaxArticleIndex.executeQuery();
1195 maxIndex = rs.getInt(1);
1200 catch(SQLException ex)
1202 restartConnection(ex);
1203 return getMaxArticleIndex(groupID);
1213 catch(SQLException ex)
1215 ex.printStackTrace();
1221 private int getMaxArticleID()
1222 throws StorageBackendException
1224 ResultSet rs = null;
1228 rs = this.pstmtGetMaxArticleID.executeQuery();
1233 maxIndex = rs.getInt(1);
1238 catch(SQLException ex)
1240 restartConnection(ex);
1241 return getMaxArticleID();
1251 catch(SQLException ex)
1253 ex.printStackTrace();
1260 public int getLastArticleNumber(Group group)
1261 throws StorageBackendException
1263 ResultSet rs = null;
1267 this.pstmtGetLastArticleNumber.setLong(1, group.getInternalID());
1268 rs = this.pstmtGetLastArticleNumber.executeQuery();
1271 return rs.getInt(1);
1278 catch(SQLException ex)
1280 restartConnection(ex);
1281 return getLastArticleNumber(group);
1291 catch(SQLException ex)
1293 ex.printStackTrace();
1300 public int getFirstArticleNumber(Group group)
1301 throws StorageBackendException
1303 ResultSet rs = null;
1306 this.pstmtGetFirstArticleNumber.setLong(1, group.getInternalID());
1307 rs = this.pstmtGetFirstArticleNumber.executeQuery();
1310 return rs.getInt(1);
1317 catch(SQLException ex)
1319 restartConnection(ex);
1320 return getFirstArticleNumber(group);
1330 catch(SQLException ex)
1332 ex.printStackTrace();
1339 * Returns a group name identified by the given id.
1342 * @throws StorageBackendException
1344 public String getGroup(int id)
1345 throws StorageBackendException
1347 ResultSet rs = null;
1351 this.pstmtGetGroup1.setInt(1, id);
1352 rs = this.pstmtGetGroup1.executeQuery();
1356 return rs.getString(1);
1363 catch(SQLException ex)
1365 restartConnection(ex);
1366 return getGroup(id);
1376 catch(SQLException ex)
1378 ex.printStackTrace();
1385 public double getEventsPerHour(int key, long gid)
1386 throws StorageBackendException
1388 String gidquery = "";
1391 gidquery = " AND group_id = " + gid;
1394 Statement stmt = null;
1395 ResultSet rs = null;
1399 stmt = this.conn.createStatement();
1400 rs = stmt.executeQuery("SELECT Count(*) / (Max(event_time) - Min(event_time))" +
1401 " * 1000 * 60 * 60 FROM events WHERE event_key = " + key + gidquery);
1405 restarts = 0; // reset error count
1406 return rs.getDouble(1);
1413 catch(SQLException ex)
1415 restartConnection(ex);
1416 return getEventsPerHour(key, gid);
1424 stmt.close(); // Implicitely closes the result sets
1427 catch(SQLException ex)
1429 ex.printStackTrace();
1435 public String getOldestArticle()
1436 throws StorageBackendException
1438 ResultSet rs = null;
1442 rs = this.pstmtGetOldestArticle.executeQuery();
1445 return rs.getString(1);
1452 catch(SQLException ex)
1454 restartConnection(ex);
1455 return getOldestArticle();
1465 catch(SQLException ex)
1467 ex.printStackTrace();
1474 public int getPostingsCount(String groupname)
1475 throws StorageBackendException
1477 ResultSet rs = null;
1481 this.pstmtGetPostingsCount.setString(1, groupname);
1482 rs = this.pstmtGetPostingsCount.executeQuery();
1485 return rs.getInt(1);
1489 Log.get().warning("Count on postings return nothing!");
1493 catch(SQLException ex)
1495 restartConnection(ex);
1496 return getPostingsCount(groupname);
1506 catch(SQLException ex)
1508 ex.printStackTrace();
1515 public List<Subscription> getSubscriptions(int feedtype)
1516 throws StorageBackendException
1518 ResultSet rs = null;
1522 List<Subscription> subs = new ArrayList<Subscription>();
1523 this.pstmtGetSubscriptions.setInt(1, feedtype);
1524 rs = this.pstmtGetSubscriptions.executeQuery();
1528 String host = rs.getString("host");
1529 String group = rs.getString("name");
1530 int port = rs.getInt("port");
1531 subs.add(new Subscription(host, port, feedtype, group));
1536 catch(SQLException ex)
1538 restartConnection(ex);
1539 return getSubscriptions(feedtype);
1549 catch(SQLException ex)
1551 ex.printStackTrace();
1558 * Checks if there is an article with the given messageid in the JDBCDatabase.
1561 * @throws StorageBackendException
1564 public boolean isArticleExisting(String messageID)
1565 throws StorageBackendException
1567 ResultSet rs = null;
1571 this.pstmtIsArticleExisting.setString(1, messageID);
1572 rs = this.pstmtIsArticleExisting.executeQuery();
1573 return rs.next() && rs.getInt(1) == 1;
1575 catch(SQLException ex)
1577 restartConnection(ex);
1578 return isArticleExisting(messageID);
1588 catch(SQLException ex)
1590 ex.printStackTrace();
1597 * Checks if there is a group with the given name in the JDBCDatabase.
1600 * @throws StorageBackendException
1603 public boolean isGroupExisting(String name)
1604 throws StorageBackendException
1606 ResultSet rs = null;
1610 this.pstmtIsGroupExisting.setString(1, name);
1611 rs = this.pstmtIsGroupExisting.executeQuery();
1614 catch(SQLException ex)
1616 restartConnection(ex);
1617 return isGroupExisting(name);
1627 catch(SQLException ex)
1629 ex.printStackTrace();
1636 public void setConfigValue(String key, String value)
1637 throws StorageBackendException
1641 conn.setAutoCommit(false);
1642 this.pstmtSetConfigValue0.setString(1, key);
1643 this.pstmtSetConfigValue0.execute();
1644 this.pstmtSetConfigValue1.setString(1, key);
1645 this.pstmtSetConfigValue1.setString(2, value);
1646 this.pstmtSetConfigValue1.execute();
1648 conn.setAutoCommit(true);
1650 catch(SQLException ex)
1652 restartConnection(ex);
1653 setConfigValue(key, value);
1658 * Closes the JDBCDatabase connection.
1660 public void shutdown()
1661 throws StorageBackendException
1665 if(this.conn != null)
1670 catch(SQLException ex)
1672 throw new StorageBackendException(ex);
1677 public void purgeGroup(Group group)
1678 throws StorageBackendException
1682 this.pstmtPurgeGroup0.setLong(1, group.getInternalID());
1683 this.pstmtPurgeGroup0.executeUpdate();
1685 this.pstmtPurgeGroup1.setLong(1, group.getInternalID());
1686 this.pstmtPurgeGroup1.executeUpdate();
1688 catch(SQLException ex)
1690 restartConnection(ex);
1695 private void restartConnection(SQLException cause)
1696 throws StorageBackendException
1699 Log.get().severe(Thread.currentThread()
1700 + ": Database connection was closed (restart " + restarts + ").");
1702 if(restarts >= MAX_RESTARTS)
1704 // Delete the current, probably broken JDBCDatabase instance.
1705 // So no one can use the instance any more.
1706 JDBCDatabaseProvider.instances.remove(Thread.currentThread());
1708 // Throw the exception upwards
1709 throw new StorageBackendException(cause);
1714 Thread.sleep(1500L * restarts);
1716 catch(InterruptedException ex)
1718 Log.get().warning("Interrupted: " + ex.getMessage());
1721 // Try to properly close the old database connection
1724 if(this.conn != null)
1729 catch(SQLException ex)
1731 Log.get().warning(ex.getMessage());
1736 // Try to reinitialize database connection
1739 catch(SQLException ex)
1741 Log.get().warning(ex.getMessage());
1742 restartConnection(ex);
1747 public boolean update(Article article)
1748 throws StorageBackendException
1750 // DELETE FROM headers WHERE article_id = ?
1752 // INSERT INTO headers ...
1754 // SELECT * FROM postings WHERE article_id = ? AND group_id = ?
1759 * Writes the flags and the name of the given group to the database.
1761 * @throws StorageBackendException
1764 public boolean update(Group group)
1765 throws StorageBackendException
1769 this.pstmtUpdateGroup.setInt(1, group.getFlags());
1770 this.pstmtUpdateGroup.setString(2, group.getName());
1771 this.pstmtUpdateGroup.setLong(3, group.getInternalID());
1772 int rs = this.pstmtUpdateGroup.executeUpdate();
1775 catch(SQLException ex)
1777 restartConnection(ex);
1778 return update(group);