HeaderRecord.java
package org.microspace.replicator.record;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import org.microspace.annotation.IndexType;
import org.microspace.table.column.Accessor;
import org.microspace.table.column.GetSetPair;
/**
* This record will sync old Space classes with new ones during file recovery.
*
* @author Gaspar Sinai - {@literal gaspar.sinai@microspace.org}
* @version 2016-06-26
*/
public class HeaderRecord implements RecordData, Serializable {
private static final long serialVersionUID = 2012062410500100001L;
private String className;
private String[] fieldNames;
private IndexType keyIndexType;
public <T> HeaderRecord (Accessor<T> accessor) {
Object object = accessor.getBlankObject();
className = object.getClass().getName();
List<GetSetPair<T>> pairs = accessor.getGetSetPairs();
fieldNames = new String[pairs.size()];
for (int i=0; i<pairs.size(); i++) {
fieldNames[i] = pairs.get(i).getName();
}
keyIndexType = accessor.getPrimaryKeyGetSetPair().getIndexType();
}
// for testing
void setClassName (String className) {
this.className = className;
}
// Reindex using the names.
public <T> AddRecord convert (AddRecord oldRecord, Accessor<T> newAccessor) {
List<GetSetPair<T>> pairs = newAccessor.getGetSetPairs();
Object[] fields = new Object[pairs.size()];
for (int i=0; i<fieldNames.length; i++) {
String fieldName = fieldNames[i];
Object fieldValue = oldRecord.getFields()[i];
GetSetPair<T> pair = newAccessor.getGetSetPair(fieldName);
if (pair == null) continue; // Field removed.
fields[pair.getIndex()] = fieldValue;
}
AddRecord add = new AddRecord (className, oldRecord.getKey(), fields);
add.setUpdateCount(oldRecord.getUpdateCount());
return add;
}
public String getClassName() {
return className;
}
public String[] getFieldNames() {
return Arrays.copyOf (fieldNames, fieldNames.length);
}
public IndexType getKeyIndexType() {
return keyIndexType;
}
}