Fix archive bug. Add patch methods. Include ID.

This commit is contained in:
Christian P. MOMON 2013-12-11 15:57:46 +01:00
parent bdc7359b79
commit 03e8764a86
6 changed files with 568 additions and 137 deletions

View file

@ -25,6 +25,9 @@ import org.joda.time.DateTime;
*/ */
public class Element { public class Element {
public static final long NO_ID = -1;
private long id;
private DateTime creationDate; private DateTime creationDate;
private DateTime editionDate; private DateTime editionDate;
private DateTime archiveDate; private DateTime archiveDate;
@ -40,6 +43,7 @@ public class Element {
* @param value * @param value
*/ */
public Element() { public Element() {
this.id = NO_ID;
} }
/** /**
@ -62,35 +66,39 @@ public class Element {
} }
public DateTime getArchiveDate() { public DateTime getArchiveDate() {
return archiveDate; return this.archiveDate;
} }
public DateTime getCreationDate() { public DateTime getCreationDate() {
return creationDate; return this.creationDate;
} }
public String getDigest() { public String getDigest() {
return digest; return this.digest;
} }
public DateTime getEditionDate() { public DateTime getEditionDate() {
return editionDate; return this.editionDate;
}
public long getId() {
return this.id;
} }
public String getKey() { public String getKey() {
return key; return this.key;
} }
public long getSize() { public long getSize() {
return size; return this.size;
} }
public String getSubkey() { public String getSubkey() {
return subkey; return this.subkey;
} }
public String getValue() { public String getValue() {
return value; return this.value;
} }
public void setArchiveDate(final DateTime date) { public void setArchiveDate(final DateTime date) {
@ -109,6 +117,10 @@ public class Element {
this.editionDate = date; this.editionDate = date;
} }
public void setId(final long id) {
this.id = id;
}
public void setKey(final String key) { public void setKey(final String key) {
this.key = key; this.key = key;
} }

View file

@ -139,6 +139,12 @@ public class FileSikevaDB implements SikevaDB {
return 0; return 0;
} }
@Override
public Elements getAllElements() throws Exception {
// TODO Auto-generated method stub
return null;
}
@Override @Override
public Elements getAllElements(final String key) throws Exception { public Elements getAllElements(final String key) throws Exception {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@ -241,12 +247,6 @@ public class FileSikevaDB implements SikevaDB {
return null; return null;
} }
@Override
public StringList getValues(final String key, final String subkey) {
// TODO Auto-generated method stub
return null;
}
@Override @Override
public long memorySize() { public long memorySize() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@ -336,4 +336,22 @@ public class FileSikevaDB implements SikevaDB {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
@Override
public void renameKey(final String oldKey, final String newKey) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void replaceInValue(final String key, final String... tokens) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void replaceInValues(final String key, final String... tokens) throws Exception {
// TODO Auto-generated method stub
}
} }

View file

@ -626,6 +626,74 @@ public class SQLSikevaDB implements SikevaDB {
} }
} }
/**
*
* @param id
* @return
* @throws SQLException
*/
public boolean exists(final Element element) throws SQLException {
boolean result;
if (element == null) {
result = false;
} else if (getElement(element.getId()) == null) {
result = false;
} else {
result = true;
}
//
return result;
}
/**
* {@inheritDoc}
*/
@Override
public Elements getAllElements() throws SQLException {
Elements result;
//
result = new Elements((int) countOfAllElements());
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
connection.setAutoCommit(true);
statement = connection
.prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements ORDER BY ARCHIVE_DATE ASC");
resultSet = statement.executeQuery();
while (resultSet.next()) {
//
Element element = new Element();
element.setId(resultSet.getLong(1));
element.setKey(resultSet.getString(2));
element.setSubkey(resultSet.getString(3));
element.setValue(resultSet.getString(4));
element.setSize(resultSet.getLong(5));
element.setDigest(resultSet.getString(6));
element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
//
result.add(element);
}
} finally {
close(connection, statement, resultSet);
}
//
return result;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -644,21 +712,23 @@ public class SQLSikevaDB implements SikevaDB {
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
.prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC"); .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? ORDER BY ARCHIVE_DATE ASC");
statement.setString(1, key); statement.setString(1, key);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
// //
Element element = new Element(); Element element = new Element();
element.setKey(resultSet.getString(1));
element.setSubkey(resultSet.getString(2)); element.setId(resultSet.getLong(1));
element.setValue(resultSet.getString(3)); element.setKey(resultSet.getString(2));
element.setSize(resultSet.getLong(4)); element.setSubkey(resultSet.getString(3));
element.setDigest(resultSet.getString(5)); element.setValue(resultSet.getString(4));
element.setCreationDate(toDateTime(resultSet.getTimestamp(6))); element.setSize(resultSet.getLong(5));
element.setEditionDate(toDateTime(resultSet.getTimestamp(7))); element.setDigest(resultSet.getString(6));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
// //
result.add(element); result.add(element);
@ -694,7 +764,7 @@ public class SQLSikevaDB implements SikevaDB {
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
.prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC"); .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC");
statement.setString(1, key); statement.setString(1, key);
statement.setString(2, subkey); statement.setString(2, subkey);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
@ -702,14 +772,16 @@ public class SQLSikevaDB implements SikevaDB {
while (resultSet.next()) { while (resultSet.next()) {
// //
Element element = new Element(); Element element = new Element();
element.setKey(resultSet.getString(1));
element.setSubkey(resultSet.getString(2)); element.setId(resultSet.getLong(1));
element.setValue(resultSet.getString(3)); element.setKey(resultSet.getString(2));
element.setSize(resultSet.getLong(4)); element.setSubkey(resultSet.getString(3));
element.setDigest(resultSet.getString(5)); element.setValue(resultSet.getString(4));
element.setCreationDate(toDateTime(resultSet.getTimestamp(6))); element.setSize(resultSet.getLong(5));
element.setEditionDate(toDateTime(resultSet.getTimestamp(7))); element.setDigest(resultSet.getString(6));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
// //
result.add(element); result.add(element);
@ -972,6 +1044,61 @@ public class SQLSikevaDB implements SikevaDB {
return this.driverClassname; return this.driverClassname;
} }
/**
*
*/
public Element getElement(final long id) throws SQLException {
Element result;
if (id == Element.NO_ID) {
result = null;
} else {
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
//
connection = getConnection();
connection.setAutoCommit(true);
statement = connection
.prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ID=?");
statement.setLong(1, id);
resultSet = statement.executeQuery();
//
if (resultSet.next()) {
//
result = new Element();
result.setId(resultSet.getLong(1));
result.setKey(resultSet.getString(2));
result.setSubkey(resultSet.getString(3));
result.setValue(resultSet.getString(4));
result.setSize(resultSet.getLong(5));
result.setDigest(resultSet.getString(6));
result.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
result.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
result.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
} else {
result = null;
}
//
if (resultSet.next()) {
throw new SQLException("More than only once result [id=" + id + "].");
}
} finally {
close(connection, statement, resultSet);
}
}
//
return result;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -988,7 +1115,7 @@ public class SQLSikevaDB implements SikevaDB {
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
.prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NULL"); .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_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();
@ -996,14 +1123,16 @@ public class SQLSikevaDB implements SikevaDB {
if (resultSet.next()) { if (resultSet.next()) {
// //
result = new Element(); result = new Element();
result.setKey(resultSet.getString(1));
result.setSubkey(resultSet.getString(2)); result.setId(resultSet.getLong(1));
result.setValue(resultSet.getString(3)); result.setKey(resultSet.getString(2));
result.setSize(resultSet.getLong(4)); result.setSubkey(resultSet.getString(3));
result.setDigest(resultSet.getString(5)); result.setValue(resultSet.getString(4));
result.setCreationDate(toDateTime(resultSet.getTimestamp(6))); result.setSize(resultSet.getLong(5));
result.setEditionDate(toDateTime(resultSet.getTimestamp(7))); result.setDigest(resultSet.getString(6));
result.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); result.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
result.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
result.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
} else { } else {
result = null; result = null;
@ -1042,7 +1171,7 @@ public class SQLSikevaDB implements SikevaDB {
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
.prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=?"); .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS 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();
@ -1051,14 +1180,16 @@ public class SQLSikevaDB implements SikevaDB {
if (resultSet.next()) { if (resultSet.next()) {
// //
result = new Element(); result = new Element();
result.setKey(resultSet.getString(1));
result.setSubkey(resultSet.getString(2)); result.setId(resultSet.getLong(1));
result.setValue(resultSet.getString(3)); result.setKey(resultSet.getString(2));
result.setSize(resultSet.getLong(4)); result.setSubkey(resultSet.getString(3));
result.setDigest(resultSet.getString(5)); result.setValue(resultSet.getString(4));
result.setCreationDate(toDateTime(resultSet.getTimestamp(6))); result.setSize(resultSet.getLong(5));
result.setEditionDate(toDateTime(resultSet.getTimestamp(7))); result.setDigest(resultSet.getString(6));
result.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); result.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
result.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
result.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
} else { } else {
result = null; result = null;
} }
@ -1095,21 +1226,23 @@ public class SQLSikevaDB implements SikevaDB {
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
.prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC"); .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC");
statement.setString(1, key); statement.setString(1, key);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
// //
Element element = new Element(); Element element = new Element();
element.setKey(resultSet.getString(1));
element.setSubkey(resultSet.getString(2)); element.setId(resultSet.getLong(1));
element.setValue(resultSet.getString(3)); element.setKey(resultSet.getString(2));
element.setSize(resultSet.getLong(4)); element.setSubkey(resultSet.getString(3));
element.setDigest(resultSet.getString(5)); element.setValue(resultSet.getString(4));
element.setCreationDate(toDateTime(resultSet.getTimestamp(6))); element.setSize(resultSet.getLong(5));
element.setEditionDate(toDateTime(resultSet.getTimestamp(7))); element.setDigest(resultSet.getString(6));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
// //
result.add(element); result.add(element);
@ -1145,7 +1278,7 @@ public class SQLSikevaDB implements SikevaDB {
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
.prepareStatement("SELECT TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=? ORDER BY CREATION_DATE ASC"); .prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NOT NULL AND TOPKEY=? AND SUBKEY=? ORDER BY CREATION_DATE ASC");
statement.setString(1, key); statement.setString(1, key);
statement.setString(2, subkey); statement.setString(2, subkey);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
@ -1153,14 +1286,16 @@ public class SQLSikevaDB implements SikevaDB {
while (resultSet.next()) { while (resultSet.next()) {
// //
Element element = new Element(); Element element = new Element();
element.setKey(resultSet.getString(1));
element.setSubkey(resultSet.getString(2)); element.setId(resultSet.getLong(1));
element.setValue(resultSet.getString(3)); element.setKey(resultSet.getString(2));
element.setSize(resultSet.getLong(4)); element.setSubkey(resultSet.getString(3));
element.setDigest(resultSet.getString(5)); element.setValue(resultSet.getString(4));
element.setCreationDate(toDateTime(resultSet.getTimestamp(6))); element.setSize(resultSet.getLong(5));
element.setEditionDate(toDateTime(resultSet.getTimestamp(7))); element.setDigest(resultSet.getString(6));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(8))); element.setCreationDate(toDateTime(resultSet.getTimestamp(7)));
element.setEditionDate(toDateTime(resultSet.getTimestamp(8)));
element.setArchiveDate(toDateTime(resultSet.getTimestamp(9)));
// //
result.add(element); result.add(element);
@ -1358,7 +1493,7 @@ public class SQLSikevaDB implements SikevaDB {
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
.prepareStatement("SELECT VALUE,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? ORDER BY CREATION_DATE ASC"); .prepareStatement("SELECT VALUE,CREATION_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY IS NOT NULL ORDER BY CREATION_DATE ASC");
statement.setString(1, key); statement.setString(1, key);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
@ -1375,47 +1510,6 @@ public class SQLSikevaDB implements SikevaDB {
return result; return result;
} }
/**
* {@inheritDoc}
*/
@Override
public StringList getValues(final String key, final String subkey) throws SQLException {
StringList result;
//
if (subkey == null) {
result = getValues(key);
} else {
//
result = new StringList((int) countOfElements(key, subkey));
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
connection.setAutoCommit(true);
statement = connection
.prepareStatement("SELECT VALUE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL AND TOPKEY=? AND SUBKEY=? ORDER BY ARCHIVE_DATE ASC");
statement.setString(1, key);
statement.setString(2, subkey);
resultSet = statement.executeQuery();
while (resultSet.next()) {
//
result.add(resultSet.getString(1));
}
} finally {
close(connection, statement, resultSet);
}
}
//
return result;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -1659,29 +1753,94 @@ public class SQLSikevaDB implements SikevaDB {
*/ */
@Override @Override
public void put(final Element element) throws SQLException { public void put(final Element element) throws SQLException {
// //
Connection connection = null; if (element == null) {
PreparedStatement statement = null; //
ResultSet resultSet = null; throw new IllegalArgumentException("element is null.");
try { } else {
connection = getConnection(); //
connection.setAutoCommit(true); if (exists(element)) {
statement = connection //
.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
//
connection = getConnection();
connection.setAutoCommit(true);
statement.setString(1, element.getKey()); // Archive existing element.
statement.setString(2, element.getSubkey()); statement = connection
statement.setString(3, element.getValue()); .prepareStatement("UPDATE sikevadb_elements SET TOPKEY=?,SUBKEY=?,VALUE=?,SIZE=?,DIGEST=?,CREATION_DATE=?,EDITION_DATE=?,ARCHIVE_DATE=? WHERE ID=?");
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.executeUpdate(); statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.setLong(9, element.getId());
} finally { statement.executeUpdate();
close(connection, statement, resultSet);
} finally {
close(connection, statement, resultSet);
}
} else if (element.getId() == Element.NO_ID) {
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
connection.setAutoCommit(true);
statement = connection
.prepareStatement("INSERT INTO sikevadb_elements (TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
statement.setString(1, element.getKey());
statement.setString(2, element.getSubkey());
statement.setString(3, element.getValue());
statement.setLong(4, element.getSize());
statement.setString(5, element.getDigest());
statement.setTimestamp(6, toTimestamp(element.getCreationDate()));
statement.setTimestamp(7, toTimestamp(element.getEditionDate()));
statement.setTimestamp(8, toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
} finally {
close(connection, statement, resultSet);
}
} else {
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
connection = getConnection();
connection.setAutoCommit(true);
statement = connection
.prepareStatement("INSERT INTO sikevadb_elements (ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE) VALUES(?,?, ?, ?, ?, ?, ?, ?, ?)");
statement.setLong(1, element.getId());
statement.setString(2, element.getKey());
statement.setString(3, element.getSubkey());
statement.setString(4, element.getValue());
statement.setLong(5, element.getSize());
statement.setString(6, element.getDigest());
statement.setTimestamp(7, toTimestamp(element.getCreationDate()));
statement.setTimestamp(8, toTimestamp(element.getEditionDate()));
statement.setTimestamp(9, toTimestamp(element.getArchiveDate()));
statement.executeUpdate();
} finally {
close(connection, statement, resultSet);
}
}
} }
} }
@ -1717,7 +1876,7 @@ public class SQLSikevaDB implements SikevaDB {
connection.setAutoCommit(false); connection.setAutoCommit(false);
// Archive existing element. // Archive existing element.
if (element.getCreationDate() != null) { if (element.getId() != Element.NO_ID) {
statement = connection statement = connection
.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL"); .prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY IS NULL AND ARCHIVE_DATE IS NULL");
@ -1786,7 +1945,7 @@ public class SQLSikevaDB implements SikevaDB {
connection.setAutoCommit(false); connection.setAutoCommit(false);
// Archive existing element. // Archive existing element.
if (element.getCreationDate() != null) { if (element.getId() != Element.NO_ID) {
statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL"); statement = connection.prepareStatement("UPDATE sikevadb_elements SET ARCHIVE_DATE=? WHERE TOPKEY=? AND SUBKEY=? AND ARCHIVE_DATE IS NULL");
statement.setTimestamp(1, toTimestamp(element.getEditionDate())); statement.setTimestamp(1, toTimestamp(element.getEditionDate()));
@ -1924,6 +2083,82 @@ public class SQLSikevaDB implements SikevaDB {
} }
} }
/**
* {@inheritDoc}
*/
@Override
public void renameKey(final String oldKey, final String newKey) throws SQLException {
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
//
connection = getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement("UPDATE sikevadb_elements SET TOPKEY=? WHERE TOPKEY=?");
statement.setString(1, newKey);
statement.setString(2, oldKey);
statement.executeUpdate();
} finally {
close(connection, statement, resultSet);
}
}
/**
* {@inheritDoc}
*/
@Override
public void replaceInValue(final String key, final String... tokens) throws SQLException {
//
String value = getValue(key);
//
for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) {
value = value.replace(tokens[tokenIndex], tokens[tokenIndex + 1]);
}
//
put(key, value);
}
/**
* {@inheritDoc}
*/
@Override
public void replaceInValues(final String key, final String... tokens) throws SQLException {
//
Elements elements = getElements(key);
long count = 0;
for (Element element : elements) {
//
System.out.println(element.getKey() + count + " " + element.getSubkey() + "/" + elements.size());
if (element.getSubkey() != null) {
//
count++;
//
String value = element.getValue();
//
for (int tokenIndex = 0; tokenIndex < tokens.length; tokenIndex += 2) {
value = value.replace(tokens[tokenIndex], tokens[tokenIndex + 1]);
}
//
put(element.getKey(), element.getSubkey(), value);
}
}
}
public void setDriverClassname(final String driverClassname) { public void setDriverClassname(final String driverClassname) {
this.driverClassname = driverClassname; this.driverClassname = driverClassname;
} }

View file

@ -60,6 +60,8 @@ public interface SikevaDB {
public long countOfElements(String key, String subkey) throws Exception; public long countOfElements(String key, String subkey) throws Exception;
public Elements getAllElements() throws Exception;
public Elements getAllElements(String key) throws Exception; public Elements getAllElements(String key) throws Exception;
public Elements getAllElements(String key, String subkey) throws Exception; public Elements getAllElements(String key, String subkey) throws Exception;
@ -94,8 +96,6 @@ public interface SikevaDB {
public StringList getValues(String key) throws Exception; public StringList getValues(String key) throws Exception;
public StringList getValues(String key, String subkey) throws Exception;
public long memorySize() throws Exception; public long memorySize() throws Exception;
public long memorySize(String key) throws Exception; public long memorySize(String key) throws Exception;
@ -125,4 +125,10 @@ public interface SikevaDB {
public void remove(final String key, final String subkey) throws Exception; public void remove(final String key, final String subkey) throws Exception;
public void removeAll(final String key) throws Exception; public void removeAll(final String key) throws Exception;
public void renameKey(final String oldKey, final String newKey) throws Exception;
public void replaceInValue(final String key, final String... tokens) throws Exception;
public void replaceInValues(final String key, final String... tokens) throws Exception;
} }

View file

@ -115,6 +115,7 @@ public class XMLSikevaDB {
// //
{ {
// //
long id = Long.parseLong(tag.attributes().getByLabel("id").getValue());
String key = StringEscapeUtils.unescapeXml(in.readContentTag("key").getContent()); String key = StringEscapeUtils.unescapeXml(in.readContentTag("key").getContent());
String subkey = StringEscapeUtils.unescapeXml(in.readNullableContentTag("subkey").getContent()); String subkey = StringEscapeUtils.unescapeXml(in.readNullableContentTag("subkey").getContent());
String value = StringEscapeUtils.unescapeXml(in.readContentTag("value").getContent()); String value = StringEscapeUtils.unescapeXml(in.readContentTag("value").getContent());
@ -133,6 +134,8 @@ public class XMLSikevaDB {
// //
result = new Element(); result = new Element();
result.setId(id);
result.setKey(key); result.setKey(key);
result.setSubkey(subkey); result.setSubkey(subkey);
result.setValue(value); result.setValue(value);
@ -196,6 +199,7 @@ public class XMLSikevaDB {
// //
{ {
out.writeTag("id", String.valueOf(source.getId()));
out.writeTag("key", StringEscapeUtils.escapeXml(source.getKey())); out.writeTag("key", StringEscapeUtils.escapeXml(source.getKey()));
if (source.getSubkey() == null) { if (source.getSubkey() == null) {
out.writeTag("subkey", null); out.writeTag("subkey", null);
@ -228,24 +232,30 @@ public class XMLSikevaDB {
public static void write(final XMLWriter out, final SikevaDB source) throws Exception { public static void write(final XMLWriter out, final SikevaDB source) throws Exception {
if (out == null) { if (out == null) {
//
throw new NullPointerException("out is null."); throw new NullPointerException("out is null.");
} else if (source == null) { } else if (source == null) {
//
out.writeEmptyTag("elements"); out.writeEmptyTag("elements");
} else { } else {
// //
out.writeStartTag("elements"); out.writeStartTag("elements");
for (String key : source.getAllKeys()) { for (String key : source.getAllKeys()) {
// //
Element element = source.getElement(key); Elements elements = source.getAllElements(key);
if (element != null) { for (Element element : elements) {
write(out, element); write(out, element);
} }
// //
for (String subkey : source.getSubkeys(key)) { for (String subkey : source.getSubkeys(key)) {
Element subElement = source.getElement(key, subkey); Elements subElements = source.getAllElements(key, subkey);
write(out, subElement); for (Element subElement : subElements) {
write(out, subElement);
}
} }
} }

View file

@ -353,6 +353,126 @@ public class SQLSikevaDBTest {
logger.debug("===== test done."); logger.debug("===== test done.");
} }
/**
* @throws Exception
*
*/
@Test
public void testPutElement01() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
Element element = new Element();
element.setKey("alpha");
element.setSubkey(null);
element.setSize(10);
element.setDigest("qsdkfqskjf");
element.setCreationDate(DateTime.now().minusDays(11));
element.setEditionDate(DateTime.now().minusDays(11));
element.setArchiveDate(DateTime.now().minusDays(10));
element.setValue("bonjour");
database.put(element);
Assert.assertEquals(0, database.countOfElements());
Assert.assertEquals(1, database.countOfArchivedElements());
Assert.assertEquals(1, database.countOfAllElements());
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testPutElement02() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
Element element = new Element();
element.setId(123);
element.setKey("alpha");
element.setSubkey(null);
element.setSize(10);
element.setDigest("qsdkfqskjf");
element.setCreationDate(DateTime.now().minusDays(11));
element.setEditionDate(DateTime.now().minusDays(11));
element.setArchiveDate(DateTime.now().minusDays(10));
element.setValue("bonjour");
database.put(element);
Assert.assertEquals(0, database.countOfElements());
Assert.assertEquals(1, database.countOfArchivedElements());
Assert.assertEquals(1, database.countOfAllElements());
//
logger.debug("===== test done.");
}
/**
* @throws Exception
*
*/
@Test
public void testPutElement03() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
{
Element element = new Element();
element.setId(123);
element.setKey("alpha");
element.setSubkey(null);
element.setSize(10);
element.setDigest("qsdkfqskjf");
element.setCreationDate(DateTime.now().minusDays(11));
element.setEditionDate(DateTime.now().minusDays(11));
element.setArchiveDate(DateTime.now().minusDays(10));
element.setValue("bonjour");
database.put(element);
}
{
Element element = new Element();
element.setKey("bravo");
element.setSubkey(null);
element.setSize(10);
element.setDigest("qsdkfqskjf");
element.setCreationDate(DateTime.now().minusDays(11));
element.setEditionDate(DateTime.now().minusDays(11));
element.setArchiveDate(DateTime.now().minusDays(10));
element.setValue("bonjour");
database.put(element);
}
Elements elements = database.getAllElements();
Assert.assertEquals(0, database.countOfElements());
Assert.assertEquals(2, database.countOfArchivedElements());
Assert.assertEquals(2, database.countOfAllElements());
Assert.assertEquals(123, elements.get(0).getId());
Assert.assertEquals(124, elements.get(1).getId());
//
logger.debug("===== test done.");
}
/** /**
* @throws Exception * @throws Exception
* *
@ -409,6 +529,36 @@ public class SQLSikevaDBTest {
logger.debug("===== test done."); logger.debug("===== test done.");
} }
/**
* @throws Exception
*
*/
@Test
public void testRenameKey01() throws Exception {
//
logger.debug("===== test starting...");
database.clearDatabase();
database.put("alpha01", "val-alpha01");
database.put("alpha01", "sub-alpha01", "val-alpha01");
database.put("alpha01", "sub-alpha02", "val-alpha02");
database.put("alpha01", "sub-alpha03", "val-alpha03");
database.put("alpha02", "sub-alpha01", "val-alpha01");
database.put("alpha03", "sub-alpha02", "val-alpha02");
database.put("alpha04", "sub-alpha03", "val-alpha03");
database.renameKey("alpha01", "november");
Elements targets = database.getAllElements();
Assert.assertEquals(7, targets.size());
Assert.assertEquals(3, database.getValues("november").size());
//
logger.debug("===== test done.");
}
/** /**
* @throws Exception * @throws Exception
* *