server

package
v0.0.0-...-23774e7 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotLeader is returned by a server when an operation is
	// submitted to it but it is not the leader. The client should
	// try submitting the operation to a different server.
	ErrNotLeader = status.Error(
		codes.FailedPrecondition,
		"keyValueServer: this node is not the leader",
	)

	// ErrTimeout is returned when a submitted operation times out.
	// This typically occurs if there are network partitions. The
	// client should try submitting the operation to a different server.
	ErrTimeout = status.Error(
		codes.Unavailable,
		"keyValueServer: the submitted operation timed out",
	)
)

Functions

This section is empty.

Types

type KeyValueStore

type KeyValueStore struct {
	// contains filtered or unexported fields
}

KeyValueStore is a data structure that stores key-value pairs.

func NewKeyValueStore

func NewKeyValueStore() *KeyValueStore

NewKeyValueStore creates a new instance of a key-value store.

func (*KeyValueStore) Apply

func (kv *KeyValueStore) Apply(operation *raft.Operation) interface{}

Apply applies an operation to the key-value store.

func (*KeyValueStore) Get

func (kv *KeyValueStore) Get(key string) string

Get returns the value of a key. If the key does not exist, an empty string is returned.

func (*KeyValueStore) NeedSnapshot

func (kv *KeyValueStore) NeedSnapshot(logSize int) bool

NeedSnapshot returns true if a snapshot should be taken of the key-value store and false otherwise.

func (*KeyValueStore) Put

func (kv *KeyValueStore) Put(key string, value string) string

Put sets the value of a key and returns the value it was set to.

func (*KeyValueStore) Restore

func (kv *KeyValueStore) Restore(snapshotReader io.Reader) error

Restore restores the key-value store from a snapshot taken by raft.

func (*KeyValueStore) Snapshot

func (kv *KeyValueStore) Snapshot(snapshotWriter io.Writer) error

Snapshot writes the state of the key-value store to a snapshot file.

type Server

type Server struct {
	pb.UnimplementedKeyValueServer
	// contains filtered or unexported fields
}

Server is a simple key-value server that is replicated using raft.

func NewServer

func NewServer(id string, address string, node *raft.Raft) (*Server, error)

NewServer creates a new Server instance with the provided ID, address, and raft node.

func (*Server) Get

func (s *Server) Get(ctx context.Context, request *pb.Request) (*pb.Response, error)

Get retreives the value of a key.

func (*Server) Put

func (s *Server) Put(ctx context.Context, request *pb.Request) (*pb.Response, error)

Put sets the value of a key.

func (*Server) Start

func (s *Server) Start() error

Start starts the server.

func (*Server) Stop

func (s *Server) Stop()

Stop stops the server.

Jump to

Keyboard shortcuts

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