Compare commits

...

11 commits
1.17 ... badway

7 changed files with 1794 additions and 1123 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.
*
@ -21,73 +21,349 @@ package fr.devinsy.sikevadb.core;
import fr.devinsy.util.strings.StringList;
/**
*
* The Interface SikevaDB.
*
* @author Christian Pierre MOMON
*/
public interface SikevaDB
{
/**
* Delete all elements of current opened database.
*
* @throws SikevaDBException
* the SikevaDB exception
*/
public void clear() throws SikevaDBException;
/**
* Close the current database session.
*
* @throws SikevaDBException
* the SikevaDB exception
*/
public void close() throws SikevaDBException;
/**
* Count of elements in current database.
*
* @return the long
* @throws SikevaDBException
* the SikevaDB exception
*/
public long countOfElements() throws SikevaDBException;
/**
* Count of elements by key.
*
* @param key
* the key
* @return the long
* @throws SikevaDBException
* the SikevaDB exception
*/
public long countOfElements(String key) throws SikevaDBException;
/**
* Count of elements by key and subkey.
*
* @param key
* the key
* @param subkey
* the subkey
* @return the long
* @throws SikevaDBException
* the SikevaDB exception
*/
public long countOfElements(String key, String subkey) throws SikevaDBException;
/**
* Creates the database.
*
* @throws SikevaDBException
* the SikevaDB exception
*/
public void create() throws SikevaDBException;
/**
* Removes the.
*
* @param key
* the key
* @throws SikevaDBException
* the SikevaDB exception
*/
void delete(final String key) throws SikevaDBException;
/**
* Delete.
*
* @param key
* the key
* @param subkey
* the subkey
* @throws SikevaDBException
* the SikevaDB exception
*/
void delete(final String key, final String subkey) throws SikevaDBException;
/**
* Delete many.
*
* @param key
* the key
* @param subkeys
* the subkeys
* @throws SikevaDBException
* the SikevaDB exception
*/
void deleteMany(final String key, final String... subkeys) throws SikevaDBException;
/**
* Gets an element by key.
*
* @param key
* the key
* @return the element
* @throws SikevaDBException
* the SikevaDB exception
*/
public Element getElement(String key) throws SikevaDBException;
/**
* Gets an element by key and sub key.
*
* @param key
* the key
* @param subkey
* the sub key
* @return the element
* @throws SikevaDBException
* the SikevaDB exception
*/
public Element getElement(String key, String subkey) throws SikevaDBException;
/**
* Gets all elements in current database.
*
* @return the elements
* @throws SikevaDBException
* the SikevaDB exception
*/
public Elements getElements() throws SikevaDBException;
/**
* Gets all elements of a top key.
*
* @param key
* the key
* @return the elements
* @throws SikevaDBException
* the SikevaDB exception
*/
public Elements getElements(String key) throws SikevaDBException;
public fr.devinsy.util.strings.StringList getKeys() throws SikevaDBException;
/**
* Gets all the top keys.
*
* @return the keys
* @throws SikevaDBException
* the SikevaDB exception
*/
public StringList getKeys() throws SikevaDBException;
/**
* Gets all the sub keys of a key.
*
* @param key
* the key
* @return the subkeys
* @throws SikevaDBException
* the SikevaDB exception
*/
public StringList getSubkeys(String key) throws SikevaDBException;
/**
* Gets the value of a key.
*
* @param key
* the key
* @return the value
* @throws SikevaDBException
* the SikevaDB exception
*/
public String getValue(String key) throws SikevaDBException;
/**
* Gets the value of a key and a sub key.
*
* @param key
* the key
* @param subkey
* the sub key
* @return the value
* @throws SikevaDBException
* the SikevaDB exception
*/
public String getValue(String key, String subkey) throws SikevaDBException;
/**
* Gets the values of a key.
*
* @param key
* the key
* @return the values
* @throws SikevaDBException
* the SikevaDB exception
*/
public StringList getValues(String key) throws SikevaDBException;
/**
* Checks if is closed.
*
* @return true, if is closed
*/
public boolean isClosed();
/**
* Checks if is created.
*
* @return true, if is created
* @throws SikevaDBException
* the SikevaDB exception
*/
public boolean isCreated() throws SikevaDBException;
/**
* Checks if is opened.
*
* @return true, if is opened
*/
public boolean isOpened();
/**
* Returns the memory size of the elements in database.
*
* @return the long
* @throws SikevaDBException
* the SikevaDB exception
*/
public long memorySize() throws SikevaDBException;
/**
* Returns the memory size of elements of a key.
*
* @param key
* the key
* @return the long
* @throws SikevaDBException
* the SikevaDB exception
*/
public long memorySize(String key) throws SikevaDBException;
/**
* Returns the memory size of element of a key and a sub key.
*
* @param key
* the key
* @param subkey
* the sub key
* @return the long
* @throws SikevaDBException
* the SikevaDB exception
*/
public long memorySize(String key, String subkey) throws SikevaDBException;
/**
* Open a database.
*
* @throws SikevaDBException
* the SikevaDB exception
*/
void open() throws SikevaDBException;
/**
* Put an element in database.
*
* @param element
* the element
* @throws SikevaDBException
* the SikevaDB exception
*/
void put(Element element) throws SikevaDBException;
/**
* Put a value for a key in current opened database.
*
* @param key
* the key
* @param value
* the value
* @throws SikevaDBException
* the SikevaDB exception
*/
void put(String key, String value) throws SikevaDBException;
/**
* Put a value for a key and a sub key in current opened database.
*
* @param key
* the key
* @param subkey
* the subkey
* @param value
* the value
* @throws SikevaDBException
* the SikevaDB exception
*/
void put(String key, String subkey, String value) throws SikevaDBException;
void remove(final String key) throws SikevaDBException;
void remove(final String key, final String subkey) throws SikevaDBException;
void removeMany(final String key, final String... subkeys) throws SikevaDBException;
/**
* Rename a key.
*
* @param oldKey
* the old key
* @param newKey
* the new key
* @throws SikevaDBException
* the SikevaDB exception
*/
void renameKey(final String oldKey, final String newKey) throws SikevaDBException;
/**
* Rename a sub key.
*
* @param key
* the key
* @param oldSubKey
* the old sub key
* @param newSubKey
* the new sub key
* @throws SikevaDBException
* the SikevaDB exception
*/
void renameSubKey(final String key, final String oldSubKey, final String newSubKey) throws SikevaDBException;
/**
* Replace a substring in a value of a key.
*
* @param key
* the key
* @param tokens
* the tokens
* @throws SikevaDBException
* the SikevaDB exception
*/
void replaceInValue(final String key, final String... tokens) throws SikevaDBException;
/**
* Replace a substring in values of all sub keys of a key.
*
* @param key
* the key
* @param tokens
* the tokens
* @throws SikevaDBException
* the SikevaDB exception
*/
void replaceInValues(final String key, final String... tokens) throws SikevaDBException;
}

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 UnopenedDatabaseException extends SikevaDBException
{
private static final long serialVersionUID = 8364599416669077052L;
/**
*
*/
public UnopenedDatabaseException()
{
super();
}
/**
*
* @param message
*/
public UnopenedDatabaseException(final String message)
{
super(message);
}
/**
*
* @param message
* @param cause
*/
public UnopenedDatabaseException(final String message, final Throwable cause)
{
super(message, cause);
}
/**
*
* @param cause
*/
public UnopenedDatabaseException(final Throwable cause)
{
super(cause);
}
}

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.
*
@ -75,11 +75,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)
{
@ -202,7 +202,6 @@ public class XMLSikevaDB
{
while (in.hasNextStartTag("element"))
{
//
Element element = readElement(in);
@ -225,11 +224,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
{
@ -278,7 +277,7 @@ public class XMLSikevaDB
{
if (out == null)
{
throw new NullPointerException("out is null.");
throw new IllegalArgumentException("out is null.");
}
else if (source == null)
{
@ -290,19 +289,16 @@ public class XMLSikevaDB
for (String key : source.getKeys())
{
//
Element root = source.getElement(key);
write(out, root);
//
Elements elements = source.getElements(key);
for (Element element : elements)
{
write(out, element);
}
//
for (String subkey : source.getSubkeys(key))
{
Element subElement = source.getElement(key, subkey);
write(out, subElement);
}
}
out.writeEndTag("elements");

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.
*
@ -27,11 +27,12 @@ import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.sikevadb.core.Archiver;
import fr.devinsy.sikevadb.core.Element;
import fr.devinsy.sikevadb.core.Elements;
import fr.devinsy.sikevadb.core.Archiver;
import fr.devinsy.sikevadb.core.SikevaDB;
import fr.devinsy.sikevadb.core.SikevaDBException;
import fr.devinsy.sikevadb.core.UnopenedDatabaseException;
import fr.devinsy.util.strings.StringList;
/**
@ -57,8 +58,8 @@ public class FileTreeSikevaDB implements SikevaDB
CLOSED
};
private final Logger logger = LoggerFactory.getLogger(FileTreeSikevaDB.class);
private static final DateTimeFormatter ISOFormatter = ISODateTimeFormat.dateHourMinuteSecondMillis();
private final Logger logger = LoggerFactory.getLogger(FileTreeSikevaDB.class);
private Status status;
private String login;
@ -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,36 @@ 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()
{
if (this.status != Status.CLOSED)
{
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 +154,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 +192,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 +229,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 +265,143 @@ public class FileTreeSikevaDB implements SikevaDB
this.configDirectory.mkdir();
}
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#remove(java.lang.String)
*/
@Override
public void delete(final String key) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
else
{
File targetFile = new File(this.dataDirectory, key);
if (targetFile.isFile())
{
boolean result = targetFile.delete();
if (!result)
{
throw new SikevaDBException("Error removing [" + targetFile.getAbsolutePath() + "]");
}
}
else
{
throw new SikevaDBException("Invalid target to remove [" + targetFile.getAbsolutePath() + "]");
}
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#remove(java.lang.String, java.lang.String)
*/
@Override
public void delete(final String key, final String subkey) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
else
{
File targetDirectory = new File(this.dataDirectory, key);
File targetFile = new File(targetDirectory, subkey);
if (targetFile.isFile())
{
boolean result = targetFile.delete();
if (!result)
{
throw new SikevaDBException("Error removing [" + targetFile.getAbsolutePath() + "]");
}
}
else
{
throw new SikevaDBException("Invalid target to remove [" + targetFile.getAbsolutePath() + "]");
}
}
}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#removeMany(java.lang.String, java.lang.String[])
*/
@Override
public void deleteMany(final String key, final String... subkeys) throws SikevaDBException
{
if (this.status == Status.CLOSED)
{
throw new UnopenedDatabaseException();
}
else if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
else
{
if ((subkeys != null) && (subkeys.length > 0))
{
for (String subkey : subkeys)
{
delete(key, subkey);
}
}
}
}
/**
* 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 +420,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 +454,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 +494,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 +533,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();
@ -413,14 +579,7 @@ public class FileTreeSikevaDB implements SikevaDB
}
else if (file.isDirectory())
{
File[] subFiles = file.listFiles();
for (File subFile : subFiles)
{
if (file.isFile())
{
result.add(subFile.getName());
}
result.add(file.getName());
}
}
}
@ -430,34 +589,48 @@ public class FileTreeSikevaDB implements SikevaDB
}
/**
* 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.");
}
@ -481,17 +654,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.");
}
@ -514,17 +689,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 + "].");
}
@ -548,17 +725,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.");
}
@ -590,9 +769,8 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
*
* @return
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#isClosed()
*/
@Override
public boolean isClosed()
@ -605,8 +783,8 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#isCreated()
*/
@Override
public boolean isCreated() throws SikevaDBException
@ -626,9 +804,8 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
*
* @return
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#isOpened()
*/
@Override
public boolean isOpened()
@ -648,16 +825,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;
@ -686,22 +867,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.");
}
@ -734,17 +918,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 + "].");
}
@ -769,8 +955,8 @@ public class FileTreeSikevaDB implements SikevaDB
return result;
}
/**
* {@inheritDoc}
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#open()
*/
@Override
public void open() throws SikevaDBException
@ -782,15 +968,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.");
}
@ -827,15 +1015,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.");
}
@ -862,15 +1052,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 + "].");
}
@ -898,98 +1090,17 @@ public class FileTreeSikevaDB implements SikevaDB
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#renameKey(java.lang.String, java.lang.String)
*/
@Override
public void remove(final String key) throws SikevaDBException
public void renameKey(final String oldKey, final String newKey) throws UnopenedDatabaseException
{
if (key == null)
if (this.status == Status.CLOSED)
{
throw new IllegalArgumentException("Null key detected.");
throw new UnopenedDatabaseException();
}
else
{
File targetFile = new File(this.dataDirectory, key);
if (targetFile.isFile())
{
boolean result = targetFile.delete();
if (!result)
{
throw new SikevaDBException("Error removing [" + targetFile.getAbsolutePath() + "]");
}
}
else
{
throw new SikevaDBException("Invalid target to remove [" + targetFile.getAbsolutePath() + "]");
}
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
*/
@Override
public void remove(final String key, final String subkey) throws SikevaDBException
{
if ((key == null) || (subkey == null))
{
throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "].");
}
else
{
File targetDirectory = new File(this.dataDirectory, key);
File targetFile = new File(targetDirectory, subkey);
if (targetFile.isFile())
{
boolean result = targetFile.delete();
if (!result)
{
throw new SikevaDBException("Error removing [" + targetFile.getAbsolutePath() + "]");
}
}
else
{
throw new SikevaDBException("Invalid target to remove [" + targetFile.getAbsolutePath() + "]");
}
}
}
/**
* {@inheritDoc}
*
* @throws SikevaDBException
*/
@Override
public void removeMany(final String key, final String... subkeys) throws SikevaDBException
{
if (key == null)
{
throw new IllegalArgumentException("Null key detected.");
}
else
{
if ((subkeys != null) && (subkeys.length > 0))
{
for (String subkey : subkeys)
{
remove(key, subkey);
}
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void renameKey(final String oldKey, final String newKey)
{
this.logger.debug("renameKey starting... [{}][{}]", oldKey, newKey);
@ -1025,13 +1136,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)
@ -1071,14 +1190,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);
@ -1096,14 +1220,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);
@ -1136,6 +1265,7 @@ public class FileTreeSikevaDB implements SikevaDB
this.logger.info("replaceInValues done.");
}
}
/**
*

File diff suppressed because it is too large Load diff

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;
/**
@ -70,6 +71,7 @@ public class TreeFileSikevaDBTest
database.put("alpha01s", "bravo5", "qlskjfmlqj");
Assert.assertEquals(0, database.getSubkeys("none").size());
Assert.assertEquals(5, database.getSubkeys("alpha01s").size());
//
logger.debug("===== test done.");
@ -101,10 +103,11 @@ public class TreeFileSikevaDBTest
StringList keys = database.getKeys();
Assert.assertEquals(5, keys.size());
Assert.assertEquals(6, keys.size());
Assert.assertTrue(keys.contains("alpha03"));
Assert.assertEquals(0, database.getSubkeys("alpha03s").size());
Assert.assertEquals(5, database.getSubkeys("alpha01s").size());
Assert.assertFalse(keys.contains("bravo4"));
//
logger.debug("===== test done.");
@ -498,7 +501,7 @@ public class TreeFileSikevaDBTest
Assert.assertEquals(12, database.countOfElements());
database.removeMany("alpha01", "bravo", "delta", "fox");
database.deleteMany("alpha01", "bravo", "delta", "fox");
Assert.assertEquals(9, database.countOfElements());
@ -557,14 +560,14 @@ public class TreeFileSikevaDBTest
database.put("alpha03", "qlskjfmlqj");
database.put("alpha04", "qlskjfmlqj");
database.put("alpha05", "qlskjfmlqj");
database.remove("alpha03");
database.delete("alpha03");
database.put("alpha01s", "bravo1", "qlskjfmlqja");
database.put("alpha01s", "bravo1", "qlskjfmlqjb");
database.put("alpha01s", "bravo2", "qlskjfmlqj");
database.put("alpha01s", "bravo3", "qlskjfmlqj");
database.put("alpha01s", "bravo4", "qlskjfmlqj");
database.put("alpha01s", "bravo5", "qlskjfmlqj");
database.remove("alpha01s", "bravo3");
database.delete("alpha01s", "bravo3");
// System.out.println(database.countOfElements() + " " +
// database.countOfArchivedElements() + " " +
@ -605,10 +608,11 @@ public class TreeFileSikevaDBTest
}
/**
* @throws UnopenedDatabaseException
*
*/
@AfterClass
public static void afterClass()
public static void afterClass() throws UnopenedDatabaseException
{
if (database != null)
{

View file

@ -566,7 +566,7 @@ public class SQLSikevaDBTest
Assert.assertEquals(12, database.countOfElements());
database.removeMany("alpha01", "bravo", "delta", "fox");
database.deleteMany("alpha01", "bravo", "delta", "fox");
Assert.assertEquals(9, database.countOfElements());
@ -625,14 +625,14 @@ public class SQLSikevaDBTest
database.put("alpha03", "qlskjfmlqj");
database.put("alpha04", "qlskjfmlqj");
database.put("alpha05", "qlskjfmlqj");
database.remove("alpha03");
database.delete("alpha03");
database.put("alpha01", "bravo1", "qlskjfmlqja");
database.put("alpha01", "bravo1", "qlskjfmlqjb");
database.put("alpha01", "bravo2", "qlskjfmlqj");
database.put("alpha01", "bravo3", "qlskjfmlqj");
database.put("alpha01", "bravo4", "qlskjfmlqj");
database.put("alpha01", "bravo5", "qlskjfmlqj");
database.remove("alpha01", "bravo3");
database.delete("alpha01", "bravo3");
// System.out.println(database.countOfElements() + " " +
// database.countOfArchivedElements() + " " +