Coverage for oc_ds_converter / oc_idmanager / oc_data_storage / redis_manager.py: 88%

51 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-25 18:06 +0000

1# SPDX-FileCopyrightText: 2023 Arianna Moretti <arianna.moretti4@unibo.it> 

2# SPDX-FileCopyrightText: 2023-2026 Arcangelo Massari <arcangelo.massari@unibo.it> 

3# 

4# SPDX-License-Identifier: ISC 

5 

6from oc_ds_converter.oc_idmanager.oc_data_storage.storage_manager import StorageManager 

7from oc_ds_converter.datasource.redis import RedisDataSource 

8import fakeredis 

9 

10class RedisStorageManager(StorageManager): 

11 """A concrete implementation of the ``StorageManager`` interface that persistently stores 

12 the IDs validity values within a REDIS database.""" 

13 

14 def __init__(self, testing=True, config_filepath: str = 'config.ini', **params) -> None: 

15 """ 

16 Constructor of the ``RedisStorageManager`` class. 

17 

18 :param database: The name of the database 

19 :type info_dir: str 

20 """ 

21 if testing: 

22 self.testing = True 

23 self.PROCESS_redis = fakeredis.FakeStrictRedis() 

24 else: 

25 self.testing = False 

26 self.PROCESS_redis = RedisDataSource("PROCESS-DB", config_filepath) 

27 super().__init__(**params) 

28 

29 

30 def set_full_value(self, id_name: str, value: dict) -> None: 

31 """ 

32 It allows to set the counter value of provenance entities. 

33 

34 :param value: The new counter value to be set 

35 :type value: dict 

36 :param id: The id string with prefix 

37 :type id: str 

38 :raises ValueError: if ``value`` is neither 0 nor 1 (0 is False, 1 is True). 

39 :return: None 

40 """ 

41 id_name = str(id_name) 

42 if not isinstance(value, dict): 

43 raise ValueError("value must be dict") 

44 if not isinstance(self.get_value(id_name), bool): 

45 id_val = True if value.get("valid") else False 

46 self.set_value(id_name, id_val) 

47 

48 def set_value(self, id: str, value: bool) -> None : 

49 """ 

50 It allows to set a value for the validity check of an id. 

51 

52 :param value: validity value for the validated id 

53 :type value: bool 

54 :param id: The id string with prefix 

55 :type id: str 

56 :raises ValueError: if ``value`` is neither 0 nor 1 (0 is False, 1 is True). 

57 :return: None 

58 """ 

59 id_name = str(id) 

60 if not isinstance(value, bool): 

61 raise ValueError("value must be int boolean") 

62 validity = 1 if value else 0 

63 self.PROCESS_redis.set(id_name, validity) 

64 

65 

66 def set_multi_value(self, list_of_tuples: list) -> None : 

67 """ 

68 It allows to set a value for the validity check of an id. 

69 

70 :param list_of_tuples: a list of tuples of ids and booleans (id, value) 

71 :type list_of_tuples: list 

72 :return: None 

73 """ 

74 redis_dict = dict() 

75 for t in list_of_tuples: 

76 if t[1] is True: 

77 redis_dict[t[0]] = 1 

78 else: 

79 redis_dict[t[0]] = 0 

80 if redis_dict: 

81 self.PROCESS_redis.mset(redis_dict) 

82 

83 

84 def get_value(self, id: str): 

85 """ 

86 It allows to read the value of the identifier. 

87 

88 :param id: The id name 

89 :type id: str 

90 :return: The requested id value (True if valid, False if invalid, None if not found). 

91 """ 

92 id_name = str(id) 

93 result = self.PROCESS_redis.get(id_name) 

94 if result: 

95 result = int(result.decode("utf-8")) if isinstance(result, bytes) else int(result) 

96 return True if result == 1 else False 

97 return None 

98 

99 def del_value(self, id: str) -> None: 

100 """ 

101 It allows to delete the identifier from the redis db. 

102 

103 :param id: The id name 

104 :type id: str 

105 :return: None 

106 """ 

107 self.PROCESS_redis.delete(id) 

108 

109 

110 def delete_storage(self): 

111 self.PROCESS_redis.flushdb() 

112 

113 def get_all_keys(self): 

114 result = [x for x in self.PROCESS_redis.scan_iter('*')] 

115 if result: 

116 if isinstance(result[0], bytes): 

117 result = {x.decode("utf-8") for x in result} 

118 else: 

119 result = set(result) 

120 else: 

121 result = set() 

122 return result