function remove_uploaded_document
Removes a specific uploaded document from the application state for a given user, with thread-safe locking and automatic cleanup of empty user entries.
/tf/active/vicechatdev/vice_ai/complex_app.py
213 - 222
simple
Purpose
This function manages the deletion of uploaded documents from a shared application state dictionary. It ensures thread-safe removal of documents by using a lock mechanism, and automatically cleans up user entries when they have no remaining documents. This is typically used in document management systems where users can upload and remove files, and the application needs to track these documents in memory.
Source Code
def remove_uploaded_document(user_email, document_id):
"""Remove uploaded document"""
with app_state['locks']['uploaded_documents']:
if user_email in app_state['uploaded_documents'] and document_id in app_state['uploaded_documents'][user_email]:
del app_state['uploaded_documents'][user_email][document_id]
# Clean up empty user entry
if not app_state['uploaded_documents'][user_email]:
del app_state['uploaded_documents'][user_email]
logger.info(f"Removed uploaded document {document_id} for user {user_email}")
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
user_email |
- | - | positional_or_keyword |
document_id |
- | - | positional_or_keyword |
Parameter Details
user_email: String representing the user's email address, used as the primary key to identify which user's documents to access in the app_state dictionary. Expected to be a valid email string that exists as a key in app_state['uploaded_documents'].
document_id: String or UUID representing the unique identifier of the document to be removed. This should correspond to a document ID that exists under the specified user's document collection in app_state['uploaded_documents'][user_email].
Return Value
This function does not return any value (implicitly returns None). It performs side effects by modifying the global app_state dictionary and logging the removal operation.
Dependencies
loggingthreading
Required Imports
import logging
from threading import Lock
Usage Example
import logging
from threading import Lock
# Setup required global state
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
app_state = {
'uploaded_documents': {
'user@example.com': {
'doc123': {'filename': 'report.pdf', 'size': 1024},
'doc456': {'filename': 'data.csv', 'size': 2048}
}
},
'locks': {
'uploaded_documents': Lock()
}
}
# Remove a document
remove_uploaded_document('user@example.com', 'doc123')
# After removal, app_state['uploaded_documents']['user@example.com'] will only contain 'doc456'
# If all documents are removed, the user entry is also deleted
remove_uploaded_document('user@example.com', 'doc456')
# Now 'user@example.com' key is removed from app_state['uploaded_documents']
Best Practices
- Always ensure app_state is properly initialized before calling this function to avoid KeyError exceptions
- The function uses a lock for thread safety, so it's safe to call from multiple threads concurrently
- The function silently succeeds if the user_email or document_id doesn't exist, so check existence beforehand if you need to handle missing documents differently
- Consider implementing error handling around this function if you need to notify users when document removal fails
- The automatic cleanup of empty user entries helps prevent memory leaks in long-running applications
- Ensure the logger is properly configured before calling this function to capture removal events
- If documents have associated files on disk, remember to delete those separately as this function only removes the metadata from app_state
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function remove_uploaded_document_v1 94.8% similar
-
function clear_user_uploaded_documents 88.0% similar
-
function remove_user_document 82.8% similar
-
function get_user_uploaded_documents 79.7% similar
-
function get_uploaded_document_v1 75.2% similar