Arrays.sort(statuses, new IndexFileLastComparator());
byte[] origCheckSum = null;
CheckSumType checkSumType = CheckSumType.NONE;
// Do a checksum of checksum - Similar to HDFS
CheckSum checkSumGenerator = null;
CheckSum fileCheckSumGenerator = null;
for(RestFileStatus status: statuses) {
String fileNameWithAbsolutePath = status.getAbsolutePath();
String shortFileName = status.getPathSuffix();
logger.info("fetching file: " + fileNameWithAbsolutePath);
// Kept for backwards compatibility
if(shortFileName.contains("checkSum.txt")) {
logger.warn("Found checksum file in old format: " + shortFileName);
} else if(shortFileName.contains(".metadata")) {
logger.debug("Reading .metadata");
// Read metadata into local file
File copyLocation = new File(dest, shortFileName);
copyFileWithCheckSum(rfs,
fileNameWithAbsolutePath,
copyLocation,
stats,
null);
// Open the local file to initialize checksum
ReadOnlyStorageMetadata metadata;
try {
metadata = new ReadOnlyStorageMetadata(copyLocation);
} catch(IOException e) {
logger.error("Error reading metadata file ", e);
throw new VoldemortException(e);
}
// Read checksum
String checkSumTypeString = (String) metadata.get(ReadOnlyStorageMetadata.CHECKSUM_TYPE);
String checkSumString = (String) metadata.get(ReadOnlyStorageMetadata.CHECKSUM);
if(checkSumTypeString != null && checkSumString != null) {
try {
origCheckSum = Hex.decodeHex(checkSumString.toCharArray());
} catch(DecoderException e) {
logger.error("Exception reading checksum file. Ignoring checksum ",
e);
continue;
}
logger.debug("Checksum from .metadata "
+ new String(Hex.encodeHex(origCheckSum)));
// Define the Global checksum generator
checkSumType = CheckSum.fromString(checkSumTypeString);
checkSumGenerator = CheckSum.getInstance(checkSumType);
}
} else if(!shortFileName.startsWith(".")) {
// Read other (.data , .index files)
File copyLocation = new File(dest, shortFileName);
fileCheckSumGenerator = copyFileWithCheckSum(rfs,
fileNameWithAbsolutePath,
copyLocation,
stats,
checkSumType);
if(fileCheckSumGenerator != null && checkSumGenerator != null) {
byte[] checkSum = fileCheckSumGenerator.getCheckSum();
if(logger.isDebugEnabled()) {
logger.debug("Checksum for " + shortFileName + " - "
+ new String(Hex.encodeHex(checkSum)));
}
checkSumGenerator.update(checkSum);