## Working with Groups in Cellframe Node The [[Node|Cellframe Node]] Python extensions provide classes and functions for working with groups in the [[Global Database (GDB)|GDB (Global DataBase)]]. GDB is a key-value store where keys can be grouped into tables (groups). Each group represents a unique string in the GDB, and the values are typically arrays of bytes, though other data types can also be stored. > [!INFO] Additional info: > Some groups are subject to synchronization. > - **Global** groups synchronize with all nodes. > - **Local** groups do not synchronize. > - **Netname** groups synchronize within a specific network. > > Networks can contain blacklists and whitelists for groups, which are manually denied or allowed [[Cellframe Node Network Config|to synchronize between nodes]]. ### Overview In this example, we will: 1. Define the group name and keys. 2. Set and retrieve values by key using the `GlobalDB.set` and `GlobalDB.get` methods. 3. Retrieve a list of key-value pairs in a group using the `GlobalDB.grLoad` method. 4. Delete entries from the group using the `GlobalDB.delete` method. ### Code Example Below is an example of working with groups in Cellframe Node using the `GlobalDB` class: ```python from DAP.Core import logIt from CellFrame.Chain import GlobalDB def init(): logIt.notice("Running plugin client") # Define the group name group_name = "local.mygroup" # Set or receive the key and its corresponding value. key1 = "my_key1" value1 = "my_value1".encode("utf-8") key2 = "my_key2" value2 = "my_value2".encode("utf-8") # Set the preset values encoded in bytes. GlobalDB.set(key1, group_name, value1) GlobalDB.set(key2, group_name, value2) # Get the value for the specified keys and decode them back: value_from_group1 = GlobalDB.get(key1, group_name).decode("utf-8") value_from_group2 = GlobalDB.get(key2, group_name).decode("utf-8") logIt.notice(f"Value from group {group_name} key {key1}: {value_from_group1}") logIt.notice(f"Value from group {group_name} key {key2}: {value_from_group2}") # It is possible to view all key-value pairs using the method group_list() # Assuming such method exists, if not, this is a placeholder for the actual implementation # For now, we will simulate it group_list = [element.key for element in GlobalDB.grLoad(group_name)] # Placeholder, replace with actual call to list keys if available logIt.notice(f"Group {group_name} list: {group_list}") # Delete the created entries GlobalDB.delete(key1, group_name) GlobalDB.delete(key2, group_name) logIt.notice(f"Deleted keys {key1} and {key2} from group {group_name}") return 0 ``` ### Explanation - **Defining Group and Keys**: Define the group name and the keys to set and retrieve values. - **Setting Values**: Use `GlobalDB.set(key, group, value)` to store values in the GDB, encoding the values in bytes. - **Retrieving Values**: Use `GlobalDB.get(key, group)` to fetch the stored values, decoding them from bytes back to strings. - **Listing Group Keys**: The `GlobalDB.grLoad(group)` method is used to list all keys in the group. Note that this is a placeholder and should be replaced with the actual method to list keys if available. - **Deleting Entries**: Use `GlobalDB.delete(key, group)` to remove entries from the group.