Added opened database check. Reviewed code.

This commit is contained in:
Christian P. MOMON 2018-02-28 11:13:38 +01:00
parent b17515229a
commit a17ca91389

View file

@ -80,12 +80,16 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* This method opens a database session. * Instantiates a new SQLSikevaDB.
* *
* @param host * @param driverClassName
* @param port * the driver class name
* @param url
* the url
* @param login * @param login
* the login
* @param password * @param password
* the password
*/ */
public SQLSikevaDB(final String driverClassName, final String url, final String login, final String password) public SQLSikevaDB(final String driverClassName, final String url, final String login, final String password)
{ {
@ -150,14 +154,12 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* This method closes the current database session. * @see fr.devinsy.sikevadb.core.SikevaDB#close()
*
*/ */
@Override @Override
public void close() public void close()
{ {
//
if (this.singleConnection != null) if (this.singleConnection != null)
{ {
try try
@ -174,7 +176,6 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
//
this.dataSource = null; this.dataSource = null;
this.status = Status.CLOSED; this.status = Status.CLOSED;
@ -376,7 +377,6 @@ public class SQLSikevaDB implements SikevaDB
/** /**
* This method creates the schema (table) used by SQLSikevaDB. * This method creates the schema (table) used by SQLSikevaDB.
* *
* @throws IOException
* @throws SikevaDBException * @throws SikevaDBException
*/ */
@Override @Override
@ -448,13 +448,17 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#delete(java.lang.String)
*/ */
@Override @Override
public void delete(final String key) throws SikevaDBException public void delete(final String key) throws SikevaDBException
{ {
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -500,13 +504,17 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#delete(java.lang.String, java.lang.String)
*/ */
@Override @Override
public void delete(final String key, final String subkey) throws SikevaDBException public void delete(final String key, final String subkey) throws SikevaDBException
{ {
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -548,19 +556,22 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#deleteMany(java.lang.String, java.lang.String[])
*/ */
@Override @Override
public void deleteMany(final String key, final String... subkeys) throws SikevaDBException public void deleteMany(final String key, final String... subkeys) throws SikevaDBException
{ {
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
else if ((subkeys != null) && (subkeys.length > 0)) else if ((subkeys != null) && (subkeys.length > 0))
{ {
//
Connection connection = null; Connection connection = null;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -816,7 +827,6 @@ public class SQLSikevaDB implements SikevaDB
ResultSet resultSet = null; ResultSet resultSet = null;
try try
{ {
//
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
@ -824,7 +834,6 @@ public class SQLSikevaDB implements SikevaDB
statement.setString(1, key); statement.setString(1, key);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
//
if (resultSet.next()) if (resultSet.next())
{ {
// //
@ -1280,20 +1289,24 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet); closeQuietly(connection, statement, resultSet);
} }
} }
getValue("", "");
// //
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#getValue(java.lang.String, java.lang.String)
*/ */
@Override @Override
public String getValue(final String key, final String subkey) throws SikevaDBException public String getValue(final String key, final String subkey) throws SikevaDBException
{ {
String result; String result;
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -1303,7 +1316,6 @@ public class SQLSikevaDB implements SikevaDB
} }
else else
{ {
//
Connection connection = null; Connection connection = null;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -1347,42 +1359,49 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#getValues(java.lang.String)
*/ */
@Override @Override
public StringList getValues(final String key) throws SikevaDBException public StringList getValues(final String key) throws SikevaDBException
{ {
StringList result; StringList result;
// if (this.status == Status.CLOSED)
result = new StringList((int) countOfElements(key));
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try
{ {
connection = getConnection(); throw new UnopenedDatabaseException();
connection.setAutoCommit(true); }
statement = connection.prepareStatement("SELECT VALUE,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC"); else
statement.setString(1, key); {
resultSet = statement.executeQuery(); //
result = new StringList((int) countOfElements(key));
while (resultSet.next()) //
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try
{ {
result.add(resultSet.getString(1)); connection = getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT VALUE,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC");
statement.setString(1, key);
resultSet = statement.executeQuery();
while (resultSet.next())
{
result.add(resultSet.getString(1));
}
}
catch (SQLException exception)
{
logger.error("Error getting values.", exception);
throw new SikevaDBException("Error getting values", exception);
}
finally
{
closeQuietly(connection, statement, resultSet);
} }
}
catch (SQLException exception)
{
logger.error("Error getting values.", exception);
throw new SikevaDBException("Error getting values", exception);
}
finally
{
closeQuietly(connection, statement, resultSet);
} }
// //
@ -1390,7 +1409,9 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Checks if is archive off.
* *
* @return true, if is archive off
*/ */
public boolean isArchiveOff() public boolean isArchiveOff()
{ {
@ -1403,7 +1424,9 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Checks if is archive on.
* *
* @return true, if is archive on
*/ */
public boolean isArchiveOn() public boolean isArchiveOn()
{ {
@ -1415,8 +1438,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* * @see fr.devinsy.sikevadb.core.SikevaDB#isClosed()
*/ */
@Override @Override
public boolean isClosed() public boolean isClosed()
@ -1436,8 +1459,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#isCreated()
*/ */
@Override @Override
public boolean isCreated() throws SikevaDBException public boolean isCreated() throws SikevaDBException
@ -1451,8 +1474,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* * @see fr.devinsy.sikevadb.core.SikevaDB#isOpened()
*/ */
@Override @Override
public boolean isOpened() public boolean isOpened()
@ -1465,51 +1488,61 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#memorySize()
*/ */
@Override @Override
public long memorySize() throws SikevaDBException public long memorySize() throws SikevaDBException
{ {
long result; long result;
// if (this.status == Status.CLOSED)
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try
{ {
connection = getConnection(); throw new UnopenedDatabaseException();
connection.setAutoCommit(true); }
statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL"); else
resultSet = statement.executeQuery(); {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try
{
connection = getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL");
resultSet = statement.executeQuery();
resultSet.next(); resultSet.next();
result = resultSet.getLong(1); result = resultSet.getLong(1);
} }
catch (SQLException exception) catch (SQLException exception)
{ {
logger.error("Error computing memory size.", exception); logger.error("Error computing memory size.", exception);
throw new SikevaDBException("Error computing memory size", exception); throw new SikevaDBException("Error computing memory size", exception);
} }
finally finally
{ {
closeQuietly(connection, statement, resultSet); closeQuietly(connection, statement, resultSet);
}
} }
// //
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#memorySize(java.lang.String)
*/ */
@Override @Override
public long memorySize(final String key) throws SikevaDBException public long memorySize(final String key) throws SikevaDBException
{ {
long result; long result;
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -1544,8 +1577,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#memorySize(java.lang.String, java.lang.String)
*/ */
@Override @Override
public long memorySize(final String key, final String subkey) throws SikevaDBException public long memorySize(final String key, final String subkey) throws SikevaDBException
@ -1553,7 +1586,11 @@ public class SQLSikevaDB implements SikevaDB
long result; long result;
// //
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -1563,7 +1600,6 @@ public class SQLSikevaDB implements SikevaDB
} }
else else
{ {
//
Connection connection = null; Connection connection = null;
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -1594,8 +1630,8 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#open()
*/ */
@Override @Override
public void open() throws SikevaDBException public void open() throws SikevaDBException
@ -1653,13 +1689,17 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#put(fr.devinsy.sikevadb.core.Element)
*/ */
@Override @Override
public void put(final Element element) throws SikevaDBException public void put(final Element element) throws SikevaDBException
{ {
if (element == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (element == null)
{ {
throw new IllegalArgumentException("element is null."); throw new IllegalArgumentException("element is null.");
} }
@ -1773,93 +1813,104 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, java.lang.String)
*/ */
@Override @Override
public void put(final String key, final String value) throws SikevaDBException public void put(final String key, final String value) throws SikevaDBException
{ {
// if (this.status == Status.CLOSED)
Element element = getElement(key);
//
if (element == null)
{ {
element = new Element(); throw new UnopenedDatabaseException();
element.setKey(key);
element.update(value);
element.setArchiveDate(null);
} }
else else
{
element.update(value);
}
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try
{ {
// //
connection = getConnection(); Element element = getElement(key);
connection.setAutoCommit(false);
// Archive existing element. //
if (element.getId() != Element.NO_ID) if (element == null)
{ {
statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); element = new Element();
element.setKey(key);
statement.setTimestamp(1, SQLSikevaDBTools.toTimestamp(element.getEditionDate())); element.update(value);
statement.setString(2, element.getKey()); element.setArchiveDate(null);
}
statement.executeUpdate(); else
statement.clearParameters(); {
statement.close(); element.update(value);
} }
// Insert new version of the element. //
statement = connection.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); Connection connection = null;
PreparedStatement statement = null;
statement.setString(1, element.getKey()); ResultSet resultSet = null;
statement.setString(2, element.getSubkey());
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
connection.commit();
}
catch (SQLException exception)
{
logger.error("Error putting element.", exception);
try try
{ {
connection.rollback(); //
connection = getConnection();
connection.setAutoCommit(false);
// Archive existing element.
if (element.getId() != Element.NO_ID)
{
statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL");
statement.setTimestamp(1, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
statement.setString(2, element.getKey());
statement.executeUpdate();
statement.clearParameters();
statement.close();
}
// Insert new version of the element.
statement = connection.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
connection.commit();
} }
catch (SQLException exception1) catch (SQLException exception)
{ {
logger.error("Rollback failed.", exception); logger.error("Error putting element.", exception);
try
{
connection.rollback();
}
catch (SQLException exception1)
{
logger.error("Rollback failed.", exception);
}
throw new SikevaDBException("Error putting element", exception);
}
finally
{
closeQuietly(connection, statement, resultSet);
} }
throw new SikevaDBException("Error putting element", exception);
}
finally
{
closeQuietly(connection, statement, resultSet);
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, java.lang.String, java.lang.String)
*/ */
@Override @Override
public void put(final String key, final String subkey, final String value) throws SikevaDBException public void put(final String key, final String subkey, final String value) throws SikevaDBException
{ {
if (key == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{ {
throw new IllegalArgumentException("Key is null."); throw new IllegalArgumentException("Key is null.");
} }
@ -1946,15 +1997,19 @@ public class SQLSikevaDB implements SikevaDB
} }
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#renameKey(java.lang.String, java.lang.String)
*/ */
@Override @Override
public void renameKey(final String oldKey, final String newKey) throws SikevaDBException public void renameKey(final String oldKey, final String newKey) throws SikevaDBException
{ {
logger.info("renameKey starting... [{}][{}]", oldKey, newKey); logger.info("renameKey starting... [{}][{}]", oldKey, newKey);
if (oldKey == null) if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (oldKey == null)
{ {
throw new IllegalArgumentException("OldKey is null."); throw new IllegalArgumentException("OldKey is null.");
} }
@ -1994,19 +2049,26 @@ public class SQLSikevaDB implements SikevaDB
logger.info("renameKey done."); logger.info("renameKey done.");
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#renameSubKey(java.lang.String, java.lang.String, java.lang.String)
*/ */
@Override @Override
public void renameSubKey(final String key, final String oldKey, final String newKey) throws SikevaDBException public void renameSubKey(final String key, final String oldKey, final String newKey) throws SikevaDBException
{ {
if ((key == null) || (oldKey == null) || (newKey == null)) if (this.status == Status.CLOSED)
{ {
throw new IllegalArgumentException("Null parameter detected [key=" + key + "][oldKey=" + oldKey + "][newKey=" + newKey + "]."); throw new UnopenedDatabaseException();
} }
else else
{ {
// TODO Auto-generated method stub if ((key == null) || (oldKey == null) || (newKey == null))
{
throw new IllegalArgumentException("Null parameter detected [key=" + key + "][oldKey=" + oldKey + "][newKey=" + newKey + "].");
}
else
{
// TODO Auto-generated method stub
}
} }
} }
@ -2016,64 +2078,77 @@ public class SQLSikevaDB implements SikevaDB
@Override @Override
public void replaceInValue(final String key, final String... tokens) throws SikevaDBException public void replaceInValue(final String key, final String... tokens) throws SikevaDBException
{ {
logger.info("replaceInValue starting... [{}]", key); if (this.status == Status.CLOSED)
//
String value = getValue(key);
//
for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2)
{ {
value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); throw new UnopenedDatabaseException();
} }
else
{
logger.info("replaceInValue starting... [{}]", key);
// String value = getValue(key);
put(key, value);
logger.info("replaceInValue done."); for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2)
{
value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]);
}
//
put(key, value);
logger.info("replaceInValue done.");
}
} }
/** /* (non-Javadoc)
* {@inheritDoc} * @see fr.devinsy.sikevadb.core.SikevaDB#replaceInValues(java.lang.String, java.lang.String[])
*/ */
@Override @Override
public void replaceInValues(final String key, final String... tokens) throws SikevaDBException public void replaceInValues(final String key, final String... tokens) throws SikevaDBException
{ {
logger.info("replaceInValues starting... [{}]", key); if (this.status == Status.CLOSED)
//
Elements elements = getElements(key);
long count = 0;
for (Element element : elements)
{ {
// throw new UnopenedDatabaseException();
logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size());
if (element.getSubkey() != null)
{
//
count += 1;
//
String value = element.getValue();
//
for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2)
{
value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]);
}
//
put(element.getKey(), element.getSubkey(), value);
}
} }
else
{
logger.info("replaceInValues starting... [{}]", key);
logger.info("replaceInValues done."); Elements elements = getElements(key);
long count = 0;
for (Element element : elements)
{
logger.info(element.getKey() + " (" + element.getSubkey() + ") " + count + "/" + elements.size());
if (element.getSubkey() != null)
{
//
count += 1;
//
String value = element.getValue();
//
for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2)
{
value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]);
}
//
put(element.getKey(), element.getSubkey(), value);
}
}
logger.info("replaceInValues done.");
}
} }
/** /**
* Sets the archive flag.
* *
* @param value
* the new archive flag
*/ */
public void setArchiveFlag(final boolean value) public void setArchiveFlag(final boolean value)
{ {
@ -2081,8 +2156,10 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Sets the driver classname.
* *
* @param driverClassname * @param driverClassname
* the new driver classname
*/ */
public void setDriverClassname(final String driverClassname) public void setDriverClassname(final String driverClassname)
{ {
@ -2090,8 +2167,10 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Sets the login.
* *
* @param login * @param login
* the new login
*/ */
public void setLogin(final String login) public void setLogin(final String login)
{ {
@ -2099,8 +2178,10 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Sets the password.
* *
* @param password * @param password
* the new password
*/ */
public void setPassword(final String password) public void setPassword(final String password)
{ {
@ -2108,12 +2189,13 @@ public class SQLSikevaDB implements SikevaDB
} }
/** /**
* Sets the url.
* *
* @param url * @param url
* the new url
*/ */
public void setUrl(final String url) public void setUrl(final String url)
{ {
this.url = url; this.url = url;
} }
} }