~learn.
0:00 / 1:08
LLD · File System

du at any depth, with no type checks

transcript

Disk usage on home: total bytes, however deep it goes. One method answers it, at any depth. No type checks anywhere.

Make file and directory unrelated, and you type-check every level. A file? Its length. A directory? Loop and recurse. Again in find, copy, count. Instead, both are nodes with a size method. A file returns its length. A directory returns the sum of its children's sizes.

Ask the root its size. It asks home. Home asks asha. Asha asks notes and the readme. The files answer: 24 bytes, and 7 bytes. The sums come back up. Notes 24. Asha 31. Home and root, 31.

Notice the size isn't stored anywhere. It's computed. So every write stays order one. If du got hot, you'd cache sizes. Mention it. Don't build it.

One interface for both. Size recurses at any depth. A new node type, like a symlink? It just implements the interface.

Full breakdown in the lecture.