Design a system to generate random UUIDs for files in a directory

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-02-09

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

β€’Recursively traverse all files in a given directory (including subdirectories).
β€’Skip hidden/system files (optional).

UUID Generation

β€’Generate a unique UUID for each file using a standard algorithm (e.g., UUIDv4).
β€’Ensure no collisions between files.

Mapping and Storage

β€’Maintain a mapping between file_path and its assigned UUID.
β€’Store this mapping persistently (in-memory, file, or simple DB like SQLite/JSON).

Updates

β€’When a new file is added β†’ assign a new UUID.
β€’When a file is deleted β†’ remove its mapping.
β€’When a file is renamed β†’ update mapping accordingly.

Lookup

β€’Provide a way to lookup a file by UUID and vice versa.

Logging & Error Handling

β€’Log progress and handle cases where directory access fails or permissions are missing.


βš™οΈ 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


1.User provides a directory path β†’ /users/data/photos.
2.System recursively scans all files β†’ photo1.jpg, photo2.png, etc.
3.For each file:
β€’Generate a random UUID (e.g., 550e8400-e29b-41d4-a716-446655440000).
β€’Store the mapping { "photo1.jpg": "550e8400..." } in uuid_map.json.
4.If a file is added/removed β†’ the mapping updates.
5.User can later lookup UUIDs or file paths via the system.


🧠 Design Considerations


UUID Version:

β€’Use UUIDv4 (random-based) for simplicity.
β€’Alternative: UUIDv5 (namespace + filename for deterministic IDs).

Persistence Option:

β€’Local file (e.g., uuid_map.json).
β€’In-memory cache + periodic sync.
β€’Optional: use SQLite or key-value store.

Efficiency:

β€’Use multithreading for large directory scans.
β€’Cache file metadata (hash or last modified time) to detect changes efficiently.


πŸ’‘ 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


β€’Add file content hashing for change detection.
β€’Sync UUID mappings across multiple machines.
β€’Integrate with cloud storage (S3, Google Drive).
β€’REST API for external lookup.
β€’File watcher for real-time updates using OS events.


Solution

Comments