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