Cloud Storage System
Design a cloud-based file storage system that supports three operations: adding a new file, copying an existing file to a new name, and querying the size of a stored file. The file names are arbitrary strings that do not imply any hierarchical directory structure, even if they contain the / character.
boolean addFile(String name, int size) – attempts to insert a new file with the given name and size (in bytes).
name already exists in the system.true when the file is successfully added, and false otherwise.boolean copyFile(String nameFrom, String nameTo) – attempts to duplicate the file identified by nameFrom under a new name nameTo.
nameFrom does not refer to an existing file, or if it points to a directory.nameTo.true when the copy succeeds, and false otherwise.int getFileSize(String name) – returns the size in bytes of the file with the given name if it exists; otherwise returns -1.
Constraints:
name is a non-empty string that may contain any visible ASCII characters, including /.name is treated as a distinct file.10^5 operations will be performed.size fit within a 32-bit signed integer.Example
Input:
["CloudStorage", "addFile", "copyFile", "copyFile", "addFile", "copyFile", "getFileSize", "getFileSize"]
[[], ["/dir1/dir2/file.txt", 10], ["/not-existing.file", "/dir1/file.txt"], ["/dir1/dir2/file.txt", "/dir1/file.txt"], ["/dir1/file.txt", 15], ["/dir1/file.txt", "/dir1/dir2/file.txt"], ["/dir1/file.txt"], ["/not-existing.file"]]
Output:
[null, true, false, true, false, false, 10, -1]
Explanation:
CloudStorage cloudStorage = new CloudStorage();cloudStorage.addFile("/dir1/dir2/file.txt", 10); // returns true; creates a 10-byte file at /dir1/dir2/file.txt.cloudStorage.copyFile("/not-existing.file", "/dir1/file.txt"); // returns false; source file does not exist.cloudStorage.copyFile("/dir1/dir2/file.txt", "/dir1/file.txt"); // returns true; copies the file to /dir1/file.txt (size remains 10 bytes).cloudStorage.addFile("/dir1/file.txt", 15); // returns false; /dir1/file.txt already exists.cloudStorage.copyFile("/dir1/file.txt", "/dir1/dir2/file.txt"); // returns false; destination file already exists.cloudStorage.getFileSize("/dir1/file.txt"); // returns 10, the size of the file.cloudStorage.getFileSize("/not-existing.file"); // returns -1 because the file does not exist.Constraints:
name is a non-empty string and may include any visible ASCII characters (including /).name is treated as a distinct file.10^5 operations will be performed.size fit within a 32-bit signed integer.