Copilot commented on code in PR #1944:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1944#discussion_r2033086122


##########
extensions/python/pythonprocessors/nifiapi/componentstate.py:
##########
@@ -0,0 +1,79 @@
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You under the Apache License, Version 2.0
+#  (the "License"); you may not use this file except in compliance with
+#  the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+from enum import Enum
+from typing import Dict
+from minifi_native import StateManager as CppFlowFile
+
+
+class Scope(Enum):
+    CLUSTER = 1
+    LOCAL = 2
+
+
+class StateMap:
+    def __init__(self, state_map: Dict[str, str]):
+        self.state_map = state_map if state_map is not None else {}
+
+    def getStateVersion(self) -> int:
+        return 1
+
+    def get(self, key) -> str:
+        if key not in self.state_map:
+            return None
+
+        return self.state_map[key]
+
+    def toMap(self) -> Dict[str, str]:
+        return self.state_map
+
+
+class StateManager:
+    """
+    Python class wrapping the StateManager CPP implementation.
+    """
+
+    def __init__(self, cpp_state_manager: CppFlowFile):
+        self.cpp_state_manager = cpp_state_manager
+
+    def setState(self, state: Dict[str, str], scope: Scope) -> bool:
+        try:
+            return self.cpp_state_manager.set(state)
+        except Exception as exception:
+            raise StateException("Set state failed") from exception
+
+    def getState(self, scope: Scope) -> StateMap:
+        try:
+            return StateMap(self.cpp_state_manager.get())
+        except Exception as exception:
+            raise StateException("Get state failed") from exception
+
+    def replace(self, old_state: StateMap, new_values: Dict[str, str], scope: 
Scope) -> bool:
+        try:
+            return self.cpp_state_manager.replace(old_state.toMap(), 
new_values)
+        except Exception as exception:
+            raise StateException("Replace state failed") from exception
+
+    def clear(self, scope: Scope):
+        try:
+            self.cpp_state_manager.clear()
+        except Exception as exception:

Review Comment:
   The 'scope' parameter in the state management methods (e.g., setState, 
getState, replace, clear) is not propagated to the underlying C++ calls. 
Consider clarifying the intended behavior or adding support for scope, and 
update the documentation accordingly.
   ```suggestion
               return self.cpp_state_manager.set(state, scope.value)
           except Exception as exception:
               raise StateException("Set state failed") from exception
   
       def getState(self, scope: Scope) -> StateMap:
           try:
               return StateMap(self.cpp_state_manager.get(scope.value))
           except Exception as exception:
               raise StateException("Get state failed") from exception
   
       def replace(self, old_state: StateMap, new_values: Dict[str, str], 
scope: Scope) -> bool:
           try:
               return self.cpp_state_manager.replace(old_state.toMap(), 
new_values, scope.value)
           except Exception as exception:
               raise StateException("Replace state failed") from exception
   
       def clear(self, scope: Scope):
           try:
               self.cpp_state_manager.clear(scope.value)
   ```



##########
extensions/python/PYTHON.md:
##########
@@ -172,6 +172,7 @@ Due to some differences between the NiFi and MiNiFi C++ 
processors and implement
 - The interface of the `ProcessContext` class is a bit more limited in MiNiFi 
C++ compared to NiFi. The available methods in `ProcessContext` are 
`getProperty`, `getStateManager`, `getName` and `getProperties`.
 - Success relationship is always present in all Python processors even if 
custom relationships are defined in the Python processor with the 
`getRelationships` method.
 - MiNiFi C++ uses a single embedded Python interpreter for all Python 
processors, so the Python processors share the same Python interpreter. This 
means that the Python processors cannot have different Python versions or use 
different Python packages. The Python packages are installed on the system or 
in a single virtualenv that is shared by all Python processors.
+- State manager API is available with the same interface as in NiFi, but 
MiNiFi C++ uses transactional state management, due to this when a state is 
changed in a processor trigger the state cannot be read due to the dirty read 
protection. The state can be read in the next trigger after the state is 
commited at the end of a session.
 

Review Comment:
   The word 'commited' is misspelled; please change it to 'committed'.
   ```suggestion
   - State manager API is available with the same interface as in NiFi, but 
MiNiFi C++ uses transactional state management, due to this when a state is 
changed in a processor trigger the state cannot be read due to the dirty read 
protection. The state can be read in the next trigger after the state is 
committed at the end of a session.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to