From 27a3ac09a49f3cb5b3263bc5bb687eb78b38efc3 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sat, 22 Apr 2017 01:29:37 +0200 Subject: [PATCH] Added Archiver. --- src/fr/devinsy/sikevadb/core/Archiver.java | 67 ++------ src/fr/devinsy/sikevadb/core/SikevaDB.java | 6 + .../sikevadb/filetree/ArchiveManager.java | 48 ------ .../sikevadb/filetree/FileTreeArchiver.java | 152 ++++++++++++++++++ .../sikevadb/filetree/FileTreeSikevaDB.java | 53 +++++- src/fr/devinsy/sikevadb/sql/SQLArchiver.java | 127 +++++++++++++++ src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java | 56 +++++-- 7 files changed, 386 insertions(+), 123 deletions(-) delete mode 100644 src/fr/devinsy/sikevadb/filetree/ArchiveManager.java create mode 100644 src/fr/devinsy/sikevadb/filetree/FileTreeArchiver.java create mode 100644 src/fr/devinsy/sikevadb/sql/SQLArchiver.java diff --git a/src/fr/devinsy/sikevadb/core/Archiver.java b/src/fr/devinsy/sikevadb/core/Archiver.java index 5c86c07..5e09052 100644 --- a/src/fr/devinsy/sikevadb/core/Archiver.java +++ b/src/fr/devinsy/sikevadb/core/Archiver.java @@ -18,72 +18,27 @@ */ package fr.devinsy.sikevadb.core; -import fr.devinsy.util.strings.StringList; - /** - * * * @author Christian Pierre MOMON */ public interface Archiver { - public void clear() throws SikevaDBException; + public enum Status + { + ACTIVATED, + SUSPENDED + } - public void close() throws SikevaDBException; + void activate(); - public long countOfElements() throws SikevaDBException; + void archive(Element element) throws SikevaDBException; - public long countOfElements(String key) throws SikevaDBException; + void clear() throws SikevaDBException; - public long countOfElements(String key, String subkey) throws SikevaDBException; + boolean isActivated(); - public Element getElement(String key) throws SikevaDBException; + boolean isSuspended(); - public Element getElement(String key, String subkey) throws SikevaDBException; - - public Elements getElements() throws SikevaDBException; - - public Elements getElements(String key) throws SikevaDBException; - - public StringList getKeys() throws SikevaDBException; - - public StringList getSubkeys(String key) throws SikevaDBException; - - public String getValue(String key) throws SikevaDBException; - - public String getValue(String key, String subkey) throws SikevaDBException; - - public StringList getValues(String key) throws SikevaDBException; - - public boolean isClosed(); - - public boolean isOpened(); - - public long memorySize() throws SikevaDBException; - - public long memorySize(String key) throws SikevaDBException; - - public long memorySize(String key, String subkey) throws SikevaDBException; - - void open() throws SikevaDBException; - - void put(Element element) throws SikevaDBException; - - void put(String key, String value) throws SikevaDBException; - - void put(String key, String subkey, String value) throws SikevaDBException; - - void remove(final String key) throws SikevaDBException; - - void remove(final String key, final String subkey) throws SikevaDBException; - - void removeMany(final String key, final String... subkeys) throws SikevaDBException; - - void renameKey(final String oldKey, final String newKey) throws SikevaDBException; - - void renameSubKey(final String key, final String oldSubKey, final String newSubKey) throws SikevaDBException; - - void replaceInValue(final String key, final String... tokens) throws SikevaDBException; - - void replaceInValues(final String key, final String... tokens) throws SikevaDBException; + void suspend(); } diff --git a/src/fr/devinsy/sikevadb/core/SikevaDB.java b/src/fr/devinsy/sikevadb/core/SikevaDB.java index 9e0ece9..add98c6 100644 --- a/src/fr/devinsy/sikevadb/core/SikevaDB.java +++ b/src/fr/devinsy/sikevadb/core/SikevaDB.java @@ -27,6 +27,8 @@ import fr.devinsy.util.strings.StringList; */ public interface SikevaDB { + public Archiver archiver() throws SikevaDBException; + public void clear() throws SikevaDBException; public void close() throws SikevaDBException; @@ -57,6 +59,10 @@ public interface SikevaDB public StringList getValues(String key) throws SikevaDBException; + boolean isArchiveActivated(); + + boolean isArchiveSuspended(); + public boolean isClosed(); public boolean isCreated() throws SikevaDBException; diff --git a/src/fr/devinsy/sikevadb/filetree/ArchiveManager.java b/src/fr/devinsy/sikevadb/filetree/ArchiveManager.java deleted file mode 100644 index b3a155b..0000000 --- a/src/fr/devinsy/sikevadb/filetree/ArchiveManager.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright (C) 2013-2016 Christian Pierre MOMON - * - * This file is part of SikevaDB, simple key value database. - * - * SikevaDB is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * SikevaDB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with SikevaDB. If not, see . - */ -package fr.devinsy.sikevadb.filetree; - -import fr.devinsy.sikevadb.core.SikevaDB; - -/** - * - * - * @author Christian Pierre MOMON - */ -public class ArchiveManager -{ - private SikevaDB database; - - /** - * - * @param source - */ - public ArchiveManager(final SikevaDB source) - { - if (this.database == null) - { - throw new IllegalArgumentException("Null parameter."); - } - else - { - this.database = source; - } - } - -} diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeArchiver.java b/src/fr/devinsy/sikevadb/filetree/FileTreeArchiver.java new file mode 100644 index 0000000..8bc9058 --- /dev/null +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeArchiver.java @@ -0,0 +1,152 @@ +/** + * 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 fr.devinsy.sikevadb.core.Archiver; +import fr.devinsy.sikevadb.core.Element; +import fr.devinsy.sikevadb.core.SikevaDB; +import fr.devinsy.sikevadb.core.SikevaDBException; + +/** + * + * + * @author Christian Pierre MOMON + */ +public class FileTreeArchiver implements Archiver +{ + private static final String ARCHIVE_PATH = "archive"; + + private Status status; + private File archiveDirectory; + private SikevaDB database; + + /** + * + * @param source + * @throws SikevaDBException + */ + public FileTreeArchiver(final FileTreeSikevaDB source) throws SikevaDBException + { + if (source == null) + { + throw new IllegalArgumentException("Null parameter."); + } + else + { + this.status = Status.ACTIVATED; + this.archiveDirectory = new File(source.getFileTreeDirectory(), ARCHIVE_PATH); + if (this.archiveDirectory.exists()) + { + if (!this.archiveDirectory.isDirectory()) + { + throw new SikevaDBException("Archive directory unavailable."); + } + } + else + { + this.archiveDirectory.mkdir(); + } + this.database = source; + } + } + + /** + * + */ + @Override + public void activate() + { + this.status = Status.ACTIVATED; + } + + /** + * + */ + @Override + public void archive(final Element element) throws SikevaDBException + { + // TODO Auto-generated method stub + + } + + /** + * + */ + @Override + public void clear() throws SikevaDBException + { + // TODO Auto-generated method stub + + } + + /** + * + * @return + */ + public File getPath() + { + return this.archiveDirectory; + } + + /** + * + */ + @Override + public boolean isActivated() + { + boolean result; + + if (this.status == Status.ACTIVATED) + { + result = true; + } + else + { + result = false; + } + + // + return result; + } + + /** + * + */ + @Override + public boolean isSuspended() + { + boolean result; + + result = !isActivated(); + + // + return result; + } + + /** + * + */ + @Override + public void suspend() + { + this.status = Status.SUSPENDED; + } +} diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java index 9f7388d..96000cf 100644 --- a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java @@ -27,9 +27,9 @@ import org.joda.time.format.ISODateTimeFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.sikevadb.core.Archiver; import fr.devinsy.sikevadb.core.Element; import fr.devinsy.sikevadb.core.Elements; -import fr.devinsy.sikevadb.core.Archiver; import fr.devinsy.sikevadb.core.SikevaDB; import fr.devinsy.sikevadb.core.SikevaDBException; import fr.devinsy.util.strings.StringList; @@ -65,7 +65,7 @@ public class FileTreeSikevaDB implements SikevaDB private String password; private File fileTreeDirectory; private File dataDirectory; - private Archiver journalizer; + private Archiver archiver; private File configDirectory; /** @@ -80,7 +80,16 @@ public class FileTreeSikevaDB implements SikevaDB this.configDirectory = new File(this.fileTreeDirectory, "config"); this.login = login; this.password = password; - this.journalizer = null; + this.archiver = null; + } + + /** + * + */ + @Override + public Archiver archiver() throws SikevaDBException + { + return this.archiver; } /** @@ -109,7 +118,7 @@ public class FileTreeSikevaDB implements SikevaDB @Override public void close() { - this.journalizer = null; + this.archiver = null; this.status = Status.CLOSED; } @@ -590,6 +599,41 @@ public class FileTreeSikevaDB implements SikevaDB return result; } + /** + * + */ + @Override + public boolean isArchiveActivated() + { + boolean result; + + if (this.status == Status.OPENED) + { + result = this.archiver.isActivated(); + } + else + { + result = false; + } + + // + return result; + } + + /** + * + */ + @Override + public boolean isArchiveSuspended() + { + boolean result; + + result = !isArchiveActivated(); + + // + return result; + } + /** * * @return @@ -780,6 +824,7 @@ public class FileTreeSikevaDB implements SikevaDB // this.status = Status.OPENED; + this.archiver = new FileTreeArchiver(this); } /** diff --git a/src/fr/devinsy/sikevadb/sql/SQLArchiver.java b/src/fr/devinsy/sikevadb/sql/SQLArchiver.java new file mode 100644 index 0000000..8dee828 --- /dev/null +++ b/src/fr/devinsy/sikevadb/sql/SQLArchiver.java @@ -0,0 +1,127 @@ +/** + * 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.sql; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import fr.devinsy.sikevadb.core.Archiver; +import fr.devinsy.sikevadb.core.Element; +import fr.devinsy.sikevadb.core.SikevaDBException; + +/** + * + * + * @author Christian Pierre MOMON + */ +public class SQLArchiver implements Archiver +{ + private static final Logger logger = LoggerFactory.getLogger(SQLArchiver.class);; + + private Status status; + private SQLSikevaDB database; + + /** + * + * @param contextName + */ + public SQLArchiver(final SQLSikevaDB source) + { + if (source == null) + { + throw new IllegalArgumentException("Null parameter."); + } + else + { + this.status = Status.ACTIVATED; + this.database = source; + } + } + + /** + * + */ + @Override + public void activate() + { + this.status = Status.ACTIVATED; + } + + /** + * + */ + @Override + public void archive(final Element element) throws SikevaDBException + { + // TODO + } + + /** + * + */ + @Override + public void clear() throws SikevaDBException + { + // TODO + } + + /** + * + */ + @Override + public boolean isActivated() + { + boolean result; + + if (this.status == Status.ACTIVATED) + { + result = true; + } + else + { + result = false; + } + + // + return result; + } + + /** + * + */ + @Override + public boolean isSuspended() + { + boolean result; + + result = !isActivated(); + + // + return result; + } + + /** + * + */ + @Override + public void suspend() + { + this.status = Status.SUSPENDED; + } +} diff --git a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java index 6351593..192de8d 100644 --- a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java @@ -36,6 +36,7 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import fr.devinsy.sikevadb.core.Archiver; import fr.devinsy.sikevadb.core.Element; import fr.devinsy.sikevadb.core.Elements; import fr.devinsy.sikevadb.core.SikevaDB; @@ -58,7 +59,6 @@ public class SQLSikevaDB implements SikevaDB 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,6 +66,7 @@ public class SQLSikevaDB implements SikevaDB private Connection singleConnection; private String contextName; private DataSource dataSource; + private Archiver archiver; /** * @@ -75,7 +76,7 @@ public class SQLSikevaDB implements SikevaDB { this.status = Status.CLOSED; this.contextName = contextName; - this.archiveFlag = true; + this.archiver = null; } /** @@ -112,9 +113,31 @@ public class SQLSikevaDB implements SikevaDB this.login = login; this.password = password; this.dataSource = null; + this.archiver = null; } } + /** + * + */ + public void activateArchiver() + { + this.archiver.activate(); + } + + /** + * + */ + @Override + public Archiver archiver() throws SikevaDBException + { + Archiver result; + + result = this.archiver; + + return result; + } + /** * This methods clear all data in current opened database. * @@ -356,7 +379,7 @@ public class SQLSikevaDB implements SikevaDB @Override public void create() throws SikevaDBException { - if (this.status == Status.OPENED) + if (this.status == Status.CLOSED) { throw new SikevaDBException("Invalid state."); } @@ -1121,11 +1144,19 @@ public class SQLSikevaDB implements SikevaDB /** * */ - public boolean isArchiveOff() + @Override + public boolean isArchiveActivated() { boolean result; - result = !this.archiveFlag; + if (this.status == Status.OPENED) + { + result = this.archiver.isActivated(); + } + else + { + result = false; + } // return result; @@ -1134,11 +1165,12 @@ public class SQLSikevaDB implements SikevaDB /** * */ - public boolean isArchiveOn() + @Override + public boolean isArchiveSuspended() { boolean result; - result = this.archiveFlag; + result = !isArchiveActivated(); // return result; @@ -1341,6 +1373,7 @@ public class SQLSikevaDB implements SikevaDB this.singleConnection = DriverManager.getConnection(this.url, this.login, this.password); logger.info("Single connection opened with [{}].", this.url); this.status = Status.OPENED; + this.archiver = new SQLArchiver(this); } else if (this.contextName != null) { @@ -1349,6 +1382,7 @@ public class SQLSikevaDB implements SikevaDB this.dataSource = (DataSource) environmentContext.lookup(this.contextName); logger.info("Database {} opened.", this.contextName); this.status = Status.OPENED; + this.archiver = new SQLArchiver(this); } else { @@ -1953,14 +1987,6 @@ public class SQLSikevaDB implements SikevaDB logger.info("replaceInValues done."); } - /** - * - */ - public void setArchiveFlag(final boolean value) - { - this.archiveFlag = value; - } - /** * * @param driverClassname