storage

package
v0.4.11 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

Storage Package

The storage package provides concurrency-safe map and cache implementations in Go.

SafeMap

SafeMap is a struct that wraps a map with a simple RWMutex to facilitate concurrency. It supports generic types for keys and values.

FifoMapCache

FifoMapCache is a struct that implements a First-In-First-Out (FIFO) cache with a maximum size. When the cache is full, the oldest entries are evicted. It supports generic types for keys and values.

GenericStack

GenericStack is a struct that implements a generic stack data structure. It supports any type of values.

Testing

Unit tests for the SafeMap and FifoMapCache are located in the storage/safeMap_test.go and storage/fifoMapCache_test.go files respectively. They cover all methods and some edge cases, including concurrent operations.

OrderedBTree

The OrderedBTree is a thread-safe implementation of a BTree that supports ordered types. It is a wrapper around the google/btree package.

Usage

First, import the storage package:

import "your_project/storage"
Initialization

Create a new instance of OrderedBTree:

btree := storage.NewOrderedBTree[int, string]()
Set

To set a value for a key in the BTree:

btree.Set(1, "value1")
Get

To get a value for a key from the BTree:

value, ok := btree.Get(1)
if ok {
    fmt.Println(value)
}
Delete

To delete a key from the BTree:

value, ok := btree.Delete(1)
if ok {
    fmt.Println("Deleted value:", value)
}
Has

To check if a key exists in the BTree:

exists := btree.Has(1)
fmt.Println("Key exists:", exists)
Len

To get the length of the BTree:

length := btree.Len()
fmt.Println("Length of BTree:", length)
Min and Max

To get the minimum and maximum key and value in the BTree:

minKey, minValue := btree.Min()
maxKey, maxValue := btree.Max()
fmt.Println("Min:", minKey, minValue)
fmt.Println("Max:", maxKey, maxValue)
Ascend and Descend

To iterate over the BTree in ascending or descending order:

btree.Ascend(func(key int, value *string) bool {
    fmt.Println(key, *value)
    return true
})

btree.Descend(func(key int, value *string) bool {
    fmt.Println(key, *value)
    return true
})
DeleteMin and DeleteMax

To delete the minimum and maximum key and value in the BTree:

minKey, minValue := btree.DeleteMin()
maxKey, maxValue := btree.DeleteMax()
fmt.Println("Deleted Min:", minKey, minValue)
fmt.Println("Deleted Max:", maxKey, maxValue)
Clone

To clone the BTree:

clone := btree.Clone()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TranslateToMapOf added in v0.4.10

func TranslateToMapOf[K comparable, V any, D any](s *SafeMap[K, V], translator func(V) D) map[K]D

TranslateToMapOf returns a map of type D from the map of type V

func WithBalancedPartitions

func WithBalancedPartitions(nRoot float64, minimumPartitions int) fifoInitializationOption

WithBalancedPartitions balances the partitions based upon the nRoot parameter, calculating the number of partitions equal to the Nth root of capacity nRoot should be greater than 1. nRoot of 2 is same as the default number of partitions, which balances the number of values in each partition close to the number of partitions. nRoot less than 2 (greater than 1) will reduce number of partitions, making each partition containing more values. nRoot greater than 2 will increase number of partitions, making each partition contains fewer values. If the calculated number of partitions is less than minimumPartitions, minimumPartitions is used.

func WithSweepFrequency added in v0.4.7

func WithSweepFrequency(frequency time.Duration) fifoInitializationOption

Types

type FifoMapCache

type FifoMapCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewFifoMapCache

func NewFifoMapCache[K comparable, V any](ctx context.Context, capacity int, options ...fifoInitializationOption) *FifoMapCache[K, V]

func (*FifoMapCache[K, V]) Capacity added in v0.4.7

func (f *FifoMapCache[K, V]) Capacity() int

Capacity returns the actual capacity of the map once the number of partitions and the partition capacity are calculated

func (*FifoMapCache[K, V]) Clear added in v0.4.2

func (f *FifoMapCache[K, V]) Clear()

Clear clears the map

func (*FifoMapCache[K, V]) Contains

func (f *FifoMapCache[K, V]) Contains(key K) bool

Contains returns true if the key of type K is in the map

func (*FifoMapCache[K, V]) Delete

func (f *FifoMapCache[K, V]) Delete(key K)

func (*FifoMapCache[K, V]) Get

func (f *FifoMapCache[K, V]) Get(key K) (value V)

Get returns the value of type V for the key of type K. If the key is not found, the zero value of V is returned.

func (*FifoMapCache[K, V]) Keys added in v0.4.2

func (f *FifoMapCache[K, V]) Keys() []K

Keys returns a slice of keys

func (*FifoMapCache[K, V]) Len

func (f *FifoMapCache[K, V]) Len() int

Len returns the length of the map

func (*FifoMapCache[K, V]) Resize added in v0.4.4

func (f *FifoMapCache[K, V]) Resize(capacity int)

func (*FifoMapCache[K, V]) Set

func (f *FifoMapCache[K, V]) Set(key K, value V)

Set sets the value of type V for the key of type K.

func (*FifoMapCache[K, V]) Sweep added in v0.4.7

func (f *FifoMapCache[K, V]) Sweep()

sweep removes partitions from the stack if the number of partitions exceeds the maxPartitions

func (*FifoMapCache[K, V]) Values added in v0.4.2

func (f *FifoMapCache[K, V]) Values() []V

Values returns a slice of values

type GenericStack

type GenericStack[T any] struct {
	// contains filtered or unexported fields
}

func NewGenericStack

func NewGenericStack[T any](initialSize int) *GenericStack[T]

NewGenericStack returns an initialized reference to a GenericStack of T

func (*GenericStack[T]) Len

func (s *GenericStack[T]) Len() int

Len returns the number of elements on the stack

func (*GenericStack[T]) Peek

func (s *GenericStack[T]) Peek(id uint64) (value T, err error)

Peek returns the value on the stack that was assigned the id requested. IDNotFoundError returned if id not found.

func (*GenericStack[T]) Pop

func (s *GenericStack[T]) Pop() T

Pop removes the next T from the stack and returns it

func (*GenericStack[T]) Push

func (s *GenericStack[T]) Push(value T) (id uint64)

Push pushes value of type T on the stack, returning the assigned id in the stack

func (*GenericStack[T]) Values added in v0.4.2

func (s *GenericStack[T]) Values() []T

Values returns a slice of all the values on the stack

type OrderedBTree added in v0.4.7

type OrderedBTree[K cmp.Ordered, V any] struct {
	*btree.BTree
	// contains filtered or unexported fields
}

OrderedBTree is a thread-safe implementation of a BTree that supports ordered types. It is a wrapper around the google/btree package.

func NewOrderedBTree added in v0.4.7

func NewOrderedBTree[K cmp.Ordered, V any]() *OrderedBTree[K, V]

NewOrderedBTree returns an initialized reference to an OrderedBTree of K and V

func (*OrderedBTree[K, V]) Ascend added in v0.4.7

func (t *OrderedBTree[K, V]) Ascend(iter func(key K, value *V) bool)

Ascend calls the iter function for every key/value pair in the BTree in ascending order.

func (*OrderedBTree[K, V]) AscendGreaterOrEqual added in v0.4.7

func (t *OrderedBTree[K, V]) AscendGreaterOrEqual(pivot K, iter func(key K, value *V) bool)

AscendGreaterOrEqual calls the iter function for every key/value pair in the BTree in ascending order starting with the first key/value pair that is greater than or equal to the pivot key.

func (*OrderedBTree[K, V]) AscendLessThan added in v0.4.7

func (t *OrderedBTree[K, V]) AscendLessThan(pivot K, iter func(key K, value *V) bool)

AscendGreaterThan calls the iter function for every key/value pair in the BTree, starting with the Min value up to the pivot key..

func (*OrderedBTree[K, V]) AscendRange added in v0.4.7

func (t *OrderedBTree[K, V]) AscendRange(greaterThanEqual, lessThan K, iter func(key K, value *V) bool)

AscendRange calls the iter function for every key/value pair in the BTree, starting with the first key/value pair that is greater than or equal to the greaterThanEqual key up to the first key/value pair that is less than the lessThan key.

func (*OrderedBTree[K, V]) Clone added in v0.4.7

func (t *OrderedBTree[K, V]) Clone() *OrderedBTree[K, V]

Clone returns a copy of the BTree

func (*OrderedBTree[K, V]) Delete added in v0.4.7

func (t *OrderedBTree[K, V]) Delete(key K) (value *V, ok bool)

Delete deletes the key of type K. If the key is not found, ok is returned as false.

func (*OrderedBTree[K, V]) DeleteMax added in v0.4.7

func (t *OrderedBTree[K, V]) DeleteMax() (key K, value *V)

DeleteMax deletes the maximum key and value in the BTree

func (*OrderedBTree[K, V]) DeleteMin added in v0.4.7

func (t *OrderedBTree[K, V]) DeleteMin() (key K, value *V)

DeleteMin deletes the minimum key and value in the BTree

func (*OrderedBTree[K, V]) Descend added in v0.4.7

func (t *OrderedBTree[K, V]) Descend(iter func(key K, value *V) bool)

Descend calls the iter function for every key/value pair in the BTree in descending order.

func (*OrderedBTree[K, V]) DescendGreaterThan added in v0.4.7

func (t *OrderedBTree[K, V]) DescendGreaterThan(pivot K, iter func(key K, value *V) bool)

DescendLessOrEqual calls the iterator for every value in the tree within the range [pivot, first], until iterator returns false

func (*OrderedBTree[K, V]) DescendLessOrEqual added in v0.4.7

func (t *OrderedBTree[K, V]) DescendLessOrEqual(pivot K, iter func(key K, value *V) bool)

DDescendGreaterThan calls the iterator for every value in the tree within the range [last, pivot), until iterator returns false

func (*OrderedBTree[K, V]) DescendRange added in v0.4.7

func (t *OrderedBTree[K, V]) DescendRange(greaterThan, lessThanEqual K, iter func(key K, value *V) bool)

DescendRange calls the iterator for every value in the tree within the range [lessOrEqual, greaterThan), until iterator returns false

func (*OrderedBTree[K, V]) Get added in v0.4.7

func (t *OrderedBTree[K, V]) Get(key K) (value *V, ok bool)

Get returns the value of type V for the key of type K. If the key is not found, ok is returned as false.

func (*OrderedBTree[K, V]) Has added in v0.4.7

func (t *OrderedBTree[K, V]) Has(key K) bool

Has returns true if the key of type K exists in the BTree.

func (*OrderedBTree[K, V]) Len added in v0.4.7

func (t *OrderedBTree[K, V]) Len() int

Len returns the length of the BTree

func (*OrderedBTree[K, V]) Max added in v0.4.7

func (t *OrderedBTree[K, V]) Max() (key K, value *V)

Max returns the maximum key and value in the BTree

func (*OrderedBTree[K, V]) Min added in v0.4.7

func (t *OrderedBTree[K, V]) Min() (key K, value *V)

Min returns the minimum key and value in the BTree

func (*OrderedBTree[K, V]) Set added in v0.4.7

func (t *OrderedBTree[K, V]) Set(key K, value *V)

Set sets the value of type V for the key of type K.

type SafeMap

type SafeMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SafeMap wraps a map[K]V with a simple RWMutex to facilitate concurrency

func NewSafeMap

func NewSafeMap[K comparable, V any](initialCapacity int) *SafeMap[K, V]

NewSafeMap returns an initialized reference to a SafeMap of K, V

func (*SafeMap[K, V]) Clear added in v0.4.11

func (s *SafeMap[K, V]) Clear()

Clear removes all the keys and values from the map

func (*SafeMap[K, V]) ClearAndResize added in v0.4.11

func (s *SafeMap[K, V]) ClearAndResize(newSize int)

ClearAndResize clears the map and resize it to the new size

func (*SafeMap[K, V]) Contains

func (s *SafeMap[K, V]) Contains(key K) bool

Contains returns true if the key of type K is in the map

func (*SafeMap[K, V]) CopyToMap added in v0.4.10

func (s *SafeMap[K, V]) CopyToMap() map[K]V

CopyToMap returns a copy of the map

func (*SafeMap[K, V]) Delete

func (s *SafeMap[K, V]) Delete(key K)

Delete deletes the key of type K from the map

func (*SafeMap[K, V]) Get

func (s *SafeMap[K, V]) Get(key K) (value V)

Get returns the value of type V for the key of type K. If the key is not found, the zero value of V is returned.

func (*SafeMap[K, V]) GetOrAdd added in v0.4.10

func (s *SafeMap[K, V]) GetOrAdd(key K, val V) (value V)

GetOrAdd returns the value of type V for the key of type K. If the key is not found, the value is added to the map and returned.

func (*SafeMap[K, V]) Has added in v0.4.8

func (s *SafeMap[K, V]) Has(key K) bool

Has returns true if the key of type K is in the map

func (*SafeMap[K, V]) Keys added in v0.4.2

func (s *SafeMap[K, V]) Keys() []K

Keys returns a slice of all the keys in the map

func (*SafeMap[K, V]) Len

func (s *SafeMap[K, V]) Len() int

Len returns the length of the map

func (*SafeMap[K, V]) Set

func (s *SafeMap[K, V]) Set(key K, value V)

Set sets the value of type V for the key of type K.

func (*SafeMap[K, V]) Values added in v0.4.2

func (s *SafeMap[K, V]) Values() []V

Values returns a slice of all the values in the map

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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