Switch intent style to Original K&R / Linux / Kernel.
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;
309 // Fill prepared statement with values;
310 // writes body to article table
311 pstmtAddArticle1.setInt(1, newArticleID);
312 pstmtAddArticle1.setBytes(2, article.getBody());
313 pstmtAddArticle1.execute();
316 Enumeration headers = article.getAllHeaders();
317 for (int n = 0; headers.hasMoreElements(); n++) {
318 Header header = (Header) headers.nextElement();
319 pstmtAddArticle2.setInt(1, newArticleID);
320 pstmtAddArticle2.setString(2, header.getName().toLowerCase());
321 pstmtAddArticle2.setString(3,
322 header.getValue().replaceAll("[\r\n]", ""));
323 pstmtAddArticle2.setInt(4, n);
324 pstmtAddArticle2.execute();
327 // For each newsgroup add a reference
328 List<Group> groups = article.getGroups();
329 for (Group group : groups) {
330 pstmtAddArticle3.setLong(1, group.getInternalID());
331 pstmtAddArticle3.setInt(2, newArticleID);
332 pstmtAddArticle3.setLong(3, getMaxArticleIndex(group.getInternalID()) + 1);
333 pstmtAddArticle3.execute();
336 // Write message-id to article_ids table
337 this.pstmtAddArticle4.setInt(1, newArticleID);
338 this.pstmtAddArticle4.setString(2, article.getMessageID());
339 this.pstmtAddArticle4.execute();
342 this.conn.setAutoCommit(true);
344 this.restarts = 0; // Reset error count
345 } catch (SQLException ex) {
347 this.conn.rollback(); // Rollback changes
348 } catch (SQLException ex2) {
349 Log.get().severe("Rollback of addArticle() failed: " + ex2);
353 this.conn.setAutoCommit(true); // and release locks
354 } catch (SQLException ex2) {
355 Log.get().severe("setAutoCommit(true) of addArticle() failed: " + ex2);
358 restartConnection(ex);
364 * Adds a group to the JDBCDatabase. This method is not accessible via NNTP.
366 * @throws java.sql.SQLException
369 public void addGroup(String name, int flags)
370 throws StorageBackendException
373 this.conn.setAutoCommit(false);
374 pstmtAddGroup0.setString(1, name);
375 pstmtAddGroup0.setInt(2, flags);
377 pstmtAddGroup0.executeUpdate();
379 this.conn.setAutoCommit(true);
380 this.restarts = 0; // Reset error count
381 } catch (SQLException ex) {
383 this.conn.rollback();
384 this.conn.setAutoCommit(true);
385 } catch (SQLException ex2) {
386 ex2.printStackTrace();
389 restartConnection(ex);
390 addGroup(name, flags);
395 public void addEvent(long time, int type, long gid)
396 throws StorageBackendException
399 this.conn.setAutoCommit(false);
400 this.pstmtAddEvent.setLong(1, time);
401 this.pstmtAddEvent.setInt(2, type);
402 this.pstmtAddEvent.setLong(3, gid);
403 this.pstmtAddEvent.executeUpdate();
405 this.conn.setAutoCommit(true);
407 } catch (SQLException ex) {
409 this.conn.rollback();
410 this.conn.setAutoCommit(true);
411 } catch (SQLException ex2) {
412 ex2.printStackTrace();
415 restartConnection(ex);
416 addEvent(time, type, gid);
421 public int countArticles()
422 throws StorageBackendException
427 rs = this.pstmtCountArticles.executeQuery();
433 } catch (SQLException ex) {
434 restartConnection(ex);
435 return countArticles();
440 } catch (SQLException ex) {
441 ex.printStackTrace();
449 public int countGroups()
450 throws StorageBackendException
455 rs = this.pstmtCountGroups.executeQuery();
461 } catch (SQLException ex) {
462 restartConnection(ex);
463 return countGroups();
468 } catch (SQLException ex) {
469 ex.printStackTrace();
477 public void delete(final String messageID)
478 throws StorageBackendException
481 this.conn.setAutoCommit(false);
483 this.pstmtDeleteArticle0.setString(1, messageID);
484 int rs = this.pstmtDeleteArticle0.executeUpdate();
486 // We do not trust the ON DELETE CASCADE functionality to delete
487 // orphaned references...
488 this.pstmtDeleteArticle1.setString(1, messageID);
489 rs = this.pstmtDeleteArticle1.executeUpdate();
491 this.pstmtDeleteArticle2.setString(1, messageID);
492 rs = this.pstmtDeleteArticle2.executeUpdate();
494 this.pstmtDeleteArticle3.setString(1, messageID);
495 rs = this.pstmtDeleteArticle3.executeUpdate();
498 this.conn.setAutoCommit(true);
499 } catch (SQLException ex) {
500 throw new StorageBackendException(ex);
505 public Article getArticle(String messageID)
506 throws StorageBackendException
510 pstmtGetArticle0.setString(1, messageID);
511 rs = pstmtGetArticle0.executeQuery();
516 byte[] body = rs.getBytes("body");
517 String headers = getArticleHeaders(rs.getInt("article_id"));
518 return new Article(headers, body);
520 } catch (SQLException ex) {
521 restartConnection(ex);
522 return getArticle(messageID);
527 } catch (SQLException ex) {
528 ex.printStackTrace();
530 restarts = 0; // Reset error count
536 * Retrieves an article by its ID.
539 * @throws StorageBackendException
542 public Article getArticle(long articleIndex, long gid)
543 throws StorageBackendException
548 this.pstmtGetArticle1.setLong(1, articleIndex);
549 this.pstmtGetArticle1.setLong(2, gid);
551 rs = this.pstmtGetArticle1.executeQuery();
554 byte[] body = rs.getBytes("body");
555 String headers = getArticleHeaders(rs.getInt("article_id"));
556 return new Article(headers, body);
560 } catch (SQLException ex) {
561 restartConnection(ex);
562 return getArticle(articleIndex, gid);
567 } catch (SQLException ex) {
568 ex.printStackTrace();
576 * Searches for fitting header values using the given regular expression.
583 * @throws StorageBackendException
586 public List<Pair<Long, String>> getArticleHeaders(Channel group, long start,
587 long end, String headerKey, String patStr)
588 throws StorageBackendException, PatternSyntaxException
591 List<Pair<Long, String>> heads = new ArrayList<Pair<Long, String>>();
594 this.pstmtGetArticleHeaders1.setString(1, group.getName());
595 this.pstmtGetArticleHeaders1.setString(2, headerKey);
596 this.pstmtGetArticleHeaders1.setLong(3, start);
598 rs = this.pstmtGetArticleHeaders1.executeQuery();
600 // Convert the "NNTP" regex to Java regex
601 patStr = patStr.replace("*", ".*");
602 Pattern pattern = Pattern.compile(patStr);
605 Long articleIndex = rs.getLong(1);
606 if (end < 0 || articleIndex <= end) // Match start is done via SQL
608 String headerValue = rs.getString(2);
609 Matcher matcher = pattern.matcher(headerValue);
610 if (matcher.matches()) {
611 heads.add(new Pair<Long, String>(articleIndex, headerValue));
615 } catch (SQLException ex) {
616 restartConnection(ex);
617 return getArticleHeaders(group, start, end, headerKey, patStr);
622 } catch (SQLException ex) {
623 ex.printStackTrace();
631 private String getArticleHeaders(long articleID)
632 throws StorageBackendException
637 this.pstmtGetArticleHeaders0.setLong(1, articleID);
638 rs = this.pstmtGetArticleHeaders0.executeQuery();
640 StringBuilder buf = new StringBuilder();
643 buf.append(rs.getString(1)); // key
645 String foldedValue = MimeUtility.fold(0, rs.getString(2));
646 buf.append(foldedValue); // value
655 return buf.toString();
656 } catch (SQLException ex) {
657 restartConnection(ex);
658 return getArticleHeaders(articleID);
663 } catch (SQLException ex) {
664 ex.printStackTrace();
671 public long getArticleIndex(Article article, Group group)
672 throws StorageBackendException
677 this.pstmtGetArticleIndex.setString(1, article.getMessageID());
678 this.pstmtGetArticleIndex.setLong(2, group.getInternalID());
680 rs = this.pstmtGetArticleIndex.executeQuery();
682 return rs.getLong(1);
686 } catch (SQLException ex) {
687 restartConnection(ex);
688 return getArticleIndex(article, group);
693 } catch (SQLException ex) {
694 ex.printStackTrace();
701 * Returns a list of Long/Article Pairs.
702 * @throws java.sql.SQLException
705 public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, long first,
707 throws StorageBackendException
712 this.pstmtGetArticleHeads.setLong(1, group.getInternalID());
713 this.pstmtGetArticleHeads.setLong(2, first);
714 this.pstmtGetArticleHeads.setLong(3, last);
715 rs = pstmtGetArticleHeads.executeQuery();
717 List<Pair<Long, ArticleHead>> articles = new ArrayList<Pair<Long, ArticleHead>>();
720 long aid = rs.getLong("article_id");
721 long aidx = rs.getLong("article_index");
722 String headers = getArticleHeaders(aid);
723 articles.add(new Pair<Long, ArticleHead>(aidx,
724 new ArticleHead(headers)));
728 } catch (SQLException ex) {
729 restartConnection(ex);
730 return getArticleHeads(group, first, last);
735 } catch (SQLException ex) {
736 ex.printStackTrace();
743 public List<Long> getArticleNumbers(long gid)
744 throws StorageBackendException
748 List<Long> ids = new ArrayList<Long>();
749 this.pstmtGetArticleIDs.setLong(1, gid);
750 rs = this.pstmtGetArticleIDs.executeQuery();
752 ids.add(rs.getLong(1));
755 } catch (SQLException ex) {
756 restartConnection(ex);
757 return getArticleNumbers(gid);
762 restarts = 0; // Clear the restart count after successful request
763 } catch (SQLException ex) {
764 ex.printStackTrace();
771 public String getConfigValue(String key)
772 throws StorageBackendException
776 this.pstmtGetConfigValue.setString(1, key);
778 rs = this.pstmtGetConfigValue.executeQuery();
780 return rs.getString(1); // First data on index 1 not 0
784 } catch (SQLException ex) {
785 restartConnection(ex);
786 return getConfigValue(key);
791 } catch (SQLException ex) {
792 ex.printStackTrace();
794 restarts = 0; // Clear the restart count after successful request
800 public int getEventsCount(int type, long start, long end, Channel channel)
801 throws StorageBackendException
806 if (channel == null) {
807 this.pstmtGetEventsCount0.setInt(1, type);
808 this.pstmtGetEventsCount0.setLong(2, start);
809 this.pstmtGetEventsCount0.setLong(3, end);
810 rs = this.pstmtGetEventsCount0.executeQuery();
812 this.pstmtGetEventsCount1.setInt(1, type);
813 this.pstmtGetEventsCount1.setLong(2, start);
814 this.pstmtGetEventsCount1.setLong(3, end);
815 this.pstmtGetEventsCount1.setLong(4, channel.getInternalID());
816 rs = this.pstmtGetEventsCount1.executeQuery();
824 } catch (SQLException ex) {
825 restartConnection(ex);
826 return getEventsCount(type, start, end, channel);
831 } catch (SQLException ex) {
832 ex.printStackTrace();
839 * Reads all Groups from the JDBCDatabase.
841 * @throws StorageBackendException
844 public List<Channel> getGroups()
845 throws StorageBackendException
848 List<Channel> buffer = new ArrayList<Channel>();
849 Statement stmt = null;
852 stmt = conn.createStatement();
853 rs = stmt.executeQuery("SELECT * FROM groups ORDER BY name");
856 String name = rs.getString("name");
857 long id = rs.getLong("group_id");
858 int flags = rs.getInt("flags");
860 Group group = new Group(name, id, flags);
865 } catch (SQLException ex) {
866 restartConnection(ex);
871 stmt.close(); // Implicitely closes ResultSets
872 } catch (SQLException ex) {
873 ex.printStackTrace();
880 public List<String> getGroupsForList(String listAddress)
881 throws StorageBackendException
886 this.pstmtGetGroupForList.setString(1, listAddress);
888 rs = this.pstmtGetGroupForList.executeQuery();
889 List<String> groups = new ArrayList<String>();
891 String group = rs.getString(1);
895 } catch (SQLException ex) {
896 restartConnection(ex);
897 return getGroupsForList(listAddress);
902 } catch (SQLException ex) {
903 ex.printStackTrace();
910 * Returns the Group that is identified by the name.
913 * @throws StorageBackendException
916 public Group getGroup(String name)
917 throws StorageBackendException
922 this.pstmtGetGroup0.setString(1, name);
923 rs = this.pstmtGetGroup0.executeQuery();
928 long id = rs.getLong("group_id");
929 int flags = rs.getInt("flags");
930 return new Group(name, id, flags);
932 } catch (SQLException ex) {
933 restartConnection(ex);
934 return getGroup(name);
939 } catch (SQLException ex) {
940 ex.printStackTrace();
947 public List<String> getListsForGroup(String group)
948 throws StorageBackendException
951 List<String> lists = new ArrayList<String>();
954 this.pstmtGetListForGroup.setString(1, group);
955 rs = this.pstmtGetListForGroup.executeQuery();
958 lists.add(rs.getString(1));
961 } catch (SQLException ex) {
962 restartConnection(ex);
963 return getListsForGroup(group);
968 } catch (SQLException ex) {
969 ex.printStackTrace();
975 private int getMaxArticleIndex(long groupID)
976 throws StorageBackendException
981 this.pstmtGetMaxArticleIndex.setLong(1, groupID);
982 rs = this.pstmtGetMaxArticleIndex.executeQuery();
986 maxIndex = rs.getInt(1);
990 } catch (SQLException ex) {
991 restartConnection(ex);
992 return getMaxArticleIndex(groupID);
997 } catch (SQLException ex) {
998 ex.printStackTrace();
1004 private int getMaxArticleID()
1005 throws StorageBackendException
1007 ResultSet rs = null;
1010 rs = this.pstmtGetMaxArticleID.executeQuery();
1014 maxIndex = rs.getInt(1);
1018 } catch (SQLException ex) {
1019 restartConnection(ex);
1020 return getMaxArticleID();
1025 } catch (SQLException ex) {
1026 ex.printStackTrace();
1033 public int getLastArticleNumber(Group group)
1034 throws StorageBackendException
1036 ResultSet rs = null;
1039 this.pstmtGetLastArticleNumber.setLong(1, group.getInternalID());
1040 rs = this.pstmtGetLastArticleNumber.executeQuery();
1042 return rs.getInt(1);
1046 } catch (SQLException ex) {
1047 restartConnection(ex);
1048 return getLastArticleNumber(group);
1053 } catch (SQLException ex) {
1054 ex.printStackTrace();
1061 public int getFirstArticleNumber(Group group)
1062 throws StorageBackendException
1064 ResultSet rs = null;
1066 this.pstmtGetFirstArticleNumber.setLong(1, group.getInternalID());
1067 rs = this.pstmtGetFirstArticleNumber.executeQuery();
1069 return rs.getInt(1);
1073 } catch (SQLException ex) {
1074 restartConnection(ex);
1075 return getFirstArticleNumber(group);
1080 } catch (SQLException ex) {
1081 ex.printStackTrace();
1088 * Returns a group name identified by the given id.
1091 * @throws StorageBackendException
1093 public String getGroup(int id)
1094 throws StorageBackendException
1096 ResultSet rs = null;
1099 this.pstmtGetGroup1.setInt(1, id);
1100 rs = this.pstmtGetGroup1.executeQuery();
1103 return rs.getString(1);
1107 } catch (SQLException ex) {
1108 restartConnection(ex);
1109 return getGroup(id);
1114 } catch (SQLException ex) {
1115 ex.printStackTrace();
1122 public double getEventsPerHour(int key, long gid)
1123 throws StorageBackendException
1125 String gidquery = "";
1127 gidquery = " AND group_id = " + gid;
1130 Statement stmt = null;
1131 ResultSet rs = null;
1134 stmt = this.conn.createStatement();
1135 rs = stmt.executeQuery("SELECT Count(*) / (Max(event_time) - Min(event_time))"
1136 + " * 1000 * 60 * 60 FROM events WHERE event_key = " + key + gidquery);
1139 restarts = 0; // reset error count
1140 return rs.getDouble(1);
1144 } catch (SQLException ex) {
1145 restartConnection(ex);
1146 return getEventsPerHour(key, gid);
1150 stmt.close(); // Implicitely closes the result sets
1152 } catch (SQLException ex) {
1153 ex.printStackTrace();
1159 public String getOldestArticle()
1160 throws StorageBackendException
1162 ResultSet rs = null;
1165 rs = this.pstmtGetOldestArticle.executeQuery();
1167 return rs.getString(1);
1171 } catch (SQLException ex) {
1172 restartConnection(ex);
1173 return getOldestArticle();
1178 } catch (SQLException ex) {
1179 ex.printStackTrace();
1186 public int getPostingsCount(String groupname)
1187 throws StorageBackendException
1189 ResultSet rs = null;
1192 this.pstmtGetPostingsCount.setString(1, groupname);
1193 rs = this.pstmtGetPostingsCount.executeQuery();
1195 return rs.getInt(1);
1197 Log.get().warning("Count on postings return nothing!");
1200 } catch (SQLException ex) {
1201 restartConnection(ex);
1202 return getPostingsCount(groupname);
1207 } catch (SQLException ex) {
1208 ex.printStackTrace();
1215 public List<Subscription> getSubscriptions(int feedtype)
1216 throws StorageBackendException
1218 ResultSet rs = null;
1221 List<Subscription> subs = new ArrayList<Subscription>();
1222 this.pstmtGetSubscriptions.setInt(1, feedtype);
1223 rs = this.pstmtGetSubscriptions.executeQuery();
1226 String host = rs.getString("host");
1227 String group = rs.getString("name");
1228 int port = rs.getInt("port");
1229 subs.add(new Subscription(host, port, feedtype, group));
1233 } catch (SQLException ex) {
1234 restartConnection(ex);
1235 return getSubscriptions(feedtype);
1240 } catch (SQLException ex) {
1241 ex.printStackTrace();
1248 * Checks if there is an article with the given messageid in the JDBCDatabase.
1251 * @throws StorageBackendException
1254 public boolean isArticleExisting(String messageID)
1255 throws StorageBackendException
1257 ResultSet rs = null;
1260 this.pstmtIsArticleExisting.setString(1, messageID);
1261 rs = this.pstmtIsArticleExisting.executeQuery();
1262 return rs.next() && rs.getInt(1) == 1;
1263 } catch (SQLException ex) {
1264 restartConnection(ex);
1265 return isArticleExisting(messageID);
1270 } catch (SQLException ex) {
1271 ex.printStackTrace();
1278 * Checks if there is a group with the given name in the JDBCDatabase.
1281 * @throws StorageBackendException
1284 public boolean isGroupExisting(String name)
1285 throws StorageBackendException
1287 ResultSet rs = null;
1290 this.pstmtIsGroupExisting.setString(1, name);
1291 rs = this.pstmtIsGroupExisting.executeQuery();
1293 } catch (SQLException ex) {
1294 restartConnection(ex);
1295 return isGroupExisting(name);
1300 } catch (SQLException ex) {
1301 ex.printStackTrace();
1308 public void setConfigValue(String key, String value)
1309 throws StorageBackendException
1312 conn.setAutoCommit(false);
1313 this.pstmtSetConfigValue0.setString(1, key);
1314 this.pstmtSetConfigValue0.execute();
1315 this.pstmtSetConfigValue1.setString(1, key);
1316 this.pstmtSetConfigValue1.setString(2, value);
1317 this.pstmtSetConfigValue1.execute();
1319 conn.setAutoCommit(true);
1320 } catch (SQLException ex) {
1321 restartConnection(ex);
1322 setConfigValue(key, value);
1327 * Closes the JDBCDatabase connection.
1329 public void shutdown()
1330 throws StorageBackendException
1333 if (this.conn != null) {
1336 } catch (SQLException ex) {
1337 throw new StorageBackendException(ex);
1342 public void purgeGroup(Group group)
1343 throws StorageBackendException
1346 this.pstmtPurgeGroup0.setLong(1, group.getInternalID());
1347 this.pstmtPurgeGroup0.executeUpdate();
1349 this.pstmtPurgeGroup1.setLong(1, group.getInternalID());
1350 this.pstmtPurgeGroup1.executeUpdate();
1351 } catch (SQLException ex) {
1352 restartConnection(ex);
1357 private void restartConnection(SQLException cause)
1358 throws StorageBackendException
1361 Log.get().severe(Thread.currentThread()
1362 + ": Database connection was closed (restart " + restarts + ").");
1364 if (restarts >= MAX_RESTARTS) {
1365 // Delete the current, probably broken JDBCDatabase instance.
1366 // So no one can use the instance any more.
1367 JDBCDatabaseProvider.instances.remove(Thread.currentThread());
1369 // Throw the exception upwards
1370 throw new StorageBackendException(cause);
1374 Thread.sleep(1500L * restarts);
1375 } catch (InterruptedException ex) {
1376 Log.get().warning("Interrupted: " + ex.getMessage());
1379 // Try to properly close the old database connection
1381 if (this.conn != null) {
1384 } catch (SQLException ex) {
1385 Log.get().warning(ex.getMessage());
1389 // Try to reinitialize database connection
1391 } catch (SQLException ex) {
1392 Log.get().warning(ex.getMessage());
1393 restartConnection(ex);
1398 public boolean update(Article article)
1399 throws StorageBackendException
1401 // DELETE FROM headers WHERE article_id = ?
1403 // INSERT INTO headers ...
1405 // SELECT * FROM postings WHERE article_id = ? AND group_id = ?
1410 * Writes the flags and the name of the given group to the database.
1412 * @throws StorageBackendException
1415 public boolean update(Group group)
1416 throws StorageBackendException
1419 this.pstmtUpdateGroup.setInt(1, group.getFlags());
1420 this.pstmtUpdateGroup.setString(2, group.getName());
1421 this.pstmtUpdateGroup.setLong(3, group.getInternalID());
1422 int rs = this.pstmtUpdateGroup.executeUpdate();
1424 } catch (SQLException ex) {
1425 restartConnection(ex);
1426 return update(group);