🔍 Code Extractor

function get_uploaded_document_v1

Maturity: 32

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

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
548 - 552
Complexity:
simple

Purpose

This function provides thread-safe access to user-uploaded documents stored in the application state. It uses a lock mechanism to prevent race conditions when multiple threads access the uploaded documents dictionary. The function looks up documents by user email and document ID, returning None if either the user or document doesn't exist.

Source Code

def get_uploaded_document(user_email, document_id):
    """Get a specific uploaded document"""
    with app_state['locks']['uploaded_documents']:
        user_docs = app_state['uploaded_documents'].get(user_email, {})
        return user_docs.get(document_id)

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 the user's document collection in the app_state dictionary. Expected to be a valid email string.

document_id: String or UUID representing the unique identifier of the document to retrieve. This is used as the secondary key within the user's document collection.

Return Value

Returns the document object associated with the given user_email and document_id if found, otherwise returns None. The exact structure of the document object depends on how documents are stored in the application state, but typically includes metadata like filename, upload timestamp, file path, and document content or reference.

Dependencies

  • threading

Required Imports

from threading import Lock

Usage Example

import threading

# Initialize app_state (typically done at application startup)
app_state = {
    'locks': {
        'uploaded_documents': threading.Lock()
    },
    'uploaded_documents': {
        'user@example.com': {
            'doc123': {
                'filename': 'report.pdf',
                'upload_time': '2024-01-15T10:30:00',
                'file_path': '/uploads/doc123.pdf'
            }
        }
    }
}

# Retrieve a document
document = get_uploaded_document('user@example.com', 'doc123')
if document:
    print(f"Found document: {document['filename']}")
else:
    print("Document not found")

# Attempt to retrieve non-existent document
missing_doc = get_uploaded_document('user@example.com', 'doc999')
print(missing_doc)  # Output: None

Best Practices

  • Ensure app_state is properly initialized before calling this function
  • The function uses a context manager (with statement) for automatic lock release, preventing deadlocks
  • Returns None for missing users or documents, so always check the return value before using it
  • This function is read-only and thread-safe, suitable for concurrent access scenarios
  • Consider implementing error handling or logging if document retrieval failures need to be tracked
  • The nested .get() calls ensure no KeyError is raised for missing keys

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_uploaded_document 95.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 get_user_uploaded_documents 87.8% 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 get_user_documents 82.7% similar

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

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function store_uploaded_document 79.7% 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
  • function store_uploaded_document_v1 78.5% similar

    Stores an uploaded document in a thread-safe global application state dictionary, organizing documents by user email and document ID with metadata including name, content, file type, and upload timestamp.

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