Commit ced26860 authored by Andre Freyssinet's avatar Andre Freyssinet

Adds and implements loadAll method.

parent 7d2814ac
......@@ -31,6 +31,7 @@ import java.io.ObjectStreamConstants;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Hashtable;
import java.util.List;
import org.objectweb.util.monolog.api.BasicLevel;
......@@ -39,7 +40,6 @@ import fr.dyade.aaa.common.encoding.ByteBufferEncoder;
import fr.dyade.aaa.common.encoding.Encodable;
import fr.dyade.aaa.common.encoding.EncodableFactory;
import fr.dyade.aaa.common.encoding.EncodableFactoryRepository;
import fr.dyade.aaa.common.encoding.SerializableWrapper;
/**
* The AbstractTransaction class implements the common part of the Transaction
......@@ -68,7 +68,17 @@ public abstract class AbstractTransaction extends BaseTransaction {
}
public AbstractTransaction() {}
/**
* Tests if the Transaction component is persistent.
*
* @return true.
*/
@Override
public final boolean isPersistent() {
return true;
}
// State of the transaction monitor.
protected int phase;
......@@ -437,7 +447,35 @@ public abstract class AbstractTransaction extends BaseTransaction {
* @see fr.dyade.aaa.util.Transaction#load(java.lang.String, java.lang.String)
*/
public final Object load(String dirName, String name) throws IOException, ClassNotFoundException {
byte[] buf = loadByteArray(dirName, name);
return loadFromByteArray(loadByteArray(dirName, name));
}
/**
* Fills the list with all objects of the component whose name begins with the prefix.
*
* @param prefix The prefix of searched objects.
* @param list The list of corresponding objects.
*/
public void loadAll(String prefix, List list) {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.DEBUG, "Transaction, loadAll(" + prefix + ")");
String[] names = getList(prefix);
for (String name : names) {
try {
list.add(load(name));
} catch (ClassNotFoundException | IOException exc) {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.WARN, "Transaction, loadAll: cannot retrieve content for " + name, exc);
else
logmon.log(BasicLevel.WARN, "Transaction, loadAll: cannot retrieve content for " + name + " - " + exc.getMessage());
}
}
return;
}
protected final Object loadFromByteArray(byte[] buf) throws IOException, ClassNotFoundException {
if (buf != null) {
if (onlyUseJavaSerialization) {
return deserialize(buf, 0);
......
......@@ -31,6 +31,7 @@ import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import org.objectweb.util.monolog.api.BasicLevel;
......@@ -244,13 +245,79 @@ public abstract class DBTransaction extends AbstractTransaction implements DBTra
return null;
}
public byte[] getFromLog(String name) throws IOException {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.DEBUG, "DBTransaction, getFromLog(" + name + ")");
// Searchs in the log a new value for the object.
Hashtable<String, DBOperation> log = perThreadContext.get().getLog();
DBOperation op = log.get(name);
if ((op != null) && ((op.type == DBOperation.SAVE) || (op.type == DBOperation.CREATE))) {
return op.value;
}
return null;
}
/**
* Tests if the Transaction component is persistent.
*
* @return true.
* Fills the list with all objects of the component whose name begins with the prefix.
*
* @param prefix The prefix of searched objects.
* @param list The list of corresponding objects.
*/
public boolean isPersistent() {
return true;
public void loadAll(String prefix, List list) {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.DEBUG, "DBTransaction, loadAll(" + prefix + ")");
try {
// Creating a statement lets us issue commands against the connection.
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT name, content FROM " + dbtable + " WHERE name LIKE '" + prefix + "%'");
while (rs.next()) {
String name = rs.getString(1);
// Try to retrieve content from memory log.
byte[] content = null;
try {
content = getFromLog(name);
} catch (IOException exc) {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.WARN, "DBTransaction, loadAll: cannot retrieve content for " + name, exc);
else
logmon.log(BasicLevel.WARN, "DBTransaction, loadAll: cannot retrieve content for " + name + " - " + exc.getMessage());
}
// If content is not present in memory log get it from DB.
if (content == null)
content = rs.getBytes(2);
if (content == null) {
logmon.log(BasicLevel.WARN, "DBTransaction, loadAll: no content for " + name);
continue;
}
// Deserializes the object and adds it to the list.
try {
list.add(loadFromByteArray(content));
} catch (ClassNotFoundException | IOException exc) {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.WARN, "DBTransaction, loadAll: cannot retrieve content for " + name, exc);
else
logmon.log(BasicLevel.WARN, "DBTransaction, loadAll: cannot retrieve content for " + name + " - " + exc.getMessage());
}
}
rs.close();
s.close();
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.DEBUG, "DBTransaction, loadAll: " + list.size());
return;
} catch (SQLException sqle) {
logmon.log(BasicLevel.DEBUG, "DBTransaction.getList()", sqle);
}
return;
}
final String fname(String dirName, String name) {
......
......@@ -37,6 +37,9 @@ import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* The JTransaction class implements a transactionnal storage.
......@@ -299,6 +302,20 @@ public final class JTransaction extends BaseTransaction implements JTransactionM
return null;
}
/**
* Fills the list with all objects of the component whose name begins with the prefix.
*
* @param prefix The prefix of searched objects.
* @param list The list of corresponding objects.
*/
public void loadAll(String prefix, List list) {
if (logmon.isLoggable(BasicLevel.DEBUG))
logmon.log(BasicLevel.DEBUG, "JTransaction, loadAll(" + prefix + ")");
// TODO (AF): To be implemented
return;
}
public final byte[] loadByteArray(String name) throws IOException {
return loadByteArray(null, name);
......
......@@ -25,6 +25,7 @@ package fr.dyade.aaa.util;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
/**
* The Transaction interface defines the API of the atomic storage component.
......@@ -135,11 +136,19 @@ public interface Transaction {
/**
* Returns an array of strings naming the objects in the component started by this prefix.
*
* @param prefix the prefix of searched object.
* @param prefix the prefix of searched objects.
* @return an array of strings naming the objects in the component started by this prefix.
*/
String[] getList(String prefix);
/**
* Fills the list with all objects of the component whose name begins with the prefix.
*
* @param prefix The prefix of searched objects.
* @param list The list of corresponding objects.
*/
void loadAll(String prefix, List list);
/**
* Returns true if the component is persistent.
* @return true if the component is persistent.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment