DiskMirror icon indicating copy to clipboard operation
DiskMirror copied to clipboard

【bug】【<= 1.2.0】《程序启动之后,最先调用了 remove 函数之后,会导致 useSize 参数的计算出现问题》的解决方案

Open BeardedManZhao opened this issue 10 months ago • 0 comments

错误的信息描述

当程序启动之后,获取到适配器对象,若您最先调用的是 remove 函数,则会导致文件空间使用量的计算的结果数值会偏小,这是因为在计算文件使用量的时候,删除操作的计算时机与其它的不太一样,此问题在 1.2.1 版本中已成功解决!

错误代码示例

package top.lingyuzhao;

import com.alibaba.fastjson2.JSONObject;
import top.lingyuzhao.diskMirror.conf.DiskMirrorConfig;
import top.lingyuzhao.diskMirror.core.Adapter;
import top.lingyuzhao.diskMirror.core.DiskMirror;
import top.lingyuzhao.diskMirror.core.Type;

import java.io.IOException;

@DiskMirrorConfig()
public final class MAIN {
    public static void main(String[] args) throws IOException {
        System.out.println("开始发送数据!");
        // 实例化出 Tcp 客户端适配器
        final Adapter adapter = DiskMirror.LocalFSAdapter.getAdapter(MAIN.class);
        // 直接将 TCP 客户端适配器中的 upload 方法进行调用
        final JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", 1);
        jsonObject.put("type", Type.Binary);
        jsonObject.put("secure.key", 0);
        jsonObject.put("fileName", "test0.jpg");

        // 删除名为 test0.jpg 的文件 这个时候 useSize 会出现问题
        final JSONObject remove = adapter.remove(jsonObject);
        System.out.println(remove);

        // 查看文件结构 这里您可以看到 字节数值为0 但目录里有文件,出现了问题
        jsonObject.remove("fileName");
        final JSONObject urls = adapter.getUrls(jsonObject);
        System.out.println(urls);
    }
}

下面是输出

开始发送数据!
{"userId":1,"type":"Binary","secure.key":0,"fileName":"test0.jpg","maxSize":134217728,"useSize":0,"res":"ok!!!!"}
{"userId":1,"type":"Binary","secure.key":0,"maxSize":134217728,"useSize":0,"res":"ok!!!!","useAgreement":true,"urls":[{"fileName":"test1.jpg","url":"http://localhost:8080/1/Binary//test1.jpg","lastModified":1713851382691,"size":4237376,"type":"Binary","isDir":false}]}

进程已结束,退出代码为 0

解决方案

解决方案 1 将 remove 放在后面调用

我们只需要确保 remove 的调用不是第一次就可以!例如下面是使用 getUrls 做为的第一个代码,后面不论如何调用,都不会出现问题。

package top.lingyuzhao;

import com.alibaba.fastjson2.JSONObject;
import top.lingyuzhao.diskMirror.conf.DiskMirrorConfig;
import top.lingyuzhao.diskMirror.core.Adapter;
import top.lingyuzhao.diskMirror.core.DiskMirror;
import top.lingyuzhao.diskMirror.core.Type;

import java.io.IOException;

@DiskMirrorConfig()
public final class MAIN {
    public static void main(String[] args) throws IOException {
        System.out.println("开始发送数据!");
        // 实例化出 Tcp 客户端适配器
        final Adapter adapter = DiskMirror.LocalFSAdapter.getAdapter(MAIN.class);
        // 直接将 TCP 客户端适配器中的 upload 方法进行调用
        final JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", 1);
        jsonObject.put("type", Type.Binary);
        jsonObject.put("secure.key", 0);

        // 查看文件结构 这样的话 getUrls 是第一个调用,就不会出现问题了
        jsonObject.remove("fileName");
        final JSONObject urls = adapter.getUrls(jsonObject);
        System.out.println(urls);

        jsonObject.put("fileName", "test0.jpg");
        // 删除名为 test0.jpg 的文件 这里不会有问题了
        final JSONObject remove = adapter.remove(jsonObject);
        System.out.println(remove);

        // 后面不论如何调用都不会有问题了
        jsonObject.remove("fileName");
        final JSONObject urls1 = adapter.getUrls(jsonObject);
        System.out.println(urls1);
    }
}

下面是运行结果

开始发送数据!
{"userId":1,"type":"Binary","secure.key":0,"useSize":8474752,"useAgreement":true,"maxSize":134217728,"urls":[{"fileName":"test0.jpg","url":"http://localhost:8080/1/Binary//test0.jpg","lastModified":1713851382691,"size":4237376,"type":"Binary","isDir":false},{"fileName":"test1.jpg","url":"http://localhost:8080/1/Binary//test1.jpg","lastModified":1713851382691,"size":4237376,"type":"Binary","isDir":false}],"res":"ok!!!!"}
{"userId":1,"type":"Binary","secure.key":0,"useSize":4237376,"useAgreement":true,"maxSize":134217728,"urls":[{"fileName":"test0.jpg","url":"http://localhost:8080/1/Binary//test0.jpg","lastModified":1713851382691,"size":4237376,"type":"Binary","isDir":false},{"fileName":"test1.jpg","url":"http://localhost:8080/1/Binary//test1.jpg","lastModified":1713851382691,"size":4237376,"type":"Binary","isDir":false}],"res":"ok!!!!","fileName":"test0.jpg"}
{"userId":1,"type":"Binary","secure.key":0,"useSize":4237376,"useAgreement":true,"maxSize":134217728,"urls":[{"fileName":"test1.jpg","url":"http://localhost:8080/1/Binary//test1.jpg","lastModified":1713851382691,"size":4237376,"type":"Binary","isDir":false}],"res":"ok!!!!"}

进程已结束,退出代码为 0

解决方案 2 手动调用 getUseSize 以进行提前的初始化

package top.lingyuzhao;

import com.alibaba.fastjson2.JSONObject;
import top.lingyuzhao.diskMirror.conf.DiskMirrorConfig;
import top.lingyuzhao.diskMirror.core.Adapter;
import top.lingyuzhao.diskMirror.core.DiskMirror;
import top.lingyuzhao.diskMirror.core.Type;

import java.io.IOException;

@DiskMirrorConfig()
public final class MAIN {
    public static void main(String[] args) throws IOException {
        System.out.println("开始发送数据!");
        // 实例化出 Tcp 客户端适配器
        final Adapter adapter = DiskMirror.LocalFSAdapter.getAdapter(MAIN.class);
        // 直接将 TCP 客户端适配器中的 upload 方法进行调用
        final JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", 1);
        jsonObject.put("type", Type.Binary);
        jsonObject.put("secure.key", 0);
        jsonObject.put("fileName", "test0.jpg");

        // 手动调用下这个函数 也可以避免问题的发生
        final long useSize = adapter.getUseSize(jsonObject);

        // 删除名为 test0.jpg 的文件 这里不会有问题了
        final JSONObject remove = adapter.remove(jsonObject);
        System.out.println(remove);

        // 后面不论如何调用都不会有问题了
        jsonObject.remove("fileName");
        final JSONObject urls1 = adapter.getUrls(jsonObject);
        System.out.println(urls1);
    }
}

下面是输出

开始发送数据!
{"userId":1,"type":"Binary","secure.key":0,"fileName":"test0.jpg","maxSize":134217728,"useSize":4237376,"res":"ok!!!!"}
{"userId":1,"type":"Binary","secure.key":0,"maxSize":134217728,"useSize":4237376,"res":"ok!!!!","useAgreement":true,"urls":[{"fileName":"test1.jpg","url":"http://localhost:8080/1/Binary//test1.jpg","lastModified":1713851382691,"size":4237376,"type":"Binary","isDir":false}]}

进程已结束,退出代码为 0

解决方案 3 更新到 1.2.1

在 1.2.1 版本中,已经进行了修复!

BeardedManZhao avatar Apr 23 '24 06:04 BeardedManZhao