🔍 Code Extractor

function get_user_documents

Maturity: 34

Thread-safe function that retrieves all documents associated with a specific user email from a global document storage dictionary.

File:
/tf/active/vicechatdev/vice_ai/app.py
Lines:
107 - 110
Complexity:
simple

Purpose

This function provides a safe way to access user-specific documents from a shared data structure in a multi-threaded Flask application. It uses a lock mechanism to prevent race conditions when multiple threads attempt to read the document storage simultaneously. The function is typically used in document management systems where users can upload and retrieve their own documents.

Source Code

def get_user_documents(user_email):
    """Get all documents for a user"""
    with document_lock:
        return uploaded_documents.get(user_email, {})

Parameters

Name Type Default Kind
user_email - - positional_or_keyword

Parameter Details

user_email: String representing the user's email address, used as a key to look up the user's documents in the uploaded_documents dictionary. Expected to be a valid email string format. If the email doesn't exist in the storage, an empty dictionary is returned.

Return Value

Returns a dictionary containing all documents associated with the given user_email. The structure of the returned dictionary depends on how documents are stored in the uploaded_documents global variable. If the user has no documents or the email doesn't exist in the storage, returns an empty dictionary {}. The return type is dict.

Dependencies

  • threading

Required Imports

from threading import Lock

Usage Example

from threading import Lock

# Initialize required global variables
uploaded_documents = {
    'user@example.com': {
        'doc1.pdf': {'size': 1024, 'uploaded_at': '2024-01-01'},
        'doc2.txt': {'size': 512, 'uploaded_at': '2024-01-02'}
    },
    'another@example.com': {
        'report.docx': {'size': 2048, 'uploaded_at': '2024-01-03'}
    }
}
document_lock = Lock()

def get_user_documents(user_email):
    """Get all documents for a user"""
    with document_lock:
        return uploaded_documents.get(user_email, {})

# Usage
user_docs = get_user_documents('user@example.com')
print(user_docs)
# Output: {'doc1.pdf': {'size': 1024, 'uploaded_at': '2024-01-01'}, 'doc2.txt': {'size': 512, 'uploaded_at': '2024-01-02'}}

# Non-existent user
empty_docs = get_user_documents('nonexistent@example.com')
print(empty_docs)
# Output: {}

Best Practices

  • Ensure that 'document_lock' is properly initialized as a threading.Lock() before any calls to this function
  • Ensure that 'uploaded_documents' is initialized as a dictionary before calling this function
  • This function is read-only and thread-safe, making it suitable for concurrent access in web applications
  • The function returns a reference to the actual dictionary stored in uploaded_documents, so modifications to the returned value will affect the original data structure. Consider returning a copy if immutability is required
  • Always validate the user_email parameter before passing it to this function to prevent unauthorized access
  • Consider implementing additional access control checks before calling this function in production environments
  • The lock acquisition is blocking, which could cause performance issues under high concurrency. Consider using read-write locks if read operations significantly outnumber writes

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_user_uploaded_documents 84.5% similar

    Retrieves all uploaded documents associated with a specific user from a thread-safe global application state dictionary.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function store_document 83.9% similar

    Thread-safe function that stores document information (file path, text content, metadata) in a global dictionary indexed by user email and document ID.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function get_uploaded_document_v1 82.7% similar

    Retrieves a specific uploaded document for a given user from a thread-safe global application state dictionary.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function get_uploaded_document 80.1% similar

    Retrieves a specific uploaded document from the application state for a given user and document ID, returning document metadata and content in a thread-safe manner.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function store_uploaded_document 73.5% similar

    Stores uploaded document metadata and content in a thread-safe application state dictionary, organized by user email and document ID.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
← Back to Browse