Design a system to generate random UUIDs for files in a directory
Problem Statement
π― Objective
Design a system that scans through all files in a given directory and assigns a unique, random UUID to each file.
The system should maintain a mapping between the file paths and their UUIDs, ensuring uniqueness and persistence across sessions.
πΉ Context
In many storage systems, each file or object needs a unique identifier for indexing, version control, or metadata tracking.
You are tasked with designing a UUID Assignment System that automatically generates and manages unique identifiers for files present in a given directory structure.
β Functional Requirements
File Scanning
UUID Generation
Mapping and Storage
Updates
Lookup
Logging & Error Handling
βοΈ Non-Functional Requirements
RequirementDescription
Uniqueness
All UUIDs must be globally unique.
Persistence
Mappings should persist between runs.
Scalability
Should handle large directories efficiently.
Performance
Fast traversal and minimal I/O operations.
Extensibility
Easy to integrate with future storage services (e.g., AWS S3).
Thread Safety
Safe UUID generation in parallel environments.
π§© Core Entities & Responsibilities
ClassDescription
FileMetadata
Holds file path, size, last modified time, and UUID.
UUIDGenerator
Responsible for creating unique random identifiers.
DirectoryScanner
Recursively scans the directory for files.
UUIDMappingStore
Stores and retrieves fileβUUID mappings (JSON, DB, etc.).
FileUUIDManager
Orchestrates scanning, UUID generation, and persistence.
Logger
Handles logs and errors.
π§Ύ Sample Class Diagram (Text Representation)
1
2ββββββββββββββββββββββββββββββββ
3β FileUUIDManager β
4ββββββββββββββββββββββββββββββββ
5β + scanDirectory(path) β
6β + assignUUIDs() β
7β + getUUID(filePath) β
8β + getFile(uuid) β
9β + persistMappings() β
10ββββββββββββββββ¬ββββββββββββββββ
11 β
12 βββββββββββββΌβββββββββββββ
13 βΌ βΌ
14ββββββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
15β DirectoryScanner β β UUIDGenerator β
16ββββββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
17β + getAllFiles(path) β β + generateUUID(): str β
18ββββββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββ
19
20ββββββββββββββββββββββββββββββββ
21β UUIDMappingStore β
22ββββββββββββββββββββββββββββββββ
23β + saveMapping(file, uuid) β
24β + loadMappings() β
25β + deleteMapping(file) β
26β + getUUID(filePath) β
27ββββββββββββββββββββββββββββββββ
28
29ββββββββββββββββββββββββββββββββ
30β FileMetadata β
31ββββββββββββββββββββββββββββββββ
32β - filePath β
33β - size β
34β - modifiedAt β
35β - uuid β
36βββββββββββββββββββββββββββββββββοΈ Example Workflow
π§ Design Considerations
UUID Version:
Persistence Option:
Efficiency:
π‘ Example Function Signatures (Python-like Pseudocode)
1
2import uuid, os, json
3
4class UUIDGenerator:
5 def generate_uuid(self):
6 return str(uuid.uuid4())
7
8class DirectoryScanner:
9 def get_all_files(self, directory):
10 files = []
11 for root, _, filenames in os.walk(directory):
12 for name in filenames:
13 files.append(os.path.join(root, name))
14 return files
15
16class UUIDMappingStore:
17 def __init__(self, file_path='uuid_map.json'):
18 self.file_path = file_path
19 self.mappings = self.load_mappings()
20
21 def load_mappings(self):
22 if os.path.exists(self.file_path):
23 with open(self.file_path, 'r') as f:
24 return json.load(f)
25 return {}
26
27 def save_mappings(self):
28 with open(self.file_path, 'w') as f:
29 json.dump(self.mappings, f, indent=4)
30
31class FileUUIDManager:
32 def __init__(self):
33 self.scanner = DirectoryScanner()
34 self.generator = UUIDGenerator()
35 self.store = UUIDMappingStore()
36
37 def assign_uuids(self, directory):
38 files = self.scanner.get_all_files(directory)
39 for f in files:
40 if f not in self.store.mappings:
41 self.store.mappings[f] = self.generator.generate_uuid()
42 self.store.save_mappings()π§± Design Patterns Used
Pattern and Purpose
Singleton
Ensure one instance of FileUUIDManager.
Factory
Create UUID generators (v4, v5, custom).
Strategy
Support different UUID generation strategies.
Observer (optional)
Watch for file system changes (auto-update).
π¬ Example Interview Prompt Summary
Design a system that assigns unique UUIDs to all files in a directory.
The system should generate and persist a mapping between file paths and UUIDs, update it when files are added or removed, and allow lookups in both directions.
Explain how you would ensure uniqueness, persistence, and scalability.
π Possible Extensions