Documentation ¶
Index ¶
Constants ¶
const ( DStoreNodeMin = 3 DStoreNodeMax = 12 )
Variables ¶
Functions ¶
func LoadPersistenceData ¶
LoadPersistenceData loads persistence data for the KVCore instance. It takes a pointer to a KVCore instance as the parameter. It retrieves the reader node from the node cluster. It checks if the persistence file exists. If the file does not exist, it creates a new file with an empty BSON object. If there is an error creating the empty BSON or file, it logs the error and returns the error. If there is an error checking the file, it logs the error and returns the error. If the file exists, it loads the data from the file using the reader node's ReadData method. If there is an error loading the data, it logs the error and gracefully handles the BSON unmarshalling error by skipping persistence data loading. After loading the data, it retrieves the syncer node from the node cluster and calls its SyncData method to synchronize the data across the node cluster. Finally, it returns nil, indicating that the persistence data loading was successful.
func ParseSettings ¶
func ParseSettings()
Types ¶
type KVCore ¶
type KVCore struct { PersistenceFile string // contains filtered or unexported fields }
KVCore represents the core data structure for key-value storage. It contains a node cluster, a persistence layer, a cache, the count of nodes, and a mutex for synchronization.
func Init ¶
Init is a function that initializes a new instance of KVCore. It takes the following parameters:
- nodeCount: an integer representing the number of nodes in the cluster.
If the nodeCount is less than 3, it is set to 3. It creates a new NodeCluster and adds three initial nodes with predefined nodes (Reader, Syncer, and Responder). If the nodeCount is greater than 3, it adds additional Responder nodes to the cluster. Finally, it creates a new KVCore instance with the NodeCluster, an empty persistence map, a new HashTable for cache, and the nodeCount. It returns a pointer to the KVCore instance.
Example usage:
core := Init(5) ... core.Set("key", "value")
Example output:
Key "key" set to "value" in syncer node Data synchronized across the node cluster Data saved to file
func (*KVCore) Delete ¶
Delete removes the specified key from the KVCore instance. It acquires a lock to ensure thread safety before performing the delete operation. It first retrieves the syncer node from the node cluster and calls its DeleteData method to remove the key. Then it synchronizes the data across the node cluster by calling the SyncData method of the syncer node. Next, it retrieves the reader node from the node cluster and calls its DeleteData method to remove the key. Finally, it saves the updated data to a file named "test.json" by calling the SaveData method of the reader node. The method also logs the deletion of the key by printing the key value to the console. It returns an error of type error.
Parameters:
- key: The key to delete.
Return value:
- error: If an error occurred during the delete operation.