package cn.reghao.tnb.oss.store.disk;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author reghao
 * @date 2022-05-23 15:00:59
 */
public class LocalStore {
    // 表示一个磁盘分区
    private final String diskVolume;
    // 磁盘分区上文件存储的目录
    private final String storeDir;
    // 磁盘分区总容量
    private final long totalSpace;
    // 磁盘分区可用容量
    private final AtomicLong availSpace;
    private final long max;

    public LocalStore(String diskVolume, String storeDir, long totalSpace, long availSpace, double maxPercent) {
        this.diskVolume = diskVolume;
        this.storeDir = storeDir;
        this.totalSpace = totalSpace;
        this.availSpace = new AtomicLong(availSpace);
        BigDecimal bigDecimal1 = new BigDecimal(totalSpace*10);
        BigDecimal bigDecimal2 = new BigDecimal(maxPercent*10);
        this.max = bigDecimal1.divide(bigDecimal2, RoundingMode.DOWN).longValue();
    }

    public String getDiskVolume() {
        return diskVolume;
    }

    public String getStoreDir() {
        return storeDir;
    }

    public long getTotalSpace() {
        return totalSpace;
    }

    public long getAvailSpace() {
        return availSpace.get();
    }

    public void setCurrentAvailSpace(long currentAvailable) {
        availSpace.getAndSet(currentAvailable);
    }
    
    @Override
    public int hashCode() {
        int result = 17;
        result = result * 31 + diskVolume.hashCode();
        return result;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }

        if (other instanceof LocalStore) {
            LocalStore o = (LocalStore) other;
            return o.diskVolume.equals(diskVolume);
        } else {
            return false;
        }
    }
}
