From 7ea53a795551394c83f5642c5a687f51e01d7968 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Tue, 25 Apr 2017 23:50:24 +0200 Subject: [PATCH] Improved Demo. Added helper methods for numeric subkey. --- src/fr/devinsy/sikevadb/core/SikevaDB.java | 16 +- .../devinsy/sikevadb/demo/SikevaDBDemo.java | 15 +- .../sikevadb/filetree/FileTreeSikevaDB.java | 105 ++++++++++++- src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java | 138 +++++++++++++++++- 4 files changed, 252 insertions(+), 22 deletions(-) diff --git a/src/fr/devinsy/sikevadb/core/SikevaDB.java b/src/fr/devinsy/sikevadb/core/SikevaDB.java index 0a1632f..1054258 100644 --- a/src/fr/devinsy/sikevadb/core/SikevaDB.java +++ b/src/fr/devinsy/sikevadb/core/SikevaDB.java @@ -37,6 +37,8 @@ public interface SikevaDB public long countOfElements(String key) throws SikevaDBException; + public long countOfElements(String key, long subkey) throws SikevaDBException; + public long countOfElements(String key, String subkey) throws SikevaDBException; public void create() throws SikevaDBException; @@ -45,6 +47,8 @@ public interface SikevaDB public Element getElement(String key) throws SikevaDBException; + public Element getElement(String key, long subkey) throws SikevaDBException; + public Element getElement(String key, String subkey) throws SikevaDBException; public Elements getElements() throws SikevaDBException; @@ -57,6 +61,8 @@ public interface SikevaDB public String getValue(String key) throws SikevaDBException; + public String getValue(String key, long subkey) throws SikevaDBException; + public String getValue(String key, String subkey) throws SikevaDBException; public StringList getValues(String key) throws SikevaDBException; @@ -75,25 +81,33 @@ public interface SikevaDB public long memorySize(String key) throws SikevaDBException; + public long memorySize(String key, long subkey) 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, long subkey, String value) 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 long subkey) 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 renameSubkey(final String key, final long oldSubKey, final long newSubKey) 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; diff --git a/src/fr/devinsy/sikevadb/demo/SikevaDBDemo.java b/src/fr/devinsy/sikevadb/demo/SikevaDBDemo.java index 26cc848..fd66176 100644 --- a/src/fr/devinsy/sikevadb/demo/SikevaDBDemo.java +++ b/src/fr/devinsy/sikevadb/demo/SikevaDBDemo.java @@ -101,10 +101,10 @@ public final class SikevaDBDemo { for (int index = 0; index < 10; index++) { - database.put("victor", String.valueOf(index), "victor" + index); + database.put("victor", index, "victor" + index); } - String value = database.getValue("victor", String.valueOf(7)); + String value = database.getValue("victor", 7); if (StringUtils.equals(value, "victor7")) { System.out.println("victor is correctly stored."); @@ -114,10 +114,9 @@ public final class SikevaDBDemo System.out.println("victor is NOT correctly stored."); } - // TODOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO String.valueOf -> - // int - database.remove("victor", String.valueOf(5)); - if (database.getValue("victor", String.valueOf(5)) == null) + // + database.remove("victor", 5); + if (database.getValue("victor", 5) == null) { System.out.println("(victor, 5) is correctly removed."); } @@ -127,6 +126,10 @@ public final class SikevaDBDemo } } + // + System.out.println("Database values:\t" + database.countOfElements()); + System.out.println("Database memory:\t" + database.memorySize() + " bytes"); + // database.close(); } diff --git a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java index e8d6da0..4e2af21 100644 --- a/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java +++ b/src/fr/devinsy/sikevadb/filetree/FileTreeSikevaDB.java @@ -99,6 +99,7 @@ public class FileTreeSikevaDB implements SikevaDB } /** + * {@inheritDoc} * */ @Override @@ -109,6 +110,7 @@ public class FileTreeSikevaDB implements SikevaDB /** * @throws IOException + * */ @Override public void clear() throws SikevaDBException @@ -128,6 +130,7 @@ public class FileTreeSikevaDB implements SikevaDB } /** + * {@inheritDoc} * */ @Override @@ -201,6 +204,22 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} + * + */ + @Override + public long countOfElements(final String key, final long subkey) throws SikevaDBException + { + long result; + + result = countOfElements(key, String.valueOf(subkey)); + + // + return result; + } + + /** + * {@inheritDoc} + * */ @Override public long countOfElements(final String key, final String subkey) @@ -231,6 +250,7 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void create() throws SikevaDBException @@ -256,6 +276,7 @@ public class FileTreeSikevaDB implements SikevaDB } /** + * {@inheritDoc} * */ @Override @@ -362,6 +383,21 @@ public class FileTreeSikevaDB implements SikevaDB return result; } + /** + * {@inheritDoc} + * + */ + @Override + public Element getElement(final String key, final long subkey) throws SikevaDBException + { + Element result; + + result = getElement(key, String.valueOf(subkey)); + + // + return result; + } + /** * {@inheritDoc} * @@ -599,6 +635,21 @@ public class FileTreeSikevaDB implements SikevaDB return result; } + /** + * {@inheritDoc} + * + */ + @Override + public String getValue(final String key, final long subkey) throws SikevaDBException + { + String result; + + result = getValue(key, String.valueOf(subkey)); + + // + return result; + } + /** * {@inheritDoc} * @@ -857,7 +908,21 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} * - * @throws SikevaDBException + */ + @Override + public long memorySize(final String key, final long subkey) throws SikevaDBException + { + long result; + + result = memorySize(key, String.valueOf(subkey)); + + // + return result; + } + + /** + * {@inheritDoc} + * */ @Override public long memorySize(final String key, final String subkey) throws SikevaDBException @@ -961,7 +1026,16 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} * - * @throws SikevaDBException + */ + @Override + public void put(final String key, final long subkey, final String value) throws SikevaDBException + { + put(key, String.valueOf(subkey), value); + } + + /** + * {@inheritDoc} + * */ @Override public void put(final String key, final String value) throws SikevaDBException @@ -996,7 +1070,6 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} * - * @throws SikevaDBException */ @Override public void put(final String key, final String subkey, final String value) throws SikevaDBException @@ -1032,7 +1105,6 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} * - * @throws SikevaDBException */ @Override public void remove(final String key) throws SikevaDBException @@ -1063,7 +1135,16 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} * - * @throws SikevaDBException + */ + @Override + public void remove(final String key, final long subkey) throws SikevaDBException + { + remove(key, String.valueOf(subkey)); + } + + /** + * {@inheritDoc} + * */ @Override public void remove(final String key, final String subkey) throws SikevaDBException @@ -1095,7 +1176,6 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} * - * @throws SikevaDBException */ @Override public void removeMany(final String key, final String... subkeys) throws SikevaDBException @@ -1118,6 +1198,7 @@ public class FileTreeSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void renameKey(final String oldKey, final String newKey) @@ -1161,7 +1242,17 @@ public class FileTreeSikevaDB implements SikevaDB * {@inheritDoc} */ @Override - public void renameSubKey(final String key, final String oldSubkey, final String newSubkey) throws SikevaDBException + public void renameSubkey(final String key, final long oldSubKey, final long newSubkey) throws SikevaDBException + { + renameSubkey(key, String.valueOf(oldSubKey), String.valueOf(newSubkey)); + } + + /** + * {@inheritDoc} + * + */ + @Override + public void renameSubkey(final String key, final String oldSubkey, final String newSubkey) throws SikevaDBException { this.logger.debug("renameSybKey starting... [{}][{}][{}]", oldSubkey, newSubkey); diff --git a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java index 6c1fcbb..786358e 100644 --- a/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java +++ b/src/fr/devinsy/sikevadb/sql/SQLSikevaDB.java @@ -139,7 +139,7 @@ public class SQLSikevaDB implements SikevaDB } /** - * This methods clear all data in current opened database. + * {@inheritDoc} * */ @Override @@ -167,7 +167,7 @@ public class SQLSikevaDB implements SikevaDB } /** - * This method closes the current database session. + * {@inheritDoc} * */ @Override @@ -246,6 +246,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public long countOfElements() throws SikevaDBException @@ -281,6 +282,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public long countOfElements(final String key) throws SikevaDBException @@ -324,6 +326,22 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * + */ + @Override + public long countOfElements(final String key, final long subkey) throws SikevaDBException + { + long result; + + result = countOfElements(key, String.valueOf(subkey)); + + // + return result; + } + + /** + * {@inheritDoc} + * */ @Override public long countOfElements(final String key, final String subkey) throws SikevaDBException @@ -371,10 +389,8 @@ public class SQLSikevaDB implements SikevaDB } /** - * This method creates the schema (table) used by SQLSikevaDB. + * {@inheritDoc} * - * @throws IOException - * @throws SikevaDBException */ @Override public void create() throws SikevaDBException @@ -446,6 +462,7 @@ public class SQLSikevaDB implements SikevaDB } /** + * {@inheritDoc} * */ @Override @@ -534,7 +551,10 @@ public class SQLSikevaDB implements SikevaDB } /** - * + * + * @param id + * @return + * @throws SikevaDBException */ public Element getElement(final long id) throws SikevaDBException { @@ -603,6 +623,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public Element getElement(final String key) throws SikevaDBException @@ -674,6 +695,22 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * + */ + @Override + public Element getElement(final String key, final long subkey) throws SikevaDBException + { + Element result; + + result = getElement(key, String.valueOf(subkey)); + + // + return result; + } + + /** + * {@inheritDoc} + * */ @Override public Element getElement(final String key, final String subkey) throws SikevaDBException @@ -750,6 +787,7 @@ public class SQLSikevaDB implements SikevaDB } /** + * {@inheritDoc} * */ @Override @@ -807,6 +845,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public Elements getElements(final String key) throws SikevaDBException @@ -872,6 +911,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public StringList getKeys() throws SikevaDBException @@ -931,6 +971,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public StringList getSubkeys(final String key) throws SikevaDBException @@ -991,6 +1032,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public String getValue(final String key) throws SikevaDBException @@ -1048,6 +1090,22 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * + */ + @Override + public String getValue(final String key, final long subkey) throws SikevaDBException + { + String result; + + result = getValue(key, String.valueOf(subkey)); + + // + return result; + } + + /** + * {@inheritDoc} + * */ @Override public String getValue(final String key, final String subkey) throws SikevaDBException @@ -1110,6 +1168,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public StringList getValues(final String key) throws SikevaDBException @@ -1151,6 +1210,7 @@ public class SQLSikevaDB implements SikevaDB } /** + * {@inheritDoc} * */ @Override @@ -1172,6 +1232,7 @@ public class SQLSikevaDB implements SikevaDB } /** + * {@inheritDoc} * */ @Override @@ -1186,6 +1247,7 @@ public class SQLSikevaDB implements SikevaDB } /** + * {@inheritDoc} * */ @Override @@ -1208,6 +1270,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public boolean isCreated() throws SikevaDBException @@ -1222,6 +1285,7 @@ public class SQLSikevaDB implements SikevaDB } /** + * {@inheritDoc} * */ @Override @@ -1237,6 +1301,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public long memorySize() throws SikevaDBException @@ -1273,6 +1338,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public long memorySize(final String key) throws SikevaDBException @@ -1316,6 +1382,22 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * + */ + @Override + public long memorySize(final String key, final long subkey) throws SikevaDBException + { + long result; + + result = memorySize(key, String.valueOf(subkey)); + + // + return result; + } + + /** + * {@inheritDoc} + * */ @Override public long memorySize(final String key, final String subkey) throws SikevaDBException @@ -1366,6 +1448,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void open() throws SikevaDBException @@ -1427,6 +1510,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void put(final Element element) throws SikevaDBException @@ -1547,6 +1631,17 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * + */ + @Override + public void put(final String key, final long subkey, final String value) throws SikevaDBException + { + put(key, String.valueOf(subkey), String.valueOf(value)); + } + + /** + * {@inheritDoc} + * */ @Override public void put(final String key, final String value) throws SikevaDBException @@ -1627,6 +1722,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void put(final String key, final String subkey, final String value) throws SikevaDBException @@ -1720,6 +1816,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void remove(final String key) throws SikevaDBException @@ -1772,6 +1869,17 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * + */ + @Override + public void remove(final String key, final long subkey) throws SikevaDBException + { + remove(key, String.valueOf(subkey)); + } + + /** + * {@inheritDoc} + * */ @Override public void remove(final String key, final String subkey) throws SikevaDBException @@ -1820,6 +1928,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void removeMany(final String key, final String... subkeys) throws SikevaDBException @@ -1872,6 +1981,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void renameKey(final String oldKey, final String newKey) throws SikevaDBException @@ -1920,9 +2030,20 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override - public void renameSubKey(final String key, final String oldKey, final String newKey) throws SikevaDBException + public void renameSubkey(final String key, final long oldSubkey, final long newSubkey) throws SikevaDBException + { + renameSubkey(key, String.valueOf(oldSubkey), String.valueOf(newSubkey)); + } + + /** + * {@inheritDoc} + * + */ + @Override + public void renameSubkey(final String key, final String oldKey, final String newKey) throws SikevaDBException { if ((key == null) || (oldKey == null) || (newKey == null)) { @@ -1936,6 +2057,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void replaceInValue(final String key, final String... tokens) throws SikevaDBException @@ -1959,6 +2081,7 @@ public class SQLSikevaDB implements SikevaDB /** * {@inheritDoc} + * */ @Override public void replaceInValues(final String key, final String... tokens) throws SikevaDBException @@ -2031,5 +2154,4 @@ public class SQLSikevaDB implements SikevaDB { this.url = url; } - }