From 6a2756c79a213377e677ba8c7a5dbea66811a496 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 2 Sep 2013 20:48:46 +0200 Subject: [PATCH] Add methods. Add tests. --- bin/fr/devinsy/sikevadb/FileSikevaDB.class | Bin 4870 -> 5126 bytes bin/fr/devinsy/sikevadb/SQLSikevaDB.class | Bin 27978 -> 29405 bytes bin/fr/devinsy/sikevadb/SikevaDB.class | Bin 2047 -> 2123 bytes src/fr/devinsy/sikevadb/FileSikevaDB.java | 18 +++ src/fr/devinsy/sikevadb/SQLSikevaDB.java | 96 +++++++++++++++- src/fr/devinsy/sikevadb/SikevaDB.java | 6 + test/fr/devinsy/sikevadb/SQLSikevaDBTest.java | 104 +++++++++++++++++- 7 files changed, 221 insertions(+), 3 deletions(-) diff --git a/bin/fr/devinsy/sikevadb/FileSikevaDB.class b/bin/fr/devinsy/sikevadb/FileSikevaDB.class index 1660bde9a2c0fd5b16b9a2c03917620279287a6d..16c52f9b6bc16b3dd501b5fd504069c587ee425b 100644 GIT binary patch delta 268 zcmZouYts-o^>5cc1_lNr25)u-CPoH@$%d>V8&&?Y2n*+?=H?ew24_~K`lmS-C1+%o zO_pPQ#AvXYk?koLuPOr*g9uO~3j-sA=;S~SIVMKd$%P!E{PqlLC{o=ZDelP+dF7ey z8Pq2;atlws#v#tcC^$KgN0Zfofro){@_lw$CPs5cc1_lNr2Ct1=;;fU+S?@FIZWd>I$~9S=LqUp>je!p+%frA3BpDg_ z83e$z1B2saL18UMp2-h{#d)0=oPlzT3=Rwu42+WxvdJoRGQY6SWG^9!$qR(pm_hs)Aiq~giB%G$Z!)*A0izbs0(o{1 JpruR7cK}raP(?Hll!jMeDkPVXHp-4?d zJ;%CG=%*D|aE)ORB9H|NVnxu1vXvc5tt_G}LZJu>_P!w}=<$#De(x^tF28rb``VAu z$uneYzq5H8fPOwcO}257G*wTDjMt|eae8HV*I*lnp0m>uoU_v32zi~xxe@kALP({% zvbyFEd7g#t@ueebif4Ns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#H7j|BWWL|}Iu0|%;Ae(EE!}XZL4Vb}=n9mQPlpn@Y{u!3@8dUPnh5j1+l$&&R ztzR;}QB_WVlLwoi=T_&>`UR}~I2`;0ChumHzFy+QNNqJHnr-F{Yx+KrF#c3UA@1%be zrMF6kp`quJQ3XB&3x6mLokbXbglIk=9B}ax1H{l**eb|!x3NVHTP+L@y+3?X6P vz+si4ez!j<@1LFM`eA`VDR4y!T$KV}NP(~ZzW`m=+x^Klx*_>O=t@SmUS)W2Q(7#J9g7)&>EX|qiBW?9atv-va2e5Pm~21W*UpcD&`=3rrD&|}cY e5aGfQF+dYBWMIP(;hOxPT@Iwx7-&2bg9!l34i5hS diff --git a/src/fr/devinsy/sikevadb/FileSikevaDB.java b/src/fr/devinsy/sikevadb/FileSikevaDB.java index 7b9be1d..79e2701 100644 --- a/src/fr/devinsy/sikevadb/FileSikevaDB.java +++ b/src/fr/devinsy/sikevadb/FileSikevaDB.java @@ -255,6 +255,24 @@ public class FileSikevaDB implements SikevaDB { return 0; } + @Override + public long memorySizeOfArchive() throws Exception { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long memorySizeOfArchive(final String key) throws Exception { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long memorySizeOfArchive(final String key, final String subkey) throws Exception { + // TODO Auto-generated method stub + return 0; + } + @Override public void open() { // TODO Auto-generated method stub diff --git a/src/fr/devinsy/sikevadb/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/SQLSikevaDB.java index bfe90c2..9e4fd35 100644 --- a/src/fr/devinsy/sikevadb/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/SQLSikevaDB.java @@ -1391,7 +1391,7 @@ public class SQLSikevaDB implements SikevaDB { try { connection = getConnection(); 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); resultSet = statement.executeQuery(); @@ -1424,7 +1424,99 @@ public class SQLSikevaDB implements SikevaDB { try { connection = getConnection(); 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(2, subkey); resultSet = statement.executeQuery(); diff --git a/src/fr/devinsy/sikevadb/SikevaDB.java b/src/fr/devinsy/sikevadb/SikevaDB.java index ce74464..83a5dda 100644 --- a/src/fr/devinsy/sikevadb/SikevaDB.java +++ b/src/fr/devinsy/sikevadb/SikevaDB.java @@ -102,6 +102,12 @@ public interface SikevaDB { 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 put(Element element) throws Exception; diff --git a/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java b/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java index cef357e..a36c481 100644 --- a/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java +++ b/test/fr/devinsy/sikevadb/SQLSikevaDBTest.java @@ -16,7 +16,6 @@ import org.junit.Test; import fr.devinsy.util.StringList; /** - * ?profileSQL=true * * @author Christian P. Momon */ @@ -47,6 +46,7 @@ public class SQLSikevaDBTest { BasicConfigurator.configure(); 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.open(); } @@ -123,6 +123,108 @@ public class SQLSikevaDBTest { 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 *