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