Design an in-memory file system
Design an in-memory file system with directories and files that supports create, read, write, list and delete by path.
Good first question for: Senior
Requirements to confirm first
- Paths such as /a/b/c.txt; create directories (with parents when asked) and files
- Write and append to a file, and read it back
- List a directory in name order; listing a file returns just that file
- Delete a file or a whole directory tree; move and rename
- Ask: permissions? Size limits? Many threads?
Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.
Show the answer key (write yours first)
Classes and what each one owns
- FileSystem
- Parses paths, walks down from the root and exposes the operations
- Node
- Abstract base: a name, a parent, created and modified times
- Directory
- A Node that holds children by name
- File
- A Node that holds content
- Path
- Value object that splits and validates a path string once
How they connect
- Directory and File both extend Node, and a Directory holds Nodes, so a tree of any depth needs no special case
- FileSystem has exactly one root Directory
- A child keeps a reference to its parent, which makes move and full-path lookups cheap
Design patterns that fit, and why
- Composite
- Files and directories are handled through one Node type; the size of a directory is the sum over its children.
- Visitor
- Search or disk-usage walks added without editing the Node classes, if several such operations are asked for.
Code sketch in Java
The core types and one real method. It is a sketch to talk through, not a full program.
abstract class Node {
final String name; Directory parent;
Node(String name) { this.name = name; }
abstract long size();
}
class File extends Node {
private final StringBuilder content = new StringBuilder();
File(String name) { super(name); }
long size() { return content.length(); }
}
class Directory extends Node {
private final TreeMap<String, Node> children = new TreeMap<>(); // sorted listing for free
Directory(String name) { super(name); }
long size() { long total = 0; for (Node n : children.values()) total += n.size(); return total; }
}
class FileSystem {
private final Directory root = new Directory("");
private Node resolve(String path) { /* split on '/', walk down, fail on a missing part */ }
}Edge cases to raise yourself
- The root path, a trailing slash, a double slash, an empty name
- Creating a file where a directory of that name already exists
- Moving a directory into its own subtree
- Deleting a directory that is not empty: refuse or recurse, by a flag
Where answers usually go wrong
- One map from the full path string to content. Listing a directory and renaming a parent then touch every key.
- Separate code paths for files and directories everywhere. A common Node type removes most of the branching.
- Parsing the path string inside every method. Parse it once into parts and reuse them.
- Forgetting that rename and move must check the destination and must not create a cycle.
Follow-up questions
- Add permissions per user
- Add symbolic links: what breaks in size and delete?
- Make it safe for many threads without one global lock