storage

package
v0.0.0-...-2aea405 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2017 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package storage provides a thread safe hash table implementation. The hash table is implemented internally as a tree structure and is self-pruning. The hash table uses a 32-bit FNV hash and each node of the tree represents 1 byte in the hash. The intention is to allow movement of entire nodes of the tree between storage instances.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hash

func Hash(key string) uint32

Hash returns the 32bit hash for a given key

Types

type GetNodeRequest

type GetNodeRequest struct {
	ID     NodeLocator
	Remove bool
	Result chan NodeResult
}

GetNodeRequest is used to request an entire node of the storage tree.

type GetRequest

type GetRequest struct {
	ID     string
	Remove bool
	Result chan Result
}

GetRequest is used to retrieve a value and optionally remove it from the storage tree.

type Hashtable

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

Hashtable implements a tree based hashtable

func NewHashtable

func NewHashtable() *Hashtable

NewHashtable creates a Hashtable instance

func (*Hashtable) FindNode

func (db *Hashtable) FindNode(id NodeLocator) (*Node, []*Node)

FindNode returns a node and the path to its parent node

func (*Hashtable) FindNodeRecurse

func (db *Hashtable) FindNodeRecurse(id []byte, parent *Node, path []*Node) (*Node, []*Node)

FindNodeRecurse returns a node and the path to its parent node

func (*Hashtable) Get

func (db *Hashtable) Get(key string) string

Get returns the value for a given key

func (*Hashtable) Remove

func (db *Hashtable) Remove(key string)

Remove removes a given key

func (*Hashtable) RemoveNode

func (db *Hashtable) RemoveNode(id NodeLocator)

RemoveNode removes a given node from the tree

func (*Hashtable) Set

func (db *Hashtable) Set(key string, value string)

Set sets the value for a given key

func (*Hashtable) SetNode

func (db *Hashtable) SetNode(id NodeLocator, value *Node) *Node

SetNode sets a given node in the tree

func (*Hashtable) SetNodeValue

func (db *Hashtable) SetNodeValue(id NodeLocator, key string, value string) *Node

SetNodeValue sets a key/value pair on a given node

type Instance

type Instance struct {

	// GetNode retrieves a node from the storage tree, optionally removing it
	GetNode chan GetNodeRequest
	// SetNode sets a node in the storage tree
	SetNode chan SetNodeRequest
	// Shutdown stops the storage worker thread
	Shutdown chan bool
	// Logger is the logger instance used by the storage instance
	Logger *log.Logger
	// Path is the path to the data file maintained by this storage instance
	Path string
	// contains filtered or unexported fields
}

Instance represents a storage tree instance

func New

func New(logWriter io.Writer, dbPath string) *Instance

New creates a new Storage instance. It also loads the data file if it exists and starts the storage thread.

func (*Instance) Close

func (db *Instance) Close()

Close shuts down the storage instance

func (*Instance) Get

func (db *Instance) Get(id string) string

Get returns the value of the given key

func (*Instance) Remove

func (db *Instance) Remove(id string) string

Remove removes the given key

func (*Instance) Set

func (db *Instance) Set(id string, value string) string

Set sets the value of the given key

type Node

type Node struct {
	Children [256]*Node
	// contains filtered or unexported fields
}

Node represents a single node in the storage tree

func NewNode

func NewNode() *Node

NewNode returns a new Node instance

func (*Node) GetValue

func (n *Node) GetValue(key string) string

GetValue returns the given value from the node

func (*Node) IsEmpty

func (n *Node) IsEmpty() bool

IsEmpty returns true if this node is empty. Leaf nodes are empty if they have no value. Non-leaf nodes are empty if they have no children.

func (*Node) IsLeaf

func (n *Node) IsLeaf() bool

IsLeaf returns true if this node is a leaf node

func (*Node) RemoveValue

func (n *Node) RemoveValue(key string)

RemoveValue removes the given value from the node

func (*Node) SetValue

func (n *Node) SetValue(key string, value string)

SetValue sets the given value on the node

type NodeKeyValuePair

type NodeKeyValuePair struct {
	Key   string
	Value string
}

NodeKeyValuePair holds a key/value pair

type NodeLocator

type NodeLocator struct {
	ID    uint32
	Bytes byte
}

NodeLocator provides an address to a specific node in the storage tree.

func GetNodeLocator

func GetNodeLocator(id string) NodeLocator

GetNodeLocator returns a NodeLocator for this ID

func (NodeLocator) GetBytes

func (id NodeLocator) GetBytes() []byte

GetBytes returns a byte slice representing this node locator

type NodeResult

type NodeResult struct {
	ID    NodeLocator
	Value *Node
}

NodeResult is returned from GetNode and SetNode

type Result

type Result struct {
	ID    string
	Value string
}

Result is returned from Get and Set

type SetNodeRequest

type SetNodeRequest struct {
	ID     NodeLocator
	Value  *Node
	Result chan NodeResult
}

SetNodeRequest is used to set an entire node of the storage tree.

type SetRequest

type SetRequest struct {
	ID     string
	Value  string
	Result chan Result
}

SetRequest is used to set a value in the storage tree.

Jump to

Keyboard shortcuts

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