Add methods. Add tests.

This commit is contained in:
Christian P. MOMON 2013-09-02 20:48:46 +02:00
parent 99377b9f8f
commit 6a2756c79a
7 changed files with 221 additions and 3 deletions

View file

@ -255,6 +255,24 @@ public class FileSikevaDB implements SikevaDB {
return 0; return 0;
} }
@Override
public long memorySizeOfArchive() throws Exception {
// TODO Auto-generated method stub
return 0;
}
@Override
public long memorySizeOfArchive(final String key) throws Exception {
// TODO Auto-generated method stub
return 0;
}
@Override
public long memorySizeOfArchive(final String key, final String subkey) throws Exception {
// TODO Auto-generated method stub
return 0;
}
@Override @Override
public void open() { public void open() {
// TODO Auto-generated method stub // TODO Auto-generated method stub

View file

@ -1391,7 +1391,7 @@ public class SQLSikevaDB implements SikevaDB {
try { try {
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements WHERE TOPKEY=?"); statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL");
statement.setString(1, key); statement.setString(1, key);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
@ -1424,7 +1424,99 @@ public class SQLSikevaDB implements SikevaDB {
try { try {
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements WHERE TOPKEY=? AND SUBKEY=?"); statement = connection.prepareStatement("SELECT SUM(SIZE) FROM elements WHERE ARCHIVE_DATE IS 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}
*/
@Override
public long memorySizeOfArchive() 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 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 elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY IS NULL");
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 Exception {
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 elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=?");
statement.setString(1, key); statement.setString(1, key);
statement.setString(2, subkey); statement.setString(2, subkey);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();

View file

@ -102,6 +102,12 @@ public interface SikevaDB {
public long memorySize(String key, String subkey) throws Exception; public long memorySize(String key, String subkey) throws Exception;
public long memorySizeOfArchive() throws Exception;
long memorySizeOfArchive(String key) throws Exception;
long memorySizeOfArchive(String key, String subkey) throws Exception;
public void open() throws Exception; public void open() throws Exception;
public void put(Element element) throws Exception; public void put(Element element) throws Exception;

View file

@ -16,7 +16,6 @@ import org.junit.Test;
import fr.devinsy.util.StringList; import fr.devinsy.util.StringList;
/** /**
* ?profileSQL=true
* *
* @author Christian P. Momon * @author Christian P. Momon
*/ */
@ -47,6 +46,7 @@ public class SQLSikevaDBTest {
BasicConfigurator.configure(); BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.ERROR); Logger.getRootLogger().setLevel(Level.ERROR);
// Add ?profileSQL=true to generate huge logs.
this.database = new SQLSikevaDB("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/sikevadb-test", "sikevadb-test", "12345678"); this.database = new SQLSikevaDB("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/sikevadb-test", "sikevadb-test", "12345678");
this.database.open(); this.database.open();
} }
@ -123,6 +123,108 @@ public class SQLSikevaDBTest {
logger.debug("===== test done."); logger.debug("===== test done.");
} }
/**
* @throws Exception
*
*/
@Test
public void testGeKeyse01() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
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("alpha01", "bravo1", "1234567890");
database.put("alpha01", "bravo1", "qlskjfmlqj");
database.put("alpha01", "bravo2", "qlskjfmlqj");
database.put("alpha01", "bravo3", "qlskjfmlqj");
database.put("alpha01", "bravo4", "qlskjfmlqj");
database.put("alpha01", "bravo5", "qlskjfmlqj");
Assert.assertEquals(0, database.getSubkeys("none").size());
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testGeKeyse02() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
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("alpha01", "bravo1", "1234567890");
database.put("alpha01", "bravo1", "qlskjfmlqj");
database.put("alpha01", "bravo2", "qlskjfmlqj");
database.put("alpha01", "bravo3", "qlskjfmlqj");
database.put("alpha01", "bravo4", "qlskjfmlqj");
database.put("alpha01", "bravo5", "qlskjfmlqj");
StringList keys = database.getKeys();
Assert.assertEquals(5, keys.size());
Assert.assertTrue(keys.contains("alpha03"));
Assert.assertEquals(1, database.getSubkeys("alpha03").size());
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testMemorySize01() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
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("alpha01", "bravo1", "1234567890");
database.put("alpha01", "bravo1", "qlskjfmlqj");
database.put("alpha01", "bravo2", "qlskjfmlqj");
database.put("alpha01", "bravo3", "qlskjfmlqj");
database.put("alpha01", "bravo4", "qlskjfmlqj");
database.put("alpha01", "bravo5", "qlskjfmlqj");
Assert.assertEquals(120, database.memorySize());
Assert.assertEquals(10, database.memorySize("alpha01"));
Assert.assertEquals(10, database.memorySize("alpha01", "bravo1"));
database.clearDatabase();
Assert.assertEquals(0, database.memorySize());
//
logger.debug("===== test done.");
}
/** /**
* @throws Exception * @throws Exception
* *