@Override
public Repo<Master> call(long tid, Master master) throws Exception {
Path path = new Path(tableInfo.exportDir, Constants.EXPORT_FILE);
BatchWriter mbw = null;
ZipInputStream zis = null;
try {
FileSystem fs = master.getFileSystem();
mbw = master.getConnector().createBatchWriter(Constants.METADATA_TABLE_NAME, new BatchWriterConfig());
zis = new ZipInputStream(fs.open(path));
Map<String,String> fileNameMappings = readMappingFile(fs, tableInfo);
String bulkDir = new Path(tableInfo.importDir).getName();
ZipEntry zipEntry;
while ((zipEntry = zis.getNextEntry()) != null) {
if (zipEntry.getName().equals(Constants.EXPORT_METADATA_FILE)) {
DataInputStream in = new DataInputStream(new BufferedInputStream(zis));
Key key = new Key();
Value val = new Value();
Mutation m = null;
Text currentRow = null;
int dirCount = 0;
while (true) {
key.readFields(in);
val.readFields(in);
Text endRow = new KeyExtent(key.getRow(), (Text) null).getEndRow();
Text metadataRow = new KeyExtent(new Text(tableInfo.tableId), endRow, null).getMetadataEntry();
Text cq;
if (key.getColumnFamily().equals(Constants.METADATA_DATAFILE_COLUMN_FAMILY)) {
String oldName = new Path(key.getColumnQualifier().toString()).getName();
String newName = fileNameMappings.get(oldName);
if (newName == null) {
throw new ThriftTableOperationException(tableInfo.tableId, tableInfo.tableName, TableOperation.IMPORT, TableOperationExceptionType.OTHER,
"File " + oldName + " does not exist in import dir");
}
cq = new Text("/" + bulkDir + "/" + newName);
} else {
cq = key.getColumnQualifier();
}
if (m == null) {
m = new Mutation(metadataRow);
Constants.METADATA_DIRECTORY_COLUMN.put(m, new Value(FastFormat.toZeroPaddedString(dirCount++, 8, 16, "/c-".getBytes(Constants.UTF8))));
currentRow = metadataRow;
}
if (!currentRow.equals(metadataRow)) {
mbw.addMutation(m);
m = new Mutation(metadataRow);
Constants.METADATA_DIRECTORY_COLUMN.put(m, new Value(FastFormat.toZeroPaddedString(dirCount++, 8, 16, "/c-".getBytes(Constants.UTF8))));
}
m.put(key.getColumnFamily(), cq, val);
if (endRow == null && Constants.METADATA_PREV_ROW_COLUMN.hasColumns(key)) {
mbw.addMutation(m);
break; // its the last column in the last row
}
}
break;
}
}
return new MoveExportedFiles(tableInfo);
} catch (IOException ioe) {
log.warn(ioe.getMessage(), ioe);
throw new ThriftTableOperationException(tableInfo.tableId, tableInfo.tableName, TableOperation.IMPORT, TableOperationExceptionType.OTHER,
"Error reading " + path + " " + ioe.getMessage());
} finally {
if (zis != null) {
try {
zis.close();
} catch (IOException ioe) {
log.warn("Failed to close zip file ", ioe);
}
}
if (mbw != null) {
mbw.close();
}
}
}