src/org/sonews/storage/impl/JDBCDatabase.java
author cli
Sun, 29 Aug 2010 18:17:37 +0200
changeset 37 74139325d305
parent 35 ed84c8bdd87b
child 38 fdfc7225f799
permissions -rw-r--r--
Switch intent style to Original K&R / Linux / Kernel.
chris@3
     1
/*
chris@3
     2
 *   SONEWS News Server
chris@3
     3
 *   see AUTHORS for the list of contributors
chris@3
     4
 *
chris@3
     5
 *   This program is free software: you can redistribute it and/or modify
chris@3
     6
 *   it under the terms of the GNU General Public License as published by
chris@3
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@3
     8
 *   (at your option) any later version.
chris@3
     9
 *
chris@3
    10
 *   This program is distributed in the hope that it will be useful,
chris@3
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3
    13
 *   GNU General Public License for more details.
chris@3
    14
 *
chris@3
    15
 *   You should have received a copy of the GNU General Public License
chris@3
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3
    17
 */
chris@3
    18
chris@3
    19
package org.sonews.storage.impl;
chris@3
    20
chris@3
    21
import java.sql.Connection;
chris@3
    22
import java.sql.DriverManager;
chris@3
    23
import java.sql.ResultSet;
chris@3
    24
import java.sql.SQLException;
chris@3
    25
import java.sql.Statement;
chris@3
    26
import java.sql.PreparedStatement;
chris@3
    27
import java.util.ArrayList;
chris@3
    28
import java.util.Enumeration;
chris@3
    29
import java.util.List;
chris@3
    30
import java.util.regex.Matcher;
chris@3
    31
import java.util.regex.Pattern;
chris@3
    32
import java.util.regex.PatternSyntaxException;
chris@3
    33
import javax.mail.Header;
chris@3
    34
import javax.mail.internet.MimeUtility;
chris@3
    35
import org.sonews.config.Config;
chris@3
    36
import org.sonews.util.Log;
chris@3
    37
import org.sonews.feed.Subscription;
chris@3
    38
import org.sonews.storage.Article;
chris@3
    39
import org.sonews.storage.ArticleHead;
chris@3
    40
import org.sonews.storage.Channel;
chris@3
    41
import org.sonews.storage.Group;
chris@3
    42
import org.sonews.storage.Storage;
chris@3
    43
import org.sonews.storage.StorageBackendException;
chris@3
    44
import org.sonews.util.Pair;
chris@3
    45
chris@3
    46
/**
chris@3
    47
 * JDBCDatabase facade class.
chris@3
    48
 * @author Christian Lins
chris@3
    49
 * @since sonews/0.5.0
chris@3
    50
 */
chris@3
    51
// TODO: Refactor this class to reduce size (e.g. ArticleDatabase GroupDatabase)
chris@3
    52
public class JDBCDatabase implements Storage
chris@3
    53
{
chris@3
    54
cli@37
    55
	public static final int MAX_RESTARTS = 2;
cli@37
    56
	private Connection conn = null;
cli@37
    57
	private PreparedStatement pstmtAddArticle1 = null;
cli@37
    58
	private PreparedStatement pstmtAddArticle2 = null;
cli@37
    59
	private PreparedStatement pstmtAddArticle3 = null;
cli@37
    60
	private PreparedStatement pstmtAddArticle4 = null;
cli@37
    61
	private PreparedStatement pstmtAddGroup0 = null;
cli@37
    62
	private PreparedStatement pstmtAddEvent = null;
cli@37
    63
	private PreparedStatement pstmtCountArticles = null;
cli@37
    64
	private PreparedStatement pstmtCountGroups = null;
cli@37
    65
	private PreparedStatement pstmtDeleteArticle0 = null;
cli@37
    66
	private PreparedStatement pstmtDeleteArticle1 = null;
cli@37
    67
	private PreparedStatement pstmtDeleteArticle2 = null;
cli@37
    68
	private PreparedStatement pstmtDeleteArticle3 = null;
cli@37
    69
	private PreparedStatement pstmtGetArticle0 = null;
cli@37
    70
	private PreparedStatement pstmtGetArticle1 = null;
cli@37
    71
	private PreparedStatement pstmtGetArticleHeaders0 = null;
cli@37
    72
	private PreparedStatement pstmtGetArticleHeaders1 = null;
cli@37
    73
	private PreparedStatement pstmtGetArticleHeads = null;
cli@37
    74
	private PreparedStatement pstmtGetArticleIDs = null;
cli@37
    75
	private PreparedStatement pstmtGetArticleIndex = null;
cli@37
    76
	private PreparedStatement pstmtGetConfigValue = null;
cli@37
    77
	private PreparedStatement pstmtGetEventsCount0 = null;
cli@37
    78
	private PreparedStatement pstmtGetEventsCount1 = null;
cli@37
    79
	private PreparedStatement pstmtGetGroupForList = null;
cli@37
    80
	private PreparedStatement pstmtGetGroup0 = null;
cli@37
    81
	private PreparedStatement pstmtGetGroup1 = null;
cli@37
    82
	private PreparedStatement pstmtGetFirstArticleNumber = null;
cli@37
    83
	private PreparedStatement pstmtGetListForGroup = null;
cli@37
    84
	private PreparedStatement pstmtGetLastArticleNumber = null;
cli@37
    85
	private PreparedStatement pstmtGetMaxArticleID = null;
cli@37
    86
	private PreparedStatement pstmtGetMaxArticleIndex = null;
cli@37
    87
	private PreparedStatement pstmtGetOldestArticle = null;
cli@37
    88
	private PreparedStatement pstmtGetPostingsCount = null;
cli@37
    89
	private PreparedStatement pstmtGetSubscriptions = null;
cli@37
    90
	private PreparedStatement pstmtIsArticleExisting = null;
cli@37
    91
	private PreparedStatement pstmtIsGroupExisting = null;
cli@37
    92
	private PreparedStatement pstmtPurgeGroup0 = null;
cli@37
    93
	private PreparedStatement pstmtPurgeGroup1 = null;
cli@37
    94
	private PreparedStatement pstmtSetConfigValue0 = null;
cli@37
    95
	private PreparedStatement pstmtSetConfigValue1 = null;
cli@37
    96
	private PreparedStatement pstmtUpdateGroup = null;
cli@37
    97
	/** How many times the database connection was reinitialized */
cli@37
    98
	private int restarts = 0;
chris@3
    99
cli@37
   100
	/**
cli@37
   101
	 * Rises the database: reconnect and recreate all prepared statements.
cli@37
   102
	 * @throws java.lang.SQLException
cli@37
   103
	 */
cli@37
   104
	protected void arise()
cli@37
   105
		throws SQLException
cli@37
   106
	{
cli@37
   107
		try {
cli@37
   108
			// Load database driver
cli@37
   109
			Class.forName(
cli@37
   110
				Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DBMSDRIVER, "java.lang.Object"));
chris@3
   111
cli@37
   112
			// Establish database connection
cli@37
   113
			this.conn = DriverManager.getConnection(
cli@37
   114
				Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_DATABASE, "<not specified>"),
cli@37
   115
				Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_USER, "root"),
cli@37
   116
				Config.inst().get(Config.LEVEL_FILE, Config.STORAGE_PASSWORD, ""));
chris@3
   117
cli@37
   118
			this.conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
cli@37
   119
			if (this.conn.getTransactionIsolation() != Connection.TRANSACTION_SERIALIZABLE) {
cli@37
   120
				Log.get().warning("Database is NOT fully serializable!");
cli@37
   121
			}
chris@3
   122
cli@37
   123
			// Prepare statements for method addArticle()
cli@37
   124
			this.pstmtAddArticle1 = conn.prepareStatement(
cli@37
   125
				"INSERT INTO articles (article_id, body) VALUES(?, ?)");
cli@37
   126
			this.pstmtAddArticle2 = conn.prepareStatement(
cli@37
   127
				"INSERT INTO headers (article_id, header_key, header_value, header_index) "
cli@37
   128
				+ "VALUES (?, ?, ?, ?)");
cli@37
   129
			this.pstmtAddArticle3 = conn.prepareStatement(
cli@37
   130
				"INSERT INTO postings (group_id, article_id, article_index)"
cli@37
   131
				+ "VALUES (?, ?, ?)");
cli@37
   132
			this.pstmtAddArticle4 = conn.prepareStatement(
cli@37
   133
				"INSERT INTO article_ids (article_id, message_id) VALUES (?, ?)");
chris@3
   134
cli@37
   135
			// Prepare statement for method addStatValue()
cli@37
   136
			this.pstmtAddEvent = conn.prepareStatement(
cli@37
   137
				"INSERT INTO events VALUES (?, ?, ?)");
chris@3
   138
cli@37
   139
			// Prepare statement for method addGroup()
cli@37
   140
			this.pstmtAddGroup0 = conn.prepareStatement(
cli@37
   141
				"INSERT INTO groups (name, flags) VALUES (?, ?)");
chris@3
   142
cli@37
   143
			// Prepare statement for method countArticles()
cli@37
   144
			this.pstmtCountArticles = conn.prepareStatement(
cli@37
   145
				"SELECT Count(article_id) FROM article_ids");
chris@3
   146
cli@37
   147
			// Prepare statement for method countGroups()
cli@37
   148
			this.pstmtCountGroups = conn.prepareStatement(
cli@37
   149
				"SELECT Count(group_id) FROM groups WHERE "
cli@37
   150
				+ "flags & " + Channel.DELETED + " = 0");
chris@3
   151
cli@37
   152
			// Prepare statements for method delete(article)
cli@37
   153
			this.pstmtDeleteArticle0 = conn.prepareStatement(
cli@37
   154
				"DELETE FROM articles WHERE article_id = "
cli@37
   155
				+ "(SELECT article_id FROM article_ids WHERE message_id = ?)");
cli@37
   156
			this.pstmtDeleteArticle1 = conn.prepareStatement(
cli@37
   157
				"DELETE FROM headers WHERE article_id = "
cli@37
   158
				+ "(SELECT article_id FROM article_ids WHERE message_id = ?)");
cli@37
   159
			this.pstmtDeleteArticle2 = conn.prepareStatement(
cli@37
   160
				"DELETE FROM postings WHERE article_id = "
cli@37
   161
				+ "(SELECT article_id FROM article_ids WHERE message_id = ?)");
cli@37
   162
			this.pstmtDeleteArticle3 = conn.prepareStatement(
cli@37
   163
				"DELETE FROM article_ids WHERE message_id = ?");
chris@3
   164
cli@37
   165
			// Prepare statements for methods getArticle()
cli@37
   166
			this.pstmtGetArticle0 = conn.prepareStatement(
cli@37
   167
				"SELECT * FROM articles  WHERE article_id = "
cli@37
   168
				+ "(SELECT article_id FROM article_ids WHERE message_id = ?)");
cli@37
   169
			this.pstmtGetArticle1 = conn.prepareStatement(
cli@37
   170
				"SELECT * FROM articles WHERE article_id = "
cli@37
   171
				+ "(SELECT article_id FROM postings WHERE "
cli@37
   172
				+ "article_index = ? AND group_id = ?)");
chris@3
   173
cli@37
   174
			// Prepare statement for method getArticleHeaders()
cli@37
   175
			this.pstmtGetArticleHeaders0 = conn.prepareStatement(
cli@37
   176
				"SELECT header_key, header_value FROM headers WHERE article_id = ? "
cli@37
   177
				+ "ORDER BY header_index ASC");
chris@3
   178
cli@37
   179
			// Prepare statement for method getArticleHeaders(regular expr pattern)
cli@37
   180
			this.pstmtGetArticleHeaders1 = conn.prepareStatement(
cli@37
   181
				"SELECT p.article_index, h.header_value FROM headers h "
cli@37
   182
				+ "INNER JOIN postings p ON h.article_id = p.article_id "
cli@37
   183
				+ "INNER JOIN groups g ON p.group_id = g.group_id "
cli@37
   184
				+ "WHERE g.name          =  ? AND "
cli@37
   185
				+ "h.header_key    =  ? AND "
cli@37
   186
				+ "p.article_index >= ? "
cli@37
   187
				+ "ORDER BY p.article_index ASC");
chris@3
   188
cli@37
   189
			this.pstmtGetArticleIDs = conn.prepareStatement(
cli@37
   190
				"SELECT article_index FROM postings WHERE group_id = ?");
chris@3
   191
cli@37
   192
			// Prepare statement for method getArticleIndex
cli@37
   193
			this.pstmtGetArticleIndex = conn.prepareStatement(
cli@37
   194
				"SELECT article_index FROM postings WHERE "
cli@37
   195
				+ "article_id = (SELECT article_id FROM article_ids "
cli@37
   196
				+ "WHERE message_id = ?) "
cli@37
   197
				+ " AND group_id = ?");
chris@3
   198
cli@37
   199
			// Prepare statements for method getArticleHeads()
cli@37
   200
			this.pstmtGetArticleHeads = conn.prepareStatement(
cli@37
   201
				"SELECT article_id, article_index FROM postings WHERE "
cli@37
   202
				+ "postings.group_id = ? AND article_index >= ? AND "
cli@37
   203
				+ "article_index <= ?");
chris@3
   204
cli@37
   205
			// Prepare statements for method getConfigValue()
cli@37
   206
			this.pstmtGetConfigValue = conn.prepareStatement(
cli@37
   207
				"SELECT config_value FROM config WHERE config_key = ?");
chris@3
   208
cli@37
   209
			// Prepare statements for method getEventsCount()
cli@37
   210
			this.pstmtGetEventsCount0 = conn.prepareStatement(
cli@37
   211
				"SELECT Count(*) FROM events WHERE event_key = ? AND "
cli@37
   212
				+ "event_time >= ? AND event_time < ?");
chris@3
   213
cli@37
   214
			this.pstmtGetEventsCount1 = conn.prepareStatement(
cli@37
   215
				"SELECT Count(*) FROM events WHERE event_key = ? AND "
cli@37
   216
				+ "event_time >= ? AND event_time < ? AND group_id = ?");
chris@3
   217
cli@37
   218
			// Prepare statement for method getGroupForList()
cli@37
   219
			this.pstmtGetGroupForList = conn.prepareStatement(
cli@37
   220
				"SELECT name FROM groups INNER JOIN groups2list "
cli@37
   221
				+ "ON groups.group_id = groups2list.group_id "
cli@37
   222
				+ "WHERE groups2list.listaddress = ?");
chris@3
   223
cli@37
   224
			// Prepare statement for method getGroup()
cli@37
   225
			this.pstmtGetGroup0 = conn.prepareStatement(
cli@37
   226
				"SELECT group_id, flags FROM groups WHERE Name = ?");
cli@37
   227
			this.pstmtGetGroup1 = conn.prepareStatement(
cli@37
   228
				"SELECT name FROM groups WHERE group_id = ?");
chris@3
   229
cli@37
   230
			// Prepare statement for method getLastArticleNumber()
cli@37
   231
			this.pstmtGetLastArticleNumber = conn.prepareStatement(
cli@37
   232
				"SELECT Max(article_index) FROM postings WHERE group_id = ?");
chris@3
   233
cli@37
   234
			// Prepare statement for method getListForGroup()
cli@37
   235
			this.pstmtGetListForGroup = conn.prepareStatement(
cli@37
   236
				"SELECT listaddress FROM groups2list INNER JOIN groups "
cli@37
   237
				+ "ON groups.group_id = groups2list.group_id WHERE name = ?");
chris@3
   238
cli@37
   239
			// Prepare statement for method getMaxArticleID()
cli@37
   240
			this.pstmtGetMaxArticleID = conn.prepareStatement(
cli@37
   241
				"SELECT Max(article_id) FROM articles");
chris@3
   242
cli@37
   243
			// Prepare statement for method getMaxArticleIndex()
cli@37
   244
			this.pstmtGetMaxArticleIndex = conn.prepareStatement(
cli@37
   245
				"SELECT Max(article_index) FROM postings WHERE group_id = ?");
chris@3
   246
cli@37
   247
			// Prepare statement for method getOldestArticle()
cli@37
   248
			this.pstmtGetOldestArticle = conn.prepareStatement(
cli@37
   249
				"SELECT message_id FROM article_ids WHERE article_id = "
cli@37
   250
				+ "(SELECT Min(article_id) FROM article_ids)");
chris@3
   251
cli@37
   252
			// Prepare statement for method getFirstArticleNumber()
cli@37
   253
			this.pstmtGetFirstArticleNumber = conn.prepareStatement(
cli@37
   254
				"SELECT Min(article_index) FROM postings WHERE group_id = ?");
chris@3
   255
cli@37
   256
			// Prepare statement for method getPostingsCount()
cli@37
   257
			this.pstmtGetPostingsCount = conn.prepareStatement(
cli@37
   258
				"SELECT Count(*) FROM postings NATURAL JOIN groups "
cli@37
   259
				+ "WHERE groups.name = ?");
chris@3
   260
cli@37
   261
			// Prepare statement for method getSubscriptions()
cli@37
   262
			this.pstmtGetSubscriptions = conn.prepareStatement(
cli@37
   263
				"SELECT host, port, name FROM peers NATURAL JOIN "
cli@37
   264
				+ "peer_subscriptions NATURAL JOIN groups WHERE feedtype = ?");
chris@3
   265
cli@37
   266
			// Prepare statement for method isArticleExisting()
cli@37
   267
			this.pstmtIsArticleExisting = conn.prepareStatement(
cli@37
   268
				"SELECT Count(article_id) FROM article_ids WHERE message_id = ?");
chris@3
   269
cli@37
   270
			// Prepare statement for method isGroupExisting()
cli@37
   271
			this.pstmtIsGroupExisting = conn.prepareStatement(
cli@37
   272
				"SELECT * FROM groups WHERE name = ?");
chris@3
   273
cli@37
   274
			// Prepare statement for method setConfigValue()
cli@37
   275
			this.pstmtSetConfigValue0 = conn.prepareStatement(
cli@37
   276
				"DELETE FROM config WHERE config_key = ?");
cli@37
   277
			this.pstmtSetConfigValue1 = conn.prepareStatement(
cli@37
   278
				"INSERT INTO config VALUES(?, ?)");
chris@3
   279
cli@37
   280
			// Prepare statements for method purgeGroup()
cli@37
   281
			this.pstmtPurgeGroup0 = conn.prepareStatement(
cli@37
   282
				"DELETE FROM peer_subscriptions WHERE group_id = ?");
cli@37
   283
			this.pstmtPurgeGroup1 = conn.prepareStatement(
cli@37
   284
				"DELETE FROM groups WHERE group_id = ?");
chris@3
   285
cli@37
   286
			// Prepare statement for method update(Group)
cli@37
   287
			this.pstmtUpdateGroup = conn.prepareStatement(
cli@37
   288
				"UPDATE groups SET flags = ?, name = ? WHERE group_id = ?");
cli@37
   289
		} catch (ClassNotFoundException ex) {
cli@37
   290
			throw new Error("JDBC Driver not found!", ex);
cli@37
   291
		}
cli@37
   292
	}
chris@3
   293
cli@37
   294
	/**
cli@37
   295
	 * Adds an article to the database.
cli@37
   296
	 * @param article
cli@37
   297
	 * @return
cli@37
   298
	 * @throws java.sql.SQLException
cli@37
   299
	 */
cli@37
   300
	@Override
cli@37
   301
	public void addArticle(final Article article)
cli@37
   302
		throws StorageBackendException
cli@37
   303
	{
cli@37
   304
		try {
cli@37
   305
			this.conn.setAutoCommit(false);
chris@3
   306
cli@37
   307
			int newArticleID = getMaxArticleID() + 1;
chris@3
   308
cli@37
   309
			// Fill prepared statement with values;
cli@37
   310
			// writes body to article table
cli@37
   311
			pstmtAddArticle1.setInt(1, newArticleID);
cli@37
   312
			pstmtAddArticle1.setBytes(2, article.getBody());
cli@37
   313
			pstmtAddArticle1.execute();
chris@3
   314
cli@37
   315
			// Add headers
cli@37
   316
			Enumeration headers = article.getAllHeaders();
cli@37
   317
			for (int n = 0; headers.hasMoreElements(); n++) {
cli@37
   318
				Header header = (Header) headers.nextElement();
cli@37
   319
				pstmtAddArticle2.setInt(1, newArticleID);
cli@37
   320
				pstmtAddArticle2.setString(2, header.getName().toLowerCase());
cli@37
   321
				pstmtAddArticle2.setString(3,
cli@37
   322
					header.getValue().replaceAll("[\r\n]", ""));
cli@37
   323
				pstmtAddArticle2.setInt(4, n);
cli@37
   324
				pstmtAddArticle2.execute();
cli@37
   325
			}
chris@3
   326
cli@37
   327
			// For each newsgroup add a reference
cli@37
   328
			List<Group> groups = article.getGroups();
cli@37
   329
			for (Group group : groups) {
cli@37
   330
				pstmtAddArticle3.setLong(1, group.getInternalID());
cli@37
   331
				pstmtAddArticle3.setInt(2, newArticleID);
cli@37
   332
				pstmtAddArticle3.setLong(3, getMaxArticleIndex(group.getInternalID()) + 1);
cli@37
   333
				pstmtAddArticle3.execute();
cli@37
   334
			}
chris@3
   335
cli@37
   336
			// Write message-id to article_ids table
cli@37
   337
			this.pstmtAddArticle4.setInt(1, newArticleID);
cli@37
   338
			this.pstmtAddArticle4.setString(2, article.getMessageID());
cli@37
   339
			this.pstmtAddArticle4.execute();
chris@3
   340
cli@37
   341
			this.conn.commit();
cli@37
   342
			this.conn.setAutoCommit(true);
chris@3
   343
cli@37
   344
			this.restarts = 0; // Reset error count
cli@37
   345
		} catch (SQLException ex) {
cli@37
   346
			try {
cli@37
   347
				this.conn.rollback();  // Rollback changes
cli@37
   348
			} catch (SQLException ex2) {
cli@37
   349
				Log.get().severe("Rollback of addArticle() failed: " + ex2);
cli@37
   350
			}
chris@3
   351
cli@37
   352
			try {
cli@37
   353
				this.conn.setAutoCommit(true); // and release locks
cli@37
   354
			} catch (SQLException ex2) {
cli@37
   355
				Log.get().severe("setAutoCommit(true) of addArticle() failed: " + ex2);
cli@37
   356
			}
chris@3
   357
cli@37
   358
			restartConnection(ex);
cli@37
   359
			addArticle(article);
cli@37
   360
		}
cli@37
   361
	}
chris@3
   362
cli@37
   363
	/**
cli@37
   364
	 * Adds a group to the JDBCDatabase. This method is not accessible via NNTP.
cli@37
   365
	 * @param name
cli@37
   366
	 * @throws java.sql.SQLException
cli@37
   367
	 */
cli@37
   368
	@Override
cli@37
   369
	public void addGroup(String name, int flags)
cli@37
   370
		throws StorageBackendException
cli@37
   371
	{
cli@37
   372
		try {
cli@37
   373
			this.conn.setAutoCommit(false);
cli@37
   374
			pstmtAddGroup0.setString(1, name);
cli@37
   375
			pstmtAddGroup0.setInt(2, flags);
chris@3
   376
cli@37
   377
			pstmtAddGroup0.executeUpdate();
cli@37
   378
			this.conn.commit();
cli@37
   379
			this.conn.setAutoCommit(true);
cli@37
   380
			this.restarts = 0; // Reset error count
cli@37
   381
		} catch (SQLException ex) {
cli@37
   382
			try {
cli@37
   383
				this.conn.rollback();
cli@37
   384
				this.conn.setAutoCommit(true);
cli@37
   385
			} catch (SQLException ex2) {
cli@37
   386
				ex2.printStackTrace();
cli@37
   387
			}
chris@3
   388
cli@37
   389
			restartConnection(ex);
cli@37
   390
			addGroup(name, flags);
cli@37
   391
		}
cli@37
   392
	}
chris@3
   393
cli@37
   394
	@Override
cli@37
   395
	public void addEvent(long time, int type, long gid)
cli@37
   396
		throws StorageBackendException
cli@37
   397
	{
cli@37
   398
		try {
cli@37
   399
			this.conn.setAutoCommit(false);
cli@37
   400
			this.pstmtAddEvent.setLong(1, time);
cli@37
   401
			this.pstmtAddEvent.setInt(2, type);
cli@37
   402
			this.pstmtAddEvent.setLong(3, gid);
cli@37
   403
			this.pstmtAddEvent.executeUpdate();
cli@37
   404
			this.conn.commit();
cli@37
   405
			this.conn.setAutoCommit(true);
cli@37
   406
			this.restarts = 0;
cli@37
   407
		} catch (SQLException ex) {
cli@37
   408
			try {
cli@37
   409
				this.conn.rollback();
cli@37
   410
				this.conn.setAutoCommit(true);
cli@37
   411
			} catch (SQLException ex2) {
cli@37
   412
				ex2.printStackTrace();
cli@37
   413
			}
chris@3
   414
cli@37
   415
			restartConnection(ex);
cli@37
   416
			addEvent(time, type, gid);
cli@37
   417
		}
cli@37
   418
	}
chris@3
   419
cli@37
   420
	@Override
cli@37
   421
	public int countArticles()
cli@37
   422
		throws StorageBackendException
cli@37
   423
	{
cli@37
   424
		ResultSet rs = null;
chris@3
   425
cli@37
   426
		try {
cli@37
   427
			rs = this.pstmtCountArticles.executeQuery();
cli@37
   428
			if (rs.next()) {
cli@37
   429
				return rs.getInt(1);
cli@37
   430
			} else {
cli@37
   431
				return -1;
cli@37
   432
			}
cli@37
   433
		} catch (SQLException ex) {
cli@37
   434
			restartConnection(ex);
cli@37
   435
			return countArticles();
cli@37
   436
		} finally {
cli@37
   437
			if (rs != null) {
cli@37
   438
				try {
cli@37
   439
					rs.close();
cli@37
   440
				} catch (SQLException ex) {
cli@37
   441
					ex.printStackTrace();
cli@37
   442
				}
cli@37
   443
				restarts = 0;
cli@37
   444
			}
cli@37
   445
		}
cli@37
   446
	}
chris@3
   447
cli@37
   448
	@Override
cli@37
   449
	public int countGroups()
cli@37
   450
		throws StorageBackendException
cli@37
   451
	{
cli@37
   452
		ResultSet rs = null;
chris@3
   453
cli@37
   454
		try {
cli@37
   455
			rs = this.pstmtCountGroups.executeQuery();
cli@37
   456
			if (rs.next()) {
cli@37
   457
				return rs.getInt(1);
cli@37
   458
			} else {
cli@37
   459
				return -1;
cli@37
   460
			}
cli@37
   461
		} catch (SQLException ex) {
cli@37
   462
			restartConnection(ex);
cli@37
   463
			return countGroups();
cli@37
   464
		} finally {
cli@37
   465
			if (rs != null) {
cli@37
   466
				try {
cli@37
   467
					rs.close();
cli@37
   468
				} catch (SQLException ex) {
cli@37
   469
					ex.printStackTrace();
cli@37
   470
				}
cli@37
   471
				restarts = 0;
cli@37
   472
			}
cli@37
   473
		}
cli@37
   474
	}
chris@3
   475
cli@37
   476
	@Override
cli@37
   477
	public void delete(final String messageID)
cli@37
   478
		throws StorageBackendException
cli@37
   479
	{
cli@37
   480
		try {
cli@37
   481
			this.conn.setAutoCommit(false);
chris@3
   482
cli@37
   483
			this.pstmtDeleteArticle0.setString(1, messageID);
cli@37
   484
			int rs = this.pstmtDeleteArticle0.executeUpdate();
chris@3
   485
cli@37
   486
			// We do not trust the ON DELETE CASCADE functionality to delete
cli@37
   487
			// orphaned references...
cli@37
   488
			this.pstmtDeleteArticle1.setString(1, messageID);
cli@37
   489
			rs = this.pstmtDeleteArticle1.executeUpdate();
chris@3
   490
cli@37
   491
			this.pstmtDeleteArticle2.setString(1, messageID);
cli@37
   492
			rs = this.pstmtDeleteArticle2.executeUpdate();
chris@3
   493
cli@37
   494
			this.pstmtDeleteArticle3.setString(1, messageID);
cli@37
   495
			rs = this.pstmtDeleteArticle3.executeUpdate();
chris@3
   496
cli@37
   497
			this.conn.commit();
cli@37
   498
			this.conn.setAutoCommit(true);
cli@37
   499
		} catch (SQLException ex) {
cli@37
   500
			throw new StorageBackendException(ex);
cli@37
   501
		}
cli@37
   502
	}
chris@3
   503
cli@37
   504
	@Override
cli@37
   505
	public Article getArticle(String messageID)
cli@37
   506
		throws StorageBackendException
cli@37
   507
	{
cli@37
   508
		ResultSet rs = null;
cli@37
   509
		try {
cli@37
   510
			pstmtGetArticle0.setString(1, messageID);
cli@37
   511
			rs = pstmtGetArticle0.executeQuery();
chris@3
   512
cli@37
   513
			if (!rs.next()) {
cli@37
   514
				return null;
cli@37
   515
			} else {
cli@37
   516
				byte[] body = rs.getBytes("body");
cli@37
   517
				String headers = getArticleHeaders(rs.getInt("article_id"));
cli@37
   518
				return new Article(headers, body);
cli@37
   519
			}
cli@37
   520
		} catch (SQLException ex) {
cli@37
   521
			restartConnection(ex);
cli@37
   522
			return getArticle(messageID);
cli@37
   523
		} finally {
cli@37
   524
			if (rs != null) {
cli@37
   525
				try {
cli@37
   526
					rs.close();
cli@37
   527
				} catch (SQLException ex) {
cli@37
   528
					ex.printStackTrace();
cli@37
   529
				}
cli@37
   530
				restarts = 0; // Reset error count
cli@37
   531
			}
cli@37
   532
		}
cli@37
   533
	}
chris@3
   534
cli@37
   535
	/**
cli@37
   536
	 * Retrieves an article by its ID.
cli@37
   537
	 * @param articleID
cli@37
   538
	 * @return
cli@37
   539
	 * @throws StorageBackendException
cli@37
   540
	 */
cli@37
   541
	@Override
cli@37
   542
	public Article getArticle(long articleIndex, long gid)
cli@37
   543
		throws StorageBackendException
cli@37
   544
	{
cli@37
   545
		ResultSet rs = null;
chris@3
   546
cli@37
   547
		try {
cli@37
   548
			this.pstmtGetArticle1.setLong(1, articleIndex);
cli@37
   549
			this.pstmtGetArticle1.setLong(2, gid);
chris@3
   550
cli@37
   551
			rs = this.pstmtGetArticle1.executeQuery();
chris@3
   552
cli@37
   553
			if (rs.next()) {
cli@37
   554
				byte[] body = rs.getBytes("body");
cli@37
   555
				String headers = getArticleHeaders(rs.getInt("article_id"));
cli@37
   556
				return new Article(headers, body);
cli@37
   557
			} else {
cli@37
   558
				return null;
cli@37
   559
			}
cli@37
   560
		} catch (SQLException ex) {
cli@37
   561
			restartConnection(ex);
cli@37
   562
			return getArticle(articleIndex, gid);
cli@37
   563
		} finally {
cli@37
   564
			if (rs != null) {
cli@37
   565
				try {
cli@37
   566
					rs.close();
cli@37
   567
				} catch (SQLException ex) {
cli@37
   568
					ex.printStackTrace();
cli@37
   569
				}
cli@37
   570
				restarts = 0;
cli@37
   571
			}
cli@37
   572
		}
cli@37
   573
	}
chris@3
   574
cli@37
   575
	/**
cli@37
   576
	 * Searches for fitting header values using the given regular expression.
cli@37
   577
	 * @param group
cli@37
   578
	 * @param start
cli@37
   579
	 * @param end
cli@37
   580
	 * @param headerKey
cli@37
   581
	 * @param pattern
cli@37
   582
	 * @return
cli@37
   583
	 * @throws StorageBackendException
cli@37
   584
	 */
cli@37
   585
	@Override
cli@37
   586
	public List<Pair<Long, String>> getArticleHeaders(Channel group, long start,
cli@37
   587
		long end, String headerKey, String patStr)
cli@37
   588
		throws StorageBackendException, PatternSyntaxException
cli@37
   589
	{
cli@37
   590
		ResultSet rs = null;
cli@37
   591
		List<Pair<Long, String>> heads = new ArrayList<Pair<Long, String>>();
cli@12
   592
cli@37
   593
		try {
cli@37
   594
			this.pstmtGetArticleHeaders1.setString(1, group.getName());
cli@37
   595
			this.pstmtGetArticleHeaders1.setString(2, headerKey);
cli@37
   596
			this.pstmtGetArticleHeaders1.setLong(3, start);
chris@3
   597
cli@37
   598
			rs = this.pstmtGetArticleHeaders1.executeQuery();
chris@3
   599
cli@37
   600
			// Convert the "NNTP" regex to Java regex
cli@37
   601
			patStr = patStr.replace("*", ".*");
cli@37
   602
			Pattern pattern = Pattern.compile(patStr);
chris@3
   603
cli@37
   604
			while (rs.next()) {
cli@37
   605
				Long articleIndex = rs.getLong(1);
cli@37
   606
				if (end < 0 || articleIndex <= end) // Match start is done via SQL
cli@37
   607
				{
cli@37
   608
					String headerValue = rs.getString(2);
cli@37
   609
					Matcher matcher = pattern.matcher(headerValue);
cli@37
   610
					if (matcher.matches()) {
cli@37
   611
						heads.add(new Pair<Long, String>(articleIndex, headerValue));
cli@37
   612
					}
cli@37
   613
				}
cli@37
   614
			}
cli@37
   615
		} catch (SQLException ex) {
cli@37
   616
			restartConnection(ex);
cli@37
   617
			return getArticleHeaders(group, start, end, headerKey, patStr);
cli@37
   618
		} finally {
cli@37
   619
			if (rs != null) {
cli@37
   620
				try {
cli@37
   621
					rs.close();
cli@37
   622
				} catch (SQLException ex) {
cli@37
   623
					ex.printStackTrace();
cli@37
   624
				}
cli@37
   625
			}
cli@37
   626
		}
chris@3
   627
cli@37
   628
		return heads;
cli@37
   629
	}
chris@3
   630
cli@37
   631
	private String getArticleHeaders(long articleID)
cli@37
   632
		throws StorageBackendException
cli@37
   633
	{
cli@37
   634
		ResultSet rs = null;
chris@3
   635
cli@37
   636
		try {
cli@37
   637
			this.pstmtGetArticleHeaders0.setLong(1, articleID);
cli@37
   638
			rs = this.pstmtGetArticleHeaders0.executeQuery();
chris@3
   639
cli@37
   640
			StringBuilder buf = new StringBuilder();
cli@37
   641
			if (rs.next()) {
cli@37
   642
				for (;;) {
cli@37
   643
					buf.append(rs.getString(1)); // key
cli@37
   644
					buf.append(": ");
cli@37
   645
					String foldedValue = MimeUtility.fold(0, rs.getString(2));
cli@37
   646
					buf.append(foldedValue); // value
cli@37
   647
					if (rs.next()) {
cli@37
   648
						buf.append("\r\n");
cli@37
   649
					} else {
cli@37
   650
						break;
cli@37
   651
					}
cli@37
   652
				}
cli@37
   653
			}
chris@3
   654
cli@37
   655
			return buf.toString();
cli@37
   656
		} catch (SQLException ex) {
cli@37
   657
			restartConnection(ex);
cli@37
   658
			return getArticleHeaders(articleID);
cli@37
   659
		} finally {
cli@37
   660
			if (rs != null) {
cli@37
   661
				try {
cli@37
   662
					rs.close();
cli@37
   663
				} catch (SQLException ex) {
cli@37
   664
					ex.printStackTrace();
cli@37
   665
				}
cli@37
   666
			}
cli@37
   667
		}
cli@37
   668
	}
chris@3
   669
cli@37
   670
	@Override
cli@37
   671
	public long getArticleIndex(Article article, Group group)
cli@37
   672
		throws StorageBackendException
cli@37
   673
	{
cli@37
   674
		ResultSet rs = null;
chris@3
   675
cli@37
   676
		try {
cli@37
   677
			this.pstmtGetArticleIndex.setString(1, article.getMessageID());
cli@37
   678
			this.pstmtGetArticleIndex.setLong(2, group.getInternalID());
chris@3
   679
cli@37
   680
			rs = this.pstmtGetArticleIndex.executeQuery();
cli@37
   681
			if (rs.next()) {
cli@37
   682
				return rs.getLong(1);
cli@37
   683
			} else {
cli@37
   684
				return -1;
cli@37
   685
			}
cli@37
   686
		} catch (SQLException ex) {
cli@37
   687
			restartConnection(ex);
cli@37
   688
			return getArticleIndex(article, group);
cli@37
   689
		} finally {
cli@37
   690
			if (rs != null) {
cli@37
   691
				try {
cli@37
   692
					rs.close();
cli@37
   693
				} catch (SQLException ex) {
cli@37
   694
					ex.printStackTrace();
cli@37
   695
				}
cli@37
   696
			}
cli@37
   697
		}
cli@37
   698
	}
chris@3
   699
cli@37
   700
	/**
cli@37
   701
	 * Returns a list of Long/Article Pairs.
cli@37
   702
	 * @throws java.sql.SQLException
cli@37
   703
	 */
cli@37
   704
	@Override
cli@37
   705
	public List<Pair<Long, ArticleHead>> getArticleHeads(Group group, long first,
cli@37
   706
		long last)
cli@37
   707
		throws StorageBackendException
cli@37
   708
	{
cli@37
   709
		ResultSet rs = null;
chris@3
   710
cli@37
   711
		try {
cli@37
   712
			this.pstmtGetArticleHeads.setLong(1, group.getInternalID());
cli@37
   713
			this.pstmtGetArticleHeads.setLong(2, first);
cli@37
   714
			this.pstmtGetArticleHeads.setLong(3, last);
cli@37
   715
			rs = pstmtGetArticleHeads.executeQuery();
chris@3
   716
cli@37
   717
			List<Pair<Long, ArticleHead>> articles = new ArrayList<Pair<Long, ArticleHead>>();
chris@3
   718
cli@37
   719
			while (rs.next()) {
cli@37
   720
				long aid = rs.getLong("article_id");
cli@37
   721
				long aidx = rs.getLong("article_index");
cli@37
   722
				String headers = getArticleHeaders(aid);
cli@37
   723
				articles.add(new Pair<Long, ArticleHead>(aidx,
cli@37
   724
					new ArticleHead(headers)));
cli@37
   725
			}
chris@3
   726
cli@37
   727
			return articles;
cli@37
   728
		} catch (SQLException ex) {
cli@37
   729
			restartConnection(ex);
cli@37
   730
			return getArticleHeads(group, first, last);
cli@37
   731
		} finally {
cli@37
   732
			if (rs != null) {
cli@37
   733
				try {
cli@37
   734
					rs.close();
cli@37
   735
				} catch (SQLException ex) {
cli@37
   736
					ex.printStackTrace();
cli@37
   737
				}
cli@37
   738
			}
cli@37
   739
		}
cli@37
   740
	}
chris@3
   741
cli@37
   742
	@Override
cli@37
   743
	public List<Long> getArticleNumbers(long gid)
cli@37
   744
		throws StorageBackendException
cli@37
   745
	{
cli@37
   746
		ResultSet rs = null;
cli@37
   747
		try {
cli@37
   748
			List<Long> ids = new ArrayList<Long>();
cli@37
   749
			this.pstmtGetArticleIDs.setLong(1, gid);
cli@37
   750
			rs = this.pstmtGetArticleIDs.executeQuery();
cli@37
   751
			while (rs.next()) {
cli@37
   752
				ids.add(rs.getLong(1));
cli@37
   753
			}
cli@37
   754
			return ids;
cli@37
   755
		} catch (SQLException ex) {
cli@37
   756
			restartConnection(ex);
cli@37
   757
			return getArticleNumbers(gid);
cli@37
   758
		} finally {
cli@37
   759
			if (rs != null) {
cli@37
   760
				try {
cli@37
   761
					rs.close();
cli@37
   762
					restarts = 0; // Clear the restart count after successful request
cli@37
   763
				} catch (SQLException ex) {
cli@37
   764
					ex.printStackTrace();
cli@37
   765
				}
cli@37
   766
			}
cli@37
   767
		}
cli@37
   768
	}
chris@3
   769
cli@37
   770
	@Override
cli@37
   771
	public String getConfigValue(String key)
cli@37
   772
		throws StorageBackendException
cli@37
   773
	{
cli@37
   774
		ResultSet rs = null;
cli@37
   775
		try {
cli@37
   776
			this.pstmtGetConfigValue.setString(1, key);
chris@3
   777
cli@37
   778
			rs = this.pstmtGetConfigValue.executeQuery();
cli@37
   779
			if (rs.next()) {
cli@37
   780
				return rs.getString(1); // First data on index 1 not 0
cli@37
   781
			} else {
cli@37
   782
				return null;
cli@37
   783
			}
cli@37
   784
		} catch (SQLException ex) {
cli@37
   785
			restartConnection(ex);
cli@37
   786
			return getConfigValue(key);
cli@37
   787
		} finally {
cli@37
   788
			if (rs != null) {
cli@37
   789
				try {
cli@37
   790
					rs.close();
cli@37
   791
				} catch (SQLException ex) {
cli@37
   792
					ex.printStackTrace();
cli@37
   793
				}
cli@37
   794
				restarts = 0; // Clear the restart count after successful request
cli@37
   795
			}
cli@37
   796
		}
cli@37
   797
	}
chris@3
   798
cli@37
   799
	@Override
cli@37
   800
	public int getEventsCount(int type, long start, long end, Channel channel)
cli@37
   801
		throws StorageBackendException
cli@37
   802
	{
cli@37
   803
		ResultSet rs = null;
chris@3
   804
cli@37
   805
		try {
cli@37
   806
			if (channel == null) {
cli@37
   807
				this.pstmtGetEventsCount0.setInt(1, type);
cli@37
   808
				this.pstmtGetEventsCount0.setLong(2, start);
cli@37
   809
				this.pstmtGetEventsCount0.setLong(3, end);
cli@37
   810
				rs = this.pstmtGetEventsCount0.executeQuery();
cli@37
   811
			} else {
cli@37
   812
				this.pstmtGetEventsCount1.setInt(1, type);
cli@37
   813
				this.pstmtGetEventsCount1.setLong(2, start);
cli@37
   814
				this.pstmtGetEventsCount1.setLong(3, end);
cli@37
   815
				this.pstmtGetEventsCount1.setLong(4, channel.getInternalID());
cli@37
   816
				rs = this.pstmtGetEventsCount1.executeQuery();
cli@37
   817
			}
cli@24
   818
cli@37
   819
			if (rs.next()) {
cli@37
   820
				return rs.getInt(1);
cli@37
   821
			} else {
cli@37
   822
				return -1;
cli@37
   823
			}
cli@37
   824
		} catch (SQLException ex) {
cli@37
   825
			restartConnection(ex);
cli@37
   826
			return getEventsCount(type, start, end, channel);
cli@37
   827
		} finally {
cli@37
   828
			if (rs != null) {
cli@37
   829
				try {
cli@37
   830
					rs.close();
cli@37
   831
				} catch (SQLException ex) {
cli@37
   832
					ex.printStackTrace();
cli@37
   833
				}
cli@37
   834
			}
cli@37
   835
		}
cli@37
   836
	}
cli@24
   837
cli@37
   838
	/**
cli@37
   839
	 * Reads all Groups from the JDBCDatabase.
cli@37
   840
	 * @return
cli@37
   841
	 * @throws StorageBackendException
cli@37
   842
	 */
cli@37
   843
	@Override
cli@37
   844
	public List<Channel> getGroups()
cli@37
   845
		throws StorageBackendException
cli@37
   846
	{
cli@37
   847
		ResultSet rs;
cli@37
   848
		List<Channel> buffer = new ArrayList<Channel>();
cli@37
   849
		Statement stmt = null;
cli@24
   850
cli@37
   851
		try {
cli@37
   852
			stmt = conn.createStatement();
cli@37
   853
			rs = stmt.executeQuery("SELECT * FROM groups ORDER BY name");
chris@3
   854
cli@37
   855
			while (rs.next()) {
cli@37
   856
				String name = rs.getString("name");
cli@37
   857
				long id = rs.getLong("group_id");
cli@37
   858
				int flags = rs.getInt("flags");
cli@37
   859
cli@37
   860
				Group group = new Group(name, id, flags);
cli@37
   861
				buffer.add(group);
cli@37
   862
			}
cli@37
   863
cli@37
   864
			return buffer;
cli@37
   865
		} catch (SQLException ex) {
cli@37
   866
			restartConnection(ex);
cli@37
   867
			return getGroups();
cli@37
   868
		} finally {
cli@37
   869
			if (stmt != null) {
cli@37
   870
				try {
cli@37
   871
					stmt.close(); // Implicitely closes ResultSets
cli@37
   872
				} catch (SQLException ex) {
cli@37
   873
					ex.printStackTrace();
cli@37
   874
				}
cli@37
   875
			}
cli@37
   876
		}
cli@37
   877
	}
cli@37
   878
cli@37
   879
	@Override
cli@37
   880
	public List<String> getGroupsForList(String listAddress)
cli@37
   881
		throws StorageBackendException
cli@37
   882
	{
cli@37
   883
		ResultSet rs = null;
cli@37
   884
cli@37
   885
		try {
cli@37
   886
			this.pstmtGetGroupForList.setString(1, listAddress);
cli@37
   887
cli@37
   888
			rs = this.pstmtGetGroupForList.executeQuery();
cli@37
   889
			List<String> groups = new ArrayList<String>();
cli@37
   890
			while (rs.next()) {
cli@37
   891
				String group = rs.getString(1);
cli@37
   892
				groups.add(group);
cli@37
   893
			}
cli@37
   894
			return groups;
cli@37
   895
		} catch (SQLException ex) {
cli@37
   896
			restartConnection(ex);
cli@37
   897
			return getGroupsForList(listAddress);
cli@37
   898
		} finally {
cli@37
   899
			if (rs != null) {
cli@37
   900
				try {
cli@37
   901
					rs.close();
cli@37
   902
				} catch (SQLException ex) {
cli@37
   903
					ex.printStackTrace();
cli@37
   904
				}
cli@37
   905
			}
cli@37
   906
		}
cli@37
   907
	}
cli@37
   908
cli@37
   909
	/**
cli@37
   910
	 * Returns the Group that is identified by the name.
cli@37
   911
	 * @param name
cli@37
   912
	 * @return
cli@37
   913
	 * @throws StorageBackendException
cli@37
   914
	 */
cli@37
   915
	@Override
cli@37
   916
	public Group getGroup(String name)
cli@37
   917
		throws StorageBackendException
cli@37
   918
	{
cli@37
   919
		ResultSet rs = null;
cli@37
   920
cli@37
   921
		try {
cli@37
   922
			this.pstmtGetGroup0.setString(1, name);
cli@37
   923
			rs = this.pstmtGetGroup0.executeQuery();
cli@37
   924
cli@37
   925
			if (!rs.next()) {
cli@37
   926
				return null;
cli@37
   927
			} else {
cli@37
   928
				long id = rs.getLong("group_id");
cli@37
   929
				int flags = rs.getInt("flags");
cli@37
   930
				return new Group(name, id, flags);
cli@37
   931
			}
cli@37
   932
		} catch (SQLException ex) {
cli@37
   933
			restartConnection(ex);
cli@37
   934
			return getGroup(name);
cli@37
   935
		} finally {
cli@37
   936
			if (rs != null) {
cli@37
   937
				try {
cli@37
   938
					rs.close();
cli@37
   939
				} catch (SQLException ex) {
cli@37
   940
					ex.printStackTrace();
cli@37
   941
				}
cli@37
   942
			}
cli@37
   943
		}
cli@37
   944
	}
cli@37
   945
cli@37
   946
	@Override
cli@37
   947
	public List<String> getListsForGroup(String group)
cli@37
   948
		throws StorageBackendException
cli@37
   949
	{
cli@37
   950
		ResultSet rs = null;
cli@37
   951
		List<String> lists = new ArrayList<String>();
cli@37
   952
cli@37
   953
		try {
cli@37
   954
			this.pstmtGetListForGroup.setString(1, group);
cli@37
   955
			rs = this.pstmtGetListForGroup.executeQuery();
cli@37
   956
cli@37
   957
			while (rs.next()) {
cli@37
   958
				lists.add(rs.getString(1));
cli@37
   959
			}
cli@37
   960
			return lists;
cli@37
   961
		} catch (SQLException ex) {
cli@37
   962
			restartConnection(ex);
cli@37
   963
			return getListsForGroup(group);
cli@37
   964
		} finally {
cli@37
   965
			if (rs != null) {
cli@37
   966
				try {
cli@37
   967
					rs.close();
cli@37
   968
				} catch (SQLException ex) {
cli@37
   969
					ex.printStackTrace();
cli@37
   970
				}
cli@37
   971
			}
cli@37
   972
		}
cli@37
   973
	}
cli@37
   974
cli@37
   975
	private int getMaxArticleIndex(long groupID)
cli@37
   976
		throws StorageBackendException
cli@37
   977
	{
cli@37
   978
		ResultSet rs = null;
cli@37
   979
cli@37
   980
		try {
cli@37
   981
			this.pstmtGetMaxArticleIndex.setLong(1, groupID);
cli@37
   982
			rs = this.pstmtGetMaxArticleIndex.executeQuery();
cli@37
   983
cli@37
   984
			int maxIndex = 0;
cli@37
   985
			if (rs.next()) {
cli@37
   986
				maxIndex = rs.getInt(1);
cli@37
   987
			}
cli@37
   988
cli@37
   989
			return maxIndex;
cli@37
   990
		} catch (SQLException ex) {
cli@37
   991
			restartConnection(ex);
cli@37
   992
			return getMaxArticleIndex(groupID);
cli@37
   993
		} finally {
cli@37
   994
			if (rs != null) {
cli@37
   995
				try {
cli@37
   996
					rs.close();
cli@37
   997
				} catch (SQLException ex) {
cli@37
   998
					ex.printStackTrace();
cli@37
   999
				}
cli@37
  1000
			}
cli@37
  1001
		}
cli@37
  1002
	}
cli@37
  1003
cli@37
  1004
	private int getMaxArticleID()
cli@37
  1005
		throws StorageBackendException
cli@37
  1006
	{
cli@37
  1007
		ResultSet rs = null;
cli@37
  1008
cli@37
  1009
		try {
cli@37
  1010
			rs = this.pstmtGetMaxArticleID.executeQuery();
cli@37
  1011
cli@37
  1012
			int maxIndex = 0;
cli@37
  1013
			if (rs.next()) {
cli@37
  1014
				maxIndex = rs.getInt(1);
cli@37
  1015
			}
cli@37
  1016
cli@37
  1017
			return maxIndex;
cli@37
  1018
		} catch (SQLException ex) {
cli@37
  1019
			restartConnection(ex);
cli@37
  1020
			return getMaxArticleID();
cli@37
  1021
		} finally {
cli@37
  1022
			if (rs != null) {
cli@37
  1023
				try {
cli@37
  1024
					rs.close();
cli@37
  1025
				} catch (SQLException ex) {
cli@37
  1026
					ex.printStackTrace();
cli@37
  1027
				}
cli@37
  1028
			}
cli@37
  1029
		}
cli@37
  1030
	}
cli@37
  1031
cli@37
  1032
	@Override
cli@37
  1033
	public int getLastArticleNumber(Group group)
cli@37
  1034
		throws StorageBackendException
cli@37
  1035
	{
cli@37
  1036
		ResultSet rs = null;
cli@37
  1037
cli@37
  1038
		try {
cli@37
  1039
			this.pstmtGetLastArticleNumber.setLong(1, group.getInternalID());
cli@37
  1040
			rs = this.pstmtGetLastArticleNumber.executeQuery();
cli@37
  1041
			if (rs.next()) {
cli@37
  1042
				return rs.getInt(1);
cli@37
  1043
			} else {
cli@37
  1044
				return 0;
cli@37
  1045
			}
cli@37
  1046
		} catch (SQLException ex) {
cli@37
  1047
			restartConnection(ex);
cli@37
  1048
			return getLastArticleNumber(group);
cli@37
  1049
		} finally {
cli@37
  1050
			if (rs != null) {
cli@37
  1051
				try {
cli@37
  1052
					rs.close();
cli@37
  1053
				} catch (SQLException ex) {
cli@37
  1054
					ex.printStackTrace();
cli@37
  1055
				}
cli@37
  1056
			}
cli@37
  1057
		}
cli@37
  1058
	}
cli@37
  1059
cli@37
  1060
	@Override
cli@37
  1061
	public int getFirstArticleNumber(Group group)
cli@37
  1062
		throws StorageBackendException
cli@37
  1063
	{
cli@37
  1064
		ResultSet rs = null;
cli@37
  1065
		try {
cli@37
  1066
			this.pstmtGetFirstArticleNumber.setLong(1, group.getInternalID());
cli@37
  1067
			rs = this.pstmtGetFirstArticleNumber.executeQuery();
cli@37
  1068
			if (rs.next()) {
cli@37
  1069
				return rs.getInt(1);
cli@37
  1070
			} else {
cli@37
  1071
				return 0;
cli@37
  1072
			}
cli@37
  1073
		} catch (SQLException ex) {
cli@37
  1074
			restartConnection(ex);
cli@37
  1075
			return getFirstArticleNumber(group);
cli@37
  1076
		} finally {
cli@37
  1077
			if (rs != null) {
cli@37
  1078
				try {
cli@37
  1079
					rs.close();
cli@37
  1080
				} catch (SQLException ex) {
cli@37
  1081
					ex.printStackTrace();
cli@37
  1082
				}
cli@37
  1083
			}
cli@37
  1084
		}
cli@37
  1085
	}
cli@37
  1086
cli@37
  1087
	/**
cli@37
  1088
	 * Returns a group name identified by the given id.
cli@37
  1089
	 * @param id
cli@37
  1090
	 * @return
cli@37
  1091
	 * @throws StorageBackendException
cli@37
  1092
	 */
cli@37
  1093
	public String getGroup(int id)
cli@37
  1094
		throws StorageBackendException
cli@37
  1095
	{
cli@37
  1096
		ResultSet rs = null;
cli@37
  1097
cli@37
  1098
		try {
cli@37
  1099
			this.pstmtGetGroup1.setInt(1, id);
cli@37
  1100
			rs = this.pstmtGetGroup1.executeQuery();
cli@37
  1101
cli@37
  1102
			if (rs.next()) {
cli@37
  1103
				return rs.getString(1);
cli@37
  1104
			} else {
cli@37
  1105
				return null;
cli@37
  1106
			}
cli@37
  1107
		} catch (SQLException ex) {
cli@37
  1108
			restartConnection(ex);
cli@37
  1109
			return getGroup(id);
cli@37
  1110
		} finally {
cli@37
  1111
			if (rs != null) {
cli@37
  1112
				try {
cli@37
  1113
					rs.close();
cli@37
  1114
				} catch (SQLException ex) {
cli@37
  1115
					ex.printStackTrace();
cli@37
  1116
				}
cli@37
  1117
			}
cli@37
  1118
		}
cli@37
  1119
	}
cli@37
  1120
cli@37
  1121
	@Override
cli@37
  1122
	public double getEventsPerHour(int key, long gid)
cli@37
  1123
		throws StorageBackendException
cli@37
  1124
	{
cli@37
  1125
		String gidquery = "";
cli@37
  1126
		if (gid >= 0) {
cli@37
  1127
			gidquery = " AND group_id = " + gid;
cli@37
  1128
		}
cli@37
  1129
cli@37
  1130
		Statement stmt = null;
cli@37
  1131
		ResultSet rs = null;
cli@37
  1132
cli@37
  1133
		try {
cli@37
  1134
			stmt = this.conn.createStatement();
cli@37
  1135
			rs = stmt.executeQuery("SELECT Count(*) / (Max(event_time) - Min(event_time))"
cli@37
  1136
				+ " * 1000 * 60 * 60 FROM events WHERE event_key = " + key + gidquery);
cli@37
  1137
cli@37
  1138
			if (rs.next()) {
cli@37
  1139
				restarts = 0; // reset error count
cli@37
  1140
				return rs.getDouble(1);
cli@37
  1141
			} else {
cli@37
  1142
				return Double.NaN;
cli@37
  1143
			}
cli@37
  1144
		} catch (SQLException ex) {
cli@37
  1145
			restartConnection(ex);
cli@37
  1146
			return getEventsPerHour(key, gid);
cli@37
  1147
		} finally {
cli@37
  1148
			try {
cli@37
  1149
				if (stmt != null) {
cli@37
  1150
					stmt.close(); // Implicitely closes the result sets
cli@37
  1151
				}
cli@37
  1152
			} catch (SQLException ex) {
cli@37
  1153
				ex.printStackTrace();
cli@37
  1154
			}
cli@37
  1155
		}
cli@37
  1156
	}
cli@37
  1157
cli@37
  1158
	@Override
cli@37
  1159
	public String getOldestArticle()
cli@37
  1160
		throws StorageBackendException
cli@37
  1161
	{
cli@37
  1162
		ResultSet rs = null;
cli@37
  1163
cli@37
  1164
		try {
cli@37
  1165
			rs = this.pstmtGetOldestArticle.executeQuery();
cli@37
  1166
			if (rs.next()) {
cli@37
  1167
				return rs.getString(1);
cli@37
  1168
			} else {
cli@37
  1169
				return null;
cli@37
  1170
			}
cli@37
  1171
		} catch (SQLException ex) {
cli@37
  1172
			restartConnection(ex);
cli@37
  1173
			return getOldestArticle();
cli@37
  1174
		} finally {
cli@37
  1175
			if (rs != null) {
cli@37
  1176
				try {
cli@37
  1177
					rs.close();
cli@37
  1178
				} catch (SQLException ex) {
cli@37
  1179
					ex.printStackTrace();
cli@37
  1180
				}
cli@37
  1181
			}
cli@37
  1182
		}
cli@37
  1183
	}
cli@37
  1184
cli@37
  1185
	@Override
cli@37
  1186
	public int getPostingsCount(String groupname)
cli@37
  1187
		throws StorageBackendException
cli@37
  1188
	{
cli@37
  1189
		ResultSet rs = null;
cli@37
  1190
cli@37
  1191
		try {
cli@37
  1192
			this.pstmtGetPostingsCount.setString(1, groupname);
cli@37
  1193
			rs = this.pstmtGetPostingsCount.executeQuery();
cli@37
  1194
			if (rs.next()) {
cli@37
  1195
				return rs.getInt(1);
cli@37
  1196
			} else {
cli@37
  1197
				Log.get().warning("Count on postings return nothing!");
cli@37
  1198
				return 0;
cli@37
  1199
			}
cli@37
  1200
		} catch (SQLException ex) {
cli@37
  1201
			restartConnection(ex);
cli@37
  1202
			return getPostingsCount(groupname);
cli@37
  1203
		} finally {
cli@37
  1204
			if (rs != null) {
cli@37
  1205
				try {
cli@37
  1206
					rs.close();
cli@37
  1207
				} catch (SQLException ex) {
cli@37
  1208
					ex.printStackTrace();
cli@37
  1209
				}
cli@37
  1210
			}
cli@37
  1211
		}
cli@37
  1212
	}
cli@37
  1213
cli@37
  1214
	@Override
cli@37
  1215
	public List<Subscription> getSubscriptions(int feedtype)
cli@37
  1216
		throws StorageBackendException
cli@37
  1217
	{
cli@37
  1218
		ResultSet rs = null;
cli@37
  1219
cli@37
  1220
		try {
cli@37
  1221
			List<Subscription> subs = new ArrayList<Subscription>();
cli@37
  1222
			this.pstmtGetSubscriptions.setInt(1, feedtype);
cli@37
  1223
			rs = this.pstmtGetSubscriptions.executeQuery();
cli@37
  1224
cli@37
  1225
			while (rs.next()) {
cli@37
  1226
				String host = rs.getString("host");
cli@37
  1227
				String group = rs.getString("name");
cli@37
  1228
				int port = rs.getInt("port");
cli@37
  1229
				subs.add(new Subscription(host, port, feedtype, group));
cli@37
  1230
			}
cli@37
  1231
cli@37
  1232
			return subs;
cli@37
  1233
		} catch (SQLException ex) {
cli@37
  1234
			restartConnection(ex);
cli@37
  1235
			return getSubscriptions(feedtype);
cli@37
  1236
		} finally {
cli@37
  1237
			if (rs != null) {
cli@37
  1238
				try {
cli@37
  1239
					rs.close();
cli@37
  1240
				} catch (SQLException ex) {
cli@37
  1241
					ex.printStackTrace();
cli@37
  1242
				}
cli@37
  1243
			}
cli@37
  1244
		}
cli@37
  1245
	}
cli@37
  1246
cli@37
  1247
	/**
cli@37
  1248
	 * Checks if there is an article with the given messageid in the JDBCDatabase.
cli@37
  1249
	 * @param name
cli@37
  1250
	 * @return
cli@37
  1251
	 * @throws StorageBackendException
cli@37
  1252
	 */
cli@37
  1253
	@Override
cli@37
  1254
	public boolean isArticleExisting(String messageID)
cli@37
  1255
		throws StorageBackendException
cli@37
  1256
	{
cli@37
  1257
		ResultSet rs = null;
cli@37
  1258
cli@37
  1259
		try {
cli@37
  1260
			this.pstmtIsArticleExisting.setString(1, messageID);
cli@37
  1261
			rs = this.pstmtIsArticleExisting.executeQuery();
cli@37
  1262
			return rs.next() && rs.getInt(1) == 1;
cli@37
  1263
		} catch (SQLException ex) {
cli@37
  1264
			restartConnection(ex);
cli@37
  1265
			return isArticleExisting(messageID);
cli@37
  1266
		} finally {
cli@37
  1267
			if (rs != null) {
cli@37
  1268
				try {
cli@37
  1269
					rs.close();
cli@37
  1270
				} catch (SQLException ex) {
cli@37
  1271
					ex.printStackTrace();
cli@37
  1272
				}
cli@37
  1273
			}
cli@37
  1274
		}
cli@37
  1275
	}
cli@37
  1276
cli@37
  1277
	/**
cli@37
  1278
	 * Checks if there is a group with the given name in the JDBCDatabase.
cli@37
  1279
	 * @param name
cli@37
  1280
	 * @return
cli@37
  1281
	 * @throws StorageBackendException
cli@37
  1282
	 */
cli@37
  1283
	@Override
cli@37
  1284
	public boolean isGroupExisting(String name)
cli@37
  1285
		throws StorageBackendException
cli@37
  1286
	{
cli@37
  1287
		ResultSet rs = null;
cli@37
  1288
cli@37
  1289
		try {
cli@37
  1290
			this.pstmtIsGroupExisting.setString(1, name);
cli@37
  1291
			rs = this.pstmtIsGroupExisting.executeQuery();
cli@37
  1292
			return rs.next();
cli@37
  1293
		} catch (SQLException ex) {
cli@37
  1294
			restartConnection(ex);
cli@37
  1295
			return isGroupExisting(name);
cli@37
  1296
		} finally {
cli@37
  1297
			if (rs != null) {
cli@37
  1298
				try {
cli@37
  1299
					rs.close();
cli@37
  1300
				} catch (SQLException ex) {
cli@37
  1301
					ex.printStackTrace();
cli@37
  1302
				}
cli@37
  1303
			}
cli@37
  1304
		}
cli@37
  1305
	}
cli@37
  1306
cli@37
  1307
	@Override
cli@37
  1308
	public void setConfigValue(String key, String value)
cli@37
  1309
		throws StorageBackendException
cli@37
  1310
	{
cli@37
  1311
		try {
cli@37
  1312
			conn.setAutoCommit(false);
cli@37
  1313
			this.pstmtSetConfigValue0.setString(1, key);
cli@37
  1314
			this.pstmtSetConfigValue0.execute();
cli@37
  1315
			this.pstmtSetConfigValue1.setString(1, key);
cli@37
  1316
			this.pstmtSetConfigValue1.setString(2, value);
cli@37
  1317
			this.pstmtSetConfigValue1.execute();
cli@37
  1318
			conn.commit();
cli@37
  1319
			conn.setAutoCommit(true);
cli@37
  1320
		} catch (SQLException ex) {
cli@37
  1321
			restartConnection(ex);
cli@37
  1322
			setConfigValue(key, value);
cli@37
  1323
		}
cli@37
  1324
	}
cli@37
  1325
cli@37
  1326
	/**
cli@37
  1327
	 * Closes the JDBCDatabase connection.
cli@37
  1328
	 */
cli@37
  1329
	public void shutdown()
cli@37
  1330
		throws StorageBackendException
cli@37
  1331
	{
cli@37
  1332
		try {
cli@37
  1333
			if (this.conn != null) {
cli@37
  1334
				this.conn.close();
cli@37
  1335
			}
cli@37
  1336
		} catch (SQLException ex) {
cli@37
  1337
			throw new StorageBackendException(ex);
cli@37
  1338
		}
cli@37
  1339
	}
cli@37
  1340
cli@37
  1341
	@Override
cli@37
  1342
	public void purgeGroup(Group group)
cli@37
  1343
		throws StorageBackendException
cli@37
  1344
	{
cli@37
  1345
		try {
cli@37
  1346
			this.pstmtPurgeGroup0.setLong(1, group.getInternalID());
cli@37
  1347
			this.pstmtPurgeGroup0.executeUpdate();
cli@37
  1348
cli@37
  1349
			this.pstmtPurgeGroup1.setLong(1, group.getInternalID());
cli@37
  1350
			this.pstmtPurgeGroup1.executeUpdate();
cli@37
  1351
		} catch (SQLException ex) {
cli@37
  1352
			restartConnection(ex);
cli@37
  1353
			purgeGroup(group);
cli@37
  1354
		}
cli@37
  1355
	}
cli@37
  1356
cli@37
  1357
	private void restartConnection(SQLException cause)
cli@37
  1358
		throws StorageBackendException
cli@37
  1359
	{
cli@37
  1360
		restarts++;
cli@37
  1361
		Log.get().severe(Thread.currentThread()
cli@37
  1362
			+ ": Database connection was closed (restart " + restarts + ").");
cli@37
  1363
cli@37
  1364
		if (restarts >= MAX_RESTARTS) {
cli@37
  1365
			// Delete the current, probably broken JDBCDatabase instance.
cli@37
  1366
			// So no one can use the instance any more.
cli@37
  1367
			JDBCDatabaseProvider.instances.remove(Thread.currentThread());
cli@37
  1368
cli@37
  1369
			// Throw the exception upwards
cli@37
  1370
			throw new StorageBackendException(cause);
cli@37
  1371
		}
cli@37
  1372
cli@37
  1373
		try {
cli@37
  1374
			Thread.sleep(1500L * restarts);
cli@37
  1375
		} catch (InterruptedException ex) {
cli@37
  1376
			Log.get().warning("Interrupted: " + ex.getMessage());
cli@37
  1377
		}
cli@37
  1378
cli@37
  1379
		// Try to properly close the old database connection
cli@37
  1380
		try {
cli@37
  1381
			if (this.conn != null) {
cli@37
  1382
				this.conn.close();
cli@37
  1383
			}
cli@37
  1384
		} catch (SQLException ex) {
cli@37
  1385
			Log.get().warning(ex.getMessage());
cli@37
  1386
		}
cli@37
  1387
cli@37
  1388
		try {
cli@37
  1389
			// Try to reinitialize database connection
cli@37
  1390
			arise();
cli@37
  1391
		} catch (SQLException ex) {
cli@37
  1392
			Log.get().warning(ex.getMessage());
cli@37
  1393
			restartConnection(ex);
cli@37
  1394
		}
cli@37
  1395
	}
cli@37
  1396
cli@37
  1397
	@Override
cli@37
  1398
	public boolean update(Article article)
cli@37
  1399
		throws StorageBackendException
cli@37
  1400
	{
cli@37
  1401
		// DELETE FROM headers WHERE article_id = ?
cli@37
  1402
cli@37
  1403
		// INSERT INTO headers ...
cli@37
  1404
cli@37
  1405
		// SELECT * FROM postings WHERE article_id = ? AND group_id = ?
cli@37
  1406
		return false;
cli@37
  1407
	}
cli@37
  1408
cli@37
  1409
	/**
cli@37
  1410
	 * Writes the flags and the name of the given group to the database.
cli@37
  1411
	 * @param group
cli@37
  1412
	 * @throws StorageBackendException
cli@37
  1413
	 */
cli@37
  1414
	@Override
cli@37
  1415
	public boolean update(Group group)
cli@37
  1416
		throws StorageBackendException
cli@37
  1417
	{
cli@37
  1418
		try {
cli@37
  1419
			this.pstmtUpdateGroup.setInt(1, group.getFlags());
cli@37
  1420
			this.pstmtUpdateGroup.setString(2, group.getName());
cli@37
  1421
			this.pstmtUpdateGroup.setLong(3, group.getInternalID());
cli@37
  1422
			int rs = this.pstmtUpdateGroup.executeUpdate();
cli@37
  1423
			return rs == 1;
cli@37
  1424
		} catch (SQLException ex) {
cli@37
  1425
			restartConnection(ex);
cli@37
  1426
			return update(group);
cli@37
  1427
		}
cli@37
  1428
	}
chris@3
  1429
}