Added Archiver.

This commit is contained in:
Christian P. MOMON 2017-04-22 01:29:37 +02:00
parent d0f0f68ff7
commit 27a3ac09a4
7 changed files with 386 additions and 123 deletions

View file

@ -18,72 +18,27 @@
*/ */
package fr.devinsy.sikevadb.core; package fr.devinsy.sikevadb.core;
import fr.devinsy.util.strings.StringList;
/** /**
*
* *
* @author Christian Pierre MOMON * @author Christian Pierre MOMON
*/ */
public interface Archiver 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; void suspend();
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;
} }

View file

@ -27,6 +27,8 @@ import fr.devinsy.util.strings.StringList;
*/ */
public interface SikevaDB public interface SikevaDB
{ {
public Archiver archiver() throws SikevaDBException;
public void clear() throws SikevaDBException; public void clear() throws SikevaDBException;
public void close() throws SikevaDBException; public void close() throws SikevaDBException;
@ -57,6 +59,10 @@ public interface SikevaDB
public StringList getValues(String key) throws SikevaDBException; public StringList getValues(String key) throws SikevaDBException;
boolean isArchiveActivated();
boolean isArchiveSuspended();
public boolean isClosed(); public boolean isClosed();
public boolean isCreated() throws SikevaDBException; public boolean isCreated() throws SikevaDBException;

View file

@ -1,48 +0,0 @@
/**
* Copyright (C) 2013-2016 Christian Pierre MOMON <christian.momon@devinsy.fr>
*
* This file is part of SikevaDB, simple key value database.
*
* SikevaDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* SikevaDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SikevaDB. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.sikevadb.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;
}
}
}

View file

@ -0,0 +1,152 @@
/**
* Copyright (C) 2013-2017 Christian Pierre MOMON <christian.momon@devinsy.fr>
*
* This file is part of SikevaDB, simple key value database.
*
* SikevaDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* SikevaDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SikevaDB. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.sikevadb.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;
}
}

View file

@ -27,9 +27,9 @@ import org.joda.time.format.ISODateTimeFormat;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.sikevadb.core.Archiver;
import fr.devinsy.sikevadb.core.Element; import fr.devinsy.sikevadb.core.Element;
import fr.devinsy.sikevadb.core.Elements; import fr.devinsy.sikevadb.core.Elements;
import fr.devinsy.sikevadb.core.Archiver;
import fr.devinsy.sikevadb.core.SikevaDB; import fr.devinsy.sikevadb.core.SikevaDB;
import fr.devinsy.sikevadb.core.SikevaDBException; import fr.devinsy.sikevadb.core.SikevaDBException;
import fr.devinsy.util.strings.StringList; import fr.devinsy.util.strings.StringList;
@ -65,7 +65,7 @@ public class FileTreeSikevaDB implements SikevaDB
private String password; private String password;
private File fileTreeDirectory; private File fileTreeDirectory;
private File dataDirectory; private File dataDirectory;
private Archiver journalizer; private Archiver archiver;
private File configDirectory; private File configDirectory;
/** /**
@ -80,7 +80,16 @@ public class FileTreeSikevaDB implements SikevaDB
this.configDirectory = new File(this.fileTreeDirectory, "config"); this.configDirectory = new File(this.fileTreeDirectory, "config");
this.login = login; this.login = login;
this.password = password; 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 @Override
public void close() public void close()
{ {
this.journalizer = null; this.archiver = null;
this.status = Status.CLOSED; this.status = Status.CLOSED;
} }
@ -590,6 +599,41 @@ public class FileTreeSikevaDB implements SikevaDB
return result; 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 * @return
@ -780,6 +824,7 @@ public class FileTreeSikevaDB implements SikevaDB
// //
this.status = Status.OPENED; this.status = Status.OPENED;
this.archiver = new FileTreeArchiver(this);
} }
/** /**

View file

@ -0,0 +1,127 @@
/**
* Copyright (C) 2013-2017 Christian Pierre MOMON <christian.momon@devinsy.fr>
*
* This file is part of SikevaDB, simple key value database.
*
* SikevaDB is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* SikevaDB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with SikevaDB. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.devinsy.sikevadb.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;
}
}

View file

@ -36,6 +36,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import fr.devinsy.sikevadb.core.Archiver;
import fr.devinsy.sikevadb.core.Element; import fr.devinsy.sikevadb.core.Element;
import fr.devinsy.sikevadb.core.Elements; import fr.devinsy.sikevadb.core.Elements;
import fr.devinsy.sikevadb.core.SikevaDB; 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 static final Logger logger = LoggerFactory.getLogger(SQLSikevaDB.class);;
private Status status; private Status status;
private boolean archiveFlag;
private String driverClassname; private String driverClassname;
private String url; private String url;
private String login; private String login;
@ -66,6 +66,7 @@ public class SQLSikevaDB implements SikevaDB
private Connection singleConnection; private Connection singleConnection;
private String contextName; private String contextName;
private DataSource dataSource; private DataSource dataSource;
private Archiver archiver;
/** /**
* *
@ -75,7 +76,7 @@ public class SQLSikevaDB implements SikevaDB
{ {
this.status = Status.CLOSED; this.status = Status.CLOSED;
this.contextName = contextName; this.contextName = contextName;
this.archiveFlag = true; this.archiver = null;
} }
/** /**
@ -112,9 +113,31 @@ public class SQLSikevaDB implements SikevaDB
this.login = login; this.login = login;
this.password = password; this.password = password;
this.dataSource = null; 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. * This methods clear all data in current opened database.
* *
@ -356,7 +379,7 @@ public class SQLSikevaDB implements SikevaDB
@Override @Override
public void create() throws SikevaDBException public void create() throws SikevaDBException
{ {
if (this.status == Status.OPENED) if (this.status == Status.CLOSED)
{ {
throw new SikevaDBException("Invalid state."); throw new SikevaDBException("Invalid state.");
} }
@ -1121,11 +1144,19 @@ public class SQLSikevaDB implements SikevaDB
/** /**
* *
*/ */
public boolean isArchiveOff() @Override
public boolean isArchiveActivated()
{ {
boolean result; boolean result;
result = !this.archiveFlag; if (this.status == Status.OPENED)
{
result = this.archiver.isActivated();
}
else
{
result = false;
}
// //
return result; return result;
@ -1134,11 +1165,12 @@ public class SQLSikevaDB implements SikevaDB
/** /**
* *
*/ */
public boolean isArchiveOn() @Override
public boolean isArchiveSuspended()
{ {
boolean result; boolean result;
result = this.archiveFlag; result = !isArchiveActivated();
// //
return result; return result;
@ -1341,6 +1373,7 @@ public class SQLSikevaDB implements SikevaDB
this.singleConnection = DriverManager.getConnection(this.url, this.login, this.password); this.singleConnection = DriverManager.getConnection(this.url, this.login, this.password);
logger.info("Single connection opened with [{}].", this.url); logger.info("Single connection opened with [{}].", this.url);
this.status = Status.OPENED; this.status = Status.OPENED;
this.archiver = new SQLArchiver(this);
} }
else if (this.contextName != null) else if (this.contextName != null)
{ {
@ -1349,6 +1382,7 @@ public class SQLSikevaDB implements SikevaDB
this.dataSource = (DataSource) environmentContext.lookup(this.contextName); this.dataSource = (DataSource) environmentContext.lookup(this.contextName);
logger.info("Database {} opened.", this.contextName); logger.info("Database {} opened.", this.contextName);
this.status = Status.OPENED; this.status = Status.OPENED;
this.archiver = new SQLArchiver(this);
} }
else else
{ {
@ -1953,14 +1987,6 @@ public class SQLSikevaDB implements SikevaDB
logger.info("replaceInValues done."); logger.info("replaceInValues done.");
} }
/**
*
*/
public void setArchiveFlag(final boolean value)
{
this.archiveFlag = value;
}
/** /**
* *
* @param driverClassname * @param driverClassname