Fixed XML export and getKeys methods (thanks Didier). Renamed getKeys

with getTopKeys.
This commit is contained in:
Christian P. MOMON 2018-02-28 16:20:16 +01:00
parent 280e8c7a57
commit 70bb0c74dd
6 changed files with 62 additions and 72 deletions

View file

@ -224,7 +224,7 @@ public interface SikevaDB
* @throws SikevaDBException * @throws SikevaDBException
* the SikevaDB exception * the SikevaDB exception
*/ */
public StringList getKeys() throws SikevaDBException; public StringList getTopKeys() throws SikevaDBException;
/** /**
* Gets all the sub keys of a key. * Gets all the sub keys of a key.

View file

@ -293,7 +293,8 @@ public class XMLSikevaDB
} }
/** /**
* Write. * Write in an XML writer the elements of a database, sorting by keys and
* sub keys.
* *
* @param out * @param out
* the out * the out
@ -317,19 +318,16 @@ public class XMLSikevaDB
{ {
out.writeStartTag("elements"); out.writeStartTag("elements");
for (String key : source.getKeys()) for (String topkey : source.getTopKeys().sort())
{ {
// //
Elements elements = source.getElements(key); Element topElement = source.getElement(topkey);
for (Element element : elements) write(out, topElement);
{
write(out, element);
}
// //
for (String subkey : source.getSubkeys(key)) for (String subkey : source.getSubkeys(topkey).sort())
{ {
Element subElement = source.getElement(key, subkey); Element subElement = source.getElement(subkey);
write(out, subElement); write(out, subElement);
} }
} }

View file

@ -739,7 +739,7 @@ public class FileTreeSikevaDB implements SikevaDB
* @see fr.devinsy.sikevadb.core.SikevaDB#getKeys() * @see fr.devinsy.sikevadb.core.SikevaDB#getKeys()
*/ */
@Override @Override
public StringList getKeys() throws SikevaDBException public StringList getTopKeys() throws SikevaDBException
{ {
StringList result; StringList result;
@ -760,15 +760,7 @@ public class FileTreeSikevaDB implements SikevaDB
} }
else if (file.isDirectory()) else if (file.isDirectory())
{ {
File[] subFiles = file.listFiles(); result.add(file.getName());
for (File subFile : subFiles)
{
if (file.isFile())
{
result.add(subFile.getName());
}
}
} }
} }
} }

View file

@ -1111,7 +1111,7 @@ public class SQLSikevaDB implements SikevaDB
connection = getConnection(); connection = getConnection();
connection.setAutoCommit(true); connection.setAutoCommit(true);
statement = connection statement = connection
.prepareStatement("SELECT ID,TOPKEY,SUBKEY,VALUE,SIZE,DIGEST,CREATION_DATE,EDITION_DATE,ARCHIVE_DATE FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL 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 NULL ORDER BY TOPKEY,SUBKEY ASC");
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
while (resultSet.next()) while (resultSet.next())
@ -1216,54 +1216,6 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getKeys()
*/
@Override
public StringList getKeys() throws SikevaDBException
{
StringList result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
result = new StringList();
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try
{
connection = getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT DISTINCT TOPKEY FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL");
resultSet = statement.executeQuery();
while (resultSet.next())
{
result.add(resultSet.getString(1));
}
}
catch (SQLException exception)
{
logger.error("Error getting keys.", exception);
throw new SikevaDBException("Error getting keys", exception);
}
finally
{
closeQuietly(connection, statement, resultSet);
}
}
//
return result;
}
/** /**
* Gets the login. * Gets the login.
* *
@ -1348,6 +1300,54 @@ public class SQLSikevaDB implements SikevaDB
return result; return result;
} }
/* (non-Javadoc)
* @see fr.devinsy.sikevadb.core.SikevaDB#getKeys()
*/
@Override
public StringList getTopKeys() throws SikevaDBException
{
StringList result;
if (isClosed())
{
throw new ClosedDatabaseException();
}
else
{
//
result = new StringList();
//
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try
{
connection = getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement("SELECT DISTINCT TOPKEY FROM sikevadb_elements WHERE ARCHIVE_DATE IS NULL");
resultSet = statement.executeQuery();
while (resultSet.next())
{
result.add(resultSet.getString(1));
}
}
catch (SQLException exception)
{
logger.error("Error getting keys.", exception);
throw new SikevaDBException("Error getting keys", exception);
}
finally
{
closeQuietly(connection, statement, resultSet);
}
}
//
return result;
}
/** /**
* Gets the url. * Gets the url.
* *

View file

@ -106,9 +106,9 @@ public class TreeFileSikevaDBTest
database.put("alpha01s", "bravo4", "qlskjfmlqj"); database.put("alpha01s", "bravo4", "qlskjfmlqj");
database.put("alpha01s", "bravo5", "qlskjfmlqj"); database.put("alpha01s", "bravo5", "qlskjfmlqj");
StringList keys = database.getKeys(); StringList keys = database.getTopKeys();
Assert.assertEquals(5, keys.size()); Assert.assertEquals(6, keys.size());
Assert.assertTrue(keys.contains("alpha03")); Assert.assertTrue(keys.contains("alpha03"));
Assert.assertEquals(0, database.getSubkeys("alpha03s").size()); Assert.assertEquals(0, database.getSubkeys("alpha03s").size());
Assert.assertEquals(5, database.getSubkeys("alpha01s").size()); Assert.assertEquals(5, database.getSubkeys("alpha01s").size());

View file

@ -170,7 +170,7 @@ public class SQLSikevaDBTest
database.put("alpha01", "bravo4", "qlskjfmlqj"); database.put("alpha01", "bravo4", "qlskjfmlqj");
database.put("alpha01", "bravo5", "qlskjfmlqj"); database.put("alpha01", "bravo5", "qlskjfmlqj");
StringList keys = database.getKeys(); StringList keys = database.getTopKeys();
Assert.assertEquals(5, keys.size()); Assert.assertEquals(5, keys.size());
Assert.assertTrue(keys.contains("alpha03")); Assert.assertTrue(keys.contains("alpha03"));