+ *
+ * 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)");
-
- statement.setLong(1, beforeDate.getMillis() / 1000);
-
- statement.executeUpdate();
-
- }
- finally
- {
- close(connection, statement, resultSet);
- }
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void clearArchive(final int maxDays) throws SQLException
- {
- //
- logger.debug("maxDays={}", maxDays);
-
- //
- if (maxDays < 0)
- {
- throw new IndexOutOfBoundsException("maxDays is negative.");
- }
- else
- {
- DateTime beforeDate = DateTime.now().minusDays(maxDays);
- clearArchive(beforeDate);
- }
- }
-
- /**
+ * This methods clear all data in current opened database.
*
*/
@Override
- public void clearDatabase() throws SQLException
+ public void clear() throws SikevaDBException
{
- //
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try
{
- //
connection = getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement("DELETE FROM sikevadb_elements");
statement.executeUpdate();
}
+ catch (SQLException exception)
+ {
+ logger.error("SQL Error getting connection.", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
/**
+ * This method closes the current database session.
*
*/
@Override
@@ -320,7 +159,7 @@ public class SQLSikevaDB implements SikevaDB
}
catch (SQLException exception)
{
- exception.printStackTrace();
+ logger.error("Error closing database.", exception);
}
finally
{
@@ -330,13 +169,17 @@ public class SQLSikevaDB implements SikevaDB
//
this.dataSource = null;
+
+ this.status = Status.CLOSED;
}
/**
*
* @param connection
+ * @param statement
+ * @param resultSet
*/
- public void close(final Connection connection, final Statement statement, final ResultSet resultSet)
+ private void closeQuietly(final Connection connection, final Statement statement, final ResultSet resultSet)
{
//
if ((connection != null) && (connection != this.singleConnection))
@@ -382,211 +225,7 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public long countOfAllElements() throws SQLException
- {
- long result;
-
- Connection connection = null;
- Statement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- statement = connection.createStatement();
- resultSet = statement.executeQuery("SELECT count(*) FROM sikevadb_elements");
-
- resultSet.next();
- result = resultSet.getLong(1);
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long countOfAllElements(final String key) throws SQLException
- {
- long result;
-
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT count(*) FROM sikevadb_elements WHERE TOPKEY=?");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- resultSet.next();
- result = resultSet.getLong(1);
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long countOfAllElements(final String key, final String subkey) throws SQLException
- {
- long result;
-
- //
- if (subkey == null)
- {
- result = countOfAllElements(key);
- }
- else
- {
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT count(*) FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=?");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
-
- resultSet.next();
- result = resultSet.getLong(1);
- }
- finally
- {
- close(connection, statement, resultSet);
- }
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long countOfArchivedElements() throws SQLException
- {
- long result;
-
- Connection connection = null;
- Statement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.createStatement();
- resultSet = statement.executeQuery("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL");
-
- resultSet.next();
- result = resultSet.getLong(1);
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long countOfArchivedElements(final String key) throws SQLException
- {
- long result;
-
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=?");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- resultSet.next();
- result = resultSet.getLong(1);
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long countOfArchivedElements(final String key, final String subkey) throws SQLException
- {
- long result;
-
- //
- if (subkey == null)
- {
- result = countOfArchivedElements(key);
- }
- else
- {
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=?");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
-
- resultSet.next();
- result = resultSet.getLong(1);
-
- }
- finally
- {
- close(connection, statement, resultSet);
- }
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws SQLException
- */
- @Override
- public long countOfElements() throws SQLException
+ public long countOfElements() throws SikevaDBException
{
long result;
@@ -602,44 +241,15 @@ public class SQLSikevaDB implements SikevaDB
resultSet.next();
result = resultSet.getLong(1);
-
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error counting elements.", exception);
+ throw new SikevaDBException("Error counting elements", exception);
}
finally
{
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- *
- * @throws SQLException
- */
- @Override
- public long countOfElements(final String key) throws SQLException
- {
- long result;
-
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- resultSet.next();
- result = resultSet.getLong(1);
- }
- finally
- {
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
//
@@ -650,14 +260,60 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public long countOfElements(final String key, final String subkey) throws SQLException
+ public long countOfElements(final String key) throws SikevaDBException
{
long result;
- //
- if (subkey == null)
+ if (key == null)
{
- result = countOfElements(key);
+ 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("SELECT count(*) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL");
+ statement.setString(1, key);
+ resultSet = statement.executeQuery();
+
+ resultSet.next();
+ result = resultSet.getLong(1);
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error counting elements.", exception);
+ throw new SikevaDBException("Error counting elements", exception);
+ }
+ finally
+ {
+ closeQuietly(connection, statement, resultSet);
+ }
+ }
+
+ //
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long countOfElements(final String key, final String subkey) throws SikevaDBException
+ {
+ long result;
+
+ if (key == null)
+ {
+ throw new IllegalArgumentException("Key is null.");
+ }
+ else if (subkey == null)
+ {
+ throw new IllegalArgumentException("Subkey is null.");
}
else
{
@@ -676,9 +332,14 @@ public class SQLSikevaDB implements SikevaDB
resultSet.next();
result = resultSet.getLong(1);
}
+ catch (SQLException exception)
+ {
+ logger.error("Error counting elements.", exception);
+ throw new SikevaDBException("Error counting elements", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
@@ -690,56 +351,74 @@ public class SQLSikevaDB implements SikevaDB
* This method creates the schema (table) used by SQLSikevaDB.
*
* @throws IOException
- * @throws SQLException
+ * @throws SikevaDBException
*/
- public void createSchema() throws IOException, SQLException
+ @Override
+ public void create() throws SikevaDBException
{
- //
- Connection connection = null;
- Statement statement = null;
- try
+ if (this.status == Status.OPENED)
+ {
+ throw new SikevaDBException("Invalid state.");
+ }
+ else
{
//
- connection = getConnection();
- connection.setAutoCommit(true);
-
- System.out.println("driver name =" + connection.getMetaData().getDriverName());
- System.out.println("driver version =" + connection.getMetaData().getDriverVersion());
- System.out.println("driver database name =" + connection.getMetaData().getDatabaseProductName());
- System.out.println("driver database version =" + connection.getMetaData().getDatabaseProductVersion());
-
- //
- StringList sqlCommands;
- String databaseProductName = connection.getMetaData().getDatabaseProductName().split(" ")[0];
- logger.debug("[datatbaseProductName={}]", databaseProductName);
- sqlCommands = SQLSikevaDBTools.loadSQLScript(SQLSikevaDB.class.getResource("/fr/devinsy/sikevadb/sql/createTable-" + databaseProductName + ".sql"));
-
- if (sqlCommands == null)
- {
- throw new FileNotFoundException("SQL script creation not found for [" + databaseProductName + "].");
- }
- else
+ Connection connection = null;
+ Statement statement = null;
+ try
{
//
- for (String sql : sqlCommands)
+ connection = getConnection();
+ connection.setAutoCommit(true);
+
+ System.out.println("driver name =" + connection.getMetaData().getDriverName());
+ System.out.println("driver version =" + connection.getMetaData().getDriverVersion());
+ System.out.println("driver database name =" + connection.getMetaData().getDatabaseProductName());
+ System.out.println("driver database version =" + connection.getMetaData().getDatabaseProductVersion());
+
+ //
+ StringList sqlCommands;
+ String databaseProductName = connection.getMetaData().getDatabaseProductName().split(" ")[0];
+ logger.debug("[datatbaseProductName={}]", databaseProductName);
+ sqlCommands = SQLSikevaDBTools.loadSQLScript(SQLSikevaDB.class.getResource("/fr/devinsy/sikevadb/sql/createTable-" + databaseProductName + ".sql"));
+
+ if (sqlCommands == null)
{
- System.out.println("sql=[" + sql + "]");
+ throw new FileNotFoundException("SQL script creation not found for [" + databaseProductName + "].");
+ }
+ else
+ {
+ //
+ for (String sql : sqlCommands)
+ {
+ System.out.println("sql=[" + sql + "]");
+
+ statement = connection.createStatement();
+ statement.execute(sql);
+ }
+
+ System.out.println("============================== APRÈS");
statement = connection.createStatement();
- statement.execute(sql);
+ statement.execute("delete from sikevadb_elements");
+
+ System.out.println("============================== APRÈS2");
}
-
- System.out.println("============================== APRÈS");
-
- statement = connection.createStatement();
- statement.execute("delete from sikevadb_elements");
-
- System.out.println("============================== APRÈS2");
}
- }
- finally
- {
- close(connection, statement, null);
+ catch (SQLException exception)
+ {
+ logger.error("Error creating schema.", exception);
+ throw new SikevaDBException("Error creating schema.", exception);
+ }
+ catch (IOException exception)
+ {
+ logger.error("Error IO creating schema.", exception);
+ throw new SikevaDBException("Error IO creating schema.", exception);
+ }
+ finally
+ {
+ closeQuietly(connection, statement, null);
+ }
}
}
@@ -747,9 +426,9 @@ public class SQLSikevaDB implements SikevaDB
*
* @param id
* @return
- * @throws SQLException
+ * @throws SikevaDBException
*/
- public boolean exists(final Element element) throws SQLException
+ public boolean exists(final Element element) throws SikevaDBException
{
boolean result;
@@ -770,420 +449,12 @@ public class SQLSikevaDB implements SikevaDB
return result;
}
- /**
- * {@inheritDoc}
- */
- @Override
- public Elements getAllElements() throws SQLException
- {
- Elements result;
-
- //
- result = new Elements((int) countOfAllElements());
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements ORDER BY ARCHIVE_DATE ASC");
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- Element element = new Element();
-
- element.setId(resultSet.getLong(1));
- element.setKey(resultSet.getString(2));
- element.setSubkey(resultSet.getString(3));
- element.setValue(resultSet.getString(4));
- element.setSize(resultSet.getLong(5));
- element.setDigest(resultSet.getString(6));
- element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
- element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
- element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
-
- //
- result.add(element);
- }
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Elements getAllElements(final String key) throws SQLException
- {
- Elements result;
-
- //
- result = new Elements((int) countOfAllElements(key));
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection
- .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- Element element = new Element();
-
- element.setId(resultSet.getLong(1));
- element.setKey(resultSet.getString(2));
- element.setSubkey(resultSet.getString(3));
- element.setValue(resultSet.getString(4));
- element.setSize(resultSet.getLong(5));
- element.setDigest(resultSet.getString(6));
- element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
- element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
- element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
-
- //
- result.add(element);
- }
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Elements getAllElements(final String key, final String subkey) throws SQLException
- {
- Elements result;
-
- //
- if (subkey == null)
- {
- result = getAllElements(key);
- }
- else
- {
- //
- result = new Elements((int) countOfAllElements(key, subkey));
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection
- .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- Element element = new Element();
-
- element.setId(resultSet.getLong(1));
- element.setKey(resultSet.getString(2));
- element.setSubkey(resultSet.getString(3));
- element.setValue(resultSet.getString(4));
- element.setSize(resultSet.getLong(5));
- element.setDigest(resultSet.getString(6));
- element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
- element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
- element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
-
- //
- result.add(element);
- }
- }
- finally
- {
- close(connection, statement, resultSet);
- }
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public StringList getAllKeys() throws SQLException
- {
- StringList result;
-
- //
- result = new StringList();
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT DISTINCT TOPKEY FROM sikevadb_elements ORDER BY CREATION_DATE ASC");
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- result.add(resultSet.getString(1));
- }
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public StringList getAllSubkeys(final String key) throws SQLException
- {
- StringList result;
-
- //
- result = new StringList();
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT DISTINCT SUBKEY FROM sikevadb_elements WHERE TOPKEY=? ORDER BY CREATION_DATE ASC");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- result.add(resultSet.getString(1));
- }
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public StringList getAllValues(final String key) throws SQLException
- {
- StringList result;
-
- //
- result = new StringList((int) countOfAllElements(key));
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- result.add(resultSet.getString(1));
- }
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public StringList getAllValues(final String key, final String subkey) throws SQLException
- {
- StringList result;
-
- //
- if (subkey == null)
- {
- result = getAllValues(key);
- }
- else
- {
- //
- result = new StringList((int) countOfAllElements(key, subkey));
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- result.add(resultSet.getString(1));
- }
- }
- finally
- {
- close(connection, statement, resultSet);
- }
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public StringList getArchivedValues(final String key) throws SQLException
- {
- StringList result;
-
- //
- result = new StringList((int) countOfArchivedElements(key));
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? ORDER BY CREATION_DATE");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- result.add(resultSet.getString(1));
- }
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public StringList getArchivedValues(final String key, final String subkey) throws SQLException
- {
- StringList result;
-
- //
- if (subkey == null)
- {
- result = getArchivedValues(key);
- }
- else
- {
- //
- result = new StringList((int) countOfArchivedElements(key, subkey));
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- result.add(resultSet.getString(1));
- }
- }
- finally
- {
- close(connection, statement, resultSet);
- }
- }
-
- //
- return result;
- }
-
/**
*
* @return
- * @throws SQLException
+ * @throws SikevaDBException
*/
- public Connection getConnection() throws SQLException
+ public Connection getConnection() throws SikevaDBException
{
Connection result;
@@ -1193,7 +464,15 @@ public class SQLSikevaDB implements SikevaDB
}
else if (this.dataSource != null)
{
- result = this.dataSource.getConnection();
+ try
+ {
+ result = this.dataSource.getConnection();
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error counting elements.", exception);
+ throw new SikevaDBException("Error counting elements", exception);
+ }
}
else
{
@@ -1204,6 +483,10 @@ public class SQLSikevaDB implements SikevaDB
return result;
}
+ /**
+ *
+ * @return
+ */
public String getContextName()
{
return this.contextName;
@@ -1221,7 +504,7 @@ public class SQLSikevaDB implements SikevaDB
/**
*
*/
- public Element getElement(final long id) throws SQLException
+ public Element getElement(final long id) throws SikevaDBException
{
Element result;
@@ -1256,25 +539,100 @@ public class SQLSikevaDB implements SikevaDB
result.setValue(resultSet.getString(4));
result.setSize(resultSet.getLong(5));
result.setDigest(resultSet.getString(6));
- result.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
- result.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
- result.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
+ result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7)));
+ result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8)));
+ result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9)));
+ //
+ if (resultSet.next())
+ {
+ throw new SikevaDBException("More than only once result [id=" + id + "].");
+ }
}
else
{
result = null;
}
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error getting element.", exception);
+ throw new SikevaDBException("Error getting element", exception);
+ }
+ finally
+ {
+ closeQuietly(connection, statement, resultSet);
+ }
+ }
+
+ //
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Element getElement(final String key) throws SikevaDBException
+ {
+ Element result;
+
+ //
+ 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("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL");
+ statement.setString(1, key);
+ resultSet = statement.executeQuery();
//
if (resultSet.next())
{
- throw new SQLException("More than only once result [id=" + id + "].");
+ //
+ result = new Element();
+
+ result.setId(resultSet.getLong(1));
+ result.setKey(resultSet.getString(2));
+ result.setSubkey(resultSet.getString(3));
+ result.setValue(resultSet.getString(4));
+ result.setSize(resultSet.getLong(5));
+ result.setDigest(resultSet.getString(6));
+ result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7)));
+ result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8)));
+ result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9)));
+
+ //
+ if (resultSet.next())
+ {
+ throw new SikevaDBException("More than only once result.");
+ }
}
+ else
+ {
+ result = null;
+ }
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error getting element.", exception);
+ throw new SikevaDBException("Error getting element", exception);
}
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
@@ -1286,73 +644,18 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public Element getElement(final String key) throws SQLException
+ public Element getElement(final String key, final String subkey) throws SikevaDBException
{
Element result;
//
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
+ if (key == null)
{
- //
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection
- .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- //
- if (resultSet.next())
- {
- //
- result = new Element();
-
- result.setId(resultSet.getLong(1));
- result.setKey(resultSet.getString(2));
- result.setSubkey(resultSet.getString(3));
- result.setValue(resultSet.getString(4));
- result.setSize(resultSet.getLong(5));
- result.setDigest(resultSet.getString(6));
- result.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
- result.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
- result.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
-
- }
- else
- {
- result = null;
- }
-
- //
- if (resultSet.next())
- {
- throw new SQLException("More than only once result [key=" + key + "].");
- }
+ throw new IllegalArgumentException("Key is null.");
}
- finally
+ else if (subkey == null)
{
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public Element getElement(final String key, final String subkey) throws SQLException
- {
- Element result;
-
- //
- if (subkey == null)
- {
- result = getElement(key);
+ throw new IllegalArgumentException("Subkey is null.");
}
else
{
@@ -1383,24 +686,30 @@ public class SQLSikevaDB implements SikevaDB
result.setValue(resultSet.getString(4));
result.setSize(resultSet.getLong(5));
result.setDigest(resultSet.getString(6));
- result.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
- result.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
- result.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
+ result.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7)));
+ result.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8)));
+ result.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9)));
+
+ //
+ if (resultSet.next())
+ {
+ throw new SikevaDBException("More than only once result.");
+ }
}
else
{
result = null;
}
- //
- if (resultSet.next())
- {
- throw new SQLException("More than only once result.");
- }
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error getting element.", exception);
+ throw new SikevaDBException("Error getting element", exception);
}
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
@@ -1409,15 +718,15 @@ public class SQLSikevaDB implements SikevaDB
}
/**
- * {@inheritDoc}
+ *
*/
@Override
- public Elements getElements(final String key) throws SQLException
+ public Elements getElements() throws SikevaDBException
{
Elements result;
//
- result = new Elements((int) countOfElements(key));
+ result = new Elements((int) countOfElements());
//
Connection connection = null;
@@ -1428,8 +737,7 @@ public class SQLSikevaDB implements SikevaDB
connection = getConnection();
connection.setAutoCommit(true);
statement = connection
- .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC");
- statement.setString(1, key);
+ .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL ORDER BY CREATION_DATE ASC");
resultSet = statement.executeQuery();
while (resultSet.next())
@@ -1443,17 +751,22 @@ public class SQLSikevaDB implements SikevaDB
element.setValue(resultSet.getString(4));
element.setSize(resultSet.getLong(5));
element.setDigest(resultSet.getString(6));
- element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
- element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
- element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
+ element.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7)));
+ element.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8)));
+ element.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9)));
//
result.add(element);
}
}
+ catch (SQLException exception)
+ {
+ logger.error("Error getting element.", exception);
+ throw new SikevaDBException("Error getting element", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
//
@@ -1464,19 +777,19 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public Elements getElements(final String key, final String subkey) throws SQLException
+ public Elements getElements(final String key) throws SikevaDBException
{
Elements result;
//
- if (subkey == null)
+ if (key == null)
{
- result = getElements(key);
+ throw new IllegalArgumentException("Key is null.");
}
else
{
//
- result = new Elements((int) countOfElements(key, subkey));
+ result = new Elements((int) countOfElements(key));
//
Connection connection = null;
@@ -1487,9 +800,8 @@ public class SQLSikevaDB implements SikevaDB
connection = getConnection();
connection.setAutoCommit(true);
statement = connection
- .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=? ORDER BY CREATION_DATE ASC");
+ .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL ORDER BY CREATION_DATE ASC");
statement.setString(1, key);
- statement.setString(2, subkey);
resultSet = statement.executeQuery();
while (resultSet.next())
@@ -1503,17 +815,22 @@ public class SQLSikevaDB implements SikevaDB
element.setValue(resultSet.getString(4));
element.setSize(resultSet.getLong(5));
element.setDigest(resultSet.getString(6));
- element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
- element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
- element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
+ element.setCreationDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(7)));
+ element.setEditionDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(8)));
+ element.setArchiveDate(SQLSikevaDBTools.toDateTime(resultSet.getTimestamp(9)));
//
result.add(element);
}
}
+ catch (SQLException exception)
+ {
+ logger.error("Error getting element.", exception);
+ throw new SikevaDBException("Error getting element", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
@@ -1525,7 +842,7 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public StringList getKeys() throws SQLException
+ public StringList getKeys() throws SikevaDBException
{
StringList result;
@@ -1545,24 +862,36 @@ public class SQLSikevaDB implements SikevaDB
while (resultSet.next())
{
- //
result.add(resultSet.getString(1));
}
}
+ catch (SQLException exception)
+ {
+ logger.error("Error getting keys.", exception);
+ throw new SikevaDBException("Error getting keys", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
//
return result;
}
+ /**
+ *
+ * @return
+ */
public String getLogin()
{
return this.login;
}
+ /**
+ *
+ * @return
+ */
public String getPassword()
{
return this.password;
@@ -1572,41 +901,57 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public StringList getSubkeys(final String key) throws SQLException
+ public StringList getSubkeys(final String key) throws SikevaDBException
{
StringList result;
//
- result = new StringList();
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
+ if (key == null)
{
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection
- .prepareStatement("SELECT DISTINCT SUBKEY,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- while (resultSet.next())
- {
- //
- result.add(resultSet.getString(1));
- }
+ throw new IllegalArgumentException("Key is null.");
}
- finally
+ else
{
- close(connection, statement, resultSet);
+ //
+ result = new StringList();
+
+ //
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try
+ {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection
+ .prepareStatement("SELECT DISTINCT SUBKEY,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC");
+ statement.setString(1, key);
+ resultSet = statement.executeQuery();
+
+ while (resultSet.next())
+ {
+ result.add(resultSet.getString(1));
+ }
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error getting subkeys.", exception);
+ throw new SikevaDBException("Error getting subkeys", exception);
+ }
+ finally
+ {
+ closeQuietly(connection, statement, resultSet);
+ }
}
//
return result;
}
+ /**
+ *
+ * @return
+ */
public String getUrl()
{
return this.url;
@@ -1616,43 +961,53 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public String getValue(final String key) throws SQLException
+ public String getValue(final String key) throws SikevaDBException
{
String result;
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
+ if (key == null)
{
- //
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- //
- if (resultSet.next())
+ throw new IllegalArgumentException("Key is null.");
+ }
+ else
+ {
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try
{
//
- result = resultSet.getString(1);
- }
- else
- {
- result = null;
- }
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL");
+ statement.setString(1, key);
+ resultSet = statement.executeQuery();
+
+ //
+ if (resultSet.next())
+ {
+ result = resultSet.getString(1);
+
+ if (resultSet.next())
+ {
+ throw new SikevaDBException("More than only once result.");
+ }
+ }
+ else
+ {
+ result = null;
+ }
- //
- if (resultSet.next())
- {
- throw new SQLException("More than only once result.");
}
- }
- finally
- {
- close(connection, statement, resultSet);
+ catch (SQLException exception)
+ {
+ logger.error("Error getting value.", exception);
+ throw new SikevaDBException("Error getting value", exception);
+ }
+ finally
+ {
+ closeQuietly(connection, statement, resultSet);
+ }
}
//
@@ -1663,13 +1018,17 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public String getValue(final String key, final String subkey) throws SQLException
+ public String getValue(final String key, final String subkey) throws SikevaDBException
{
String result;
- if (subkey == null)
+ if (key == null)
{
- result = getValue(key);
+ throw new IllegalArgumentException("Key is null.");
+ }
+ else if (subkey == null)
+ {
+ throw new IllegalArgumentException("Subkey is null.");
}
else
{
@@ -1690,23 +1049,26 @@ public class SQLSikevaDB implements SikevaDB
//
if (resultSet.next())
{
- //
result = resultSet.getString(1);
+
+ if (resultSet.next())
+ {
+ throw new SikevaDBException("More than only once result.");
+ }
}
else
{
result = null;
}
-
- //
- if (resultSet.next())
- {
- throw new SQLException("More than only once result.");
- }
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error getting value.", exception);
+ throw new SikevaDBException("Error getting value", exception);
}
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
@@ -1718,7 +1080,7 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public StringList getValues(final String key) throws SQLException
+ public StringList getValues(final String key) throws SikevaDBException
{
StringList result;
@@ -1739,13 +1101,64 @@ public class SQLSikevaDB implements SikevaDB
while (resultSet.next())
{
- //
result.add(resultSet.getString(1));
}
}
+ catch (SQLException exception)
+ {
+ logger.error("Error getting values.", exception);
+ throw new SikevaDBException("Error getting values", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
+ }
+
+ //
+ return result;
+ }
+
+ /**
+ *
+ */
+ public boolean isArchiveOff()
+ {
+ boolean result;
+
+ result = !this.archiveFlag;
+
+ //
+ return result;
+ }
+
+ /**
+ *
+ */
+ public boolean isArchiveOn()
+ {
+ boolean result;
+
+ result = this.archiveFlag;
+
+ //
+ return result;
+ }
+
+ /**
+ *
+ */
+ @Override
+ public boolean isClosed()
+ {
+ boolean result;
+
+ if (this.status == Status.CLOSED)
+ {
+ result = true;
+ }
+ else
+ {
+ result = false;
}
//
@@ -1756,7 +1169,36 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public long memorySize() throws SQLException
+ public boolean isCreated() throws SikevaDBException
+ {
+ boolean result;
+
+ // TODO
+ result = false;
+
+ //
+ return result;
+ }
+
+ /**
+ *
+ */
+ @Override
+ public boolean isOpened()
+ {
+ boolean result;
+
+ result = !isClosed();
+
+ //
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public long memorySize() throws SikevaDBException
{
long result;
@@ -1768,15 +1210,20 @@ public class SQLSikevaDB implements SikevaDB
{
connection = getConnection();
connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements");
+ statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL");
resultSet = statement.executeQuery();
resultSet.next();
result = resultSet.getLong(1);
}
+ catch (SQLException exception)
+ {
+ logger.error("Error computing memory size.", exception);
+ throw new SikevaDBException("Error computing memory size", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
//
@@ -1787,28 +1234,39 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public long memorySize(final String key) throws SQLException
+ public long memorySize(final String key) throws SikevaDBException
{
long result;
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
+ if (key == null)
{
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- resultSet.next();
- result = resultSet.getLong(1);
+ throw new IllegalArgumentException("Key is null.");
}
- finally
+ else
{
- close(connection, statement, resultSet);
+ Connection connection = null;
+ PreparedStatement statement = null;
+ ResultSet resultSet = null;
+ try
+ {
+ connection = getConnection();
+ connection.setAutoCommit(true);
+ statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=?");
+ statement.setString(1, key);
+ resultSet = statement.executeQuery();
+
+ resultSet.next();
+ result = resultSet.getLong(1);
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error computing key memory size.", exception);
+ throw new SikevaDBException("Error computing key memory size.", exception);
+ }
+ finally
+ {
+ closeQuietly(connection, statement, resultSet);
+ }
}
//
@@ -1819,14 +1277,18 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public long memorySize(final String key, final String subkey) throws SQLException
+ public long memorySize(final String key, final String subkey) throws SikevaDBException
{
long result;
//
- if (subkey == null)
+ if (key == null)
{
- result = memorySize(key);
+ throw new IllegalArgumentException("Key is null.");
+ }
+ else if (subkey == null)
+ {
+ throw new IllegalArgumentException("Subkey is null.");
}
else
{
@@ -1846,9 +1308,14 @@ public class SQLSikevaDB implements SikevaDB
resultSet.next();
result = resultSet.getLong(1);
}
+ catch (SQLException exception)
+ {
+ logger.error("Error computing subkey memory size.", exception);
+ throw new SikevaDBException("Error computing subkey memory size", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
@@ -1856,164 +1323,62 @@ public class SQLSikevaDB implements SikevaDB
return result;
}
- /**
- *
- */
- @Override
- public long memorySizeOfAll() throws SQLException
- {
- long result;
-
- result = memorySize() + memorySizeOfArchive();
-
- //
- return result;
- }
-
- /**
- *
- */
- @Override
- public long memorySizeOfAll(final String key) throws SQLException
- {
- long result;
-
- result = memorySize(key) + memorySizeOfArchive(key);
-
- //
- return result;
- }
-
/**
* {@inheritDoc}
*/
@Override
- public long memorySizeOfArchive() throws SQLException
+ public void open() throws SikevaDBException
{
- long result;
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL");
- resultSet = statement.executeQuery();
-
- resultSet.next();
- result = resultSet.getLong(1);
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long memorySizeOfArchive(final String key) throws SQLException
- {
- long result;
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=?");
- statement.setString(1, key);
- resultSet = statement.executeQuery();
-
- resultSet.next();
- result = resultSet.getLong(1);
- }
- finally
- {
- close(connection, statement, resultSet);
- }
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long memorySizeOfArchive(final String key, final String subkey) throws SQLException
- {
- long result;
-
- //
- if (subkey == null)
- {
- result = memorySize(key);
- }
- else
{
//
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("SELECT SUM(SIZE) FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=?");
- statement.setString(1, key);
- statement.setString(2, subkey);
- resultSet = statement.executeQuery();
+ close();
- resultSet.next();
- result = resultSet.getLong(1);
- }
- finally
+ //
+ if (this.url != null)
{
- close(connection, statement, resultSet);
+ Class.forName(this.driverClassname).newInstance();
+ this.singleConnection = DriverManager.getConnection(this.url, this.login, this.password);
+ logger.info("Single connection opened with [{}].", this.url);
+ this.status = Status.OPENED;
+ }
+ else if (this.contextName != null)
+ {
+ Context initialContext = new InitialContext();
+ Context environmentContext = (Context) initialContext.lookup("java:comp/env");
+ this.dataSource = (DataSource) environmentContext.lookup(this.contextName);
+ logger.info("Database {} opened.", this.contextName);
+ this.status = Status.OPENED;
+ }
+ else
+ {
+ throw new IllegalArgumentException("Undefined source.");
}
}
-
- //
- return result;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void open() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException, NamingException
- {
- //
- close();
-
- //
- if (this.url != null)
+ catch (InstantiationException exception)
{
- Class.forName(this.driverClassname).newInstance();
- this.singleConnection = DriverManager.getConnection(this.url, this.login, this.password);
- logger.info("Single connection opened with [{}].", this.url);
+ logger.error("Error opening database.", exception);
+ throw new SikevaDBException("Error opening database.");
}
- else if (this.contextName != null)
+ catch (IllegalAccessException exception)
{
- Context initialContext = new InitialContext();
- Context environmentContext = (Context) initialContext.lookup("java:comp/env");
- this.dataSource = (DataSource) environmentContext.lookup(this.contextName);
- logger.info("Database {} opened.", this.contextName);
+ logger.error("Error opening database.", exception);
+ throw new SikevaDBException("Error opening database.");
}
- else
+ catch (ClassNotFoundException exception)
{
- throw new IllegalArgumentException("Undefined source.");
+ logger.error("Error opening database.", exception);
+ throw new SikevaDBException("Error opening database.");
+ }
+ catch (SQLException exception)
+ {
+ logger.error("Error opening database.", exception);
+ throw new SikevaDBException("Error opening database.");
+ }
+ catch (NamingException exception)
+ {
+ logger.error("Error opening database.", exception);
+ throw new SikevaDBException("Error opening database.");
}
}
@@ -2021,7 +1386,7 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public void put(final Element element) throws SQLException
+ public void put(final Element element) throws SikevaDBException
{
if (element == null)
{
@@ -2049,16 +1414,21 @@ public class SQLSikevaDB implements SikevaDB
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
- statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
- statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
- statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
+ statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate()));
+ statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
+ statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate()));
statement.setLong(9, element.getId());
statement.executeUpdate();
}
+ catch (SQLException exception)
+ {
+ logger.error("Error putting element.", exception);
+ throw new SikevaDBException("Error putting element", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
else if (element.getId() == Element.NO_ID)
@@ -2078,15 +1448,20 @@ public class SQLSikevaDB implements SikevaDB
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
- statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
- statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
- statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
+ statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate()));
+ statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
+ statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
}
+ catch (SQLException exception)
+ {
+ logger.error("Error putting element.", exception);
+ throw new SikevaDBException("Error putting element", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
else
@@ -2108,15 +1483,20 @@ public class SQLSikevaDB implements SikevaDB
statement.setString(4, element.getValue());
statement.setLong(5, element.getSize());
statement.setString(6, element.getDigest());
- statement.setTimestamp(7, toTimestamp(element.getCreationDate()));
- statement.setTimestamp(8, toTimestamp(element.getEditionDate()));
- statement.setTimestamp(9, toTimestamp(element.getArchiveDate()));
+ statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getCreationDate()));
+ statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
+ statement.setTimestamp(9, SQLSikevaDBTools.toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
}
+ catch (SQLException exception)
+ {
+ logger.error("Error putting element.", exception);
+ throw new SikevaDBException("Error putting element", exception);
+ }
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
}
@@ -2126,7 +1506,7 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public void put(final String key, final String value) throws SQLException
+ public void put(final String key, final String value) throws SikevaDBException
{
//
Element element = getElement(key);
@@ -2159,7 +1539,7 @@ public class SQLSikevaDB implements SikevaDB
{
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.getEditionDate()));
+ statement.setTimestamp(1, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
statement.setString(2, element.getKey());
statement.executeUpdate();
@@ -2175,126 +1555,30 @@ public class SQLSikevaDB implements SikevaDB
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
- statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
- statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
- statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
+ statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate()));
+ statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
+ statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
connection.commit();
}
- catch (Exception exception)
+ catch (SQLException exception)
{
- connection.rollback();
- }
- finally
- {
- close(connection, statement, resultSet);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void put(final String key, final String subkey, final String value) throws SQLException
- {
- //
- 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);
- }
-
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
- {
- //
- connection = getConnection();
- connection.setAutoCommit(false);
-
- // Archive existing element.
- if (element.getId() != Element.NO_ID)
+ logger.error("Error putting element.", exception);
+ try
{
- statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL");
-
- statement.setTimestamp(1, toTimestamp(element.getEditionDate()));
- statement.setString(2, element.getKey());
- statement.setString(3, element.getSubkey());
-
- statement.executeUpdate();
- statement.clearParameters();
- statement.close();
+ connection.rollback();
}
-
- // Insert new version of the element.
- statement = connection.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
-
- statement.setString(1, element.getKey());
- statement.setString(2, element.getSubkey());
- statement.setString(3, element.getValue());
- statement.setLong(4, element.getSize());
- statement.setString(5, element.getDigest());
- statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
- statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
- statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
-
- statement.executeUpdate();
-
- connection.commit();
- }
- catch (Exception exception)
- {
- connection.rollback();
- }
- finally
- {
- close(connection, statement, resultSet);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void remove(final String key) 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 TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL");
-
- statement.setString(1, key);
-
- int rowCount = statement.executeUpdate();
-
- if (rowCount == 0)
+ catch (SQLException exception1)
{
- logger.warn("Remove action without existing target [key=" + key + "]");
+ logger.error("Rollback failed.", exception);
}
+ throw new SikevaDBException("Error putting element", exception);
}
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
@@ -2302,51 +1586,34 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public void remove(final String key, final String subkey) throws SQLException
+ public void put(final String key, final String subkey, final String value) throws SikevaDBException
{
- //
- Connection connection = null;
- PreparedStatement statement = null;
- ResultSet resultSet = null;
- try
+ if (key == null)
{
- //
- 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 + "]");
- }
+ throw new IllegalArgumentException("Key is null.");
}
- finally
+ else if (subkey == null)
{
- close(connection, statement, resultSet);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void removeAll(final String key) throws SQLException
- {
- //
- Element element = getElement(key);
-
- if (element == null)
- {
- throw new NullPointerException("Undefined element [key=" + key + "]");
+ throw new IllegalArgumentException("Subkey is null.");
}
else
{
//
- element.archive();
+ 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);
+ }
//
Connection connection = null;
@@ -2356,16 +1623,54 @@ public class SQLSikevaDB implements SikevaDB
{
//
connection = getConnection();
- connection.setAutoCommit(true);
- statement = connection.prepareStatement("DELETE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY IS NOT NULL");
+ connection.setAutoCommit(false);
+
+ // Archive existing element.
+ if (element.getId() != Element.NO_ID)
+ {
+ statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL");
+
+ statement.setTimestamp(1, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
+ statement.setString(2, element.getKey());
+ statement.setString(3, element.getSubkey());
+
+ statement.executeUpdate();
+ statement.clearParameters();
+ statement.close();
+ }
+
+ // Insert new version of the element.
+ statement = connection.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, element.getKey());
+ statement.setString(2, element.getSubkey());
+ statement.setString(3, element.getValue());
+ statement.setLong(4, element.getSize());
+ statement.setString(5, element.getDigest());
+ statement.setTimestamp(6, SQLSikevaDBTools.toTimestamp(element.getCreationDate()));
+ statement.setTimestamp(7, SQLSikevaDBTools.toTimestamp(element.getEditionDate()));
+ statement.setTimestamp(8, SQLSikevaDBTools.toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
+
+ connection.commit();
+ }
+ catch (Exception exception)
+ {
+ logger.error("Error putting element.", exception);
+ try
+ {
+ connection.rollback();
+ }
+ catch (SQLException exception1)
+ {
+ logger.error("Rollback failed.", exception);
+ }
+ throw new SikevaDBException("Error putting element", exception);
}
finally
{
- close(connection, statement, resultSet);
+ closeQuietly(connection, statement, resultSet);
}
}
}
@@ -2374,10 +1679,113 @@ public class SQLSikevaDB implements SikevaDB
* {@inheritDoc}
*/
@Override
- public void removeMany(final String key, final String... subkeys) throws SQLException
+ public void remove(final String key) throws SikevaDBException
{
- //
- if ((subkeys != null) && (subkeys.length > 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();
}
}