Imagine we design a simple file system metadata.
There are 2 types of entities: 'file' and 'directory'.
Each entity is given an integer 'entity id' and has a 'name'.
File entities also have a 'size' field of how much space they consume in bytes.
Directory entities have a list of 'children', representing entity ids inside them.
Overall, we have the following structures :
EntityId: int
Entity = {
type: ‘file’ | ‘directory’
name: string
oneof {
size: int
children: [EntityId]
}
}Filesystem: map<EntityId, Entity>Given this the file system:
root (id=1)
dir (id=2)
file1 (id=4): 100b
file2 (id=5): 200b
file3 (id=3): 300bwill be represented as the following:
Filesystem = { 1: { type: 'directory', name: "root", children: [2, 3] }, //600
2: { type: 'directory', name: "dir", children: [4, 5] }, //300
4: { type: 'file', name: "file1", size: 100 },
5: { type: 'file', name: "file2", size: 200 },
3: { type: 'file', name: "file3", size: 300 } }
Q1. Write a function, that given the file system and entity id can return the size of that entity.
Q2. Optimize if we query the same file system for multiple entity ids?
enum entityType {
file="file",
directory="directory"
}class Entity {
entityType type;
string name;
int size;
vector<int> children
}unordered_map<int, Entity> fileSystemMetadata