loadContainerState

open suspend override fun loadContainerState(userId: String): KarlContainerState?

Retrieves the stored KarlContainerState for the specified user from the database.

This method performs a parameterized query to fetch the user's container state and reconstructs the KarlContainerState object from the stored BLOB data and version information.

Query Operation:

SELECT state_data, version FROM container_states WHERE user_id = ?

Data Reconstruction:

  • BLOB Deserialization: Converts stored binary data back to state object

  • Version Restoration: Reconstructs version information for state tracking

  • Null Handling: Returns null if no state exists for the specified user

Performance Characteristics:

  • Primary Key Lookup: O(1) query performance via user_id primary key

  • Memory Efficient: Only loads requested user's data

  • Non-blocking: Executes on Dispatchers.IO for coroutine compatibility

Error Handling & Recovery:

  • SQLException: Database access errors with comprehensive logging

  • Graceful Degradation: Returns null on any error rather than throwing

  • Resource Cleanup: Automatic closure of ResultSet and PreparedStatement

Return Value Semantics:

  • Non-null: Valid KarlContainerState with data and version information

  • Null: No stored state exists for the user OR error occurred during retrieval

Threading Context:

  • All database operations execute on Dispatchers.IO

  • Safe to call from any coroutine context

  • Non-blocking operation suitable for reactive architectures

Usage Patterns:

val state = storage.loadContainerState("user123")
if (state != null) {
// Process loaded state with data and version
println("Loaded state version: ${state.version}")
} else {
// No state exists or error occurred - initialize new state
val newState = KarlContainerState.createDefault()
}

Return

KarlContainerState containing the user's persisted state data and version, or null if no state exists for the user or if an error occurs during retrieval.

Parameters

userId

Unique identifier for the user whose state should be retrieved. Must match the user ID used during saveContainerState operations.

See also

The state data structure being retrieved

Corresponding state persistence operation

Dispatchers.IO

Coroutine context for database operations