package com.mx.hbasefile.hadoop.hdfs.api;

import com.mx.hbasefile.hadoop.hdfs.boot.HdfsConfigProperties;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName HdfsTemplate
 * @Author zzrdark
 * @Date 2019-08-14 17:52
 * @Description TODO
 **/

public class HdfsTemplate implements HdfsOperations {

    private FileSystem fileSystem;

    private HdfsConfigProperties hdfsConfigProperties;


    public HdfsTemplate(FileSystem fileSystem, HdfsConfigProperties hdfsConfigProperties) {
        this.fileSystem = fileSystem;
        this.hdfsConfigProperties = hdfsConfigProperties;
    }

    @Override
    public void saveFile(String dir, String name, InputStream input, long length, short replication) throws IOException {
        //查看dir是否存在 不存在则新增路劲
        Path dirPath = new Path(dir);
        if(!fileSystem.exists(dirPath)){
            if (!fileSystem.mkdirs(dirPath, FsPermission.getDirDefault())){
                //TODO 文件路劲新增失败
                throw new IOException("文件路劲新增失败");
            }
        }

        Long defaultBlockSize = hdfsConfigProperties.getDefaultBlockSize();

        //判断文件大小 设定 blockSize
        Path filePath = new Path(dir + "/" + name);
        long blockSize = length <= defaultBlockSize/2 ? defaultBlockSize/2 : defaultBlockSize;
        //设置默认权限
        FSDataOutputStream fsdos = fileSystem.create(filePath,true,hdfsConfigProperties.getBufferSize(),replication,blockSize);
        try {
            fileSystem.setPermission(filePath,FsPermission.getFileDefault());

            byte[] buffer = new byte[512 * 1024];
            int len = -1;

            while ((len=input.read(buffer))!=-1){
                fsdos.write(buffer);
            }
        }finally{
            fsdos.close();
            input.close();
        }

    }

    @Override
    public void deleteFile(String dir, String name) throws IOException {
        Path path = new Path(dir+"/"+name);
        fileSystem.delete(path,false);
    }

    @Override
    public InputStream openFile(String dir, String name) throws IOException {
        Path path = new Path(dir+"/"+name);
        return fileSystem.open(path);
    }

    @Override
    public void mikDir(String dir) throws IOException {
        fileSystem.mkdirs(new Path(dir));
    }

    @Override
    public void deleteDir(String dir) throws IOException {
        fileSystem.delete(new Path(dir), true);

    }

    @Override
    public void moveFile(String src, String dest) throws IOException {
        Path dirPath = new Path(dest);
        if(!fileSystem.exists(dirPath)){
            if (!fileSystem.mkdirs(dirPath, FsPermission.getDirDefault())){
                //TODO 文件路劲新增失败
                throw new IOException("文件路劲新增失败");
            }
        }
//        fileSystem.moveToLocalFile(new Path(src),dirPath);
        fileSystem.rename(new Path(src),dirPath);
    }

}
