Coverage for src/dataknobs_fsm/storage/memory.py: 100%

1"""In-memory storage backend for execution history.

2

3 This is a thin wrapper around UnifiedDatabaseStorage that uses

4 dataknobs_data's memory backend.

5"""

6

7 from dataknobs_fsm.core.data_modes import DataHandlingMode

8 from dataknobs_fsm.storage.base import StorageBackend, StorageConfig, StorageFactory

9 from dataknobs_fsm.storage.database import UnifiedDatabaseStorage

10

11

12 class InMemoryStorage(UnifiedDatabaseStorage):

13 """In-memory storage implementation using dataknobs_data's memory backend.

14

15 This storage backend uses dataknobs_data's AsyncMemoryDatabase which

16 provides in-memory storage with support for:

17 - LRU eviction based on max_size

18 - Mode-specific compression for REFERENCE mode

19 - Fast queries with in-memory indexing

20 - Automatic cleanup of old entries

21 """

22

23 def __init__(self, config: StorageConfig):

24 """Initialize in-memory storage.

25

26 Args:

27 config: Storage configuration.

28 """

29 # Ensure we use the memory backend

30 if 'type' not in config.connection_params:

31 config.connection_params['type'] = 'memory'

32

33 # Set memory-specific defaults

34 if 'max_size' not in config.connection_params:

35 config.connection_params['max_size'] = 1000

36

37 # Enable indexing for fast queries

38 if 'enable_indexing' not in config.connection_params:

39 config.connection_params['enable_indexing'] = True

40

41 # Configure mode-specific optimizations

42 self._configure_mode_optimizations(config)

43

44 super().__init__(config)

45

46 def _configure_mode_optimizations(self, config: StorageConfig) -> None:

47 """Configure mode-specific optimizations for memory storage.

48

49 Args:

50 config: Storage configuration to modify.

51 """

52 # For REFERENCE mode, enable compression by default

53 if not config.mode_specific_config:

54 config.mode_specific_config = {}

55

56 if DataHandlingMode.REFERENCE not in config.mode_specific_config:

57 config.mode_specific_config[DataHandlingMode.REFERENCE] = {

58 'compress': True,

59 'eviction_policy': 'lru',

60 'cache_size': 100

61 }

62

63 # For DIRECT mode, use minimal storage

64 if DataHandlingMode.DIRECT not in config.mode_specific_config:

65 config.mode_specific_config[DataHandlingMode.DIRECT] = {

66 'store_paths': False,

67 'store_snapshots': False,

68 'max_history': 10 # Keep only last 10 for DIRECT mode

69 }

70

71 # For COPY mode, full storage but with size limits

72 if DataHandlingMode.COPY not in config.mode_specific_config:

73 config.mode_specific_config[DataHandlingMode.COPY] = {

74 'store_paths': True,

75 'store_snapshots': True,

76 'max_size_mb': 100 # Limit total size for COPY mode

77 }

78

79

80# Register memory backend

81 StorageFactory.register(StorageBackend.MEMORY, InMemoryStorage)