keystore

package module
v0.0.0-...-82b166b Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: BSD-3-Clause Imports: 6 Imported by: 0

README

keystore

Go Report Card

An in-memory key/value store written using the Go Language.

Overview

The keystore can be loaded from disk and will be flushed to disk on shutdown. You have the option of including the keystore as a library in an existing project or it can be run independently as a service by executing cmd/keystore. You can choose from the transport layers provided including TCP/UDP/HTTP. The clients are provided to access the store.

Maturity

This is the first stab. I need to add some better fine grained error handling. I will add a request type that will allow you to flush the store to disk.

Installation

For the library simply run go get github.com/landonia/keystore

For the service simply run go get github.com/landonia/keystore/cmd/keystore

Execute Service

Using the provided command to run the keystore as an independent service.

keystore -dataPath ~/keystore/backup -httpAddr 8080 -tcpAddr 8081 -udpAddr 8082

Use as Library

	package main
	
	import (
  		"github.com/landonia/keystore"
  		"github.com/landonia/keystore/transport"
  	)
  	
  	func main() {
  	
  		// Create a key store in disk
  		keystore := keystore.NewService("path/to/file/location")

  		// Bind the network protocols that are required
  		transport.StartHTTPServer(":8080", keystore.RequestChannel)
  		transport.StartTCPServer(":8081", keystore.RequestChannel)
  		transport.StartUDPServer(":8082", keystore.RequestChannel)

  		// Start
  		keystore.Start()

  		// .... wait until application ends and then shutdown and wait
  		<-keystore.Stop()
  	}

Client Library Example

You will find a simple example of using the client transport libraries in cmd/clientexample/main.go

Future

This can be used now but I want to add more request types and much better fine grained error handling (as it will assume any client error is a disconnect type right now). I would also want to add protocol buffer data types to make it easier to create clients in other languages.

About

keystore was written by Landon Wainwright | GitHub.

Follow me on Twitter @landoman!

Documentation

Overview

Package keystore provides an in memory key/value store service library

Package keystore provides an in memory key/value store service library

Package keystore provides an in memory key/value store service library

Package keystore provides an in memory key/value store service library

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyValueStore

type KeyValueStore interface {
	// GetValueType This is used to attempt to parse the value into the interface value provided
	// See https://golang.org/pkg/encoding/json/#Unmarshal for more information
	GetValueType(key string, v interface{}) error

	// GetValue will return the raw value that has been stored
	GetValue(key string) (interface{}, error)

	// GetBool returns a bool for the key specified or if the key does not exist
	// or the value is not of the correct type an error is returned
	GetBool(key string) (interface{}, error)

	// GetInt returns an int for the key specified or if the key does not exist
	// or the value is not of the correct type an error is returned
	GetInt(key string) (interface{}, error)

	// GetFloat returns an float for the key specified or if the key does not exist
	// or the value is not of the correct type an error is returned
	GetFloat(key string) (interface{}, error)

	// GetString returns a string for the key specified or if the key does not exist
	// or the value is not of the correct type an error is returned
	GetString(key string) (interface{}, error)

	// GetArray returns an array type for the key specified or if the key does not exist
	// or the value is not of the correct type an error is returned
	GetArray(key string) (interface{}, error)

	// GetMap returns a map type for the key specified or if the key does not exist
	// or the value is not of the correct type an error is returned
	GetMap(key string) (interface{}, error)

	// SetValue will store an arbitrary type value for the key specified
	SetValue(key string, value interface{}) error

	// SetBool will store an boolean type value for the key specified
	SetBool(key string, value interface{}) error

	// SetInt will store an int type value for the key specified
	SetInt(key string, value interface{}) error

	// SetFloat will store a float type value for the key specified
	SetFloat(key string, value interface{}) error

	// SetString will store a string type value for the key specified
	SetString(key string, value interface{}) error

	// SetArray will store an array type value for the key specified
	SetArray(key string, value interface{}) error

	// SetMap will store a map type value for the key specified
	SetMap(key string, value interface{}) error

	// DeleteKey will delete the key and value from the store
	DeleteKey(key string)
}

KeyValueStore Defines the interface for a simple key value store type. It can be used by both the server and the clients to provide a uniform API that only differs by the transport method

type Op

type Op uint

Op is the operation type for the request to the data store

const (
	READ   Op = 1 << iota // A request to read the value
	WRITE  Op = 1 << iota // A request to write a value
	DELETE Op = 1 << iota // A request to delete the key and value
)

Flags for the request operation type

type Request

type Request struct {
	Op              Op             // The operation required
	Key             string         // The key (used for all requests)
	Value           *ValueHolder   // The request value (used for write requests only)
	ResponseChannel chan *Response // The return channel
}

Request are the requests that will be sent over the channel for operations on the store, such as a read or write

func NewDeleteRequest

func NewDeleteRequest(key string) *Request

NewDeleteRequest will generate a new Request for reading a key

func NewReadRequest

func NewReadRequest(key string, dType Type) *Request

NewReadRequest will generate a new Request for reading a key

func NewWriteRequest

func NewWriteRequest(key string, dType Type, value interface{}) *Request

NewWriteRequest will generate a new Request for reading a key

type Response

type Response struct {
	Success bool         // True if the operation was a success (Error may still be present for write and delete operations)
	Error   string       // Will contain any errors
	Value   *ValueHolder // The response values
}

Response will be returned for each Request containing the result of the operation. If the data type had been specified the value will be contained within the specific value type objects otherwise it will be placed in the arbitrary value field. On a write operation the value returned will be the existing value.

type Service

type Service struct {
	*Sync // Adopt the sync struct
	// contains filtered or unexported fields
}

Service is the wrapper for the in-memory data store service

func NewService

func NewService(filePath string) *Service

NewService will initialise a new keystore If filePath is not an empty string the contents of the file will initialise the store and the in-memory values will be written to the store on shutdown

func (*Service) Start

func (ks *Service) Start()

Start will bootstrap the keystore service ready to receive requests

func (*Service) Stop

func (ks *Service) Stop() chan bool

Stop will shutdown the keystore

type Store

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

Store creates a new in-memory store that can be read or written to disk

func NewEmptyStore

func NewEmptyStore() *Store

NewEmptyStore creates a new empty Store purely in memory and backed by no store

func NewStoreFromFile

func NewStoreFromFile(filePath string) *Store

NewStoreFromFile creates a new empty Store that is backed by disk

func (*Store) DeleteKey

func (s *Store) DeleteKey(key string)

DeleteKey will delete the key from the store

func (*Store) GetArray

func (s *Store) GetArray(key string) (interface{}, error)

GetArray implements KeyValueStore interface

func (*Store) GetBool

func (s *Store) GetBool(key string) (interface{}, error)

GetBool implements KeyValueStore interface

func (*Store) GetFloat

func (s *Store) GetFloat(key string) (interface{}, error)

GetFloat implements KeyValueStore interface

func (*Store) GetInt

func (s *Store) GetInt(key string) (interface{}, error)

GetInt implements KeyValueStore interface

func (*Store) GetMap

func (s *Store) GetMap(key string) (interface{}, error)

GetMap implements KeyValueStore interface

func (*Store) GetString

func (s *Store) GetString(key string) (interface{}, error)

GetString implements KeyValueStore interface

func (*Store) GetValue

func (s *Store) GetValue(key string) (val interface{}, err error)

GetValue implements KeyValueStore interface

func (*Store) KeyExists

func (s *Store) KeyExists(key string) (exists bool)

KeyExists returns true if the key exists in the store

func (*Store) ReadFromDisk

func (s *Store) ReadFromDisk() (err error)

ReadFromDisk loads the configuration from disk into this Store It returns an error if the data cannot be loaded from disk

func (*Store) SaveToDisk

func (s *Store) SaveToDisk() (err error)

SaveToDisk will flush the current to disk if their is a valid filepath

func (*Store) SetArray

func (s *Store) SetArray(key string, value interface{}) error

SetArray implements KeyValueStore interface

func (*Store) SetBool

func (s *Store) SetBool(key string, value interface{}) error

SetBool implements KeyValueStore interface

func (*Store) SetFloat

func (s *Store) SetFloat(key string, value interface{}) error

SetFloat implements KeyValueStore interface

func (*Store) SetInt

func (s *Store) SetInt(key string, value interface{}) error

SetInt implements KeyValueStore interface

func (*Store) SetMap

func (s *Store) SetMap(key string, value interface{}) error

SetMap implements KeyValueStore interface

func (*Store) SetString

func (s *Store) SetString(key string, value interface{}) error

SetString implements KeyValueStore interface

func (*Store) SetValue

func (s *Store) SetValue(key string, value interface{}) error

SetValue implements KeyValueStore interface

func (*Store) UpdateFilePath

func (s *Store) UpdateFilePath(filePath string)

UpdateFilePath will update the current file path to allow the data to be saved to that location

type Sync

type Sync struct {

	// All requests to the store are sent over this channel
	RequestChannel chan *Request
}

Sync implements the KeyValueStore and provides a synchronous blocking API that can be used with the keystore, server and client transports

func (*Sync) DeleteKey

func (s *Sync) DeleteKey(key string)

DeleteKey implements KeyValueStore

func (*Sync) GetArray

func (s *Sync) GetArray(key string) (interface{}, error)

GetArray implements KeyValueStore

func (*Sync) GetBool

func (s *Sync) GetBool(key string) (interface{}, error)

GetBool implements KeyValueStore

func (*Sync) GetFloat

func (s *Sync) GetFloat(key string) (interface{}, error)

GetFloat implements KeyValueStore

func (*Sync) GetInt

func (s *Sync) GetInt(key string) (interface{}, error)

GetInt implements KeyValueStore

func (*Sync) GetMap

func (s *Sync) GetMap(key string) (interface{}, error)

GetMap implements KeyValueStore

func (*Sync) GetString

func (s *Sync) GetString(key string) (interface{}, error)

GetString implements KeyValueStore

func (*Sync) GetValue

func (s *Sync) GetValue(key string) (interface{}, error)

GetValue implements KeyValueStore

func (*Sync) GetValueType

func (s *Sync) GetValueType(key string, v interface{}) error

GetValueType implements KeyValueStore

func (*Sync) SetArray

func (s *Sync) SetArray(key string, value interface{}) error

SetArray implements KeyValueStore

func (*Sync) SetBool

func (s *Sync) SetBool(key string, value interface{}) error

SetBool implements KeyValueStore

func (*Sync) SetFloat

func (s *Sync) SetFloat(key string, value interface{}) error

SetFloat implements KeyValueStore

func (*Sync) SetInt

func (s *Sync) SetInt(key string, value interface{}) error

SetInt implements KeyValueStore

func (*Sync) SetMap

func (s *Sync) SetMap(key string, value interface{}) error

SetMap implements KeyValueStore

func (*Sync) SetString

func (s *Sync) SetString(key string, value interface{}) error

SetString implements KeyValueStore

func (*Sync) SetValue

func (s *Sync) SetValue(key string, value interface{}) error

SetValue implements KeyValueStore

type Type

type Type uint

Type allows the requester to specify the type of data it is expecting This allows the caller to either handle the value or the error directly without having to check the type. NONE can be specified meaning any type will be accepted

const (
	BOOL   Type = 1 << iota // Expecting a boolean
	INT    Type = 1 << iota // Expecting an int
	FLOAT  Type = 1 << iota // Expecting a float
	STRING Type = 1 << iota // Expecting a string
	ARRAY  Type = 1 << iota // Expecting an array
	MAP    Type = 1 << iota // Expecting a map
	NONE   Type = 1 << iota // Expecting any type
)

Flags the data types

type ValueHolder

type ValueHolder struct {
	Type Type        // The data type expected (if read Op)
	Val  interface{} // The arbitrary value if NONE DataType specified
}

ValueHolder wraps the value, but if no error the value will be of the type expected

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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