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