🔍 Code Extractor

function get_document_v7

Maturity: 44

Retrieves a document by its ID from an in-memory cache or loads it from persistent storage if not cached.

File:
/tf/active/vicechatdev/vice_ai/complex_app.py
Lines:
578 - 592
Complexity:
moderate

Purpose

This function implements a two-tier document retrieval system with thread-safe access. It first checks an in-memory cache (app_state['documents']) for the requested document. If not found in memory, it attempts to load the document from persistent storage using load_document_from_file(), caches it in memory for future access, and returns it. Returns None if the document is not found in either location. The function uses a lock to ensure thread-safe concurrent access to the document cache.

Source Code

def get_document(doc_id):
    """Get a document by ID"""
    with app_state['locks']['documents']:
        # Try memory first
        document = app_state['documents'].get(doc_id)
        if document:
            return document
        
        # If not in memory, try loading from file
        document = load_document_from_file(doc_id)
        if document:
            app_state['documents'][doc_id] = document
            return document
        
        return None

Parameters

Name Type Default Kind
doc_id - - positional_or_keyword

Parameter Details

doc_id: A unique identifier for the document to retrieve. Expected to be a string or UUID that uniquely identifies a document in the system. This ID is used to look up the document in the cache and as a parameter to load_document_from_file() if the document needs to be loaded from storage.

Return Value

Returns a document object if found (type depends on the document structure used by the application, likely a dictionary or custom object containing document data). Returns None if the document with the specified doc_id is not found in either the in-memory cache or persistent storage.

Dependencies

  • threading

Required Imports

from threading import Lock

Usage Example

import threading
from threading import Lock

# Initialize app_state
app_state = {
    'locks': {
        'documents': Lock()
    },
    'documents': {}
}

# Define load_document_from_file function (stub)
def load_document_from_file(doc_id):
    # Implementation to load from file system or database
    return {'id': doc_id, 'content': 'Document content'}

# Use get_document
doc_id = 'doc_12345'
document = get_document(doc_id)

if document:
    print(f"Document found: {document}")
else:
    print("Document not found")

Best Practices

  • Ensure app_state is properly initialized before calling this function
  • The lock mechanism prevents race conditions when multiple threads access documents simultaneously
  • Documents are cached in memory after first load to improve performance on subsequent accesses
  • Always check if the returned value is None before using the document
  • Consider implementing cache eviction strategies if memory usage becomes a concern
  • Ensure load_document_from_file() handles errors gracefully and returns None for missing documents
  • The function assumes load_document_from_file() is thread-safe or handles its own locking

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_uploaded_document 69.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_uploaded_document_v1 68.4% 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 save_document 62.5% similar

    Saves a document object to both in-memory application state and persistent file storage, updating its timestamp in the process.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function get_user_documents 61.6% 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 load_document_from_file 61.5% similar

    Loads a document from a JSON file stored in a documents directory, deserializes it into a ComplexDocument object, and returns it.

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