Refactored code: added database status check in methods, replaced

NullPointerException with IllegarlArgumentException, fixed SQL create
method, improved code…
This commit is contained in:
Christian P. MOMON 2018-02-28 15:50:48 +01:00
parent 615cc5e908
commit 280e8c7a57
6 changed files with 1523 additions and 1417 deletions

View file

@ -22,23 +22,23 @@ package fr.devinsy.sikevadb.core;
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class UnopenedDatabaseException extends SikevaDBException
public class ClosedDatabaseException extends SikevaDBException
{
private static final long serialVersionUID = 8364599416669077052L;
/**
*
*/
public UnopenedDatabaseException()
public ClosedDatabaseException()
{
super();
super("Invalid database status for this operation: closed.");
}
/**
*
* @param message
*/
public UnopenedDatabaseException(final String message)
public ClosedDatabaseException(final String message)
{
super(message);
}
@ -48,7 +48,7 @@ public class UnopenedDatabaseException extends SikevaDBException
* @param message
* @param cause
*/
public UnopenedDatabaseException(final String message, final Throwable cause)
public ClosedDatabaseException(final String message, final Throwable cause)
{
super(message, cause);
}
@ -57,7 +57,7 @@ public class UnopenedDatabaseException extends SikevaDBException
*
* @param cause
*/
public UnopenedDatabaseException(final Throwable cause)
public ClosedDatabaseException(final Throwable cause)
{
super(cause);
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (C) 2018 Christian Pierre MOMON <christian.momon@devinsy.fr>
*
* This file is part of SikevaDB, simple key value database.
*
* SikevaDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* SikevaDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SikevaDB. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.sikevadb.core;
/**
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class OpenedDatabaseException extends SikevaDBException
{
private static final long serialVersionUID = 8364599416669077052L;
/**
*
*/
public OpenedDatabaseException()
{
super("Invalid database status for this operation: opened.");
}
/**
*
* @param message
*/
public OpenedDatabaseException(final String message)
{
super(message);
}
/**
*
* @param message
* @param cause
*/
public OpenedDatabaseException(final String message, final Throwable cause)
{
super(message, cause);
}
/**
*
* @param cause
*/
public OpenedDatabaseException(final Throwable cause)
{
super(cause);
}
}

View file

@ -81,11 +81,11 @@ public class XMLSikevaDB
{
if (out == null)
{
throw new NullPointerException("out is null.");
throw new IllegalArgumentException("out is null.");
}
else if (source == null)
{
throw new NullPointerException("source is null.");
throw new IllegalArgumentException("source is null.");
}
else if (fileName == null)
{
@ -249,11 +249,11 @@ public class XMLSikevaDB
if (out == null)
{
throw new NullPointerException("out is null.");
throw new IllegalArgumentException("out is null.");
}
else if (source == null)
{
throw new NullPointerException("element is null.");
throw new IllegalArgumentException("element is null.");
}
else
{
@ -307,7 +307,7 @@ public class XMLSikevaDB
{
if (out == null)
{
throw new NullPointerException("out is null.");
throw new IllegalArgumentException("out is null.");
}
else if (source == null)
{

View file

@ -30,8 +30,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.sikevadb.core.Archiver;
import fr.devinsy.sikevadb.core.ClosedDatabaseException;
import fr.devinsy.sikevadb.core.Element;
import fr.devinsy.sikevadb.core.Elements;
import fr.devinsy.sikevadb.core.OpenedDatabaseException;
import fr.devinsy.sikevadb.core.SikevaDB;
import fr.devinsy.sikevadb.core.SikevaDBException;
import fr.devinsy.util.ToolBox;
@ -113,7 +115,19 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public Archiver archiver() throws SikevaDBException
{
return this.archiver;
Archiver result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = this.archiver;
}
//
return result;
}
/* (non-Javadoc)
@ -122,7 +136,11 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public void clear() throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
try
{
@ -137,10 +155,6 @@ public class FileTreeSikevaDB implements SikevaDB
throw new SikevaDBException("Error clearing database", exception);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
@ -149,16 +163,16 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public void close() throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
this.archiver.close();
this.status = Status.CLOSED;
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
@ -169,9 +183,12 @@ public class FileTreeSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
File[] topFiles = this.dataDirectory.listFiles();
result = 0;
@ -188,10 +205,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -205,9 +218,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -225,11 +240,6 @@ public class FileTreeSikevaDB implements SikevaDB
result = subFiles.length;
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -243,7 +253,14 @@ public class FileTreeSikevaDB implements SikevaDB
{
long result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = countOfElements(key, String.valueOf(subkey));
}
//
return result;
@ -257,9 +274,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
if ((key == null) || (subkey == null))
throw new ClosedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -276,11 +295,6 @@ public class FileTreeSikevaDB implements SikevaDB
result = 0;
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -292,9 +306,9 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public void create() throws SikevaDBException
{
if (this.status == Status.OPENED)
if (isOpened())
{
throw new SikevaDBException("Invalid database status (opened).");
throw new OpenedDatabaseException();
}
else if (this.homeDirectory == null)
{
@ -318,9 +332,11 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public void delete(final String key) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -342,20 +358,22 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#delete(java.lang.String, long)
*/
@Override
public void delete(final String key, final long subkey) throws SikevaDBException
{
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
delete(key, String.valueOf(subkey));
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#delete(java.lang.String, java.lang.String)
@ -363,9 +381,11 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public void delete(final String key, final String subkey) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if ((key == null) || (subkey == null))
throw new ClosedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -388,11 +408,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#deleteMany(java.lang.String, java.lang.String[])
@ -400,9 +415,11 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public void deleteMany(final String key, final String... subkeys) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -417,11 +434,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (opened).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#destroy()
@ -431,7 +443,7 @@ public class FileTreeSikevaDB implements SikevaDB
{
if (isOpened())
{
throw new SikevaDBException("Invalid database status (opened).");
throw new OpenedDatabaseException();
}
else if (this.homeDirectory == null)
{
@ -547,9 +559,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
Element result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -563,11 +577,6 @@ public class FileTreeSikevaDB implements SikevaDB
result.setSubkey(null);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -581,7 +590,14 @@ public class FileTreeSikevaDB implements SikevaDB
{
Element result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = getElement(key, String.valueOf(subkey));
}
//
return result;
@ -595,9 +611,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
Element result;
if (isOpened())
if (isClosed())
{
if ((key == null) || (subkey == null))
throw new ClosedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -614,11 +632,6 @@ public class FileTreeSikevaDB implements SikevaDB
result.setSubkey(subkey);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -632,7 +645,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
Elements result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = new Elements((int) countOfElements());
@ -661,10 +678,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -678,9 +691,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
Elements result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -700,11 +715,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -733,7 +743,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
StringList result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = new StringList((int) countOfElements());
@ -758,10 +772,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -805,9 +815,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
StringList result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -826,11 +838,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -844,9 +851,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
String result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -864,11 +873,6 @@ public class FileTreeSikevaDB implements SikevaDB
result = null;
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -882,7 +886,14 @@ public class FileTreeSikevaDB implements SikevaDB
{
String result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = getValue(key, String.valueOf(subkey));
}
//
return result;
@ -896,9 +907,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
String result;
if (isOpened())
if (isClosed())
{
if ((key == null) || (subkey == null))
throw new ClosedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -917,11 +930,6 @@ public class FileTreeSikevaDB implements SikevaDB
result = null;
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -935,9 +943,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
StringList result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -964,11 +974,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -982,7 +987,7 @@ public class FileTreeSikevaDB implements SikevaDB
{
boolean result;
if (this.status == Status.OPENED)
if (isOpened())
{
result = this.archiver.isActivated();
}
@ -1052,7 +1057,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
result = 0;
@ -1083,10 +1092,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1100,7 +1105,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
if (key == null)
{
@ -1131,10 +1140,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1148,7 +1153,14 @@ public class FileTreeSikevaDB implements SikevaDB
{
long result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = memorySize(key, String.valueOf(subkey));
}
//
return result;
@ -1162,7 +1174,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
if ((key == null) || (subkey == null))
{
@ -1185,10 +1201,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1224,9 +1236,11 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public void put(final Element element) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if (element == null)
throw new ClosedDatabaseException();
}
else if (element == null)
{
throw new IllegalArgumentException("element is null.");
}
@ -1262,20 +1276,22 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, long, java.lang.String)
*/
@Override
public void put(final String key, final long subkey, final String value) throws SikevaDBException
{
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
put(key, String.valueOf(subkey), value);
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, java.lang.String)
@ -1283,9 +1299,11 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public void put(final String key, final String value) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -1311,11 +1329,6 @@ public class FileTreeSikevaDB implements SikevaDB
put(element);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, java.lang.String, java.lang.String)
@ -1323,9 +1336,11 @@ public class FileTreeSikevaDB implements SikevaDB
@Override
public void put(final String key, final String subkey, final String value) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if ((key == null) || (subkey == null))
throw new ClosedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -1352,11 +1367,6 @@ public class FileTreeSikevaDB implements SikevaDB
put(element);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#renameKey(java.lang.String, java.lang.String)
@ -1366,9 +1376,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
this.logger.debug("renameKey starting... [{}][{}]", oldKey, newKey);
if (isOpened())
if (isClosed())
{
if (oldKey == null)
throw new ClosedDatabaseException();
}
else if (oldKey == null)
{
throw new IllegalArgumentException("OldKey is null.");
}
@ -1397,11 +1409,6 @@ public class FileTreeSikevaDB implements SikevaDB
oldFile.renameTo(newFile);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
this.logger.debug("renameKey done.");
}
@ -1411,9 +1418,16 @@ public class FileTreeSikevaDB implements SikevaDB
*/
@Override
public void renameSubkey(final String key, final long oldSubKey, final long newSubkey) throws SikevaDBException
{
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
renameSubkey(key, String.valueOf(oldSubKey), String.valueOf(newSubkey));
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#renameSubkey(java.lang.String, java.lang.String, java.lang.String)
@ -1423,9 +1437,11 @@ public class FileTreeSikevaDB implements SikevaDB
{
this.logger.debug("renameSybKey starting... [{}][{}][{}]", oldSubkey, newSubkey);
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Top key is null.");
}
@ -1459,11 +1475,6 @@ public class FileTreeSikevaDB implements SikevaDB
throw new IllegalArgumentException("Invalid oldKey [" + oldSubkey + "].");
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
this.logger.debug("renameSubKey done.");
}
@ -1476,24 +1487,21 @@ public class FileTreeSikevaDB implements SikevaDB
{
this.logger.info("replaceInValue starting... [{}]", key);
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
String value = getValue(key);
//
for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2)
{
value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]);
}
//
put(key, value);
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
this.logger.info("replaceInValue done.");
}
@ -1506,9 +1514,12 @@ public class FileTreeSikevaDB implements SikevaDB
{
this.logger.info("replaceInValues starting... [{}]", key);
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
Elements elements = getElements(key);
long count = 0;
@ -1535,10 +1546,6 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
this.logger.info("replaceInValues done.");
}

View file

@ -37,8 +37,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.sikevadb.core.Archiver;
import fr.devinsy.sikevadb.core.ClosedDatabaseException;
import fr.devinsy.sikevadb.core.Element;
import fr.devinsy.sikevadb.core.Elements;
import fr.devinsy.sikevadb.core.OpenedDatabaseException;
import fr.devinsy.sikevadb.core.SikevaDB;
import fr.devinsy.sikevadb.core.SikevaDBException;
import fr.devinsy.util.strings.StringList;
@ -64,8 +66,10 @@ public class SQLSikevaDB implements SikevaDB
private String url;
private String login;
private String password;
private Connection singleConnection;
private String contextName;
private Connection singleConnection;
private DataSource dataSource;
private Archiver archiver;
@ -98,19 +102,19 @@ public class SQLSikevaDB implements SikevaDB
{
if (StringUtils.isBlank(driverClassName))
{
throw new NullPointerException("driverClassName is null.");
throw new IllegalArgumentException("driverClassName is null.");
}
else if (StringUtils.isBlank(url))
{
throw new NullPointerException("url is null.");
throw new IllegalArgumentException("url is null.");
}
else if (StringUtils.isBlank(login))
{
throw new NullPointerException("login is null.");
throw new IllegalArgumentException("login is null.");
}
else if (password == null)
{
throw new NullPointerException("password is null");
throw new IllegalArgumentException("password is null.");
}
else
{
@ -126,11 +130,20 @@ public class SQLSikevaDB implements SikevaDB
/**
* Activate archiver.
*
* @throws ClosedDatabaseException
*/
public void activateArchiver()
public void activateArchiver() throws ClosedDatabaseException
{
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
this.archiver.activate();
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#archiver()
@ -140,8 +153,16 @@ public class SQLSikevaDB implements SikevaDB
{
Archiver result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = this.archiver;
}
//
return result;
}
@ -151,7 +172,11 @@ public class SQLSikevaDB implements SikevaDB
@Override
public void clear() throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
Connection connection = null;
PreparedStatement statement = null;
@ -173,10 +198,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
@ -268,7 +289,11 @@ public class SQLSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
Connection connection = null;
Statement statement = null;
@ -293,10 +318,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -310,9 +331,11 @@ public class SQLSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -342,11 +365,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -360,7 +378,14 @@ public class SQLSikevaDB implements SikevaDB
{
long result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = countOfElements(key, String.valueOf(subkey));
}
//
return result;
@ -374,9 +399,11 @@ public class SQLSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -411,11 +438,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -427,12 +449,15 @@ public class SQLSikevaDB implements SikevaDB
@Override
public void create() throws SikevaDBException
{
if (this.status == Status.CLOSED)
if (isOpened())
{
throw new SikevaDBException("Invalid state.");
throw new OpenedDatabaseException();
}
else
{
//
open();
//
Connection connection = null;
Statement statement = null;
@ -475,6 +500,9 @@ public class SQLSikevaDB implements SikevaDB
System.out.println("============================== APRÈS2");
}
//
close();
}
catch (SQLException exception)
{
@ -499,9 +527,11 @@ public class SQLSikevaDB implements SikevaDB
@Override
public void delete(final String key) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -546,20 +576,22 @@ public class SQLSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#delete(java.lang.String, long)
*/
@Override
public void delete(final String key, final long subkey) throws SikevaDBException
{
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
delete(key, String.valueOf(subkey));
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#delete(java.lang.String, java.lang.String)
@ -567,9 +599,11 @@ public class SQLSikevaDB implements SikevaDB
@Override
public void delete(final String key, final String subkey) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -610,11 +644,6 @@ public class SQLSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (opened).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#deleteMany(java.lang.String, java.lang.String[])
@ -622,9 +651,11 @@ public class SQLSikevaDB implements SikevaDB
@Override
public void deleteMany(final String key, final String... subkeys) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -669,20 +700,22 @@ public class SQLSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#destroy()
*/
@Override
public void destroy() throws SikevaDBException
{
if (isOpened())
{
throw new OpenedDatabaseException();
}
else
{
// TODO
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#exists()
@ -712,6 +745,12 @@ public class SQLSikevaDB implements SikevaDB
{
boolean result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
if (element == null)
{
result = false;
@ -724,6 +763,7 @@ public class SQLSikevaDB implements SikevaDB
{
result = true;
}
}
//
return result;
@ -758,7 +798,7 @@ public class SQLSikevaDB implements SikevaDB
}
else
{
throw new NullPointerException("Connection is not initialized.");
throw new IllegalArgumentException("Connection not initialized.");
}
//
@ -808,9 +848,11 @@ public class SQLSikevaDB implements SikevaDB
{
Element result;
if (isOpened())
if (isClosed())
{
if (id == Element.NO_ID)
throw new ClosedDatabaseException();
}
else if (id == Element.NO_ID)
{
result = null;
}
@ -865,11 +907,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -883,9 +920,11 @@ public class SQLSikevaDB implements SikevaDB
{
Element result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -941,11 +980,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -959,7 +993,14 @@ public class SQLSikevaDB implements SikevaDB
{
Element result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = getElement(key, String.valueOf(subkey));
}
//
return result;
@ -973,9 +1014,11 @@ public class SQLSikevaDB implements SikevaDB
{
Element result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -1037,11 +1080,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1055,7 +1093,11 @@ public class SQLSikevaDB implements SikevaDB
{
Elements result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
result = new Elements((int) countOfElements());
@ -1101,10 +1143,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1118,9 +1156,11 @@ public class SQLSikevaDB implements SikevaDB
{
Elements result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -1171,11 +1211,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1189,7 +1224,11 @@ public class SQLSikevaDB implements SikevaDB
{
StringList result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
result = new StringList();
@ -1220,10 +1259,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1267,9 +1302,11 @@ public class SQLSikevaDB implements SikevaDB
{
StringList result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -1306,11 +1343,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1339,9 +1371,11 @@ public class SQLSikevaDB implements SikevaDB
{
String result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -1385,11 +1419,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1403,7 +1432,14 @@ public class SQLSikevaDB implements SikevaDB
{
String result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = getValue(key, String.valueOf(subkey));
}
//
return result;
@ -1417,8 +1453,10 @@ public class SQLSikevaDB implements SikevaDB
{
String result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
if (key == null)
{
throw new IllegalArgumentException("Key is null.");
@ -1468,11 +1506,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1486,7 +1519,11 @@ public class SQLSikevaDB implements SikevaDB
{
StringList result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
result = new StringList((int) countOfElements(key));
@ -1518,10 +1555,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1535,7 +1568,7 @@ public class SQLSikevaDB implements SikevaDB
{
boolean result;
if (this.status == Status.OPENED)
if (isOpened())
{
result = this.archiver.isActivated();
}
@ -1605,7 +1638,11 @@ public class SQLSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
Connection connection = null;
@ -1631,10 +1668,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1648,8 +1681,10 @@ public class SQLSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
if (key == null)
{
throw new IllegalArgumentException("Key is null.");
@ -1680,11 +1715,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1698,7 +1728,14 @@ public class SQLSikevaDB implements SikevaDB
{
long result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
result = memorySize(key, String.valueOf(subkey));
}
//
return result;
@ -1712,9 +1749,11 @@ public class SQLSikevaDB implements SikevaDB
{
long result;
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -1750,11 +1789,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
//
return result;
@ -1827,9 +1861,11 @@ public class SQLSikevaDB implements SikevaDB
@Override
public void put(final Element element) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if (element == null)
throw new ClosedDatabaseException();
}
else if (element == null)
{
throw new IllegalArgumentException("element is null.");
}
@ -1882,8 +1918,7 @@ public class SQLSikevaDB implements SikevaDB
{
connection = getConnection();
connection.setAutoCommit(true);
statement = connection
.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
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());
@ -1943,20 +1978,22 @@ public class SQLSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, long, java.lang.String)
*/
@Override
public void put(final String key, final long subkey, final String value) throws SikevaDBException
{
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
put(key, String.valueOf(subkey), String.valueOf(value));
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, java.lang.String)
@ -1964,7 +2001,11 @@ public class SQLSikevaDB implements SikevaDB
@Override
public void put(final String key, final String value) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
Element element = getElement(key);
@ -2039,10 +2080,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
@ -2051,9 +2088,11 @@ public class SQLSikevaDB implements SikevaDB
@Override
public void put(final String key, final String subkey, final String value) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if (key == null)
throw new ClosedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Key is null.");
}
@ -2139,11 +2178,6 @@ public class SQLSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#renameKey(java.lang.String, java.lang.String)
@ -2153,9 +2187,11 @@ public class SQLSikevaDB implements SikevaDB
{
logger.info("renameKey starting... [{}][{}]", oldKey, newKey);
if (isOpened())
if (isClosed())
{
if (oldKey == null)
throw new ClosedDatabaseException();
}
else if (oldKey == null)
{
throw new IllegalArgumentException("OldKey is null.");
}
@ -2191,11 +2227,6 @@ public class SQLSikevaDB implements SikevaDB
closeQuietly(connection, statement, resultSet);
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
logger.info("renameKey done.");
}
@ -2205,9 +2236,16 @@ public class SQLSikevaDB implements SikevaDB
*/
@Override
public void renameSubkey(final String key, final long oldSubkey, final long newSubkey) throws SikevaDBException
{
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
renameSubkey(key, String.valueOf(oldSubkey), String.valueOf(newSubkey));
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#renameSubkey(java.lang.String, java.lang.String, java.lang.String)
@ -2215,9 +2253,11 @@ public class SQLSikevaDB implements SikevaDB
@Override
public void renameSubkey(final String key, final String oldKey, final String newKey) throws SikevaDBException
{
if (isOpened())
if (isClosed())
{
if ((key == null) || (oldKey == null) || (newKey == null))
throw new ClosedDatabaseException();
}
else if ((key == null) || (oldKey == null) || (newKey == null))
{
throw new IllegalArgumentException("Null parameter detected [key=" + key + "][oldKey=" + oldKey + "][newKey=" + newKey + "].");
}
@ -2226,11 +2266,6 @@ public class SQLSikevaDB implements SikevaDB
// TODO Auto-generated method stub
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#replaceInValue(java.lang.String, java.lang.String[])
@ -2240,7 +2275,11 @@ public class SQLSikevaDB implements SikevaDB
{
logger.info("replaceInValue starting... [{}]", key);
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
String value = getValue(key);
@ -2254,10 +2293,6 @@ public class SQLSikevaDB implements SikevaDB
//
put(key, value);
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
logger.info("replaceInValue done.");
}
@ -2270,7 +2305,11 @@ public class SQLSikevaDB implements SikevaDB
{
logger.info("replaceInValues starting... [{}]", key);
if (isOpened())
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
Elements elements = getElements(key);
@ -2298,10 +2337,6 @@ public class SQLSikevaDB implements SikevaDB
}
}
}
else
{
throw new SikevaDBException("Invalid database status (closed).");
}
logger.info("replaceInValues done.");
}

View file

@ -701,7 +701,7 @@ public class SQLSikevaDBTest
// "12345678");
database = new SQLSikevaDB("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:sikevadb-unittest;sql.syntax_mys=true", "sa", "");
database.open();
database.create();
database.open();
}
}