Storage Manager¶
manager
¶
Storage manager for coordinating multiple storage backends.
The storage manager provides a unified interface for working with different storage backends and supports features like caching, fallback, and replication.
Classes¶
StorageManager
¶
StorageManager(primary: StorageBackend, secondary: StorageBackend | None = None, use_cache: bool = True, cache_size_mb: float | None = 100.0)
Manager for coordinating multiple storage backends.
Supports: - Primary/secondary backend configuration - In-memory caching for fast access - Automatic fallback to secondary storage - Result migration between backends
Initialize storage manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary
|
StorageBackend
|
Primary storage backend |
required |
secondary
|
StorageBackend | None
|
Optional secondary/backup storage backend |
None
|
use_cache
|
bool
|
Enable in-memory caching |
True
|
cache_size_mb
|
float | None
|
Maximum cache size in MB |
100.0
|
Source code in optiscope/storage/manager.py
Functions¶
save_result
¶
save_result(key: str, result: OptimizationResult, metadata: dict[str, Any] | None = None, replicate: bool = True) -> None
Save result to storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Unique identifier for the result |
required |
result
|
OptimizationResult
|
OptimizationResult to save |
required |
metadata
|
dict[str, Any] | None
|
Optional metadata |
None
|
replicate
|
bool
|
Also save to secondary backend if available |
True
|
Source code in optiscope/storage/manager.py
load_result
¶
load_result(key: str, use_cache: bool = True) -> OptimizationResult
Load result from storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Unique identifier for the result |
required |
use_cache
|
bool
|
Check cache first |
True
|
Returns:
| Type | Description |
|---|---|
OptimizationResult
|
OptimizationResult object |
Source code in optiscope/storage/manager.py
delete_result
¶
Delete result from storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Unique identifier for the result |
required |
from_all
|
bool
|
Delete from all backends (primary, secondary, cache) |
True
|
Source code in optiscope/storage/manager.py
rename_result
¶
Rename a result by changing its key across all configured backends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_key
|
str
|
The current unique identifier for the result. |
required |
new_key
|
str
|
The new unique identifier for the result. |
required |
Source code in optiscope/storage/manager.py
exists_result
¶
Check if result exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Unique identifier for the result |
required |
check_all
|
bool
|
Check all backends (otherwise just primary) |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if result exists |
Source code in optiscope/storage/manager.py
list_results
¶
List result keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str | None
|
Optional prefix filter |
None
|
from_backend
|
str
|
Which backend to query ("primary", "secondary", "cache") |
'primary'
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of result keys |
Source code in optiscope/storage/manager.py
migrate_result
¶
Migrate result between backends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Result key to migrate |
required |
from_backend
|
str
|
Source backend ("primary", "secondary", "cache") |
required |
to_backend
|
str
|
Destination backend ("primary", "secondary", "cache") |
required |
Source code in optiscope/storage/manager.py
sync_backends
¶
Synchronize results between backends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
direction
|
str
|
Sync direction: - "primary_to_secondary": Copy from primary to secondary - "secondary_to_primary": Copy from secondary to primary - "bidirectional": Two-way sync (newer wins) |
'primary_to_secondary'
|
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dictionary with sync statistics |
Source code in optiscope/storage/manager.py
get_storage_info
¶
Get information about all storage backends.
Source code in optiscope/storage/manager.py
clear_cache
¶
clear_all_results
¶
Clear all results from storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_all
|
bool
|
Clear from all backends (primary, secondary, cache) |
True
|
Source code in optiscope/storage/manager.py
close
¶
get_result_storage_type
¶
get_result_storage_type(key: str) -> StorageType | None
Determine the storage type for a given result key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
Unique identifier for the result. |
required |
Returns:
| Type | Description |
|---|---|
StorageType | None
|
The StorageType of the backend where the result is found, or None if not found. |
Source code in optiscope/storage/manager.py
__enter__
¶
__enter__() -> StorageManager
__exit__
¶
to_dict
¶
Serialize the storage manager to a dictionary.
Source code in optiscope/storage/manager.py
from_dict
classmethod
¶
from_dict(data: dict[str, Any]) -> StorageManager
Deserialize a storage manager from a dictionary.
Source code in optiscope/storage/manager.py
Functions¶
create_memory_storage
¶
create_memory_storage(**kwargs: Any) -> MemoryStorage
Create memory storage backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Configuration options |
{}
|
Returns:
| Type | Description |
|---|---|
MemoryStorage
|
MemoryStorage instance |
create_filesystem_storage
¶
create_filesystem_storage(base_path: str | Path, **kwargs: Any) -> FileSystemStorage
Create filesystem storage backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_path
|
str | Path
|
Base directory for storage |
required |
**kwargs
|
Any
|
Configuration options |
{}
|
Returns:
| Type | Description |
|---|---|
FileSystemStorage
|
FileSystemStorage instance |
Source code in optiscope/storage/manager.py
create_database_storage
¶
create_database_storage(connection_string: str, **kwargs: Any) -> DatabaseStorage
Create database storage backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_string
|
str
|
Database connection string |
required |
**kwargs
|
Any
|
Configuration options |
{}
|
Returns:
| Type | Description |
|---|---|
DatabaseStorage
|
DatabaseStorage instance |
Raises:
| Type | Description |
|---|---|
StorageError
|
If database backend not available |
Source code in optiscope/storage/manager.py
create_storage_manager
¶
create_storage_manager(primary_type: str = 'memory', primary_config: dict[str, Any] | None = None, secondary_type: str | None = None, secondary_config: dict[str, Any] | None = None, **manager_kwargs: Any) -> StorageManager
Create storage manager with specified backends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary_type
|
str
|
Type of primary backend ("memory", "filesystem", "database") |
'memory'
|
primary_config
|
dict[str, Any] | None
|
Configuration for primary backend |
None
|
secondary_type
|
str | None
|
Optional type of secondary backend |
None
|
secondary_config
|
dict[str, Any] | None
|
Configuration for secondary backend |
None
|
**manager_kwargs
|
Any
|
Additional manager configuration |
{}
|
Returns:
| Type | Description |
|---|---|
StorageManager
|
StorageManager instance |