kvcore

package
v0.0.0-...-6f1e108 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DStoreNodeMin = 3
	DStoreNodeMax = 12
)

Variables

View Source
var (

	// Settings
	Port      string
	Host      string
	JsonStore string
	LogFile   string
	RunCLI    bool
	DSNodes   int
)

Functions

func LoadPersistenceData

func LoadPersistenceData(core *KVCore) error

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

func Init(nodeCount int) *KVCore

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

func (core *KVCore) Delete(key string) error

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.

func (*KVCore) Get

func (core *KVCore) Get(key string) (map[string]interface{}, bool)

Get retrieves the value associated with the specified key from the KVCore.

func (*KVCore) GetALlKeys

func (core *KVCore) GetALlKeys() []string

func (*KVCore) GetAll

func (core *KVCore) GetAll() map[string]string

func (*KVCore) Set

func (core *KVCore) Set(key string, value string) error

Set sets the value of a key in the KVCore.

Parameters:

  • key: The key to set.
  • value: The value to set for the key.

Return value:

  • error: If an error occurred during the set operation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL