etlt.dimension.RegularDimension   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 89
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A RegularDimension.get_id() 0 25 2
A RegularDimension.post_call_stored_procedure() 0 10 1
A RegularDimension.pre_call_stored_procedure() 0 7 1
A RegularDimension.__init__() 0 11 1
A RegularDimension.call_stored_procedure() 0 10 1
A RegularDimension.pre_load_data() 0 5 1
1
import abc
2
from typing import Any, Dict, Optional
3
4
5
class RegularDimension(metaclass=abc.ABCMeta):
6
    """
7
    Abstract parent class for translating natural key to a technical key of a regular dimension.
8
    """
9
10
    # ------------------------------------------------------------------------------------------------------------------
11
    def __init__(self):
12
        """
13
        Object constructor.
14
        """
15
16
        self._map: Dict[Any, Optional[int]] = {}
17
        """
18
        The map from natural keys to a technical keys.
19
        """
20
21
        self.pre_load_data()
22
23
    # ------------------------------------------------------------------------------------------------------------------
24
    def get_id(self, natural_key: Any, enhancement: Any = None) -> Optional[int]:
25
        """
26
        Returns the technical ID for a natural key or None if the given natural key is not valid.
27
28
        :param natural_key: The natural key.
29
        :param enhancement: Enhancement data of the dimension row.
30
        """
31
        # If the natural key is known return the technical ID immediately.
32
        if natural_key in self._map:
33
            return self._map[natural_key]
34
35
        # The natural key is not in the map of this dimension. Call a stored procedure for translating the natural key
36
        # to a technical key.
37
        self.pre_call_stored_procedure()
38
        success = False
39
        try:
40
            key = self.call_stored_procedure(natural_key, enhancement)
41
            success = True
42
        finally:
43
            self.post_call_stored_procedure(success)
44
45
        # Add the translation for natural key to technical ID to the map.
46
        self._map[natural_key] = key
47
48
        return key
49
50
    # ------------------------------------------------------------------------------------------------------------------
51
    @abc.abstractmethod
52
    def call_stored_procedure(self, natural_key: Any, enhancement: Any) -> Optional[int]:
53
        """
54
        Calls a stored procedure for getting the technical key of a natural key. Returns the technical ID or None if
55
        the given natural key is not valid.
56
57
        :param natural_key: The natural key.
58
        :param enhancement: Enhancement data of the dimension row.
59
        """
60
        raise NotImplementedError()
61
62
    # ------------------------------------------------------------------------------------------------------------------
63
    def pre_load_data(self) -> None:
64
        """
65
        Can be overridden to preload lookup data from a dimension table.
66
        """
67
        pass
68
69
    # ------------------------------------------------------------------------------------------------------------------
70
    def pre_call_stored_procedure(self) -> None:
71
        """
72
        This method is invoked before call the stored procedure for getting the technical key of a natural key.
73
74
        In a concurrent environment override this method to acquire a lock on the dimension or dimension hierarchy.
75
        """
76
        pass
77
78
    # ------------------------------------------------------------------------------------------------------------------
79
    def post_call_stored_procedure(self, success: bool) -> None:
80
        """
81
        This method is invoked after calling the stored procedure for getting the technical key of a natural key.
82
83
        In a concurrent environment override this method to release a lock on the dimension or dimension hierarchy and
84
        to commit or rollback the transaction.
85
86
        :param success: True: the stored procedure is executed successfully. False: an exception has occurred.
87
        """
88
        pass
89
90
# ----------------------------------------------------------------------------------------------------------------------
91