Documentation
¶
Index ¶
- type HashTable
- type Node
- func (n *Node) DeleteData(key string)
- func (n *Node) GetAllData() map[string]string
- func (n *Node) GetAllKeys() []string
- func (n *Node) GetData(key string) (string, bool)
- func (n *Node) ReadData(filePath string) error
- func (n *Node) SaveData(filename string) error
- func (n *Node) SetData(key string, value string)
- func (n *Node) SyncData(nodeCluster *NodeCluster)
- type NodeCluster
- type NodeType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HashTable ¶
type HashTable struct {
// contains filtered or unexported fields
}
HashTable is a thread-safe hash table implementation.
func NewHashTable ¶
func NewHashTable() *HashTable
NewHashTable returns a new instance of HashTable with an initialized data map. The data map is created using the make() function with the map[string]string type. The function returns a pointer to the HashTable struct.
func (*HashTable) Delete ¶
Delete deletes the value associated with the specified key from the HashTable. It acquires a write lock to ensure thread safety and calls the delete function on the underlying data map to remove the key-value pair.
Parameters:
- key: The key of the value to delete.
Usage example:
ht := NewHashTable() ht.Set("key1", "value1") ht.Delete("key1") value, exists := ht.Get("key1") // exists will be false, and value will be an empty string. assert.False(t, exists) assert.Empty(t, value)
Note:
The Delete method can also be used on a Node's data field as shown in the example below in conjunction with the DeleteData method on the Node: node := &Node{ ID: "node1", Type: Responder, data: NewHashTable(), } node.SetData("key1", "value1") node.DeleteData("key1")
func (*HashTable) Get ¶
Get retrieves the value associated with the specified key from the HashTable. If the key does not exist in the HashTable, the second return value will be false.
func (*HashTable) Set ¶
Set sets the value for the specified key in the hash table. It acquires a lock to ensure thread safety and then updates the value in the underlying data map. Finally, it releases the lock.
Parameters: - key: a string representing the key for which to set the value. - value: a string representing the value to set for the specified key.
Return value: none.
type Node ¶
Node represents a node in a cluster. It contains an ID, a NodeType, a HashTable for data storage, and a sync.RWMutex for thread safety.
func NewNode ¶
NewNode returns a new instance of Node with the specified ID and NodeType. It initializes the data field with a new HashTable using the NewHashTable function. The function returns a pointer to the Node struct.
func (*Node) DeleteData ¶
DeleteData deletes the value associated with the specified key from the Node's data field.
Parameters:
- key: The key of the value to delete.
Note:
This method acquires a write lock to ensure thread safety and calls the Delete method on the Node's data field to remove the key-value pair.
Usage example:
node := &Node{ ID: "node1", Type: Responder, data: NewHashTable(), } node.SetData("key1", "value1") node.DeleteData("key1") value, exists := node.GetData("key1") // exists will be false, and value will be an empty string. assert.False(t, exists) assert.Empty(t, value)
func (*Node) GetAllData ¶
func (*Node) GetAllKeys ¶
func (*Node) GetData ¶
GetData retrieves the value associated with the specified key from the Node's data. It acquires a read lock to ensure thread safety and calls the Get method on the underlying HashTable data structure to retrieve the value. If the key does not exist in the HashTable, the second return value will be false.
Parameters:
- key: A string representing the key for which to retrieve the value.
Return values:
- string: The value associated with the key, if it exists in the HashTable.
- bool: A boolean indicating whether the key exists in the HashTable.
Example usage:
node := NewNode("node1", Responder) node.SetData("key1", "value1") value, exists := node.GetData("key1") // exists will be true, and value will be "value1". fmt.Println(exists, value) value, exists := node.GetData("key2") // exists will be false, and value will be an empty string. fmt.Println(exists, value)
func (*Node) SaveData ¶
SaveData saves the data of the Node to a file specified by the filename parameter. It acquires a read lock to ensure thread safety while retrieving the data from the Node, then marshals the data into a BSON format using the bson.Marshal function. The marshaled data is then written to the file using the os.WriteFile function.
Parameters:
- filename: The name of the file to which the data will be saved.
Return value:
- An error indicating any issue encountered while saving the data, or nil if the operation was successful.
Usage example:
node := &Node{ ID: "node1", Type: Responder, data: NewHashTable(), } err := node.SaveData("test.json") if err != nil { fmt.Printf("Error saving data: %v\n", err) }
Note:
The SaveData method can also be used on a KVCore instance as shown in the example below: core := &KVCore{ nodeCluster: &NodeCluster{}, persistence: nil, cache: &HashTable{}, nodeCount: 0, } err := core.SaveData("test.json") if err != nil { fmt.Printf("Error saving data: %v\n", err) }
func (*Node) SyncData ¶
func (n *Node) SyncData(nodeCluster *NodeCluster)
SyncData synchronizes data between nodes in a given node cluster. It acquires a write lock on the calling node to ensure thread safety. It then iterates over each node in the node cluster and depending on the node type, it either retrieves data from the current node and sets it on the responder nodes, or retrieves data from the responder nodes and sets it on the reader node.
Parameters:
- nodeCluster: Pointer to a NodeCluster object containing the nodes to synchronize.
Return value: none.
type NodeCluster ¶
type NodeCluster struct {
Nodes []*Node
}
NodeCluster represents a collection of Node instances. Nodes field is a slice of Node pointers, containing all the nodes in the cluster.
func NewNodeCluster ¶
func NewNodeCluster() *NodeCluster
NewNodeCluster creates a new instance of NodeCluster with an empty slice of Nodes.
func (*NodeCluster) AddNode ¶
func (nc *NodeCluster) AddNode(node *Node)
AddNode appends a new node to the NodeCluster's Nodes slice. The new node is added at the end of the slice. Parameters:
- node: A pointer to the Node to be added.
Return value: none.
func (*NodeCluster) AddNodes ¶
func (nc *NodeCluster) AddNodes(nodes []*Node)
AddNodes appends multiple nodes to the NodeCluster's slice of Nodes.
func (*NodeCluster) GetReaderNode ¶
func (nc *NodeCluster) GetReaderNode() *Node
GetReaderNode searches through the list of nodes in the NodeCluster and returns the first node with type Reader. If no Reader node is found, it returns nil.
Return value:
- *Node: A pointer to the first Reader node found in the NodeCluster, or nil if not found.
func (*NodeCluster) GetResponderNode ¶
func (nc *NodeCluster) GetResponderNode() *Node
func (*NodeCluster) GetSyncerNode ¶
func (nc *NodeCluster) GetSyncerNode() *Node
GetSyncerNode returns the first node in the NodeCluster that has the type Syncer. If no such node exists, it returns nil.