RoomDataStorage

class RoomDataStorage(karlDao: KarlDao, ioDispatcher: CoroutineDispatcher = Dispatchers.IO) : DataStorage(source)

Room-based implementation of the DataStorage interface for the KARL framework.

This class provides a robust, production-ready data persistence layer using Android's Room database library. It implements the complete DataStorage contract with support for user data isolation, efficient querying, and transactional safety guarantees.

Database Architecture:

  • Entity-Based Design: Uses Room entities to map domain models to SQL tables

  • DAO Pattern: Leverages Data Access Objects for type-safe database operations

  • Transaction Support: Ensures atomic operations for data consistency

  • Migration Support: Built-in schema versioning for database evolution

Performance Characteristics:

  • Thread Safety: All database operations are dispatched to IO thread pool

  • Connection Pooling: Leverages Room's built-in connection management

  • Query Optimization: Uses indexed queries for efficient data retrieval

  • Memory Efficiency: Minimal object allocation through entity reuse

Data Isolation: User data is strictly partitioned by userId, ensuring complete privacy isolation between different users of the same application. This supports multi-user scenarios and privacy compliance requirements.

Coroutine Integration: All operations are implemented as suspending functions that work seamlessly with Kotlin coroutines. Database operations are automatically dispatched to the IO dispatcher to prevent blocking the main thread.

Error Handling: The implementation provides robust error handling with appropriate exception propagation and recovery mechanisms for database-related failures.

Since

1.0.0

Author

KARL Development Team

Parameters

karlDao

The Room DAO instance providing type-safe database access operations

ioDispatcher

Coroutine dispatcher for background database operations, defaults to Dispatchers.IO for optimal I/O performance

See also

The interface contract this implementation fulfills

The data access object providing database operations

The Room database configuration and entity definitions

Constructors

Link copied to clipboard
constructor(karlDao: KarlDao, ioDispatcher: CoroutineDispatcher = Dispatchers.IO)

Functions

Link copied to clipboard
open suspend override fun deleteUserData(userId: String)

Permanently removes all stored data for a specific user.

Link copied to clipboard
open suspend override fun initialize()

Initializes the Room-based storage system for operation.

Link copied to clipboard
open suspend override fun loadContainerState(userId: String): KarlContainerState?

Retrieves the most recently saved learning state for a specific user.

Link copied to clipboard
open suspend override fun loadRecentInteractionData(userId: String, limit: Int, type: String?): List<InteractionData>

Retrieves recent user interactions for contextual prediction generation.

Link copied to clipboard
open suspend override fun release()

Releases database resources and connections for clean shutdown.

Link copied to clipboard
open suspend override fun saveContainerState(userId: String, state: KarlContainerState)

Persists the current learning state for a specific user with transactional safety.

Link copied to clipboard
open suspend override fun saveInteractionData(data: InteractionData)

Persists individual user interaction data for context and analysis.