diff --git a/.classpath b/.classpath index 3400f50..bded8da 100644 --- a/.classpath +++ b/.classpath @@ -23,6 +23,8 @@ - + + + diff --git a/build.num b/build.num index c11b408..4f5ab8e 100644 --- a/build.num +++ b/build.num @@ -1,3 +1,3 @@ #Build Number for ANT. Do not edit! #Sat Feb 08 16:31:19 CET 2014 -build.number=11 +build.number=0 diff --git a/build.properties b/build.properties index f704c19..49da2c5 100644 --- a/build.properties +++ b/build.properties @@ -1,3 +1,3 @@ product.name=sikevadb product.revision.major=0 -product.revision.minor=1 +product.revision.minor=2 diff --git a/lib/commons-io-2.4-sources.jar b/lib/commons-io-2.4-sources.jar new file mode 100644 index 0000000..f4ebc7b Binary files /dev/null and b/lib/commons-io-2.4-sources.jar differ diff --git a/lib/commons-io-2.4.jar b/lib/commons-io-2.4.jar new file mode 100644 index 0000000..90035a4 Binary files /dev/null and b/lib/commons-io-2.4.jar differ diff --git a/lib/devinsy-utils-0.2.12-sources.zip b/lib/devinsy-utils-0.2.12-sources.zip deleted file mode 100644 index 3cbd960..0000000 Binary files a/lib/devinsy-utils-0.2.12-sources.zip and /dev/null differ diff --git a/lib/devinsy-utils-0.2.12.jar b/lib/devinsy-utils-0.2.12.jar deleted file mode 100644 index 7b46e1e..0000000 Binary files a/lib/devinsy-utils-0.2.12.jar and /dev/null differ diff --git a/lib/devinsy-utils-0.3.3-sources.zip b/lib/devinsy-utils-0.3.3-sources.zip new file mode 100644 index 0000000..647b15d Binary files /dev/null and b/lib/devinsy-utils-0.3.3-sources.zip differ diff --git a/lib/devinsy-utils-0.3.3.jar b/lib/devinsy-utils-0.3.3.jar new file mode 100644 index 0000000..834d81a Binary files /dev/null and b/lib/devinsy-utils-0.3.3.jar differ diff --git a/src/fr/devinsy/sikevadb/FileSikevaDB.java b/src/fr/devinsy/sikevadb/FileSikevaDB.java deleted file mode 100644 index f544612..0000000 --- a/src/fr/devinsy/sikevadb/FileSikevaDB.java +++ /dev/null @@ -1,422 +0,0 @@ -/** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY - * - * 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 . - */ -package fr.devinsy.sikevadb; - -import java.io.File; - -import org.joda.time.DateTime; - -import fr.devinsy.util.StringList; - -/** - * - * - * @author Christian Pierre MOMON - */ -public class FileSikevaDB implements SikevaDB -{ - /** - * - * @param path - */ - public FileSikevaDB(final File path) - { - - } - - /** - * - * @param path - */ - public FileSikevaDB(final String path) - { - - } - - @Override - public void archive(final String key) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void archive(final String key, final String subkey) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void clearAllArchive() throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void clearArchive(final DateTime beforeDate) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void clearArchive(final int maxDays) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void clearDatabase() throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void close() - { - // TODO Auto-generated method stub - - } - - @Override - public long countOfAllElements() - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long countOfAllElements(final String key) - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long countOfAllElements(final String key, final String subkey) - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long countOfArchivedElements() - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long countOfArchivedElements(final String key) - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long countOfArchivedElements(final String key, final String subkey) - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long countOfElements() - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long countOfElements(final String key) - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long countOfElements(final String key, final String subkey) - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public Elements getAllElements() throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public Elements getAllElements(final String key) throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public Elements getAllElements(final String key, final String subkey) throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringList getAllKeys() throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringList getAllSubkeys(final String key) throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringList getAllValues(final String key) - { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringList getAllValues(final String key, final String subkey) - { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringList getArchivedValues(final String key) - { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringList getArchivedValues(final String key, final String subkey) - { - // TODO Auto-generated method stub - return null; - } - - @Override - public Element getElement(final String key) throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public Element getElement(final String key, final String subkey) throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public Elements getElements(final String key) throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public Elements getElements(final String key, final String subkey) throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringList getKeys() throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringList getSubkeys(final String key) throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getValue(final String key) throws Exception - { - // TODO Auto-generated method stub - return null; - } - - @Override - public String getValue(final String key, final String subkey) - { - // TODO Auto-generated method stub - return null; - } - - @Override - public StringList getValues(final String key) - { - // TODO Auto-generated method stub - return null; - } - - @Override - public long memorySize() - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long memorySize(final String key) - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long memorySize(final String key, final String subkey) - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long memorySizeOfAll() throws Exception - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long memorySizeOfAll(final String key) throws Exception - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long memorySizeOfArchive() throws Exception - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long memorySizeOfArchive(final String key) throws Exception - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public long memorySizeOfArchive(final String key, final String subkey) throws Exception - { - // TODO Auto-generated method stub - return 0; - } - - @Override - public void open() - { - // TODO Auto-generated method stub - - } - - @Override - public void put(final Element element) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void put(final String key, final String value) - { - // TODO Auto-generated method stub - - } - - @Override - public void put(final String key, final String subkey, final String value) - { - // TODO Auto-generated method stub - - } - - @Override - public void remove(final String key) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void remove(final String key, final String subkey) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void removeAll(final String key) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void removeMany(final String key, final String... subkeys) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void renameKey(final String oldKey, final String newKey) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void replaceInValue(final String key, final String... tokens) throws Exception - { - // TODO Auto-generated method stub - - } - - @Override - public void replaceInValues(final String key, final String... tokens) throws Exception - { - // TODO Auto-generated method stub - - } -} diff --git a/src/fr/devinsy/sikevadb/SikevaDB.java b/src/fr/devinsy/sikevadb/SikevaDB.java deleted file mode 100644 index 57e92fb..0000000 --- a/src/fr/devinsy/sikevadb/SikevaDB.java +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY - * - * 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 . - */ -package fr.devinsy.sikevadb; - -import org.joda.time.DateTime; - -import fr.devinsy.util.StringList; - -/** - * All elements = archived elements + elements. - * - * - * - * @author Christian Pierre MOMON - */ -public interface SikevaDB -{ - public void archive(final String key) throws Exception; - - public void archive(final String key, final String subkey) throws Exception; - - public void clearAllArchive() throws Exception; - - public void clearArchive(final DateTime beforeDate) throws Exception; - - public void clearArchive(final int maxDays) throws Exception; - - public void clearDatabase() throws Exception; - - public void close() throws Exception; - - public long countOfAllElements() throws Exception; - - public long countOfAllElements(String key) throws Exception; - - public long countOfAllElements(String key, String subkey) throws Exception; - - public long countOfArchivedElements() throws Exception; - - public long countOfArchivedElements(String key) throws Exception; - - public long countOfArchivedElements(String key, String subkey) throws Exception; - - public long countOfElements() throws Exception; - - public long countOfElements(String key) throws Exception; - - public long countOfElements(String key, String subkey) throws Exception; - - public Elements getAllElements() throws Exception; - - public Elements getAllElements(String key) throws Exception; - - public Elements getAllElements(String key, String subkey) throws Exception; - - public StringList getAllKeys() throws Exception; - - public StringList getAllSubkeys(String key) throws Exception; - - public StringList getAllValues(String key) throws Exception; - - public StringList getAllValues(String key, String subkey) throws Exception; - - public StringList getArchivedValues(String key) throws Exception; - - public StringList getArchivedValues(String key, String subkey) throws Exception; - - public Element getElement(String key) throws Exception; - - public Element getElement(String key, String subkey) throws Exception; - - public Elements getElements(String key) throws Exception; - - public Elements getElements(String key, String subkey) throws Exception; - - public StringList getKeys() throws Exception; - - public StringList getSubkeys(String key) throws Exception; - - public String getValue(String key) throws Exception; - - public String getValue(String key, String subkey) throws Exception; - - public StringList getValues(String key) throws Exception; - - public long memorySize() throws Exception; - - public long memorySize(String key) throws Exception; - - public long memorySize(String key, String subkey) throws Exception; - - public long memorySizeOfAll() throws Exception; - - long memorySizeOfAll(String key) throws Exception; - - public long memorySizeOfArchive() throws Exception; - - long memorySizeOfArchive(String key) throws Exception; - - long memorySizeOfArchive(String key, String subkey) throws Exception; - - public void open() throws Exception; - - public void put(Element element) throws Exception; - - public void put(String key, String value) throws Exception; - - public void put(String key, String subkey, String value) throws Exception; - - public void remove(final String key) throws Exception; - - public void remove(final String key, final String subkey) throws Exception; - - public void removeAll(final String key) throws Exception; - - public void removeMany(final String key, final String... subkeys) throws Exception; - - public void renameKey(final String oldKey, final String newKey) throws Exception; - - public void replaceInValue(final String key, final String... tokens) throws Exception; - - public void replaceInValues(final String key, final String... tokens) throws Exception; -} diff --git a/src/fr/devinsy/sikevadb/SikevaDBLauncher.java b/src/fr/devinsy/sikevadb/SikevaDBLauncher.java new file mode 100644 index 0000000..4feb1d2 --- /dev/null +++ b/src/fr/devinsy/sikevadb/SikevaDBLauncher.java @@ -0,0 +1,78 @@ +/** + * Copyright (C) 2013-2017 Christian Pierre MOMON + * + * 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 . + */ +package fr.devinsy.sikevadb; + +import java.io.File; + +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.PropertyConfigurator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.sikevadb.cli.SikevaCLI; + +/** + * This class Siba stands for Simple Backup. This is the main class. + * + * + */ +public final class SikevaDBLauncher +{ + private static Logger logger = LoggerFactory.getLogger(SikevaDBLauncher.class); + + /** + * + */ + private SikevaDBLauncher() + { + } + + /** + * + * @param args + */ + public static void main(final String[] args) + { + // Configure log. + File loggerConfig = new File("log4j.properties"); + if (loggerConfig.exists()) + { + PropertyConfigurator.configure(loggerConfig.getAbsolutePath()); + logger.info("Dedicated log configuration done."); + logger.info("Configuration file was found in [{}].", loggerConfig.getAbsoluteFile()); + } + else + { + BasicConfigurator.configure(); + logger.info("Basic log configuration done."); + logger.info("Configuration file was not found in [{}].", loggerConfig.getAbsoluteFile()); + } + + // Run. + if (args.length == 0) + { + // TODO + // SikevaGUI.run(); + } + else + { + SikevaCLI.run(args); + } + } +} diff --git a/src/fr/devinsy/sikevadb/cli/SikevaCLI.java b/src/fr/devinsy/sikevadb/cli/SikevaCLI.java new file mode 100644 index 0000000..7009474 --- /dev/null +++ b/src/fr/devinsy/sikevadb/cli/SikevaCLI.java @@ -0,0 +1,166 @@ +/** + * Copyright (C) 2013-2017 Christian Pierre MOMON + * + * 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 . + */ + +package fr.devinsy.sikevadb.cli; + +import java.io.File; + +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.PropertyConfigurator; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import utils.BuildInformation; +import fr.devinsy.sikevadb.core.SikevaDBException; +import fr.devinsy.util.strings.StringList; + +/** + * This class manage a Command Line Interface for SikevaDB. + * + * + */ +public final class SikevaCLI +{ + private static Logger logger = LoggerFactory.getLogger(SikevaCLI.class); + + /** + * + */ + private SikevaCLI() + { + } + + /** + * This method displays the CLI help. + * + */ + public static void help() + { + StringList message = new StringList(); + + message.append("SikevaDB CLI version ").appendln(BuildInformation.instance().version()); + message.appendln("Usage:"); + message.appendln(" sikevadbcli [ -h | -help | --help ]"); + message.appendln(" sikevadbcli get key"); + message.appendln(" sikevadbcli get key subkey"); + + System.out.println(message.toString()); + } + + /** + * + * @param args + */ + public static void main(final String[] args) + { + // Configure log. + File loggerConfig = new File("log4j.properties"); + if (loggerConfig.exists()) + { + PropertyConfigurator.configure(loggerConfig.getAbsolutePath()); + logger.info("Dedicated log configuration done."); + logger.info("Configuration file was found in [{}].", loggerConfig.getAbsoluteFile()); + } + else + { + BasicConfigurator.configure(); + logger.info("Basic log configuration done."); + logger.info("Configuration file was not found in [{}].", loggerConfig.getAbsoluteFile()); + } + + // Run. + SikevaCLI.run(args); + } + + /** + * + * This method launch CLI. + * + * @param args + * necessary arguments + */ + public static void run(final String[] args) + { + try + { + // Set default catch. + Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() + { + @Override + public void uncaughtException(final Thread thread, final Throwable exception) + { + String message; + if (exception instanceof OutOfMemoryError) + { + message = "Java ran out of memory!\n\n"; + } + else + { + message = String.format("An error occured: %1s(%2s)", exception.getClass(), exception.getMessage()); + } + + logger.error("uncaughtException ", exception); + logger.error(message); + logger.info("Oups, an unexpected error occured. Please try again."); + } + }); + + // This part implements an automate. + int parameterCount = args.length; + if (parameterCount == 0) + { + help(); + } + else if (StringUtils.equals(args[0], "-h") || StringUtils.equals(args[0], "-help") || StringUtils.equals(args[0], "--help")) + { + help(); + } + else if (StringUtils.equals(args[0], "get")) + { + switch (parameterCount) + { + case 2: + { + } + break; + + case 3: + { + } + break; + + default: + throw new SikevaDBException("Bad parameter count."); + } + } + else + { + System.out.println("Bad usage detected."); + help(); + } + } + catch (SikevaDBException exception) + { + System.err.println("SibaException = " + exception.getMessage()); + logger.error(exception.getMessage(), exception); + help(); + } + } +} diff --git a/src/fr/devinsy/sikevadb/cli/SikevaCLIException.java b/src/fr/devinsy/sikevadb/cli/SikevaCLIException.java new file mode 100644 index 0000000..5a86281 --- /dev/null +++ b/src/fr/devinsy/sikevadb/cli/SikevaCLIException.java @@ -0,0 +1,63 @@ +/** + * Copyright (C) 2013-2017 Christian Pierre MOMON + * + * 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 . + */ +package fr.devinsy.sikevadb.cli; + +/** + * + */ +public class SikevaCLIException extends Exception +{ + private static final long serialVersionUID = 2986878456227891377L; + + /** + * + */ + public SikevaCLIException() + { + super(); + } + + /** + * + * @param message + */ + public SikevaCLIException(final String message) + { + super(message); + } + + /** + * + * @param message + * @param cause + */ + public SikevaCLIException(final String message, final Throwable cause) + { + super(message, cause); + } + + /** + * + * @param cause + */ + public SikevaCLIException(final Throwable cause) + { + super(cause); + } +} \ No newline at end of file diff --git a/src/fr/devinsy/sikevadb/core/Archiver.java b/src/fr/devinsy/sikevadb/core/Archiver.java new file mode 100644 index 0000000..5c86c07 --- /dev/null +++ b/src/fr/devinsy/sikevadb/core/Archiver.java @@ -0,0 +1,89 @@ +/** + * Copyright (C) 2013-2017 Christian Pierre MOMON + * + * 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 . + */ +package fr.devinsy.sikevadb.core; + +import fr.devinsy.util.strings.StringList; + +/** + * + * + * @author Christian Pierre MOMON + */ +public interface Archiver +{ + public void clear() throws SikevaDBException; + + public void close() throws SikevaDBException; + + public long countOfElements() throws SikevaDBException; + + public long countOfElements(String key) throws SikevaDBException; + + public long countOfElements(String key, String subkey) throws SikevaDBException; + + public Element getElement(String key) throws SikevaDBException; + + public Element getElement(String key, String subkey) throws SikevaDBException; + + public Elements getElements() throws SikevaDBException; + + public Elements getElements(String key) throws SikevaDBException; + + public StringList getKeys() throws SikevaDBException; + + public StringList getSubkeys(String key) throws SikevaDBException; + + public String getValue(String key) throws SikevaDBException; + + public String getValue(String key, String subkey) throws SikevaDBException; + + public StringList getValues(String key) throws SikevaDBException; + + public boolean isClosed(); + + public boolean isOpened(); + + public long memorySize() throws SikevaDBException; + + public long memorySize(String key) throws SikevaDBException; + + public long memorySize(String key, String subkey) throws SikevaDBException; + + void open() throws SikevaDBException; + + void put(Element element) throws SikevaDBException; + + void put(String key, String value) throws SikevaDBException; + + 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; + + void renameKey(final String oldKey, final String newKey) throws SikevaDBException; + + void renameSubKey(final String key, final String oldSubKey, final String newSubKey) throws SikevaDBException; + + void replaceInValue(final String key, final String... tokens) throws SikevaDBException; + + void replaceInValues(final String key, final String... tokens) throws SikevaDBException; +} diff --git a/src/fr/devinsy/sikevadb/Element.java b/src/fr/devinsy/sikevadb/core/Element.java similarity index 92% rename from src/fr/devinsy/sikevadb/Element.java rename to src/fr/devinsy/sikevadb/core/Element.java index 5601215..3d21d46 100644 --- a/src/fr/devinsy/sikevadb/Element.java +++ b/src/fr/devinsy/sikevadb/core/Element.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY + * Copyright (C) 2013-2017 Christian Pierre MOMON * * This file is part of SikevaDB, simple key value database. * @@ -16,7 +16,7 @@ * You should have received a copy of the GNU Affero General Public License * along with SikevaDB. If not, see . */ -package fr.devinsy.sikevadb; +package fr.devinsy.sikevadb.core; import org.apache.commons.codec.digest.DigestUtils; import org.joda.time.DateTime; @@ -33,6 +33,7 @@ public class Element private long id; private DateTime creationDate; private DateTime editionDate; + /* archiveDate field is reserved for journalizer. */ private DateTime archiveDate; private String key; private String subkey; @@ -161,6 +162,10 @@ public class Element this.value = value; } + /** + * + * @param value + */ public void update(final String value) { // @@ -173,7 +178,7 @@ public class Element if (value == null) { this.size = 0; - this.digest = null; + this.digest = DigestUtils.md5Hex(""); } else { diff --git a/src/fr/devinsy/sikevadb/Elements.java b/src/fr/devinsy/sikevadb/core/Elements.java similarity index 90% rename from src/fr/devinsy/sikevadb/Elements.java rename to src/fr/devinsy/sikevadb/core/Elements.java index aab2696..9da7d68 100644 --- a/src/fr/devinsy/sikevadb/Elements.java +++ b/src/fr/devinsy/sikevadb/core/Elements.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY + * Copyright (C) 2013-2017 Christian Pierre MOMON * * This file is part of SikevaDB, simple key value database. * @@ -16,7 +16,7 @@ * You should have received a copy of the GNU Affero General Public License * along with SikevaDB. If not, see . */ -package fr.devinsy.sikevadb; +package fr.devinsy.sikevadb.core; import java.util.ArrayList; diff --git a/src/fr/devinsy/sikevadb/core/SikevaDB.java b/src/fr/devinsy/sikevadb/core/SikevaDB.java new file mode 100644 index 0000000..9e0ece9 --- /dev/null +++ b/src/fr/devinsy/sikevadb/core/SikevaDB.java @@ -0,0 +1,93 @@ +/** + * Copyright (C) 2013-2017 Christian Pierre MOMON + * + * 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 . + */ +package fr.devinsy.sikevadb.core; + +import fr.devinsy.util.strings.StringList; + +/** + * + * + * @author Christian Pierre MOMON + */ +public interface SikevaDB +{ + public void clear() throws SikevaDBException; + + public void close() throws SikevaDBException; + + public long countOfElements() throws SikevaDBException; + + public long countOfElements(String key) throws SikevaDBException; + + public long countOfElements(String key, String subkey) throws SikevaDBException; + + public void create() throws SikevaDBException; + + public Element getElement(String key) throws SikevaDBException; + + public Element getElement(String key, String subkey) throws SikevaDBException; + + public Elements getElements() throws SikevaDBException; + + public Elements getElements(String key) throws SikevaDBException; + + public fr.devinsy.util.strings.StringList getKeys() throws SikevaDBException; + + public StringList getSubkeys(String key) throws SikevaDBException; + + public String getValue(String key) throws SikevaDBException; + + public String getValue(String key, String subkey) throws SikevaDBException; + + public StringList getValues(String key) throws SikevaDBException; + + public boolean isClosed(); + + public boolean isCreated() throws SikevaDBException; + + public boolean isOpened(); + + public long memorySize() throws SikevaDBException; + + public long memorySize(String key) throws SikevaDBException; + + public long memorySize(String key, String subkey) throws SikevaDBException; + + void open() throws SikevaDBException; + + void put(Element element) throws SikevaDBException; + + void put(String key, String value) throws SikevaDBException; + + 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; + + void renameKey(final String oldKey, final String newKey) throws SikevaDBException; + + void renameSubKey(final String key, final String oldSubKey, final String newSubKey) throws SikevaDBException; + + void replaceInValue(final String key, final String... tokens) throws SikevaDBException; + + void replaceInValues(final String key, final String... tokens) throws SikevaDBException; +} diff --git a/src/fr/devinsy/sikevadb/core/SikevaDBException.java b/src/fr/devinsy/sikevadb/core/SikevaDBException.java new file mode 100644 index 0000000..71cb8b1 --- /dev/null +++ b/src/fr/devinsy/sikevadb/core/SikevaDBException.java @@ -0,0 +1,64 @@ +/** + * Copyright (C) 2013-2016 Christian Pierre MOMON + * + * 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 . + */ +package fr.devinsy.sikevadb.core; + +/** + * + * @author Christian Pierre MOMON (christian.momon@devinsy.fr) + */ +public class SikevaDBException extends Exception +{ + private static final long serialVersionUID = 8323299610751708972L; + + /** + * + */ + public SikevaDBException() + { + super(); + } + + /** + * + * @param message + */ + public SikevaDBException(final String message) + { + super(message); + } + + /** + * + * @param message + * @param cause + */ + public SikevaDBException(final String message, final Throwable cause) + { + super(message, cause); + } + + /** + * + * @param cause + */ + public SikevaDBException(final Throwable cause) + { + super(cause); + } +} diff --git a/src/fr/devinsy/sikevadb/SikevaDBFactory.java b/src/fr/devinsy/sikevadb/core/SikevaDBFactory.java similarity index 86% rename from src/fr/devinsy/sikevadb/SikevaDBFactory.java rename to src/fr/devinsy/sikevadb/core/SikevaDBFactory.java index c39d37f..8f3ee41 100644 --- a/src/fr/devinsy/sikevadb/SikevaDBFactory.java +++ b/src/fr/devinsy/sikevadb/core/SikevaDBFactory.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY + * Copyright (C) 2013-2017 Christian Pierre MOMON * * This file is part of SikevaDB, simple key value database. * @@ -16,10 +16,11 @@ * You should have received a copy of the GNU Affero General Public License * along with SikevaDB. If not, see . */ -package fr.devinsy.sikevadb; +package fr.devinsy.sikevadb.core; import java.io.File; +import fr.devinsy.sikevadb.filetree.FileTreeSikevaDB; import fr.devinsy.sikevadb.sql.SQLSikevaDB; /** @@ -38,7 +39,7 @@ public class SikevaDBFactory { SikevaDB result; - result = new FileSikevaDB(path); + result = new FileTreeSikevaDB(path, null, null); // return result; @@ -46,7 +47,7 @@ public class SikevaDBFactory /** * - * @param path + * @param contextName * @return */ public static SikevaDB get(final String contextName) diff --git a/src/fr/devinsy/sikevadb/SikevaDBUtils.java b/src/fr/devinsy/sikevadb/core/SikevaDBUtils.java similarity index 88% rename from src/fr/devinsy/sikevadb/SikevaDBUtils.java rename to src/fr/devinsy/sikevadb/core/SikevaDBUtils.java index 18b9e10..0093d4b 100644 --- a/src/fr/devinsy/sikevadb/SikevaDBUtils.java +++ b/src/fr/devinsy/sikevadb/core/SikevaDBUtils.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY + * Copyright (C) 2013-2017 Christian Pierre MOMON * * This file is part of SikevaDB, simple key value database. * @@ -16,7 +16,7 @@ * You should have received a copy of the GNU Affero General Public License * along with SikevaDB. If not, see . */ -package fr.devinsy.sikevadb; +package fr.devinsy.sikevadb.core; import java.util.Date; diff --git a/src/fr/devinsy/sikevadb/XMLSikevaDB.java b/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java similarity index 95% rename from src/fr/devinsy/sikevadb/XMLSikevaDB.java rename to src/fr/devinsy/sikevadb/core/XMLSikevaDB.java index 23b0bc6..7b64ddd 100644 --- a/src/fr/devinsy/sikevadb/XMLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/core/XMLSikevaDB.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY + * Copyright (C) 2013-2017 Christian Pierre MOMON * * This file is part of SikevaDB, simple key value database. * @@ -16,7 +16,7 @@ * You should have received a copy of the GNU Affero General Public License * along with SikevaDB. If not, see . */ -package fr.devinsy.sikevadb; +package fr.devinsy.sikevadb.core; import java.io.File; import java.io.OutputStream; @@ -87,7 +87,6 @@ public class XMLSikevaDB } else { - // XMLZipWriter target = null; try { @@ -117,7 +116,6 @@ public class XMLSikevaDB */ public static void importData(final SikevaDB database, final File file) throws Exception { - // XMLReader in = null; // try { // in = new XMLZipReader(file); @@ -196,7 +194,6 @@ public class XMLSikevaDB */ public static void readElements(final SikevaDB target, final XMLReader in) throws Exception { - // XMLTag list = in.readListTag("elements"); @@ -279,7 +276,6 @@ public class XMLSikevaDB */ public static void write(final XMLWriter out, final SikevaDB source) throws Exception { - if (out == null) { throw new NullPointerException("out is null."); @@ -292,10 +288,10 @@ public class XMLSikevaDB { out.writeStartTag("elements"); - for (String key : source.getAllKeys()) + for (String key : source.getKeys()) { // - Elements elements = source.getAllElements(key); + Elements elements = source.getElements(key); for (Element element : elements) { write(out, element); @@ -304,11 +300,8 @@ public class XMLSikevaDB // for (String subkey : source.getSubkeys(key)) { - Elements subElements = source.getAllElements(key, subkey); - for (Element subElement : subElements) - { - write(out, subElement); - } + Element subElement = source.getElement(key, subkey); + write(out, subElement); } } diff --git a/src/fr/devinsy/sikevadb/filetree/ArchiveManager.java b/src/fr/devinsy/sikevadb/filetree/ArchiveManager.java new file mode 100644 index 0000000..b3a155b --- /dev/null +++ b/src/fr/devinsy/sikevadb/filetree/ArchiveManager.java @@ -0,0 +1,48 @@ +/** + * Copyright (C) 2013-2016 Christian Pierre MOMON + * + * 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 . + */ +package fr.devinsy.sikevadb.filetree; + +import fr.devinsy.sikevadb.core.SikevaDB; + +/** + * + * + * @author Christian Pierre MOMON + */ +public class ArchiveManager +{ + private SikevaDB database; + + /** + * + * @param source + */ + public ArchiveManager(final SikevaDB source) + { + if (this.database == null) + { + throw new IllegalArgumentException("Null parameter."); + } + else + { + this.database = source; + } + } + +} diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java new file mode 100644 index 0000000..9f7388d --- /dev/null +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java @@ -0,0 +1,1157 @@ +/** + * Copyright (C) 2013-2017 Christian Pierre MOMON + * + * 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 . + */ +package fr.devinsy.sikevadb.filetree; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.ISODateTimeFormat; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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.util.strings.StringList; + +/** + *
+ * private long id;					IN
+ * private DateTime creationDate;	IN
+ * private DateTime editionDate;	IN
+ * private DateTime archiveDate;	OUT		DEPRECATED
+ * private String key;				IN/OUT
+ * private String subkey;			IN/OUT
+ * private long size;				IN
+ * private String digest;			IN
+ * private String value;			IN
+ * 
+ * + * @author Christian Pierre MOMON + */ +public class FileTreeSikevaDB implements SikevaDB +{ + public enum Status + { + OPENED, + CLOSED + }; + + private final Logger logger = LoggerFactory.getLogger(FileTreeSikevaDB.class); + private static final DateTimeFormatter ISOFormatter = ISODateTimeFormat.dateHourMinuteSecondMillis(); + + private Status status; + private String login; + private String password; + private File fileTreeDirectory; + private File dataDirectory; + private Archiver journalizer; + private File configDirectory; + + /** + * + * @param contextName + */ + public FileTreeSikevaDB(final File fileTreeDirectory, final String login, final String password) + { + this.status = Status.CLOSED; + this.fileTreeDirectory = fileTreeDirectory; + this.dataDirectory = new File(this.fileTreeDirectory, "data"); + this.configDirectory = new File(this.fileTreeDirectory, "config"); + this.login = login; + this.password = password; + this.journalizer = null; + } + + /** + * @throws IOException + */ + @Override + public void clear() throws SikevaDBException + { + try + { + // 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."); + } + else + { + File targetDirectory = new File(this.dataDirectory, key); + File[] subFiles = targetDirectory.listFiles(); + + if (subFiles == null) + { + result = 1; + } + else + { + result = subFiles.length; + } + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public long countOfElements(final String key, final String subkey) + { + long result; + + 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; + } + + /** + * {@inheritDoc} + */ + @Override + public void create() throws SikevaDBException + { + 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 + */ + public File getConfigDirectory() + { + return this.configDirectory; + } + + /** + * + * @return + */ + public File getDataDirectory() + { + return this.dataDirectory; + } + + /** + * {@inheritDoc} + * + */ + @Override + public Element getElement(final String key) throws SikevaDBException + { + Element result; + + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + File targetFile = new File(this.dataDirectory, key); + result = FileTreeSikevaDBTools.loadElement(targetFile); + if (result != null) + { + result.setKey(key); + result.setSubkey(null); + } + } + + // + return result; + } + + /** + * {@inheritDoc} + * + */ + @Override + public Element getElement(final String key, final String subkey) throws SikevaDBException + { + Element result; + + // + 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); + + result = FileTreeSikevaDBTools.loadElement(targetFile); + if (result != null) + { + result.setKey(key); + result.setSubkey(subkey); + } + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public Elements getElements() throws SikevaDBException + { + Elements result; + + result = new Elements((int) countOfElements()); + + File[] topFiles = this.dataDirectory.listFiles(); + for (File file : topFiles) + { + if (file.isFile()) + { + Element element = getElement(file.getName()); + + result.add(element); + } + else if (file.isDirectory()) + { + File[] subFiles = file.listFiles(); + + for (File subFile : subFiles) + { + if (subFile.isFile()) + { + Element element = getElement(file.getName(), subFile.getName()); + + result.add(element); + } + } + } + } + + // + return result; + } + + /** + * {@inheritDoc} + * + */ + @Override + public Elements getElements(final String key) throws SikevaDBException + { + Elements result; + + 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); + } + } + } + + // + return result; + } + + /** + * + * @return + */ + public File getFileTreeDirectory() + { + return this.fileTreeDirectory; + } + + /** + * {@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()) + { + File[] subFiles = file.listFiles(); + + for (File subFile : subFiles) + { + if (file.isFile()) + { + result.add(subFile.getName()); + } + } + } + } + + // + return result; + } + + /** + * + * @return + */ + public String getLogin() + { + return this.login; + } + + /** + * + * @return + */ + public String getPassword() + { + return this.password; + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public StringList getSubkeys(final String key) throws SikevaDBException + { + StringList result; + + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + // + result = new StringList(); + + File targetDirectory = new File(this.dataDirectory, key); + if (targetDirectory.exists()) + { + File[] subfiles = targetDirectory.listFiles(); + for (File subfile : subfiles) + { + result.add(subfile.getName()); + } + } + } + + // + return result; + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public String getValue(final String key) throws SikevaDBException + { + String result; + + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + File targetFile = new File(this.dataDirectory, key); + if (targetFile.exists()) + { + Element element = FileTreeSikevaDBTools.loadElement(targetFile); + + result = element.getValue(); + } + else + { + result = null; + } + } + + // + return result; + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public String getValue(final String key, final String subkey) throws SikevaDBException + { + String result; + + 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()) + { + Element element = FileTreeSikevaDBTools.loadElement(targetFile); + + result = element.getValue(); + } + else + { + result = null; + } + } + + // + return result; + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public StringList getValues(final String key) throws SikevaDBException + { + StringList result; + + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + // + result = new StringList((int) countOfElements(key)); + + // + File targetDirectory = new File(this.dataDirectory, key); + + if (targetDirectory.isDirectory()) + { + File[] subFiles = targetDirectory.listFiles(); + + for (File subFile : subFiles) + { + if (subFile.isFile()) + { + Element element = FileTreeSikevaDBTools.loadElement(subFile); + + result.add(element.getValue()); + } + } + } + } + + // + return result; + } + + /** + * + * @return + */ + @Override + public boolean isClosed() + { + boolean result; + + result = !isOpened(); + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isCreated() throws SikevaDBException + { + boolean result; + + if ((this.fileTreeDirectory.isDirectory()) && (this.dataDirectory.isDirectory()) && (this.configDirectory.isDirectory())) + { + result = true; + } + else + { + result = false; + } + + // + return result; + } + + /** + * + * @return + */ + @Override + public boolean isOpened() + { + boolean result; + + if (this.status == Status.OPENED) + { + result = true; + } + else + { + result = false; + } + + // + return result; + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public long memorySize() throws SikevaDBException + { + long result; + + // + result = 0; + + // + File[] topFiles = this.dataDirectory.listFiles(); + for (File file : topFiles) + { + if (file.isFile()) + { + Element element = FileTreeSikevaDBTools.loadElement(file); + + 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(); + } + } + } + } + + // + return result; + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public long memorySize(final String key) throws SikevaDBException + { + long result; + + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + File targetFile = new File(this.dataDirectory, key); + if (targetFile.isFile()) + { + Element element = getElement(key); + + result = element.getSize(); + } + else if (targetFile.isDirectory()) + { + result = 0; + for (File subFile : targetFile.listFiles()) + { + Element element = getElement(key, subFile.getName()); + + result += element.getSize(); + } + } + else + { + result = 0; + } + } + + // + return result; + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public long memorySize(final String key, final String subkey) throws SikevaDBException + { + long result; + + 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()) + { + Element element = FileTreeSikevaDBTools.loadElement(targetFile); + + result = element.getSize(); + } + else + { + result = 0; + } + } + + // + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public void open() throws SikevaDBException + { + // + close(); + + // + this.status = Status.OPENED; + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public void put(final Element element) throws SikevaDBException + { + if (element == null) + { + throw new IllegalArgumentException("element is null."); + } + else if (element.getKey() == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + if (element.getSubkey() == null) + { + File targetFile = new File(this.dataDirectory, element.getKey()); + FileTreeSikevaDBTools.saveElement(targetFile, element); + } + else + { + File targetDirectory = new File(this.dataDirectory, element.getKey()); + if (!targetDirectory.exists()) + { + boolean result = targetDirectory.mkdir(); + if (result == false) + { + throw new SikevaDBException("Error creating key directory [" + targetDirectory + "]"); + } + } + else if (!targetDirectory.isDirectory()) + { + throw new SikevaDBException("Invalid key directory [" + element.getKey() + "]"); + } + + File targetFile = new File(targetDirectory, element.getSubkey()); + FileTreeSikevaDBTools.saveElement(targetFile, element); + } + } + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public void put(final String key, final String value) throws SikevaDBException + { + if (key == null) + { + throw new IllegalArgumentException("Null key detected."); + } + else + { + // + Element element = getElement(key); + + // + if (element == null) + { + element = new Element(); + element.setKey(key); + element.update(value); + element.setArchiveDate(null); + } + else + { + element.update(value); + } + + // + put(element); + } + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public void put(final String key, final String subkey, final String value) throws SikevaDBException + { + if ((key == null) || (subkey == null)) + { + throw new IllegalArgumentException("Null key detected [key=" + key + "][subkey=" + subkey + "]."); + } + else + { + // + Element element = getElement(key, subkey); + + // + if (element == null) + { + element = new Element(); + element.setKey(key); + element.setSubkey(subkey); + element.update(value); + element.setArchiveDate(null); + } + else + { + element.update(value); + } + + // + put(element); + } + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @Override + public void remove(final String key) throws SikevaDBException + { + 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() + "]"); + } + } + } + + /** + * {@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); + + if (oldKey == null) + { + 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()) + { + if (newFile.isFile()) + { + throw new IllegalArgumentException("Invalid newKey [" + newKey + "]."); + } + else + { + oldFile.renameTo(newFile); + } + } + else + { + oldFile.renameTo(newFile); + } + } + + this.logger.debug("renameKey done."); + } + + /** + * {@inheritDoc} + */ + @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) + { + 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 + { + File targetDirectory = new File(this.dataDirectory, key); + File oldFile = new File(targetDirectory, oldSubkey); + File newFile = new File(targetDirectory, newSubkey); + + 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 + */ + @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) + { + value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); + } + + // + put(key, value); + + this.logger.info("replaceInValue done."); + } + + /** + * {@inheritDoc} + * + * @throws SikevaDBException + */ + @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) + { + 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."); + } + + /** + * + * @param login + */ + public void setLogin(final String login) + { + this.login = login; + } + + /** + * + * @param password + */ + public void setPassword(final String password) + { + this.password = password; + } +} diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDBTools.java b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDBTools.java new file mode 100644 index 0000000..3400547 --- /dev/null +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDBTools.java @@ -0,0 +1,212 @@ +/** + * Copyright (C) 2013-2017 Christian Pierre MOMON + * + * 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 . + */ +package fr.devinsy.sikevadb.filetree; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +import javax.xml.stream.XMLStreamException; + +import org.joda.time.DateTime; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.sikevadb.core.Element; +import fr.devinsy.sikevadb.core.SikevaDBException; +import fr.devinsy.util.xml.XMLBadFormatException; +import fr.devinsy.util.xml.XMLReader; +import fr.devinsy.util.xml.XMLWriter; + +/** + * + * + * @author Christian Pierre MOMON + */ +public class FileTreeSikevaDBTools +{ + private static final Logger LOGGER = LoggerFactory.getLogger(FileTreeSikevaDBTools.class); + + /** + * + * @param source + * @return + * @throws FileNotFoundException + * @throws XMLStreamException + * @throws XMLBadFormatException + */ + public static Element loadElement(final File source) throws SikevaDBException + { + Element result; + + if (source == null) + { + result = null; + } + else if (!source.isFile()) + { + result = null; + } + else + { + XMLReader in = null; + try + { + result = new Element(); + + in = new XMLReader(source); + + in.readXMLHeader(); + + in.readStartTag("element"); + { + // private long id; IN + long id = Long.parseLong(in.readContentTag("id").getContent()); + result.setId(id); + + // private DateTime creationDate; IN + DateTime creationDate = DateTime.parse(in.readContentTag("creation_date").getContent()); + result.setCreationDate(creationDate); + + // private DateTime editionDate; IN + DateTime editionDate = DateTime.parse(in.readContentTag("edition_date").getContent()); + result.setEditionDate(editionDate); + + // private long size; IN + long size = Long.parseLong(in.readContentTag("size").getContent()); + result.setSize(size); + + // private String digest; IN + String digest = in.readContentTag("digest").getContent(); + result.setDigest(digest); + + // private String value; IN + String value = in.readContentTag("value").getContent(); + result.setValue(value); + } + in.readEndTag("element"); + } + catch (XMLStreamException exception) + { + throw new SikevaDBException("Error loading element.", exception); + } + catch (XMLBadFormatException exception) + { + throw new SikevaDBException("Error loading element.", exception); + } + catch (FileNotFoundException exception) + { + throw new SikevaDBException("Error loading element.", exception); + } + finally + { + if (in != null) + { + try + { + in.close(); + } + catch (XMLStreamException exception) + { + LOGGER.error("Finally close failed.", exception); + } + + } + } + } + + // + return result; + } + + /** + * + * @param target + * @param source + * @throws SikevaDBException + */ + public static void saveElement(final File target, final Element source) throws SikevaDBException + { + LOGGER.debug("[target={}]", target); + + if (target == null) + { + throw new IllegalArgumentException("Target file is null."); + } + else if (target.isDirectory()) + { + throw new IllegalArgumentException("Target is a directory."); + } + else + { + XMLWriter out = null; + try + { + out = new XMLWriter(target); + + out.writeXMLHeader(); + + out.writeStartTag("element"); + { + // private long id; IN + out.writeTag("id", source.getId()); + + // private DateTime creationDate; IN + out.writeTag("creation_date", source.getCreationDate().toString()); + + // private DateTime editionDate; IN + out.writeTag("edition_date", source.getEditionDate().toString()); + + // private long size; IN + out.writeTag("size", source.getSize()); + + // private String digest; IN + out.writeTag("digest", source.getDigest()); + + // private String value; IN + out.writeTag("value", source.getValue()); + } + out.writeEndTag("element"); + } + catch (UnsupportedEncodingException exception) + { + throw new SikevaDBException("Error saving element.", exception); + } + catch (FileNotFoundException exception) + { + throw new SikevaDBException("File Not Found saving element.", exception); + } + finally + { + if (out != null) + { + try + { + out.close(); + } + catch (IOException exception) + { + LOGGER.error("Finally close failed.", exception); + } + } + } + } + } +} diff --git a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java index d913abe..6351593 100644 --- a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY + * Copyright (C) 2013-2017 Christian Pierre MOMON * * This file is part of SikevaDB, simple key value database. * @@ -26,7 +26,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.Date; import javax.naming.Context; import javax.naming.InitialContext; @@ -34,14 +33,14 @@ import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.commons.lang3.StringUtils; -import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import fr.devinsy.sikevadb.Element; -import fr.devinsy.sikevadb.Elements; -import fr.devinsy.sikevadb.SikevaDB; -import fr.devinsy.util.StringList; +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.util.strings.StringList; /** * @@ -50,8 +49,16 @@ import fr.devinsy.util.StringList; */ public class SQLSikevaDB implements SikevaDB { - private static final Logger logger = LoggerFactory.getLogger(SQLSikevaDB.class); + public enum Status + { + OPENED, + CLOSED + }; + private static final Logger logger = LoggerFactory.getLogger(SQLSikevaDB.class);; + + private Status status; + private boolean archiveFlag; private String driverClassname; private String url; private String login; @@ -66,10 +73,13 @@ public class SQLSikevaDB implements SikevaDB */ public SQLSikevaDB(final String contextName) { + this.status = Status.CLOSED; this.contextName = contextName; + this.archiveFlag = true; } /** + * This method opens a database session. * * @param host * @param port @@ -96,6 +106,7 @@ public class SQLSikevaDB implements SikevaDB } else { + this.status = Status.CLOSED; this.driverClassname = driverClassName; this.url = url; this.login = login; @@ -105,207 +116,35 @@ public class SQLSikevaDB implements SikevaDB } /** - * {@inheritDoc} - */ - @Override - public void archive(final String key) throws SQLException - { - // - Element element = getElement(key); - - if (element == null) - { - throw new NullPointerException("Undefined element [key=" + key + "]"); - } - else - { - // - element.archive(); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); - - statement.setTimestamp(1, toTimestamp(element.getArchiveDate())); - statement.setString(2, element.getKey()); - - statement.executeUpdate(); - - } - finally - { - close(connection, statement, resultSet); - } - } - } - - /** - * {@inheritDoc} - */ - @Override - public void archive(final String key, final String subkey) throws SQLException - { - // - if (subkey == null) - { - archive(key); - } - else - { - // - Element element = getElement(key, subkey); - - if (element == null) - { - throw new NullPointerException("Undefined element [key=" + key + "][subkey=" + subkey + "]"); - } - else - { - // - element.archive(); - - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); - - statement.setTimestamp(1, toTimestamp(element.getArchiveDate())); - statement.setString(2, element.getKey()); - statement.setString(3, element.getSubkey()); - - statement.executeUpdate(); - - } - finally - { - close(connection, statement, resultSet); - } - } - } - } - - /** - * {@inheritDoc} - */ - @Override - public void clearAllArchive() throws SQLException - { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL"); - - statement.executeUpdate(); - - } - finally - { - close(connection, statement, resultSet); - } - } - - /** - * {@inheritDoc} - */ - @Override - public void clearArchive(final DateTime beforeDate) throws SQLException - { - // - if (beforeDate == null) - { - throw new NullPointerException("beforeDate is null."); - } - else - { - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try - { - // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND UNIX_TIMESTAMP(ARCHIVE_DATE) 0)) + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); + + statement.setString(1, key); + + int rowCount = statement.executeUpdate(); + + if (rowCount == 0) + { + logger.warn("Remove action without existing target [key=" + key + "]"); + } + } + catch (SQLException exception) + { + logger.error("Error removing element.", exception); + try + { + connection.rollback(); + } + catch (SQLException exception1) + { + logger.error("Rollback failed.", exception); + } + throw new SikevaDBException("Error removing element", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void remove(final String key, final String subkey) throws SikevaDBException + { + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if (subkey == null) + { + throw new IllegalArgumentException("Subkey is null."); + } + else + { + // + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); + + statement.setString(1, key); + statement.setString(2, subkey); + + int rowCount = statement.executeUpdate(); + + if (rowCount == 0) + { + logger.warn("Remove action without existing target [key=" + key + "][subkey=" + subkey + "]"); + } + } + catch (SQLException exception) + { + logger.error("Error removing subkey.", exception); + throw new SikevaDBException("Error removing subkey", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void removeMany(final String key, final String... subkeys) throws SikevaDBException + { + if (key == null) + { + throw new IllegalArgumentException("Key is null."); + } + else if ((subkeys != null) && (subkeys.length > 0)) { // Connection connection = null; @@ -2407,9 +1815,14 @@ public class SQLSikevaDB implements SikevaDB logger.warn("Remove action without existing target [key=" + key + "][subkeys=" + new StringList(subkeys).toStringWithCommas() + "]"); } } + catch (SQLException exception) + { + logger.error("Error removing subkeys.", exception); + throw new SikevaDBException("Error removing subkeys", exception); + } finally { - close(connection, statement, resultSet); + closeQuietly(connection, statement, resultSet); } } } @@ -2418,29 +1831,45 @@ public class SQLSikevaDB implements SikevaDB * {@inheritDoc} */ @Override - public void renameKey(final String oldKey, final String newKey) throws SQLException + public void renameKey(final String oldKey, final String newKey) throws SikevaDBException { logger.info("renameKey starting... [{}][{}]", oldKey, newKey); - // - Connection connection = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try + if (oldKey == null) + { + throw new IllegalArgumentException("OldKey is null."); + } + else if (newKey == null) + { + throw new IllegalArgumentException("NewKey is null."); + } + else { // - connection = getConnection(); - connection.setAutoCommit(true); - statement = connection.prepareStatement("UPDATE sikevadb_elements SET TOPKEY=? WHERE TOPKEY=?"); + Connection connection = null; + PreparedStatement statement = null; + ResultSet resultSet = null; + try + { + // + connection = getConnection(); + connection.setAutoCommit(true); + statement = connection.prepareStatement("UPDATE sikevadb_elements SET TOPKEY=? WHERE TOPKEY=?"); - statement.setString(1, newKey); - statement.setString(2, oldKey); + statement.setString(1, newKey); + statement.setString(2, oldKey); - statement.executeUpdate(); - } - finally - { - close(connection, statement, resultSet); + statement.executeUpdate(); + } + catch (SQLException exception) + { + logger.error("Error renaming subkey.", exception); + throw new SikevaDBException("Error renaming subkey", exception); + } + finally + { + closeQuietly(connection, statement, resultSet); + } } logger.info("renameKey done."); @@ -2450,7 +1879,23 @@ public class SQLSikevaDB implements SikevaDB * {@inheritDoc} */ @Override - public void replaceInValue(final String key, final String... tokens) throws SQLException + public void renameSubKey(final String key, final String oldKey, final String newKey) throws SikevaDBException + { + if ((key == null) || (oldKey == null) || (newKey == null)) + { + throw new IllegalArgumentException("Null parameter detected [key=" + key + "][oldKey=" + oldKey + "][newKey=" + newKey + "]."); + } + else + { + // TODO Auto-generated method stub + } + } + + /** + * {@inheritDoc} + */ + @Override + public void replaceInValue(final String key, final String... tokens) throws SikevaDBException { logger.info("replaceInValue starting... [{}]", key); @@ -2460,7 +1905,6 @@ public class SQLSikevaDB implements SikevaDB // for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) { - // value = value.replaceAll(tokens[tokenIndex], tokens[tokenIndex + 1]); } @@ -2474,7 +1918,7 @@ public class SQLSikevaDB implements SikevaDB * {@inheritDoc} */ @Override - public void replaceInValues(final String key, final String... tokens) throws SQLException + public void replaceInValues(final String key, final String... tokens) throws SikevaDBException { logger.info("replaceInValues starting... [{}]", key); @@ -2490,7 +1934,7 @@ public class SQLSikevaDB implements SikevaDB if (element.getSubkey() != null) { // - count++; + count += 1; // String value = element.getValue(); @@ -2509,89 +1953,48 @@ public class SQLSikevaDB implements SikevaDB logger.info("replaceInValues done."); } + /** + * + */ + public void setArchiveFlag(final boolean value) + { + this.archiveFlag = value; + } + + /** + * + * @param driverClassname + */ public void setDriverClassname(final String driverClassname) { this.driverClassname = driverClassname; } + /** + * + * @param login + */ public void setLogin(final String login) { this.login = login; } + /** + * + * @param password + */ public void setPassword(final String password) { this.password = password; } + /** + * + * @param url + */ public void setUrl(final String url) { this.url = url; } - /** - * - * @param source - * @return - */ - public static DateTime toDateTime(final java.sql.Timestamp source) - { - DateTime result; - - if (source == null) - { - result = null; - } - else - { - result = new DateTime(source.getTime()); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static java.sql.Timestamp toTimestamp(final Date source) - { - java.sql.Timestamp result; - - if (source == null) - { - result = null; - } - else - { - result = new java.sql.Timestamp(source.getTime()); - } - - // - return result; - } - - /** - * - * @param source - * @return - */ - public static java.sql.Timestamp toTimestamp(final DateTime source) - { - java.sql.Timestamp result; - - if (source == null) - { - result = null; - } - else - { - result = new java.sql.Timestamp(source.getMillis()); - } - - // - return result; - } } diff --git a/src/fr/devinsy/sikevadb/sql/SQLSikevaDBTools.java b/src/fr/devinsy/sikevadb/sql/SQLSikevaDBTools.java index 6a2aa56..a4b0d5f 100644 --- a/src/fr/devinsy/sikevadb/sql/SQLSikevaDBTools.java +++ b/src/fr/devinsy/sikevadb/sql/SQLSikevaDBTools.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY + * Copyright (C) 2013-2017 Christian Pierre MOMON * * This file is part of SikevaDB, simple key value database. * @@ -22,13 +22,15 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.StringReader; import java.net.URL; +import java.util.Date; import org.apache.commons.lang3.StringUtils; +import org.joda.time.DateTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.util.FileTools; -import fr.devinsy.util.StringList; +import fr.devinsy.util.strings.StringList; /** * @@ -117,4 +119,70 @@ public class SQLSikevaDBTools // return result; } + + /** + * + * @param source + * @return + */ + public static DateTime toDateTime(final java.sql.Timestamp source) + { + DateTime result; + + if (source == null) + { + result = null; + } + else + { + result = new DateTime(source.getTime()); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static java.sql.Timestamp toTimestamp(final Date source) + { + java.sql.Timestamp result; + + if (source == null) + { + result = null; + } + else + { + result = new java.sql.Timestamp(source.getTime()); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public static java.sql.Timestamp toTimestamp(final DateTime source) + { + java.sql.Timestamp result; + + if (source == null) + { + result = null; + } + else + { + result = new java.sql.Timestamp(source.getMillis()); + } + + // + return result; + } } diff --git a/src/utils/BuildInformation.java b/src/utils/BuildInformation.java new file mode 100644 index 0000000..e186d3b --- /dev/null +++ b/src/utils/BuildInformation.java @@ -0,0 +1,189 @@ +/** + * Copyright (C) 2013-2016 Christian Pierre MOMON + * + * 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 . + */ +package utils; + +import java.io.IOException; +import java.net.URL; +import java.util.Properties; + +import org.apache.commons.lang3.exception.ExceptionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Christian Pierre MOMON (christian.momon@devinsy.fr) + */ +public class BuildInformation +{ + private static class SingletonHolder + { + private static final BuildInformation instance = new BuildInformation(); + } + + private static final Logger logger = LoggerFactory.getLogger(BuildInformation.class); + + private static String BUILD_INFORMATION_FILE = "/fr/devinsy/sikevadb/build_information.properties"; + + private String productName; + private String majorRevision; + private String minorRevision; + private String buildNumber; + private String buildDate; + private String generator; + private String buildAuthor; + + /** + * + */ + private BuildInformation() + { + Properties build = new Properties(); + + try + { + // + URL buildInformationFile = BuildInformation.class.getResource(BUILD_INFORMATION_FILE); + + if (buildInformationFile != null) + { + build.load(BuildInformation.class.getResource(BUILD_INFORMATION_FILE).openStream()); + } + + // + this.productName = build.getProperty("product.name", "DevInProgress"); + this.majorRevision = build.getProperty("product.revision.major", "d"); + this.minorRevision = build.getProperty("product.revision.minor", "e"); + this.buildNumber = build.getProperty("product.revision.build", "v"); + this.buildDate = build.getProperty("product.revision.date", "today"); + this.generator = build.getProperty("product.revision.generator", "n/a"); + this.buildAuthor = build.getProperty("product.revision.author", "n/a"); + + } + catch (IOException exception) + { + logger.error("Error loading the build.properties file: " + exception.getMessage()); + logger.error(ExceptionUtils.getStackTrace(exception)); + + this.productName = "n/a"; + this.majorRevision = "n/a"; + this.minorRevision = "n/a"; + this.buildNumber = "n/a"; + this.buildDate = "n/a"; + this.generator = "n/a"; + this.buildAuthor = "n/a"; + } + } + + /** + * + * @return + */ + public String buildAuthor() + { + return this.buildAuthor; + } + + public String buildDate() + { + return this.buildDate; + } + + public String buildNumber() + { + return this.buildNumber; + } + + public String generator() + { + return this.generator; + } + + public String majorRevision() + { + return this.majorRevision; + } + + public String minorRevision() + { + return this.minorRevision; + } + + public String productName() + { + return this.productName; + } + + /** + * + */ + @Override + public String toString() + { + String result; + + result = String.format("%s %s.%s.%s built on %s by %s", this.productName, this.majorRevision, this.minorRevision, this.buildNumber, this.buildDate, this.buildAuthor); + + // + return result; + } + + /** + * + * @return + */ + public String version() + { + String result; + + result = String.format("%s.%s.%s", this.majorRevision, this.minorRevision, this.buildNumber); + + // + return result; + } + + /** + * + * @return + */ + public static BuildInformation instance() + { + return SingletonHolder.instance; + } + + /** + * + */ + public static boolean isDefined() + { + boolean result; + + if (BuildInformation.class.getResource(BUILD_INFORMATION_FILE) == null) + { + result = false; + } + else + { + result = true; + } + + // + return result; + } +} diff --git a/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java new file mode 100644 index 0000000..8ee7903 --- /dev/null +++ b/test/fr/devinsy/sikevadb/filetree/TreeFileSikevaDBTest.java @@ -0,0 +1,644 @@ +/** + * Copyright (C) 2013-2017 Christian Pierre MOMON, DEVINSY + * + * 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 . + */ +package fr.devinsy.sikevadb.filetree; + +import java.io.File; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.log4j.BasicConfigurator; +import org.apache.log4j.Level; +import org.apache.log4j.Logger; +import org.joda.time.DateTime; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +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.util.strings.StringList; + +/** + * + * @author Christian Pierre MOMON + */ +public class TreeFileSikevaDBTest +{ + private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TreeFileSikevaDBTest.class); + private static FileTreeSikevaDB database; + + /** + * @throws Exception + */ + @Test + public void testGetKeyse01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + database.put("alpha01", "1234567890"); + database.put("alpha01", "qlskjfmlqj"); + database.put("alpha02", "qlskjfmlqj"); + database.put("alpha03", "qlskjfmlqj"); + database.put("alpha04", "qlskjfmlqj"); + database.put("alpha05", "qlskjfmlqj"); + database.put("alpha01s", "bravo1", "1234567890"); + database.put("alpha01s", "bravo1", "qlskjfmlqj"); + database.put("alpha01s", "bravo2", "qlskjfmlqj"); + database.put("alpha01s", "bravo3", "qlskjfmlqj"); + database.put("alpha01s", "bravo4", "qlskjfmlqj"); + database.put("alpha01s", "bravo5", "qlskjfmlqj"); + + Assert.assertEquals(0, database.getSubkeys("none").size()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testGetKeyse02() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + database.put("alpha01", "1234567890"); + database.put("alpha01", "qlskjfmlqj"); + database.put("alpha02", "qlskjfmlqj"); + database.put("alpha03", "qlskjfmlqj"); + database.put("alpha04", "qlskjfmlqj"); + database.put("alpha05", "qlskjfmlqj"); + database.put("alpha01s", "bravo1", "1234567890"); + database.put("alpha01s", "bravo1", "qlskjfmlqj"); + database.put("alpha01s", "bravo2", "qlskjfmlqj"); + database.put("alpha01s", "bravo3", "qlskjfmlqj"); + database.put("alpha01s", "bravo4", "qlskjfmlqj"); + database.put("alpha01s", "bravo5", "qlskjfmlqj"); + + StringList keys = database.getKeys(); + + Assert.assertEquals(5, keys.size()); + Assert.assertTrue(keys.contains("alpha03")); + Assert.assertEquals(0, database.getSubkeys("alpha03s").size()); + Assert.assertEquals(5, database.getSubkeys("alpha01s").size()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testMemorySize01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + Assert.assertEquals(0, database.memorySize()); + + database.put("alpha01", "1234567890"); + database.put("alpha01", "qlskjfmlqj"); + database.put("alpha02", "qlskjfmlqj"); + database.put("alpha03", "qlskjfmlqj"); + database.put("alpha04", "qlskjfmlqj"); + database.put("alpha05", "qlskjfmlqj"); + database.put("alpha01s", "bravo1", "1234567890"); + database.put("alpha01s", "bravo1", "qlskjfmlqj"); + database.put("alpha01s", "bravo2", "qlskjfmlqj"); + database.put("alpha01s", "bravo3", "qlskjfmlqj"); + database.put("alpha01s", "bravo4", "qlskjfmlqj"); + database.put("alpha01s", "bravo5", "qlskjfmlqj"); + + Assert.assertEquals(100, database.memorySize()); + Assert.assertEquals(10, database.memorySize("alpha03")); + Assert.assertEquals(50, database.memorySize("alpha01s")); + Assert.assertEquals(10, database.memorySize("alpha01s", "bravo1")); + + database.clear(); + + Assert.assertEquals(0, database.memorySize()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testPut01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + String source = "bonjour"; + database.put("alpha01", source); + String target = database.getValue("alpha01"); + Assert.assertEquals(source, target); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testPut02() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + String source = "bonjour"; + database.put("alpha01", source); + + String target = database.getValue("alpha01"); + Assert.assertEquals(source, target); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test(expected = IllegalArgumentException.class) + public void testPut03() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + database.getValue("foo", null); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testPut04() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + String source = "bonjour"; + database.put("alpha02", "bravo", source); + + String target = database.getValue("alpha02", "bravo"); + Assert.assertEquals(source, target); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testPut05() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + String source = "bonjour"; + database.put("alpha01", source); + + String target = database.getValue("alpha01"); + Assert.assertEquals(source, target); + + String source2 = "au revoir"; + database.put("alpha01", source2); + + target = database.getValue("alpha01"); + Assert.assertEquals(source2, target); + + // TODO StringList targets = database.getArchivedValues("alpha01"); + // TODO Assert.assertEquals(1, targets.size()); + // TODO Assert.assertEquals(source, targets.get(0)); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testPut06() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + String source = "bonjour"; + database.put("alpha01", "bravo", source); + + String target = database.getValue("alpha01", "bravo"); + Assert.assertEquals(source, target); + + String source2 = "au revoir"; + database.put("alpha01", "bravo", source2); + + target = database.getValue("alpha01", "bravo"); + Assert.assertEquals(source2, target); + + // TODO StringList targets = database.getArchivedValues("alpha01", + // "bravo"); + // TODO Assert.assertEquals(1, targets.size()); + // TODO Assert.assertEquals(source, targets.get(0)); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testPutElement01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + Element element = new Element(); + + element.setKey("alpha"); + element.setSubkey(null); + element.setSize(10); + element.setDigest("qsdkfqskjf"); + element.setCreationDate(DateTime.now().minusDays(11)); + element.setEditionDate(DateTime.now().minusDays(11)); + element.setArchiveDate(DateTime.now().minusDays(10)); + element.setValue("bonjour"); + + database.put(element); + + Assert.assertEquals(1, database.countOfElements()); + // TODO Assert.assertEquals(1, database.countOfArchivedElements()); + // TODO Assert.assertEquals(1, database.countOfAllElements()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testPutElement02() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + logger.debug("count={}", database.countOfElements()); + + Element element = new Element(); + + element.setId(123); + element.setKey("alpha"); + element.setSubkey(null); + element.setSize(10); + element.setDigest("qsdkfqskjf"); + element.setCreationDate(DateTime.now().minusDays(11)); + element.setEditionDate(DateTime.now().minusDays(11)); + element.setArchiveDate(DateTime.now().minusDays(10)); + element.setValue("bonjour"); + + database.put(element); + + Assert.assertEquals(1, database.countOfElements()); + // TODO Assert.assertEquals(1, database.countOfArchivedElements()); + // TODO Assert.assertEquals(1, database.countOfAllElements()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testPutElement03() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + { + Element element = new Element(); + + element.setId(123); + element.setKey("alpha"); + element.setSubkey(null); + element.setSize(10); + element.setDigest("qsdkfqskjf"); + element.setCreationDate(DateTime.now().minusDays(11)); + element.setEditionDate(DateTime.now().minusDays(11)); + element.setArchiveDate(DateTime.now().minusDays(10)); + element.setValue("bonjour"); + + database.put(element); + } + + { + Element element = new Element(); + + element.setKey("bravo"); + element.setSubkey(null); + element.setSize(10); + element.setDigest("qsdkfqskjf"); + element.setCreationDate(DateTime.now().minusDays(11)); + element.setEditionDate(DateTime.now().minusDays(11)); + element.setArchiveDate(DateTime.now().minusDays(10)); + element.setValue("bonjour"); + + database.put(element); + } + + // TODO Elements elements = database.getAllElements(); + Elements elements = database.getElements(); + + Assert.assertEquals(2, elements.size()); + Assert.assertEquals(2, database.countOfElements()); + // TODO Assert.assertEquals(2, database.countOfArchivedElements()); + // TODO Assert.assertEquals(2, database.countOfAllElements()); + + // TODO Assert.assertEquals(123, elements.get(0).getId()); + // TODO Assert.assertEquals(124, elements.get(1).getId()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testRandom01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + String surSource = RandomStringUtils.random(128); + String source = org.apache.commons.codec.binary.Base64.encodeBase64String(surSource.getBytes()); + database.put("alpha01", source); + + String target = database.getValue("alpha01"); + Assert.assertEquals(source, target); + + String surTarget = new String(Base64.decodeBase64(target)); + Assert.assertEquals(surSource, surTarget); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testRemove01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + String source = "bonjour"; + database.put("alpha01", "bravo", source); + + String target = database.getValue("alpha01", "bravo"); + Assert.assertEquals(source, target); + + String source2 = "au revoir"; + database.put("alpha01", "bravo", source2); + + target = database.getValue("alpha01", "bravo"); + Assert.assertEquals(source2, target); + + // TODO StringList targets = database.getArchivedValues("alpha01", + // "bravo"); + // TODO Assert.assertEquals(1, targets.size()); + // TODO Assert.assertEquals(source, targets.get(0)); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testRemoveMany01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + database.put("alpha01", "alpha", "Allo"); + database.put("alpha01", "bravo", "Bonjour"); + database.put("alpha01", "charlie", "Courage"); + database.put("alpha01", "delta", "Droiture"); + database.put("alpha01", "echo", "Europe"); + database.put("alpha01", "fox", "Force"); + + database.put("alpha02", "alpha", "Allo"); + database.put("alpha02", "bravo", "Bonjour"); + database.put("alpha02", "charlie", "Courage"); + database.put("alpha02", "delta", "Droiture"); + database.put("alpha02", "echo", "Europe"); + database.put("alpha02", "fox", "Force"); + + Assert.assertEquals(12, database.countOfElements()); + + database.removeMany("alpha01", "bravo", "delta", "fox"); + + Assert.assertEquals(9, database.countOfElements()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testRenameKey01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + database.put("alpha01", "val-alpha01"); + database.put("alpha01s", "sub-alpha01", "val-alpha01"); + database.put("alpha01s", "sub-alpha02", "val-alpha02"); + database.put("alpha01s", "sub-alpha03", "val-alpha03"); + database.put("alpha02s", "sub-alpha01", "val-alpha01"); + database.put("alpha03s", "sub-alpha02", "val-alpha02"); + database.put("alpha04s", "sub-alpha03", "val-alpha03"); + + database.renameKey("alpha01s", "november"); + + Elements targets = database.getElements(); + + Assert.assertEquals(7, targets.size()); + Assert.assertEquals(3, database.getValues("november").size()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testSize01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + Assert.assertEquals(0, database.countOfElements()); + // TODO Assert.assertEquals(0, database.countOfArchivedElements()); + // TODO Assert.assertEquals(0, database.countOfAllElements()); + + database.put("alpha01", "qlskjfmlqja"); + database.put("alpha01", "qlskjfmlqjb"); + database.put("alpha02", "qlskjfmlqj"); + database.put("alpha03", "qlskjfmlqj"); + database.put("alpha04", "qlskjfmlqj"); + database.put("alpha05", "qlskjfmlqj"); + database.remove("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"); + + // System.out.println(database.countOfElements() + " " + + // database.countOfArchivedElements() + " " + + // database.countOfAllElements()); + + Assert.assertEquals(8, database.countOfElements()); + // TODO Assert.assertEquals(4, database.countOfArchivedElements()); + // TODO Assert.assertEquals(10, database.countOfAllElements()); + + database.clear(); + + Assert.assertEquals(0, database.countOfElements()); + // TODO Assert.assertEquals(0, database.countOfArchivedElements()); + // TODO Assert.assertEquals(0, database.countOfAllElements()); + + // + logger.debug("===== test done."); + } + + /** + * @throws Exception + */ + @Test + public void testTittle01() throws Exception + { + // + logger.debug("===== test starting..."); + + database.clear(); + + String source = "ME ME MEààààà ıııı éééé"; + database.put("alpha01", source); + String target = database.getValue("alpha01"); + Assert.assertEquals(source, target); + + // + logger.debug("===== test done."); + } + + /** + * + */ + @AfterClass + public static void afterClass() + { + if (database != null) + { + database.close(); + } + } + + /** + * @throws SikevaDBException + */ + @BeforeClass + public static void beforeClass() throws SikevaDBException + { + BasicConfigurator.configure(); + Logger.getRootLogger().setLevel(Level.DEBUG); + + // Add ?profileSQL=true to generate huge logs. + + // database = new SQLSikevaDB("com.mysql.jdbc.Driver", + // "jdbc:mysql://localhost:3306/sikevadb-test", "sikevadb-test", + // "12345678"); + + File rootDirectory = new File("/tmp/footest"); + database = new FileTreeSikevaDB(rootDirectory, null, null); + + if (!database.isCreated()) + { + database.create(); + } + + database.open(); + } +} diff --git a/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java b/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java index f8b7a26..bd4132e 100644 --- a/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java +++ b/test/fr/devinsy/sikevadb/sql/SQLSikevaDBTest.java @@ -1,5 +1,5 @@ /** - * Copyright (C) 2013-2016 Christian Pierre MOMON, DEVINSY + * Copyright (C) 2013-2017 Christian Pierre MOMON, DEVINSY * * This file is part of SikevaDB, simple key value database. * @@ -18,11 +18,6 @@ */ package fr.devinsy.sikevadb.sql; -import java.io.IOException; -import java.sql.SQLException; - -import javax.naming.NamingException; - import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.RandomStringUtils; import org.apache.log4j.BasicConfigurator; @@ -34,10 +29,10 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import fr.devinsy.sikevadb.Element; -import fr.devinsy.sikevadb.Elements; -import fr.devinsy.sikevadb.sql.SQLSikevaDB; -import fr.devinsy.util.StringList; +import fr.devinsy.sikevadb.core.Element; +import fr.devinsy.sikevadb.core.Elements; +import fr.devinsy.sikevadb.core.SikevaDBException; +import fr.devinsy.util.strings.StringList; /** * @@ -57,7 +52,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); Element element = new Element(); @@ -74,11 +69,11 @@ public class SQLSikevaDBTest database.put("alpha", "bravo", "toto"); - database.clearArchive(5); + // TODO database.clearArchive(5); Assert.assertEquals(1, database.countOfElements()); - Assert.assertEquals(0, database.countOfArchivedElements()); - Assert.assertEquals(1, database.countOfAllElements()); + // TODO Assert.assertEquals(0, database.countOfArchivedElements()); + // TODO Assert.assertEquals(1, database.countOfAllElements()); // logger.debug("===== test done."); @@ -86,7 +81,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testClearArchive02() throws Exception @@ -94,7 +88,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); Element element = new Element(); @@ -111,11 +105,11 @@ public class SQLSikevaDBTest database.put("alpha", "bravo", "toto"); - database.clearArchive(DateTime.now().minusDays(5)); + // TODO database.clearArchive(DateTime.now().minusDays(5)); Assert.assertEquals(1, database.countOfElements()); - Assert.assertEquals(0, database.countOfArchivedElements()); - Assert.assertEquals(1, database.countOfAllElements()); + // TODO Assert.assertEquals(0, database.countOfArchivedElements()); + // TODO Assert.assertEquals(1, database.countOfAllElements()); // logger.debug("===== test done."); @@ -123,7 +117,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testGeKeyse01() throws Exception @@ -131,7 +124,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); database.put("alpha01", "1234567890"); database.put("alpha01", "qlskjfmlqj"); @@ -154,7 +147,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testGeKeyse02() throws Exception @@ -162,7 +154,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); database.put("alpha01", "1234567890"); database.put("alpha01", "qlskjfmlqj"); @@ -190,7 +182,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testMemorySize01() throws Exception @@ -198,7 +189,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); Assert.assertEquals(0, database.memorySize()); @@ -215,12 +206,12 @@ public class SQLSikevaDBTest database.put("alpha01", "bravo4", "qlskjfmlqj"); database.put("alpha01", "bravo5", "qlskjfmlqj"); - Assert.assertEquals(120, database.memorySize()); + Assert.assertEquals(100, database.memorySize()); Assert.assertEquals(60, database.memorySize("alpha01")); Assert.assertEquals(10, database.memorySize("alpha03")); Assert.assertEquals(10, database.memorySize("alpha01", "bravo1")); - database.clearDatabase(); + database.clear(); Assert.assertEquals(0, database.memorySize()); @@ -230,7 +221,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testPut01() throws Exception @@ -238,7 +228,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); String source = "bonjour"; database.put("alpha01", source); @@ -251,7 +241,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testPut02() throws Exception @@ -259,7 +248,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); String source = "bonjour"; database.put("alpha01", source); @@ -267,33 +256,22 @@ public class SQLSikevaDBTest String target = database.getValue("alpha01"); Assert.assertEquals(source, target); - target = database.getValue("alpha01", null); - Assert.assertEquals(source, target); - // logger.debug("===== test done."); } /** * @throws Exception - * */ - @Test + @Test(expected = IllegalArgumentException.class) public void testPut03() throws Exception { // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); - String source = "bonjour"; - database.put("alpha01", null, source); - - String target = database.getValue("alpha01"); - Assert.assertEquals(source, target); - - target = database.getValue("alpha01", null); - Assert.assertEquals(source, target); + database.getValue("foo", null); // logger.debug("===== test done."); @@ -301,7 +279,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testPut04() throws Exception @@ -309,7 +286,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); String source = "bonjour"; database.put("alpha02", "bravo", source); @@ -323,7 +300,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testPut05() throws Exception @@ -331,7 +307,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); String source = "bonjour"; database.put("alpha01", source); @@ -345,9 +321,9 @@ public class SQLSikevaDBTest target = database.getValue("alpha01"); Assert.assertEquals(source2, target); - StringList targets = database.getArchivedValues("alpha01"); - Assert.assertEquals(1, targets.size()); - Assert.assertEquals(source, targets.get(0)); + // TODO StringList targets = database.getArchivedValues("alpha01"); + // TODO Assert.assertEquals(1, targets.size()); + // TODO Assert.assertEquals(source, targets.get(0)); // logger.debug("===== test done."); @@ -355,7 +331,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testPut06() throws Exception @@ -363,7 +338,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); String source = "bonjour"; database.put("alpha01", "bravo", source); @@ -377,9 +352,10 @@ public class SQLSikevaDBTest target = database.getValue("alpha01", "bravo"); Assert.assertEquals(source2, target); - StringList targets = database.getArchivedValues("alpha01", "bravo"); - Assert.assertEquals(1, targets.size()); - Assert.assertEquals(source, targets.get(0)); + // TODO StringList targets = database.getArchivedValues("alpha01", + // "bravo"); + // TODO Assert.assertEquals(1, targets.size()); + // TODO Assert.assertEquals(source, targets.get(0)); // logger.debug("===== test done."); @@ -387,7 +363,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testPutElement01() throws Exception @@ -395,7 +370,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); Element element = new Element(); @@ -411,8 +386,8 @@ public class SQLSikevaDBTest database.put(element); Assert.assertEquals(0, database.countOfElements()); - Assert.assertEquals(1, database.countOfArchivedElements()); - Assert.assertEquals(1, database.countOfAllElements()); + // TODO Assert.assertEquals(1, database.countOfArchivedElements()); + // TODO Assert.assertEquals(1, database.countOfAllElements()); // logger.debug("===== test done."); @@ -420,7 +395,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testPutElement02() throws Exception @@ -428,7 +402,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); Element element = new Element(); @@ -445,8 +419,8 @@ public class SQLSikevaDBTest database.put(element); Assert.assertEquals(0, database.countOfElements()); - Assert.assertEquals(1, database.countOfArchivedElements()); - Assert.assertEquals(1, database.countOfAllElements()); + // TODO Assert.assertEquals(1, database.countOfArchivedElements()); + // TODO Assert.assertEquals(1, database.countOfAllElements()); // logger.debug("===== test done."); @@ -454,7 +428,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testPutElement03() throws Exception @@ -462,7 +435,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); { Element element = new Element(); @@ -495,14 +468,15 @@ public class SQLSikevaDBTest database.put(element); } - Elements elements = database.getAllElements(); + // TODO Elements elements = database.getAllElements(); + Elements elements = database.getElements(); Assert.assertEquals(0, database.countOfElements()); - Assert.assertEquals(2, database.countOfArchivedElements()); - Assert.assertEquals(2, database.countOfAllElements()); + // TODO Assert.assertEquals(2, database.countOfArchivedElements()); + // TODO Assert.assertEquals(2, database.countOfAllElements()); - Assert.assertEquals(123, elements.get(0).getId()); - Assert.assertEquals(124, elements.get(1).getId()); + // TODO Assert.assertEquals(123, elements.get(0).getId()); + // TODO Assert.assertEquals(124, elements.get(1).getId()); // logger.debug("===== test done."); @@ -510,7 +484,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testRandom01() throws Exception @@ -518,11 +491,11 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); String surSource = RandomStringUtils.random(128); String source = org.apache.commons.codec.binary.Base64.encodeBase64String(surSource.getBytes()); - database.put("alpha01", null, source); + database.put("alpha01", source); String target = database.getValue("alpha01"); Assert.assertEquals(source, target); @@ -536,7 +509,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testRemove01() throws Exception @@ -544,7 +516,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); String source = "bonjour"; database.put("alpha01", "bravo", source); @@ -558,9 +530,10 @@ public class SQLSikevaDBTest target = database.getValue("alpha01", "bravo"); Assert.assertEquals(source2, target); - StringList targets = database.getArchivedValues("alpha01", "bravo"); - Assert.assertEquals(1, targets.size()); - Assert.assertEquals(source, targets.get(0)); + // TODO StringList targets = database.getArchivedValues("alpha01", + // "bravo"); + // TODO Assert.assertEquals(1, targets.size()); + // TODO Assert.assertEquals(source, targets.get(0)); // logger.debug("===== test done."); @@ -568,7 +541,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testRemoveMany01() throws Exception @@ -576,7 +548,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); database.put("alpha01", "alpha", "Allo"); database.put("alpha01", "bravo", "Bonjour"); @@ -604,7 +576,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testRenameKey01() throws Exception @@ -612,7 +583,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); database.put("alpha01", "val-alpha01"); database.put("alpha01", "sub-alpha01", "val-alpha01"); @@ -624,7 +595,7 @@ public class SQLSikevaDBTest database.renameKey("alpha01", "november"); - Elements targets = database.getAllElements(); + Elements targets = database.getElements(); Assert.assertEquals(7, targets.size()); Assert.assertEquals(3, database.getValues("november").size()); @@ -635,7 +606,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testSize01() throws Exception @@ -643,11 +613,11 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); Assert.assertEquals(0, database.countOfElements()); - Assert.assertEquals(0, database.countOfArchivedElements()); - Assert.assertEquals(0, database.countOfAllElements()); + // TODO Assert.assertEquals(0, database.countOfArchivedElements()); + // TODO Assert.assertEquals(0, database.countOfAllElements()); database.put("alpha01", "qlskjfmlqja"); database.put("alpha01", "qlskjfmlqjb"); @@ -655,7 +625,6 @@ public class SQLSikevaDBTest database.put("alpha03", "qlskjfmlqj"); database.put("alpha04", "qlskjfmlqj"); database.put("alpha05", "qlskjfmlqj"); - database.archive("alpha02"); database.remove("alpha03"); database.put("alpha01", "bravo1", "qlskjfmlqja"); database.put("alpha01", "bravo1", "qlskjfmlqjb"); @@ -663,22 +632,21 @@ public class SQLSikevaDBTest database.put("alpha01", "bravo3", "qlskjfmlqj"); database.put("alpha01", "bravo4", "qlskjfmlqj"); database.put("alpha01", "bravo5", "qlskjfmlqj"); - database.archive("alpha01", "bravo2"); database.remove("alpha01", "bravo3"); // System.out.println(database.countOfElements() + " " + // database.countOfArchivedElements() + " " + // database.countOfAllElements()); - Assert.assertEquals(6, database.countOfElements()); - Assert.assertEquals(4, database.countOfArchivedElements()); - Assert.assertEquals(10, database.countOfAllElements()); + Assert.assertEquals(8, database.countOfElements()); + // TODO Assert.assertEquals(4, database.countOfArchivedElements()); + // TODO Assert.assertEquals(10, database.countOfAllElements()); - database.clearDatabase(); + database.clear(); Assert.assertEquals(0, database.countOfElements()); - Assert.assertEquals(0, database.countOfArchivedElements()); - Assert.assertEquals(0, database.countOfAllElements()); + // TODO Assert.assertEquals(0, database.countOfArchivedElements()); + // TODO Assert.assertEquals(0, database.countOfAllElements()); // logger.debug("===== test done."); @@ -686,7 +654,6 @@ public class SQLSikevaDBTest /** * @throws Exception - * */ @Test public void testTittle01() throws Exception @@ -694,7 +661,7 @@ public class SQLSikevaDBTest // logger.debug("===== test starting..."); - database.clearDatabase(); + database.clear(); String source = "ME ME MEààààà ıııı éééé"; database.put("alpha01", source); @@ -718,16 +685,10 @@ public class SQLSikevaDBTest } /** - * @throws NamingException - * @throws SQLException - * @throws ClassNotFoundException - * @throws IllegalAccessException - * @throws InstantiationException - * @throws IOException - * + * @throws SikevaDBException */ @BeforeClass - public static void beforeClass() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, NamingException, IOException + public static void beforeClass() throws SikevaDBException { BasicConfigurator.configure(); Logger.getRootLogger().setLevel(Level.ERROR); @@ -740,6 +701,6 @@ public class SQLSikevaDBTest database = new SQLSikevaDB("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:sikevadb-unittest;sql.syntax_mys=true", "sa", ""); database.open(); - database.createSchema(); + database.create(); } }