diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java index 1276be2..ddd8d13 100644 --- a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2017 Christian Pierre MOMON + * Copyright (C) 2013-2018 Christian Pierre MOMON * * 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,121 +84,79 @@ 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 { - try + if (this.status == Status.CLOSED) { - // TODO journalize("clear database"); - - FileUtils.deleteDirectory(this.dataDirectory); - this.dataDirectory.mkdir(); - } - catch (IOException exception) - { - this.logger.error("Error clearing database", exception); - throw new SikevaDBException("Error clearing database", exception); - } - } - - /** - * - */ - @Override - public void close() - { - this.journalizer = null; - - this.status = Status.CLOSED; - } - - /** - * {@inheritDoc} - * - */ - @Override - public long countOfElements() - { - long result; - - File[] topFiles = this.dataDirectory.listFiles(); - - result = 0; - for (File file : topFiles) - { - if (file.isDirectory()) - { - File[] subFiles = file.listFiles(); - result += subFiles.length; - } - else - { - result += 1; - } - } - - // - return result; - } - - /** - * {@inheritDoc} - * - */ - @Override - public long countOfElements(final String key) - { - long result; - - if (key == null) - { - throw new IllegalArgumentException("Null key detected."); + throw new UnopenedDatabaseException(); } else { - File targetDirectory = new File(this.dataDirectory, key); - File[] subFiles = targetDirectory.listFiles(); + try + { + // TODO journalize("clear database"); - if (subFiles == null) - { - result = 1; + FileUtils.deleteDirectory(this.dataDirectory); + this.dataDirectory.mkdir(); } - else + catch (IOException exception) { - result = subFiles.length; + this.logger.error("Error clearing database", exception); + throw new SikevaDBException("Error clearing database", exception); } } - - // - return result; } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#close() */ @Override - public long countOfElements(final String key, final String subkey) + public void close() throws UnopenedDatabaseException { - long result; - - if ((key == null) || (subkey == null)) + if (this.status == Status.CLOSED) { - throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); + throw new UnopenedDatabaseException(); } else { - File targetDirectory = new File(this.dataDirectory, key); - File targetFile = new File(targetDirectory, subkey); - if (targetFile.exists()) + this.journalizer = null; + + this.status = Status.CLOSED; + } + } + + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#countOfElements() + */ + @Override + public long countOfElements() throws UnopenedDatabaseException + { + long result; + + if (this.status == Status.CLOSED) + { + throw new UnopenedDatabaseException(); + } + else + { + File[] topFiles = this.dataDirectory.listFiles(); + + result = 0; + for (File file : topFiles) { - result = 1; - } - else - { - result = 0; + if (file.isDirectory()) + { + File[] subFiles = file.listFiles(); + result += subFiles.length; + } + else + { + result += 1; + } } } @@ -205,60 +164,155 @@ public class FileTreeSikevaDB implements SikevaDB return result; } - /** - * {@inheritDoc} + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#countOfElements(java.lang.String) + */ + @Override + 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."); + } + else + { + File targetDirectory = new File(this.dataDirectory, key); + File[] subFiles = targetDirectory.listFiles(); + + if (subFiles == null) + { + result = 1; + } + else + { + result = subFiles.length; + } + } + } + + // + return result; + } + + /* (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) 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 + "]."); + } + else + { + File targetDirectory = new File(this.dataDirectory, key); + File targetFile = new File(targetDirectory, subkey); + if (targetFile.exists()) + { + result = 1; + } + else + { + result = 0; + } + } + } + // + return result; + } + + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#create() */ @Override public void create() throws SikevaDBException { - if (this.status == Status.OPENED) + if (this.status == Status.CLOSED) { - throw new SikevaDBException("Invalid state."); - } - else if (this.fileTreeDirectory == null) - { - throw new SikevaDBException("Invalid root directory."); - } - else if (this.fileTreeDirectory.exists()) - { - throw new SikevaDBException("Root directory already is existing."); + throw new UnopenedDatabaseException(); } else { - this.fileTreeDirectory.mkdir(); - this.dataDirectory.mkdir(); - this.configDirectory.mkdir(); + if (this.status == Status.OPENED) + { + throw new SikevaDBException("Invalid state."); + } + else if (this.fileTreeDirectory == null) + { + throw new SikevaDBException("Invalid root directory."); + } + else if (this.fileTreeDirectory.exists()) + { + throw new SikevaDBException("Root directory already is existing."); + } + else + { + this.fileTreeDirectory.mkdir(); + this.dataDirectory.mkdir(); + this.configDirectory.mkdir(); + } } } /** - * - * @return + * Gets the config directory. + * + * @return the config directory */ public File getConfigDirectory() { - return this.configDirectory; + File result; + + result = this.configDirectory; + + return result; } /** - * - * @return + * Gets the data directory. + * + * @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,34 +365,80 @@ 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; - result = new Elements((int) countOfElements()); - - File[] topFiles = this.dataDirectory.listFiles(); - for (File file : topFiles) + if (this.status == Status.CLOSED) { - if (file.isFile()) - { - Element element = getElement(file.getName()); + throw new UnopenedDatabaseException(); + } + else + { + result = new Elements((int) countOfElements()); - result.add(element); - } - else if (file.isDirectory()) + File[] topFiles = this.dataDirectory.listFiles(); + for (File file : topFiles) { - File[] subFiles = file.listFiles(); - - for (File subFile : subFiles) + if (file.isFile()) { - if (subFile.isFile()) + Element element = getElement(file.getName()); + + result.add(element); + } + else if (file.isDirectory()) + { + File[] subFiles = file.listFiles(); + + for (File subFile : subFiles) { - Element element = getElement(file.getName(), subFile.getName()); + if (subFile.isFile()) + { + Element element = getElement(file.getName(), subFile.getName()); + + result.add(element); + } + } + } + } + } + // + return result; + } + + /* (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."); + } + else + { + result = new Elements((int) countOfElements(key)); + + File targetDirectory = new File(this.dataDirectory, key); + if (targetDirectory.exists()) + { + File[] files = targetDirectory.listFiles(); + for (File subfile : files) + { + Element element = getElement(key, subfile.getName()); result.add(element); } @@ -349,31 +451,46 @@ public class FileTreeSikevaDB implements SikevaDB } /** - * {@inheritDoc} - * + * Gets the file tree directory. + * + * @return the file tree directory + */ + public File getFileTreeDirectory() + { + File result; + + result = this.fileTreeDirectory; + + // + return result; + } + + /* (non-Javadoc) + * @see fr.devinsy.sikevadb.core.SikevaDB#getKeys() */ @Override - public Elements getElements(final String key) throws SikevaDBException + public StringList getKeys() throws SikevaDBException { - Elements result; + StringList result; - if (key == null) + if (this.status == Status.CLOSED) { - throw new IllegalArgumentException("Null key detected."); + throw new UnopenedDatabaseException(); } else { - result = new Elements((int) countOfElements(key)); + result = new StringList((int) countOfElements()); - File targetDirectory = new File(this.dataDirectory, key); - if (targetDirectory.exists()) + File[] topFiles = this.dataDirectory.listFiles(); + for (File file : topFiles) { - File[] files = targetDirectory.listFiles(); - for (File subfile : files) + if (file.isFile()) { - Element element = getElement(key, subfile.getName()); - - result.add(element); + result.add(file.getName()); + } + else if (file.isDirectory()) + { + result.add(file.getName()); } } } @@ -383,73 +500,48 @@ public class FileTreeSikevaDB implements SikevaDB } /** - * - * @return + * Gets the login. + * + * @return the login */ - public File getFileTreeDirectory() + public String getLogin() { - return this.fileTreeDirectory; - } + String result; - /** - * {@inheritDoc} - * - * @throws SikevaDBException - */ - @Override - public StringList getKeys() throws SikevaDBException - { - StringList result; - - // - result = new StringList((int) countOfElements()); - - File[] topFiles = this.dataDirectory.listFiles(); - for (File file : topFiles) - { - if (file.isFile()) - { - result.add(file.getName()); - } - else if (file.isDirectory()) - { - result.add(file.getName()); - } - } + result = this.login; // return result; } /** - * - * @return - */ - public String getLogin() - { - return this.login; - } - - /** - * - * @return + * Gets the password. + * + * @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,40 +736,45 @@ 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; - // - result = 0; - - // - File[] topFiles = this.dataDirectory.listFiles(); - for (File file : topFiles) + if (this.status == Status.CLOSED) { - if (file.isFile()) - { - Element element = FileTreeSikevaDBTools.loadElement(file); + throw new UnopenedDatabaseException(); + } + else + { + // + result = 0; - result += element.getSize(); - } - else if (file.isDirectory()) + // + File[] topFiles = this.dataDirectory.listFiles(); + for (File file : topFiles) { - File[] subFiles = file.listFiles(); - - for (File subFile : subFiles) + if (file.isFile()) { - if (subFile.isFile()) - { - Element element = FileTreeSikevaDBTools.loadElement(subFile); + Element element = FileTreeSikevaDBTools.loadElement(file); - result += element.getSize(); + result += element.getSize(); + } + else if (file.isDirectory()) + { + File[] subFiles = file.listFiles(); + + for (File subFile : subFiles) + { + if (subFile.isFile()) + { + Element element = FileTreeSikevaDBTools.loadElement(subFile); + + result += element.getSize(); + } } } } @@ -683,17 +784,19 @@ 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,156 +1094,181 @@ 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 { - this.logger.debug("renameKey starting... [{}][{}]", oldKey, newKey); - - if (oldKey == null) + if (this.status == Status.CLOSED) { - throw new IllegalArgumentException("OldKey is null."); - } - else if (newKey == null) - { - throw new IllegalArgumentException("OldKey is null."); + throw new UnopenedDatabaseException(); } else { - File oldFile = new File(this.dataDirectory, oldKey); - File newFile = new File(this.dataDirectory, newKey); + this.logger.debug("renameKey starting... [{}][{}]", oldKey, newKey); - if (oldFile.isFile()) + if (oldKey == null) { - if (newFile.isFile()) + throw new IllegalArgumentException("OldKey is null."); + } + else if (newKey == null) + { + throw new IllegalArgumentException("OldKey is null."); + } + else + { + File oldFile = new File(this.dataDirectory, oldKey); + File newFile = new File(this.dataDirectory, newKey); + + if (oldFile.isFile()) { - throw new IllegalArgumentException("Invalid newKey [" + newKey + "]."); + if (newFile.isFile()) + { + throw new IllegalArgumentException("Invalid newKey [" + newKey + "]."); + } + else + { + oldFile.renameTo(newFile); + } } else { oldFile.renameTo(newFile); } } - else - { - oldFile.renameTo(newFile); - } - } - this.logger.debug("renameKey done."); + 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 { - this.logger.debug("renameSybKey starting... [{}][{}][{}]", oldSubkey, newSubkey); - - if (key == null) + if (this.status == Status.CLOSED) { - throw new IllegalArgumentException("Top key is null."); - } - if (oldSubkey == null) - { - throw new IllegalArgumentException("OldKey is null."); - } - else if (newSubkey == null) - { - throw new IllegalArgumentException("OldKey is null."); + throw new UnopenedDatabaseException(); } else { - File targetDirectory = new File(this.dataDirectory, key); - File oldFile = new File(targetDirectory, oldSubkey); - File newFile = new File(targetDirectory, newSubkey); - if (oldFile.isFile()) + this.logger.debug("renameSybKey starting... [{}][{}][{}]", oldSubkey, newSubkey); + + if (key == null) { - if (newFile.isFile()) - { - throw new IllegalArgumentException("Invalid newKey [" + newSubkey + "]."); - } - else - { - oldFile.renameTo(newFile); - } + throw new IllegalArgumentException("Top key is null."); + } + if (oldSubkey == null) + { + throw new IllegalArgumentException("OldKey is null."); + } + else if (newSubkey == null) + { + throw new IllegalArgumentException("OldKey is null."); } else { - throw new IllegalArgumentException("Invalid oldKey [" + oldSubkey + "]."); - } - } + File targetDirectory = new File(this.dataDirectory, key); + File oldFile = new File(targetDirectory, oldSubkey); + File newFile = new File(targetDirectory, newSubkey); - this.logger.debug("renameSubKey done."); + if (oldFile.isFile()) + { + if (newFile.isFile()) + { + throw new IllegalArgumentException("Invalid newKey [" + newSubkey + "]."); + } + else + { + oldFile.renameTo(newFile); + } + } + else + { + throw new IllegalArgumentException("Invalid oldKey [" + oldSubkey + "]."); + } + } + + 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 { - this.logger.info("replaceInValue starting... [{}]", key); - - // - String value = getValue(key); - - // - for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + if (this.status == Status.CLOSED) { - value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + throw new UnopenedDatabaseException(); } + else + { + this.logger.info("replaceInValue starting... [{}]", key); - // - put(key, value); + // + String value = getValue(key); - this.logger.info("replaceInValue done."); + // + for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) + { + value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(key, value); + + 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 { - this.logger.info("replaceInValues starting... [{}]", key); - - // - Elements elements = getElements(key); - - long count = 0; - for (Element element : elements) + if (this.status == Status.CLOSED) { - this.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); - } + throw new UnopenedDatabaseException(); } + else + { + this.logger.info("replaceInValues starting... [{}]", key); - this.logger.info("replaceInValues done."); + // + Elements elements = getElements(key); + + long count = 0; + for (Element element : elements) + { + this.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); + } + } + + this.logger.info("replaceInValues done."); + } } /** diff --git a/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java index e7f2bf2..c4aedf2 100644 --- a/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java +++ b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java @@ -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) {