Added opened database test in method for FileTree implementation.

This commit is contained in:
Christian P. MOMON 2018-02-28 10:29:13 +01:00
parent 4f1a466007
commit eaacc3a3d6
2 changed files with 533 additions and 389 deletions

View file

@ -1,5 +1,5 @@
/**
* Copyright (C) 2013-2017 Christian Pierre MOMON <christian.momon@devinsy.fr>
* Copyright (C) 2013-2018 Christian Pierre MOMON <christian.momon@devinsy.fr>
*
* This file is part of SikevaDB, simple key value database.
*
@ -32,6 +32,7 @@ import fr.devinsy.sikevadb.core.Element;
import fr.devinsy.sikevadb.core.Elements;
import fr.devinsy.sikevadb.core.SikevaDB;
import fr.devinsy.sikevadb.core.SikevaDBException;
import fr.devinsy.sikevadb.core.UnopenedDatabaseException;
import fr.devinsy.util.strings.StringList;
/**
@ -83,11 +84,17 @@ public class FileTreeSikevaDB implements SikevaDB
this.journalizer = null;
}
/**
* @throws IOException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#clear()
*/
@Override
public void clear() throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
try
{
@ -102,27 +109,40 @@ public class FileTreeSikevaDB implements SikevaDB
throw new SikevaDBException("Error clearing database", exception);
}
}
}
/**
*
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#close()
*/
@Override
public void close()
public void close() throws UnopenedDatabaseException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
this.journalizer = null;
this.status = Status.CLOSED;
}
}
/**
* {@inheritDoc}
*
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#countOfElements()
*/
@Override
public long countOfElements()
public long countOfElements() throws UnopenedDatabaseException
{
long result;
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
File[] topFiles = this.dataDirectory.listFiles();
result = 0;
@ -138,20 +158,26 @@ public class FileTreeSikevaDB implements SikevaDB
result += 1;
}
}
}
//
return result;
}
/**
* {@inheritDoc}
*
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#countOfElements(java.lang.String)
*/
@Override
public long countOfElements(final String key)
public long countOfElements(final String key) throws UnopenedDatabaseException
{
long result;
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
@ -170,19 +196,26 @@ public class FileTreeSikevaDB implements SikevaDB
result = subFiles.length;
}
}
}
//
return result;
}
/**
* {@inheritDoc}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#countOfElements(java.lang.String, java.lang.String)
*/
@Override
public long countOfElements(final String key, final String subkey)
public long countOfElements(final String key, final String subkey) throws UnopenedDatabaseException
{
long result;
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
@ -200,16 +233,22 @@ public class FileTreeSikevaDB implements SikevaDB
result = 0;
}
}
}
//
return result;
}
/**
* {@inheritDoc}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#create()
*/
@Override
public void create() throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
if (this.status == Status.OPENED)
{
@ -230,35 +269,50 @@ public class FileTreeSikevaDB implements SikevaDB
this.configDirectory.mkdir();
}
}
}
/**
* Gets the config directory.
*
* @return
* @return the config directory
*/
public File getConfigDirectory()
{
return this.configDirectory;
File result;
result = this.configDirectory;
return result;
}
/**
* Gets the data directory.
*
* @return
* @return the data directory
*/
public File getDataDirectory()
{
return this.dataDirectory;
File result;
result = this.dataDirectory;
//
return result;
}
/**
* {@inheritDoc}
*
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getElement(java.lang.String)
*/
@Override
public Element getElement(final String key) throws SikevaDBException
{
Element result;
if (key == null)
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -277,17 +331,19 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
*
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getElement(java.lang.String, java.lang.String)
*/
@Override
public Element getElement(final String key, final String subkey) throws SikevaDBException
{
Element result;
//
if ((key == null) || (subkey == null))
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -309,14 +365,20 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getElements()
*/
@Override
public Elements getElements() throws SikevaDBException
{
Elements result;
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
result = new Elements((int) countOfElements());
File[] topFiles = this.dataDirectory.listFiles();
@ -343,20 +405,25 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
}
//
return result;
}
/**
* {@inheritDoc}
*
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getElements(java.lang.String)
*/
@Override
public Elements getElements(final String key) throws SikevaDBException
{
Elements result;
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
@ -377,31 +444,41 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
}
//
return result;
}
/**
* Gets the file tree directory.
*
* @return
* @return the file tree directory
*/
public File getFileTreeDirectory()
{
return this.fileTreeDirectory;
File result;
result = this.fileTreeDirectory;
//
return result;
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getKeys()
*/
@Override
public StringList getKeys() throws SikevaDBException
{
StringList result;
//
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
result = new StringList((int) countOfElements());
File[] topFiles = this.dataDirectory.listFiles();
@ -416,40 +493,55 @@ public class FileTreeSikevaDB implements SikevaDB
result.add(file.getName());
}
}
}
//
return result;
}
/**
* Gets the login.
*
* @return
* @return the login
*/
public String getLogin()
{
return this.login;
String result;
result = this.login;
//
return result;
}
/**
* Gets the password.
*
* @return
* @return the password
*/
public String getPassword()
{
return this.password;
String result;
result = this.password;
//
return result;
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getSubkeys(java.lang.String)
*/
@Override
public StringList getSubkeys(final String key) throws SikevaDBException
{
StringList result;
if (key == null)
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -473,17 +565,19 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getValue(java.lang.String)
*/
@Override
public String getValue(final String key) throws SikevaDBException
{
String result;
if (key == null)
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -506,17 +600,19 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getValue(java.lang.String, java.lang.String)
*/
@Override
public String getValue(final String key, final String subkey) throws SikevaDBException
{
String result;
if ((key == null) || (subkey == null))
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -540,17 +636,19 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getValues(java.lang.String)
*/
@Override
public StringList getValues(final String key) throws SikevaDBException
{
StringList result;
if (key == null)
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -582,9 +680,8 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
*
* @return
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#isClosed()
*/
@Override
public boolean isClosed()
@ -597,8 +694,8 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#isCreated()
*/
@Override
public boolean isCreated() throws SikevaDBException
@ -618,9 +715,8 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
*
* @return
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#isOpened()
*/
@Override
public boolean isOpened()
@ -640,16 +736,20 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#memorySize()
*/
@Override
public long memorySize() throws SikevaDBException
{
long result;
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
//
result = 0;
@ -678,22 +778,25 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
}
}
//
return result;
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#memorySize(java.lang.String)
*/
@Override
public long memorySize(final String key) throws SikevaDBException
{
long result;
if (key == null)
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -726,17 +829,19 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#memorySize(java.lang.String, java.lang.String)
*/
@Override
public long memorySize(final String key, final String subkey) throws SikevaDBException
{
long result;
if ((key == null) || (subkey == null))
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -761,8 +866,8 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#open()
*/
@Override
public void open() throws SikevaDBException
@ -774,15 +879,17 @@ public class FileTreeSikevaDB implements SikevaDB
this.status = Status.OPENED;
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#put(fr.devinsy.sikevadb.core.Element)
*/
@Override
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.");
}
@ -819,15 +926,17 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, java.lang.String)
*/
@Override
public void put(final String key, final String value) throws SikevaDBException
{
if (key == null)
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -854,15 +963,17 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#put(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public void put(final String key, final String subkey, final String value) throws SikevaDBException
{
if ((key == null) || (subkey == null))
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -890,15 +1001,17 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#remove(java.lang.String)
*/
@Override
public void remove(final String key) throws SikevaDBException
{
if (key == null)
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
@ -921,15 +1034,17 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#remove(java.lang.String, java.lang.String)
*/
@Override
public void remove(final String key, final String subkey) throws SikevaDBException
{
if ((key == null) || (subkey == null))
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
@ -953,15 +1068,17 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#removeMany(java.lang.String, java.lang.String[])
*/
@Override
public void removeMany(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("Null key detected.");
}
@ -977,11 +1094,17 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
/**
* {@inheritDoc}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#renameKey(java.lang.String, java.lang.String)
*/
@Override
public void renameKey(final String oldKey, final String newKey)
public void renameKey(final String oldKey, final String newKey) throws UnopenedDatabaseException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
this.logger.debug("renameKey starting... [{}][{}]", oldKey, newKey);
@ -1017,13 +1140,21 @@ public class FileTreeSikevaDB implements SikevaDB
this.logger.debug("renameKey done.");
}
}
/**
* {@inheritDoc}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#renameSubKey(java.lang.String, java.lang.String, java.lang.String)
*/
@Override
public void renameSubKey(final String key, final String oldSubkey, final String newSubkey) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
this.logger.debug("renameSybKey starting... [{}][{}][{}]", oldSubkey, newSubkey);
if (key == null)
@ -1063,14 +1194,19 @@ public class FileTreeSikevaDB implements SikevaDB
this.logger.debug("renameSubKey done.");
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#replaceInValue(java.lang.String, java.lang.String[])
*/
@Override
public void replaceInValue(final String key, final String... tokens) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
this.logger.info("replaceInValue starting... [{}]", key);
@ -1088,14 +1224,19 @@ public class FileTreeSikevaDB implements SikevaDB
this.logger.info("replaceInValue done.");
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#replaceInValues(java.lang.String, java.lang.String[])
*/
@Override
public void replaceInValues(final String key, final String... tokens) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else
{
this.logger.info("replaceInValues starting... [{}]", key);
@ -1128,6 +1269,7 @@ public class FileTreeSikevaDB implements SikevaDB
this.logger.info("replaceInValues done.");
}
}
/**
*

View file

@ -1,5 +1,5 @@
/**
* Copyright (C) 2013-2017 Christian Pierre MOMON, DEVINSY
* Copyright (C) 2013-2018 Christian Pierre MOMON, DEVINSY
*
* This file is part of SikevaDB, simple key value database.
*
@ -34,6 +34,7 @@ import org.junit.Test;
import fr.devinsy.sikevadb.core.Element;
import fr.devinsy.sikevadb.core.Elements;
import fr.devinsy.sikevadb.core.SikevaDBException;
import fr.devinsy.sikevadb.core.UnopenedDatabaseException;
import fr.devinsy.util.strings.StringList;
/**
@ -607,10 +608,11 @@ public class TreeFileSikevaDBTest
}
/**
* @throws UnopenedDatabaseException
*
*/
@AfterClass
public static void afterClass()
public static void afterClass() throws UnopenedDatabaseException
{
if (database != null)
{