Some work on XDAEMON command.
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;
56 private Connection conn = null;
57 private PreparedStatement pstmtAddArticle1 = null;
58 private PreparedStatement pstmtAddArticle2 = null;
59 private PreparedStatement pstmtAddArticle3 = null;
60 private PreparedStatement pstmtAddArticle4 = null;
61 private PreparedStatement pstmtAddGroup0 = null;
62 private PreparedStatement pstmtAddEvent = null;
63 private PreparedStatement pstmtCountArticles = null;
64 private PreparedStatement pstmtCountGroups = null;
65 private PreparedStatement pstmtDeleteArticle0 = null;
66 private PreparedStatement pstmtDeleteArticle1 = null;
67 private PreparedStatement pstmtDeleteArticle2 = null;
68 private PreparedStatement pstmtDeleteArticle3 = null;
69 private PreparedStatement pstmtGetArticle0 = null;
70 private PreparedStatement pstmtGetArticle1 = null;
71 private PreparedStatement pstmtGetArticleHeaders0 = null;
72 private PreparedStatement pstmtGetArticleHeaders1 = null;
73 private PreparedStatement pstmtGetArticleHeads = null;
74 private PreparedStatement pstmtGetArticleIDs = null;
75 private PreparedStatement pstmtGetArticleIndex = null;
76 private PreparedStatement pstmtGetConfigValue = null;
77 private PreparedStatement pstmtGetEventsCount0 = null;
78 private PreparedStatement pstmtGetEventsCount1 = null;
79 private PreparedStatement pstmtGetGroupForList = null;
80 private PreparedStatement pstmtGetGroup0 = null;
81 private PreparedStatement pstmtGetGroup1 = null;
82 private PreparedStatement pstmtGetFirstArticleNumber = null;
83 private PreparedStatement pstmtGetListForGroup = null;
84 private PreparedStatement pstmtGetLastArticleNumber = null;
85 private PreparedStatement pstmtGetMaxArticleID = null;
86 private PreparedStatement pstmtGetMaxArticleIndex = null;
87 private PreparedStatement pstmtGetOldestArticle = null;
88 private PreparedStatement pstmtGetPostingsCount = null;
89 private PreparedStatement pstmtGetSubscriptions = null;
90 private PreparedStatement pstmtIsArticleExisting = null;
91 private PreparedStatement pstmtIsGroupExisting = null;
92 private PreparedStatement pstmtPurgeGroup0 = null;
93 private PreparedStatement pstmtPurgeGroup1 = null;
94 private PreparedStatement pstmtSetConfigValue0 = null;
95 private PreparedStatement pstmtSetConfigValue1 = null;
96 private PreparedStatement pstmtUpdateGroup = null;
97 /** How many times the database connection was reinitialized */
98 private int restarts = 0;
101 * Rises the database: reconnect and recreate all prepared statements.
102 * @throws java.lang.SQLException
104 protected void arise()
108 // Load database driver
110 Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DBMSDRIVER, "java.lang.Object"));
112 // Establish database connection
113 this.conn = DriverManager.getConnection(
114 Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DATABASE, "<not specified>"),
115 Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_USER, "root"),
116 Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_PASSWORD, ""));
118 this.conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
119 if (this.conn.getTransactionIsolation() != Connection.TRANSACTION_SERIALIZABLE) {
120 Log.get().warning("Database is NOT fully serializable!");
123 // Prepare statements for method addArticle()
124 this.pstmtAddArticle1 = conn.prepareStatement(
125 "INSERT INTO articles (article_id, body) VALUES(?, ?)");
126 this.pstmtAddArticle2 = conn.prepareStatement(
127 "INSERT INTO headers (article_id, header_key, header_value, header_index) "
128 + "VALUES (?, ?, ?, ?)");
129 this.pstmtAddArticle3 = conn.prepareStatement(
130 "INSERT INTO postings (group_id, article_id, article_index)"
131 + "VALUES (?, ?, ?)");
132 this.pstmtAddArticle4 = conn.prepareStatement(
133 "INSERT INTO article_ids (article_id, message_id) VALUES (?, ?)");
135 // Prepare statement for method addStatValue()
136 this.pstmtAddEvent = conn.prepareStatement(
137 "INSERT INTO events VALUES (?, ?, ?)");
139 // Prepare statement for method addGroup()
140 this.pstmtAddGroup0 = conn.prepareStatement(
141 "INSERT INTO groups (name, flags) VALUES (?, ?)");
143 // Prepare statement for method countArticles()
144 this.pstmtCountArticles = conn.prepareStatement(
145 "SELECT Count(article_id) FROM article_ids");
147 // Prepare statement for method countGroups()
148 this.pstmtCountGroups = conn.prepareStatement(
149 "SELECT Count(group_id) FROM groups WHERE "
150 + "flags & " + Channel.DELETED + " = 0");
152 // Prepare statements for method delete(article)
153 this.pstmtDeleteArticle0 = conn.prepareStatement(
154 "DELETE FROM articles WHERE article_id = "
155 + "(SELECT article_id FROM article_ids WHERE message_id = ?)");
156 this.pstmtDeleteArticle1 = conn.prepareStatement(
157 "DELETE FROM headers WHERE article_id = "
158 + "(SELECT article_id FROM article_ids WHERE message_id = ?)");
159 this.pstmtDeleteArticle2 = conn.prepareStatement(
160 "DELETE FROM postings WHERE article_id = "
161 + "(SELECT article_id FROM article_ids WHERE message_id = ?)");
162 this.pstmtDeleteArticle3 = conn.prepareStatement(
163 "DELETE FROM article_ids WHERE message_id = ?");
165 // Prepare statements for methods getArticle()
166 this.pstmtGetArticle0 = conn.prepareStatement(
167 "SELECT * FROM articles WHERE article_id = "
168 + "(SELECT article_id FROM article_ids WHERE message_id = ?)");
169 this.pstmtGetArticle1 = conn.prepareStatement(
170 "SELECT * FROM articles WHERE article_id = "
171 + "(SELECT article_id FROM postings WHERE "
172 + "article_index = ? AND group_id = ?)");
174 // Prepare statement for method getArticleHeaders()
175 this.pstmtGetArticleHeaders0 = conn.prepareStatement(
176 "SELECT header_key, header_value FROM headers WHERE article_id = ? "
177 + "ORDER BY header_index ASC");
179 // Prepare statement for method getArticleHeaders(regular expr pattern)
180 this.pstmtGetArticleHeaders1 = conn.prepareStatement(
181 "SELECT p.article_index, h.header_value FROM headers h "
182 + "INNER JOIN postings p ON h.article_id = p.article_id "
183 + "INNER JOIN groups g ON p.group_id = g.group_id "
184 + "WHERE g.name = ? AND "
185 + "h.header_key = ? AND "
186 + "p.article_index >= ? "
187 + "ORDER BY p.article_index ASC");
189 this.pstmtGetArticleIDs = conn.prepareStatement(
190 "SELECT article_index FROM postings WHERE group_id = ?");
192 // Prepare statement for method getArticleIndex
193 this.pstmtGetArticleIndex = conn.prepareStatement(
194 "SELECT article_index FROM postings WHERE "
195 + "article_id = (SELECT article_id FROM article_ids "
196 + "WHERE message_id = ?) "
197 + " AND group_id = ?");
199 // Prepare statements for method getArticleHeads()
200 this.pstmtGetArticleHeads = conn.prepareStatement(
201 "SELECT article_id, article_index FROM postings WHERE "
202 + "postings.group_id = ? AND article_index >= ? AND "
203 + "article_index <= ?");
205 // Prepare statements for method getConfigValue()
206 this.pstmtGetConfigValue = conn.prepareStatement(
207 "SELECT config_value FROM config WHERE config_key = ?");
209 // Prepare statements for method getEventsCount()
210 this.pstmtGetEventsCount0 = conn.prepareStatement(
211 "SELECT Count(*) FROM events WHERE event_key = ? AND "
212 + "event_time >= ? AND event_time < ?");
214 this.pstmtGetEventsCount1 = conn.prepareStatement(
215 "SELECT Count(*) FROM events WHERE event_key = ? AND "
216 + "event_time >= ? AND event_time < ? AND group_id = ?");
218 // Prepare statement for method getGroupForList()
219 this.pstmtGetGroupForList = conn.prepareStatement(
220 "SELECT name FROM groups INNER JOIN groups2list "
221 + "ON groups.group_id = groups2list.group_id "
222 + "WHERE groups2list.listaddress = ?");
224 // Prepare statement for method getGroup()
225 this.pstmtGetGroup0 = conn.prepareStatement(
226 "SELECT group_id, flags FROM groups WHERE Name = ?");
227 this.pstmtGetGroup1 = conn.prepareStatement(
228 "SELECT name FROM groups WHERE group_id = ?");
230 // Prepare statement for method getLastArticleNumber()
231 this.pstmtGetLastArticleNumber = conn.prepareStatement(
232 "SELECT Max(article_index) FROM postings WHERE group_id = ?");
234 // Prepare statement for method getListForGroup()
235 this.pstmtGetListForGroup = conn.prepareStatement(
236 "SELECT listaddress FROM groups2list INNER JOIN groups "
237 + "ON groups.group_id = groups2list.group_id WHERE name = ?");
239 // Prepare statement for method getMaxArticleID()
240 this.pstmtGetMaxArticleID = conn.prepareStatement(
241 "SELECT Max(article_id) FROM articles");
243 // Prepare statement for method getMaxArticleIndex()
244 this.pstmtGetMaxArticleIndex = conn.prepareStatement(
245 "SELECT Max(article_index) FROM postings WHERE group_id = ?");
247 // Prepare statement for method getOldestArticle()
248 this.pstmtGetOldestArticle = conn.prepareStatement(
249 "SELECT message_id FROM article_ids WHERE article_id = "
250 + "(SELECT Min(article_id) FROM article_ids)");
252 // Prepare statement for method getFirstArticleNumber()
253 this.pstmtGetFirstArticleNumber = conn.prepareStatement(
254 "SELECT Min(article_index) FROM postings WHERE group_id = ?");
256 // Prepare statement for method getPostingsCount()
257 this.pstmtGetPostingsCount = conn.prepareStatement(
258 "SELECT Count(*) FROM postings NATURAL JOIN groups "
259 + "WHERE groups.name = ?");
261 // Prepare statement for method getSubscriptions()
262 this.pstmtGetSubscriptions = conn.prepareStatement(
263 "SELECT host, port, name FROM peers NATURAL JOIN "
264 + "peer_subscriptions NATURAL JOIN groups WHERE feedtype = ?");
266 // Prepare statement for method isArticleExisting()
267 this.pstmtIsArticleExisting = conn.prepareStatement(
268 "SELECT Count(article_id) FROM article_ids WHERE message_id = ?");
270 // Prepare statement for method isGroupExisting()
271 this.pstmtIsGroupExisting = conn.prepareStatement(
272 "SELECT * FROM groups WHERE name = ?");
274 // Prepare statement for method setConfigValue()
275 this.pstmtSetConfigValue0 = conn.prepareStatement(
276 "DELETE FROM config WHERE config_key = ?");
277 this.pstmtSetConfigValue1 = conn.prepareStatement(
278 "INSERT INTO config VALUES(?, ?)");
280 // Prepare statements for method purgeGroup()
281 this.pstmtPurgeGroup0 = conn.prepareStatement(
282 "DELETE FROM peer_subscriptions WHERE group_id = ?");
283 this.pstmtPurgeGroup1 = conn.prepareStatement(
284 "DELETE FROM groups WHERE group_id = ?");
286 // Prepare statement for method update(Group)
287 this.pstmtUpdateGroup = conn.prepareStatement(
288 "UPDATE groups SET flags = ?, name = ? WHERE group_id = ?");
289 } catch (ClassNotFoundException ex) {
290 throw new Error("JDBC Driver not found!", ex);
295 * Adds an article to the database.
298 * @throws java.sql.SQLException
301 public void addArticle(final Article article)
302 throws StorageBackendException
305 this.conn.setAutoCommit(false);
307 int newArticleID = getMaxArticleID() + 1;
308 addArticle(article, newArticleID);
310 this.conn.setAutoCommit(true);
312 this.restarts = 0; // Reset error count
313 } catch (SQLException ex) {
315 this.conn.rollback(); // Rollback changes
316 } catch (SQLException ex2) {
317 Log.get().severe("Rollback of addArticle() failed: " + ex2);
321 this.conn.setAutoCommit(true); // and release locks
322 } catch (SQLException ex2) {
323 Log.get().severe("setAutoCommit(true) of addArticle() failed: " + ex2);
326 restartConnection(ex);
332 * Adds an article to the database.
335 * @throws java.sql.SQLException
337 void addArticle(final Article article, final int newArticleID)
338 throws SQLException, StorageBackendException
340 // Fill prepared statement with values;
341 // writes body to article table
342 pstmtAddArticle1.setInt(1, newArticleID);
343 pstmtAddArticle1.setBytes(2, article.getBody());
344 pstmtAddArticle1.execute();
347 Enumeration headers = article.getAllHeaders();
348 for (int n = 0; headers.hasMoreElements(); n++) {
349 Header header = (Header) headers.nextElement();
350 pstmtAddArticle2.setInt(1, newArticleID);
351 pstmtAddArticle2.setString(2, header.getName().toLowerCase());
352 pstmtAddArticle2.setString(3,
353 header.getValue().replaceAll("[\r\n]", ""));
354 pstmtAddArticle2.setInt(4, n);
355 pstmtAddArticle2.execute();
358 // For each newsgroup add a reference
359 List<Group> groups = article.getGroups();
360 for (Group group : groups) {
361 pstmtAddArticle3.setLong(1, group.getInternalID());
362 pstmtAddArticle3.setInt(2, newArticleID);
363 pstmtAddArticle3.setLong(3, getMaxArticleIndex(group.getInternalID()) + 1);
364 pstmtAddArticle3.execute();
367 // Write message-id to article_ids table
368 this.pstmtAddArticle4.setInt(1, newArticleID);
369 this.pstmtAddArticle4.setString(2, article.getMessageID());
370 this.pstmtAddArticle4.execute();
374 * Adds a group to the JDBCDatabase. This method is not accessible via NNTP.
376 * @throws java.sql.SQLException
379 public void addGroup(String name, int flags)
380 throws StorageBackendException
383 this.conn.setAutoCommit(false);
384 pstmtAddGroup0.setString(1, name);
385 pstmtAddGroup0.setInt(2, flags);
387 pstmtAddGroup0.executeUpdate();
389 this.conn.setAutoCommit(true);
390 this.restarts = 0; // Reset error count
391 } catch (SQLException ex) {
393 this.conn.rollback();
394 this.conn.setAutoCommit(true);
395 } catch (SQLException ex2) {
396 ex2.printStackTrace();
399 restartConnection(ex);
400 addGroup(name, flags);
405 public void addEvent(long time, int type, long gid)
406 throws StorageBackendException
409 this.conn.setAutoCommit(false);
410 this.pstmtAddEvent.setLong(1, time);
411 this.pstmtAddEvent.setInt(2, type);
412 this.pstmtAddEvent.setLong(3, gid);
413 this.pstmtAddEvent.executeUpdate();
415 this.conn.setAutoCommit(true);
417 } catch (SQLException ex) {
419 this.conn.rollback();
420 this.conn.setAutoCommit(true);
421 } catch (SQLException ex2) {
422 ex2.printStackTrace();
425 restartConnection(ex);
426 addEvent(time, type, gid);
431 public int countArticles()
432 throws StorageBackendException
437 rs = this.pstmtCountArticles.executeQuery();
443 } catch (SQLException ex) {
444 restartConnection(ex);
445 return countArticles();
450 } catch (SQLException ex) {
451 ex.printStackTrace();
459 public int countGroups()
460 throws StorageBackendException
465 rs = this.pstmtCountGroups.executeQuery();
471 } catch (SQLException ex) {
472 restartConnection(ex);
473 return countGroups();
478 } catch (SQLException ex) {
479 ex.printStackTrace();
487 public void delete(final String messageID)
488 throws StorageBackendException
491 this.conn.setAutoCommit(false);
493 this.pstmtDeleteArticle0.setString(1, messageID);
494 int rs = this.pstmtDeleteArticle0.executeUpdate();
496 // We do not trust the ON DELETE CASCADE functionality to delete
497 // orphaned references...
498 this.pstmtDeleteArticle1.setString(1, messageID);
499 rs = this.pstmtDeleteArticle1.executeUpdate();
501 this.pstmtDeleteArticle2.setString(1, messageID);
502 rs = this.pstmtDeleteArticle2.executeUpdate();
504 this.pstmtDeleteArticle3.setString(1, messageID);
505 rs = this.pstmtDeleteArticle3.executeUpdate();
508 this.conn.setAutoCommit(true);
509 } catch (SQLException ex) {
510 throw new StorageBackendException(ex);
515 public Article getArticle(String messageID)
516 throws StorageBackendException
520 pstmtGetArticle0.setString(1, messageID);
521 rs = pstmtGetArticle0.executeQuery();
526 byte[] body = rs.getBytes("body");
527 String headers = getArticleHeaders(rs.getInt("article_id"));
528 return new Article(headers, body);
530 } catch (SQLException ex) {
531 restartConnection(ex);
532 return getArticle(messageID);
537 } catch (SQLException ex) {
538 ex.printStackTrace();
540 restarts = 0; // Reset error count
546 * Retrieves an article by its ID.
549 * @throws StorageBackendException
552 public Article getArticle(long articleIndex, long gid)
553 throws StorageBackendException
558 this.pstmtGetArticle1.setLong(1, articleIndex);
559 this.pstmtGetArticle1.setLong(2, gid);
561 rs = this.pstmtGetArticle1.executeQuery();
564 byte[] body = rs.getBytes("body");
565 String headers = getArticleHeaders(rs.getInt("article_id"));
566 return new Article(headers, body);
570 } catch (SQLException ex) {
571 restartConnection(ex);
572 return getArticle(articleIndex, gid);
577 } catch (SQLException ex) {
578 ex.printStackTrace();
586 * Searches for fitting header values using the given regular expression.
593 * @throws StorageBackendException
596 public List<Pair<Long, String>> getArticleHeaders(Channel group, long start,
597 long end, String headerKey, String patStr)
598 throws StorageBackendException, PatternSyntaxException
601 List<Pair<Long, String>> heads = new ArrayList<Pair<Long, String>>();
604 this.pstmtGetArticleHeaders1.setString(1, group.getName());
605 this.pstmtGetArticleHeaders1.setString(2, headerKey);
606 this.pstmtGetArticleHeaders1.setLong(3, start);
608 rs = this.pstmtGetArticleHeaders1.executeQuery();
610 // Convert the "NNTP" regex to Java regex
611 patStr = patStr.replace("*", ".*");
612 Pattern pattern = Pattern.compile(patStr);
615 Long articleIndex = rs.getLong(1);
616 if (end < 0 || articleIndex <= end) // Match start is done via SQL
618 String headerValue = rs.getString(2);
619 Matcher matcher = pattern.matcher(headerValue);
620 if (matcher.matches()) {
621 heads.add(new Pair<Long, String>(articleIndex, headerValue));
625 } catch (SQLException ex) {
626 restartConnection(ex);
627 return getArticleHeaders(group, start, end, headerKey, patStr);
632 } catch (SQLException ex) {
633 ex.printStackTrace();
641 private String getArticleHeaders(long articleID)
642 throws StorageBackendException
647 this.pstmtGetArticleHeaders0.setLong(1, articleID);
648 rs = this.pstmtGetArticleHeaders0.executeQuery();
650 StringBuilder buf = new StringBuilder();
653 buf.append(rs.getString(1)); // key
655 String foldedValue = MimeUtility.fold(0, rs.getString(2));
656 buf.append(foldedValue); // value
665 return buf.toString();
666 } catch (SQLException ex) {
667 restartConnection(ex);
668 return getArticleHeaders(articleID);
673 } catch (SQLException ex) {
674 ex.printStackTrace();
681 public long getArticleIndex(Article article, Group group)
682 throws StorageBackendException
687 this.pstmtGetArticleIndex.setString(1, article.getMessageID());
688 this.pstmtGetArticleIndex.setLong(2, group.getInternalID());
690 rs = this.pstmtGetArticleIndex.executeQuery();
692 return rs.getLong(1);
696 } catch (SQLException ex) {
697 restartConnection(ex);
698 return getArticleIndex(article, group);
703 } catch (SQLException ex) {
704 ex.printStackTrace();
711 * Returns a list of Long/Article Pairs.
712 * @throws java.sql.SQLException
715 public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, long first,
717 throws StorageBackendException
722 this.pstmtGetArticleHeads.setLong(1, group.getInternalID());
723 this.pstmtGetArticleHeads.setLong(2, first);
724 this.pstmtGetArticleHeads.setLong(3, last);
725 rs = pstmtGetArticleHeads.executeQuery();
727 List<Pair<Long, ArticleHead>> articles = new ArrayList<Pair<Long, ArticleHead>>();
730 long aid = rs.getLong("article_id");
731 long aidx = rs.getLong("article_index");
732 String headers = getArticleHeaders(aid);
733 articles.add(new Pair<Long, ArticleHead>(aidx,
734 new ArticleHead(headers)));
738 } catch (SQLException ex) {
739 restartConnection(ex);
740 return getArticleHeads(group, first, last);
745 } catch (SQLException ex) {
746 ex.printStackTrace();
753 public List<Long> getArticleNumbers(long gid)
754 throws StorageBackendException
758 List<Long> ids = new ArrayList<Long>();
759 this.pstmtGetArticleIDs.setLong(1, gid);
760 rs = this.pstmtGetArticleIDs.executeQuery();
762 ids.add(rs.getLong(1));
765 } catch (SQLException ex) {
766 restartConnection(ex);
767 return getArticleNumbers(gid);
772 restarts = 0; // Clear the restart count after successful request
773 } catch (SQLException ex) {
774 ex.printStackTrace();
781 public String getConfigValue(String key)
782 throws StorageBackendException
786 this.pstmtGetConfigValue.setString(1, key);
788 rs = this.pstmtGetConfigValue.executeQuery();
790 return rs.getString(1); // First data on index 1 not 0
794 } catch (SQLException ex) {
795 restartConnection(ex);
796 return getConfigValue(key);
801 } catch (SQLException ex) {
802 ex.printStackTrace();
804 restarts = 0; // Clear the restart count after successful request
810 public int getEventsCount(int type, long start, long end, Channel channel)
811 throws StorageBackendException
816 if (channel == null) {
817 this.pstmtGetEventsCount0.setInt(1, type);
818 this.pstmtGetEventsCount0.setLong(2, start);
819 this.pstmtGetEventsCount0.setLong(3, end);
820 rs = this.pstmtGetEventsCount0.executeQuery();
822 this.pstmtGetEventsCount1.setInt(1, type);
823 this.pstmtGetEventsCount1.setLong(2, start);
824 this.pstmtGetEventsCount1.setLong(3, end);
825 this.pstmtGetEventsCount1.setLong(4, channel.getInternalID());
826 rs = this.pstmtGetEventsCount1.executeQuery();
834 } catch (SQLException ex) {
835 restartConnection(ex);
836 return getEventsCount(type, start, end, channel);
841 } catch (SQLException ex) {
842 ex.printStackTrace();
849 * Reads all Groups from the JDBCDatabase.
851 * @throws StorageBackendException
854 public List<Channel> getGroups()
855 throws StorageBackendException
858 List<Channel> buffer = new ArrayList<Channel>();
859 Statement stmt = null;
862 stmt = conn.createStatement();
863 rs = stmt.executeQuery("SELECT * FROM groups ORDER BY name");
866 String name = rs.getString("name");
867 long id = rs.getLong("group_id");
868 int flags = rs.getInt("flags");
870 Group group = new Group(name, id, flags);
875 } catch (SQLException ex) {
876 restartConnection(ex);
881 stmt.close(); // Implicitely closes ResultSets
882 } catch (SQLException ex) {
883 ex.printStackTrace();
890 public List<String> getGroupsForList(String listAddress)
891 throws StorageBackendException
896 this.pstmtGetGroupForList.setString(1, listAddress);
898 rs = this.pstmtGetGroupForList.executeQuery();
899 List<String> groups = new ArrayList<String>();
901 String group = rs.getString(1);
905 } catch (SQLException ex) {
906 restartConnection(ex);
907 return getGroupsForList(listAddress);
912 } catch (SQLException ex) {
913 ex.printStackTrace();
920 * Returns the Group that is identified by the name.
923 * @throws StorageBackendException
926 public Group getGroup(String name)
927 throws StorageBackendException
932 this.pstmtGetGroup0.setString(1, name);
933 rs = this.pstmtGetGroup0.executeQuery();
938 long id = rs.getLong("group_id");
939 int flags = rs.getInt("flags");
940 return new Group(name, id, flags);
942 } catch (SQLException ex) {
943 restartConnection(ex);
944 return getGroup(name);
949 } catch (SQLException ex) {
950 ex.printStackTrace();
957 public List<String> getListsForGroup(String group)
958 throws StorageBackendException
961 List<String> lists = new ArrayList<String>();
964 this.pstmtGetListForGroup.setString(1, group);
965 rs = this.pstmtGetListForGroup.executeQuery();
968 lists.add(rs.getString(1));
971 } catch (SQLException ex) {
972 restartConnection(ex);
973 return getListsForGroup(group);
978 } catch (SQLException ex) {
979 ex.printStackTrace();
985 private int getMaxArticleIndex(long groupID)
986 throws StorageBackendException
991 this.pstmtGetMaxArticleIndex.setLong(1, groupID);
992 rs = this.pstmtGetMaxArticleIndex.executeQuery();
996 maxIndex = rs.getInt(1);
1000 } catch (SQLException ex) {
1001 restartConnection(ex);
1002 return getMaxArticleIndex(groupID);
1007 } catch (SQLException ex) {
1008 ex.printStackTrace();
1014 private int getMaxArticleID()
1015 throws StorageBackendException
1017 ResultSet rs = null;
1020 rs = this.pstmtGetMaxArticleID.executeQuery();
1024 maxIndex = rs.getInt(1);
1028 } catch (SQLException ex) {
1029 restartConnection(ex);
1030 return getMaxArticleID();
1035 } catch (SQLException ex) {
1036 ex.printStackTrace();
1043 public int getLastArticleNumber(Group group)
1044 throws StorageBackendException
1046 ResultSet rs = null;
1049 this.pstmtGetLastArticleNumber.setLong(1, group.getInternalID());
1050 rs = this.pstmtGetLastArticleNumber.executeQuery();
1052 return rs.getInt(1);
1056 } catch (SQLException ex) {
1057 restartConnection(ex);
1058 return getLastArticleNumber(group);
1063 } catch (SQLException ex) {
1064 ex.printStackTrace();
1071 public int getFirstArticleNumber(Group group)
1072 throws StorageBackendException
1074 ResultSet rs = null;
1076 this.pstmtGetFirstArticleNumber.setLong(1, group.getInternalID());
1077 rs = this.pstmtGetFirstArticleNumber.executeQuery();
1079 return rs.getInt(1);
1083 } catch (SQLException ex) {
1084 restartConnection(ex);
1085 return getFirstArticleNumber(group);
1090 } catch (SQLException ex) {
1091 ex.printStackTrace();
1098 * Returns a group name identified by the given id.
1101 * @throws StorageBackendException
1103 public String getGroup(int id)
1104 throws StorageBackendException
1106 ResultSet rs = null;
1109 this.pstmtGetGroup1.setInt(1, id);
1110 rs = this.pstmtGetGroup1.executeQuery();
1113 return rs.getString(1);
1117 } catch (SQLException ex) {
1118 restartConnection(ex);
1119 return getGroup(id);
1124 } catch (SQLException ex) {
1125 ex.printStackTrace();
1132 public double getEventsPerHour(int key, long gid)
1133 throws StorageBackendException
1135 String gidquery = "";
1137 gidquery = " AND group_id = " + gid;
1140 Statement stmt = null;
1141 ResultSet rs = null;
1144 stmt = this.conn.createStatement();
1145 rs = stmt.executeQuery("SELECT Count(*) / (Max(event_time) - Min(event_time))"
1146 + " * 1000 * 60 * 60 FROM events WHERE event_key = " + key + gidquery);
1149 restarts = 0; // reset error count
1150 return rs.getDouble(1);
1154 } catch (SQLException ex) {
1155 restartConnection(ex);
1156 return getEventsPerHour(key, gid);
1160 stmt.close(); // Implicitely closes the result sets
1162 } catch (SQLException ex) {
1163 ex.printStackTrace();
1169 public String getOldestArticle()
1170 throws StorageBackendException
1172 ResultSet rs = null;
1175 rs = this.pstmtGetOldestArticle.executeQuery();
1177 return rs.getString(1);
1181 } catch (SQLException ex) {
1182 restartConnection(ex);
1183 return getOldestArticle();
1188 } catch (SQLException ex) {
1189 ex.printStackTrace();
1196 public int getPostingsCount(String groupname)
1197 throws StorageBackendException
1199 ResultSet rs = null;
1202 this.pstmtGetPostingsCount.setString(1, groupname);
1203 rs = this.pstmtGetPostingsCount.executeQuery();
1205 return rs.getInt(1);
1207 Log.get().warning("Count on postings return nothing!");
1210 } catch (SQLException ex) {
1211 restartConnection(ex);
1212 return getPostingsCount(groupname);
1217 } catch (SQLException ex) {
1218 ex.printStackTrace();
1225 public List<Subscription> getSubscriptions(int feedtype)
1226 throws StorageBackendException
1228 ResultSet rs = null;
1231 List<Subscription> subs = new ArrayList<Subscription>();
1232 this.pstmtGetSubscriptions.setInt(1, feedtype);
1233 rs = this.pstmtGetSubscriptions.executeQuery();
1236 String host = rs.getString("host");
1237 String group = rs.getString("name");
1238 int port = rs.getInt("port");
1239 subs.add(new Subscription(host, port, feedtype, group));
1243 } catch (SQLException ex) {
1244 restartConnection(ex);
1245 return getSubscriptions(feedtype);
1250 } catch (SQLException ex) {
1251 ex.printStackTrace();
1258 * Checks if there is an article with the given messageid in the JDBCDatabase.
1261 * @throws StorageBackendException
1264 public boolean isArticleExisting(String messageID)
1265 throws StorageBackendException
1267 ResultSet rs = null;
1270 this.pstmtIsArticleExisting.setString(1, messageID);
1271 rs = this.pstmtIsArticleExisting.executeQuery();
1272 return rs.next() && rs.getInt(1) == 1;
1273 } catch (SQLException ex) {
1274 restartConnection(ex);
1275 return isArticleExisting(messageID);
1280 } catch (SQLException ex) {
1281 ex.printStackTrace();
1288 * Checks if there is a group with the given name in the JDBCDatabase.
1291 * @throws StorageBackendException
1294 public boolean isGroupExisting(String name)
1295 throws StorageBackendException
1297 ResultSet rs = null;
1300 this.pstmtIsGroupExisting.setString(1, name);
1301 rs = this.pstmtIsGroupExisting.executeQuery();
1303 } catch (SQLException ex) {
1304 restartConnection(ex);
1305 return isGroupExisting(name);
1310 } catch (SQLException ex) {
1311 ex.printStackTrace();
1318 public void setConfigValue(String key, String value)
1319 throws StorageBackendException
1322 conn.setAutoCommit(false);
1323 this.pstmtSetConfigValue0.setString(1, key);
1324 this.pstmtSetConfigValue0.execute();
1325 this.pstmtSetConfigValue1.setString(1, key);
1326 this.pstmtSetConfigValue1.setString(2, value);
1327 this.pstmtSetConfigValue1.execute();
1329 conn.setAutoCommit(true);
1330 } catch (SQLException ex) {
1331 restartConnection(ex);
1332 setConfigValue(key, value);
1337 * Closes the JDBCDatabase connection.
1339 public void shutdown()
1340 throws StorageBackendException
1343 if (this.conn != null) {
1346 } catch (SQLException ex) {
1347 throw new StorageBackendException(ex);
1352 public void purgeGroup(Group group)
1353 throws StorageBackendException
1356 this.pstmtPurgeGroup0.setLong(1, group.getInternalID());
1357 this.pstmtPurgeGroup0.executeUpdate();
1359 this.pstmtPurgeGroup1.setLong(1, group.getInternalID());
1360 this.pstmtPurgeGroup1.executeUpdate();
1361 } catch (SQLException ex) {
1362 restartConnection(ex);
1367 private void restartConnection(SQLException cause)
1368 throws StorageBackendException
1371 Log.get().severe(Thread.currentThread()
1372 + ": Database connection was closed (restart " + restarts + ").");
1374 if (restarts >= MAX_RESTARTS) {
1375 // Delete the current, probably broken JDBCDatabase instance.
1376 // So no one can use the instance any more.
1377 JDBCDatabaseProvider.instances.remove(Thread.currentThread());
1379 // Throw the exception upwards
1380 throw new StorageBackendException(cause);
1384 Thread.sleep(1500L * restarts);
1385 } catch (InterruptedException ex) {
1386 Log.get().warning("Interrupted: " + ex.getMessage());
1389 // Try to properly close the old database connection
1391 if (this.conn != null) {
1394 } catch (SQLException ex) {
1395 Log.get().warning(ex.getMessage());
1399 // Try to reinitialize database connection
1401 } catch (SQLException ex) {
1402 Log.get().warning(ex.getMessage());
1403 restartConnection(ex);
1408 public boolean update(Article article)
1409 throws StorageBackendException
1411 ResultSet rs = null;
1413 // Retrieve internal article_id
1414 this.pstmtGetArticle0.setString(1, article.getMessageID());
1415 rs = this.pstmtGetArticle0.executeQuery();
1416 int articleID = rs.getInt("article_id");
1418 delete(article.getMessageID());
1420 this.conn.setAutoCommit(false);
1421 addArticle(article, articleID);
1423 this.conn.setAutoCommit(true);
1425 } catch (SQLException ex) {
1427 this.conn.rollback();
1428 } catch(SQLException ex2) {
1429 Log.get().severe("Rollback failed: " + ex2.getMessage());
1431 restartConnection(ex);
1432 return update(article);
1437 * Writes the flags and the name of the given group to the database.
1439 * @throws StorageBackendException
1442 public boolean update(Group group)
1443 throws StorageBackendException
1446 this.pstmtUpdateGroup.setInt(1, group.getFlags());
1447 this.pstmtUpdateGroup.setString(2, group.getName());
1448 this.pstmtUpdateGroup.setLong(3, group.getInternalID());
1449 int rs = this.pstmtUpdateGroup.executeUpdate();
1451 } catch (SQLException ex) {
1452 restartConnection(ex);
1453 return update(group);