use of org.apache.crail.CrailNodeType in project incubator-crail by apache.
the class NameNodeService method createFile.
@Override
public short createFile(RpcRequestMessage.CreateFileReq request, RpcResponseMessage.CreateFileRes response, RpcNameNodeState errorState) throws Exception {
// check protocol
if (!RpcProtocol.verifyProtocol(RpcProtocol.CMD_CREATE_FILE, request, response)) {
return RpcErrors.ERR_PROTOCOL_MISMATCH;
}
// get params
FileName fileHash = request.getFileName();
CrailNodeType type = request.getFileType();
boolean writeable = type.isDirectory() ? false : true;
int storageClass = request.getStorageClass();
int locationClass = request.getLocationClass();
boolean enumerable = request.isEnumerable();
// check params
if (type.isContainer() && locationClass > 0) {
return RpcErrors.ERR_DIR_LOCATION_AFFINITY_MISMATCH;
}
// rpc
AbstractNode parentInfo = fileTree.retrieveParent(fileHash, errorState);
if (errorState.getError() != RpcErrors.ERR_OK) {
return errorState.getError();
}
if (parentInfo == null) {
return RpcErrors.ERR_PARENT_MISSING;
}
if (!parentInfo.getType().isContainer()) {
return RpcErrors.ERR_PARENT_NOT_DIR;
}
if (storageClass < 0) {
storageClass = parentInfo.getStorageClass();
}
if (locationClass < 0) {
locationClass = parentInfo.getLocationClass();
}
AbstractNode fileInfo = fileTree.createNode(fileHash.getFileComponent(), type, storageClass, locationClass, enumerable);
try {
AbstractNode oldNode = parentInfo.putChild(fileInfo);
if (oldNode != null && oldNode.getFd() != fileInfo.getFd()) {
appendToDeleteQueue(oldNode);
}
} catch (Exception e) {
return RpcErrors.ERR_FILE_EXISTS;
}
fileTable.put(fileInfo.getFd(), fileInfo);
NameNodeBlockInfo fileBlock = blockStore.getBlock(fileInfo.getStorageClass(), fileInfo.getLocationClass());
if (fileBlock == null) {
return RpcErrors.ERR_NO_FREE_BLOCKS;
}
if (!fileInfo.addBlock(0, fileBlock)) {
return RpcErrors.ERR_ADD_BLOCK_FAILED;
}
NameNodeBlockInfo parentBlock = null;
if (fileInfo.getDirOffset() >= 0) {
int index = CrailUtils.computeIndex(fileInfo.getDirOffset());
parentBlock = parentInfo.getBlock(index);
if (parentBlock == null) {
parentBlock = blockStore.getBlock(parentInfo.getStorageClass(), parentInfo.getLocationClass());
if (parentBlock == null) {
return RpcErrors.ERR_NO_FREE_BLOCKS;
}
if (!parentInfo.addBlock(index, parentBlock)) {
blockStore.addBlock(parentBlock);
parentBlock = parentInfo.getBlock(index);
if (parentBlock == null) {
blockStore.addBlock(fileBlock);
return RpcErrors.ERR_CREATE_FILE_FAILED;
}
}
}
parentInfo.incCapacity(CrailConstants.DIRECTORY_RECORD);
}
if (writeable) {
fileInfo.updateToken();
response.shipToken(true);
} else {
response.shipToken(false);
}
response.setParentInfo(parentInfo);
response.setFileInfo(fileInfo);
response.setFileBlock(fileBlock);
response.setDirBlock(parentBlock);
if (CrailConstants.DEBUG) {
LOG.info("createFile: fd " + fileInfo.getFd() + ", parent " + parentInfo.getFd() + ", writeable " + writeable + ", token " + fileInfo.getToken() + ", capacity " + fileInfo.getCapacity() + ", dirOffset " + fileInfo.getDirOffset());
}
return RpcErrors.ERR_OK;
}
Aggregations