[Java] 202012-3 带配额的文件系统 (60分 debugging)

本文介绍了一个简单的文件系统模拟实现,包括文件创建、删除及配额设置等功能。通过Java代码展示了如何构建目录和文件结构,以及如何进行基本的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.*;
import java.util.*;

class FileSystem
{
    private class Node 
    {
        // File
        private long filesize;

        // Directory
        private long LD;
        private long LR;
        private long LD_size;
        private long LR_size;
        private Map<String, Node> map;

        // Common
        private Node father;
        private boolean isDir;

        public Node(Node father, boolean isDir, long LD, long LR, Map<String, Node> map)
        {
            this.father = father;
            this.isDir = isDir;
            this.LD = LD;
            this.LR = LR;
            this.map = map;
        }
        public boolean contains(String child)
        {
            if (map.containsKey(child))
            {
                return true;
            }
            return false;
        }
        public Node get(String child)
        {
            return map.get(child);
        }
        public void put(String child, Node node)
        {
            map.put(child, node);
            return;
        }
        public void remove(String child)
        {
            map.remove(child);
            return;
        }
        public void setL(long LD, long LR)
        {
            this.LD = LD;
            this.LR = LR;
        }
    }

    private Node root;
    public FileSystem()
    {
        this.root = new Node(null, true, 0, 0, new HashMap<>());
    }
    
    public boolean create(String file_path, long file_size)
    {
        Node r = this.root;
        String[] path = file_path.split("/");
        for (int i = 0; i < path.length; i++)
        {
            String child = path[i];
            // file
            if (i == path.length - 1) 
            {
                if (r.isDir && r.contains(child))
                {
                    r = r.get(child);
                    if (r.isDir)
                    {
                        return false;
                    }
                    else
                    {
                        remove(file_path);
                        create(file_path, file_size);
                        return true;
                    }
                }
                else if (r.isDir)
                {
                    Node newNode = new Node(r, false, 0, 0, null);
                    newNode.filesize = file_size;
                    if (r.LD != 0 && r.LD < r.LD_size + file_size)
                    {
                        return false;
                    }
                    for (Node temp_r = r; temp_r != null; temp_r = temp_r.father)
                    {
                        if (temp_r.LR != 0 && temp_r.LR < temp_r.LR_size + file_size) {
                            return false;
                        }
                    }
                    r.LD_size += file_size;
                    for (Node temp_r = r; temp_r != null; temp_r = temp_r.father)
                    {
                        temp_r.LR_size += file_size;
                    }
                    r.put(child, newNode);
                }
            }
            // direcoty
            else 
            {
                if (r.isDir && r.contains(child)) 
                {
                    r = r.get(child);
                    if (!r.isDir)
                    {
                        return false;
                    }
                }
                else
                {
                    Node newNode = new Node(r, true, 0, 0, new HashMap<>());
                    r.put(child, newNode);
                    r = newNode;
                }
            }
        }
        return true;
    }

    public boolean remove(String file_path)
    {
        Node r = this.root;
        String[] path = file_path.split("/");
        for (int i = 0; i < path.length; i++)
        {
            String child = path[i];
            // the file to remove
            if (i == path.length - 1)
            {
                if (r.isDir && r.contains(child))
                {
                    Node node = r.get(child);
                    long size = 0;
                    if (node.isDir)
                    {
                        size = node.LR_size;
                    }
                    else
                    {
                        size = node.filesize;
                        r.LD_size -= size;
                    }
                    for (Node temp_r = r; temp_r != null; temp_r = temp_r.father)
                    {
                        temp_r.LR_size -= size;
                    }
                    r.remove(child);
                }
            }
            else
            {
                if (r.isDir && r.contains(child))
                {
                    r = r.get(child);
                }
                else
                {
                    break;
                }
            }
        }
        return true;
    }

    public boolean quota(String file_path, long LD, long LR)
    {
        Node r = this.root;
        String[] path = file_path.split("/");
        if (path.length == 0)
        {
            if (LD != 0 && LD < r.LD_size) {
                return false;
            }
            if (LR != 0 && LR < r.LR_size) {
                return false;
            }
        }
        else
        {
            for (int i = 0; i < path.length; i++)
            {
                String child = path[i];
                if (r.contains(child))
                {
                    r = r.get(child);
                    if (!r.isDir)
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            if (LD != 0 && r.LD_size > LD || LR != 0 && r.LR_size > LR)
            {
                return false;
            }
            r.setL(LD, LR);
        }
        return true;
    }

}

public class Main
{
    public Main() throws IOException
    {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

        FileSystem fs = new FileSystem();
        int n = Integer.parseInt(f.readLine());
        StringTokenizer st = null;
        while ((n--) > 0)
        {
            st = new StringTokenizer(f.readLine());
            boolean succeed = false;
            String op = st.nextToken();
            String file_path = st.nextToken();
            switch(op)
            {
                case "C":
                    long file_size = Integer.parseInt(st.nextToken());
                    succeed = fs.create(file_path, file_size);
                    break;
                case "R":
                    succeed = fs.remove(file_path);
                    break;
                case "Q":
                    long LD = Long.parseLong(st.nextToken());
                    long LR = Long.parseLong(st.nextToken());
                    succeed = fs.quota(file_path, LD, LR);
                    break;
            }
            // output
            if (succeed)
            {
                out.println('Y');
            }
            else
            {
                out.println('N');
            }
        }
        out.close();
        f.close();
    }

    public static void main(String[] args) throws IOException 
    {
        new Main();
    }

}

2025-06-28 11:42:45 25/06/28 03:42:45 INFO Worker: Executor app-20250628034119-0000/32 finished with state EXITED message Command exited with code 1 exitStatus 1 2025-06-28 11:42:45 25/06/28 03:42:45 INFO ExternalShuffleBlockResolver: Clean up non-shuffle and non-RDD files associated with the finished executor 32 2025-06-28 11:42:45 25/06/28 03:42:45 INFO ExternalShuffleBlockResolver: Executor is not registered (appId=app-20250628034119-0000, execId=32) 2025-06-28 11:42:45 25/06/28 03:42:45 INFO Worker: Asked to launch executor app-20250628034119-0000/33 for KafkaNumberAdder 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: Changing view acls to: spark 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: Changing modify acls to: spark 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: Changing view acls groups to: 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: Changing modify acls groups to: 2025-06-28 11:42:45 25/06/28 03:42:45 INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: spark; groups with view permissions: EMPTY; users with modify permissions: spark; groups with modify permissions: EMPTY 2025-06-28 11:42:45 25/06/28 03:42:45 INFO ExecutorRunner: Launch command: "/opt/bitnami/java/bin/java" "-cp" "/opt/bitnami/spark/conf/:/opt/bitnami/spark/jars/*" "-Xmx1024M" "-Dspark.driver.port=36357" "-Dspark.ui.port=4041" "-Djava.net.preferIPv6Addresses=false" "-XX:+IgnoreUnrecognizedVMOptions" "--add-opens=java.base/java.lang=ALL-UNNAMED" "--add-opens=java.base/java.lang.invoke=ALL-UNNAMED" "--add-opens=java.base/java.lang.reflect=ALL-UNNAMED" "--add-opens=java.base/java.io=ALL-UNNAMED" "--add-opens=java.base/java.net=ALL-UNNAMED" "--add-opens=java.base/java.nio=ALL-UNNAMED" "--add-opens=java.base/java.util=ALL-UNNAMED" "--add-opens=java.base/java.util.concurrent=ALL-UNNAMED" "--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED" "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED" "--add-opens=java.base/sun.nio.cs=ALL-UNNAMED" "--add-opens=java.base/sun.security.action=ALL-UNNAMED" "--add-opens=java.base/sun.util.calendar=ALL-UNNAMED" "--add-opens=java.security.jgss/sun.security.krb5=ALL-UNNAMED" "-Djdk.reflect.useDirectMethodHandle=false" "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED" "--add-opens=java.base/java.lang=ALL-UNNAMED" "org.apache.spark.executor.CoarseGrainedExecutorBackend" "--driver-url" "spark://CoarseGrainedScheduler@spark-master:36357" "--executor-id" "33" "--hostname" "172.20.0.4" "--cores" "2" "--app-id" "app-20250628034119-0000" "--worker-url" "spark://[email protected]:43009" "--resourceProfileId" "0" worker节点的日志
最新发布
06-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值