Design an in-memory file system 00:00:00 Make a prediction You will resolve paths one segment at a time and reject a move that would make a directory its own ancestor. A directory named home contains sun. Someone moves home inside sun. If accepted, what happens the next time a recursive listing follows the children? It can loop through home and sun indefinitely. Refuse the move before modifying the original or destination parent. Check ancestry as well as name conflicts. 00:00:31 The box called all Nested boxes labelled home and trips can each hold a cap with the same name because the owning boxes differ. Moving one whole box preserves all its contents; putting an outer box inside its own inner box cannot work. Resolve names relative to their parent directory. Distinguish files from directories and preserve the tree invariant during moves. A file system gives names to pieces of data and arranges the names in a tree. An in-memory one keeps all of it in objects of a running program. Nothing survives a restart, and that is the point: no disk, no blocks, only the rules. A tree with two kinds of nodes is one of the most common shapes in software: menus, organisation charts, documents, scenes in a game, components on a page. Here everybody knows the rules already, so the talk is about objects from the first minute. 00:01:28 Resolve a path one segment at a time To read the path slash notes slash plan, begin at the root directory and resolve notes in its children map. It must be a directory before traversal continues. Look up plan inside that directory. A file owns content; a directory owns named children. A request for slash notes slash plan slash draft must fail if plan is a file. Never silently treat a file as an empty directory. Resolve home from the root, then sun from home, then photo from sun. If sun is a file, the final step must fail because a file cannot contain the next component. An implementation that treats every path as one unstructured string can accidentally accept a nonexistent directory relationship. 00:02:21 Rename without losing the subtree Moving a directory beneath one of its own descendants would create a cycle. Check the destination ancestry before mutating either parent. If the move is refused, a listing at the original path should still show all its children. Check whether the destination is below the source before changing either parent. Moving home under its own sun directory creates a cycle and tree traversal may never terminate. After rejection, list the original parent and verify that the entire subtree is still reachable there. If a path parser silently treats a file as an empty directory, a later write can replace or hide existing contents. Test walking past a known file. A flat map keyed by full path may suit simple reads, but renaming an entire subtree then has a different cost and consistency burden. tmpfs on Linux. Keeps all files in virtual memory. Unmount it and everything is gone. It can be limited by size and by the number of inodes, the two limits of our page on sizes. Jimfs for Java. An in-memory file system by Google behind the standard java.nio.file interfaces. It can behave like a Unix or a Windows system, which is useful to test path code for both. 00:03:40 Try an interview follow-up How would it survive a crash? I would record the intended change in a durable log before acknowledging success. Replay uses validated records and stops at a torn or corrupt entry. To checkpoint, write and persist a complete snapshot, install it safely, and only then discard the log portion it covers. A failed checkpoint must leave a recoverable previous state. A reader has an open file when another client removes its directory entry. Must the next read through that handle fail? Ask which file-system contract is intended. Under Unix-style semantics the name can disappear while the existing open handle still refers to the file. Track namespace entries and open references separately if that is the promise. 00:04:33 What to take with you Two kinds of nodes share a small base: a name, a parent, a size. A directory keeps a map from name to node and is the only one that sets the parent. One class, the file system, turns paths into nodes and holds the rules. Two helpers carry the code. walk follows the parts and knows what went wrong. parent and name gives the place where something will be created, removed or moved.