package smap

import "github.com/Diarkis/diarkis/smap"

Package smap ▷

Goroutine-safe map and array that is optimized for multi-goroutine process usage with plethora of utility functions.

Index

Variables

var ErrKeyAlreadyExists = errors.New("key already exists")

ErrKeyAlreadyExists error returned when the targeted key already exists.

var ErrPrimitiveDataType = errors.New("invalid data type")

ErrPrimitiveDataType errors returned when the value cannot be stored in the map.

Functions

func Encode

func Encode(sm SyncMap) ([]byte, error)

Encode encodes the stored keys and values as a snapshot. Use Decode to convert it back to *SyncMap

Types

type Element

type Element struct {
	ID     string
	Points int64
}

Element is a value struct that is inserted in SyncArray. Element.ID contains the actual value.

type Item

type Item struct {
	ID     string
	Points int64
	Index  int
}

Item represents each element stored in the list with its index.

type KeysIfOperation

type KeysIfOperation func(key string) bool

KeysIfOperation filters keys to be returned by KeysIf.

type KeysIfValueOperation

type KeysIfValueOperation func(key string, value interface{}) bool

KeysIfValueOperation filters keys to be returned by KeysIfValue.

type RemoveOperation

type RemoveOperation func(exists bool, storedValue interface{}) bool

RemoveOperation is a callback to be invoked when executing RemoveIf. The callback is called while the lock is held and because of that you MUST NOT try to access the internal map with the same key. If the callback returns true, the key and its value will be removed from the map.

type SyncArray

type SyncArray struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

SyncArray is a thread-safe array.

func NewASC

func NewASC() *SyncArray

NewASC creates a new SyncArray in ascending order.

func NewDESC

func NewDESC() *SyncArray

NewDESC creates a new SyncArray in descending order.

func (*SyncArray) Clear

func (sa *SyncArray) Clear()

Clear deletes all elements in the array.

func (*SyncArray) GetAll

func (sa *SyncArray) GetAll() []*Item

GetAll returns a list of all items.

func (*SyncArray) GetAt

func (sa *SyncArray) GetAt(index int) *Item

GetAt returns item with its index. Returns nil if the index is invalid.

func (*SyncArray) GetRange

func (sa *SyncArray) GetRange(from int, to int) []*Item

GetRange returns a list of items with in the given range.

func (*SyncArray) Insert

func (sa *SyncArray) Insert(elm *Element) int

Insert inserts the given element in the ascending or descending order. Returns the inserted index.

func (*SyncArray) Length

func (sa *SyncArray) Length() int

Length returns the length of the array.

func (*SyncArray) Remove

func (sa *SyncArray) Remove(elm *Element) int

Remove deletes an element that matches with the value interface{} of the given Element. Returns the index of the deleted element that holds the given value or -1 if not deleted.

func (*SyncArray) Search

func (sa *SyncArray) Search(elm *Element) *Item

Search returns the searched result item by the given Element with its index.

func (*SyncArray) SearchRange

func (sa *SyncArray) SearchRange(elm *Element, up int, down int) []*Item

SearchRange returns a list of items searched by the given Element and given range up to down.

type SyncMap

type SyncMap []*shard

SyncMap is a thread-safe map. It does NOT support having a SyncMap instance as a value. You may NOT add an instance of a SyncMap as Encode and Decode will NOT work properly with a SyncMap as a value.

func Decode

func Decode(encoded []byte) (SyncMap, error)

Decode converts the encoded *SyncMap back to *SyncMap. If invalid data is given, the function may return an error.

func New

func New() SyncMap

New creates and returns a new SyncMap instance

func (SyncMap) AllowAllTypes

func (sm SyncMap) AllowAllTypes(key string)

AllowAllTypes allows the value type of be anything including *SyncMap data type to be stored.

[IMPORTANT] Encode and Decode do NOT support *SyncMap and other structs.

func (SyncMap) AllowNonPrimitive

func (sm SyncMap) AllowNonPrimitive(key string)

AllowNonPrimitive allows the given key's value to be non-primitive. This operation is per internal shared map. To retrieve the stored value that is not primitive, use Get.

func (SyncMap) Clear

func (sm SyncMap) Clear()

Clear removes all keys and values.

func (SyncMap) Count

func (sm SyncMap) Count() int

Count returns the number of elements stored.

func (SyncMap) Exists

func (sm SyncMap) Exists(key string) bool

Exists returns true if the given key exists.

func (SyncMap) Get

func (sm SyncMap) Get(key string) (interface{}, bool)

Get returns the value of the given key as an interface{}.

func (SyncMap) GetAsBool

func (sm SyncMap) GetAsBool(key string) (bool, bool)

GetAsBool returns the stored value of the given key.

func (SyncMap) GetAsBoolArray

func (sm SyncMap) GetAsBoolArray(key string) ([]bool, bool)

GetAsBoolArray returns the stored value of the given key.

func (SyncMap) GetAsBytes

func (sm SyncMap) GetAsBytes(key string) ([]byte, bool)

GetAsBytes returns the stored value of the given key.

func (SyncMap) GetAsBytesArray

func (sm SyncMap) GetAsBytesArray(key string) ([][]byte, bool)

GetAsBytesArray returns the stored value of the given key.

func (SyncMap) GetAsFloat32

func (sm SyncMap) GetAsFloat32(key string) (float32, bool)

GetAsFloat32 returns the stored value of the given key.

func (SyncMap) GetAsFloat32Array

func (sm SyncMap) GetAsFloat32Array(key string) ([]float32, bool)

GetAsFloat32Array returns the stored value of the given key.

func (SyncMap) GetAsFloat64

func (sm SyncMap) GetAsFloat64(key string) (float64, bool)

GetAsFloat64 returns the stored value of the given key.

func (SyncMap) GetAsFloat64Array

func (sm SyncMap) GetAsFloat64Array(key string) ([]float64, bool)

GetAsFloat64Array returns the stored value of the given key.

func (SyncMap) GetAsInt

func (sm SyncMap) GetAsInt(key string) (int, bool)

GetAsInt returns the stored value of the given key.

func (SyncMap) GetAsInt16

func (sm SyncMap) GetAsInt16(key string) (int16, bool)

GetAsInt16 returns the stored value of the given key.

func (SyncMap) GetAsInt16Array

func (sm SyncMap) GetAsInt16Array(key string) ([]int16, bool)

GetAsInt16Array returns the stored value of the given key.

func (SyncMap) GetAsInt32

func (sm SyncMap) GetAsInt32(key string) (int32, bool)

GetAsInt32 returns the stored value of the given key.

func (SyncMap) GetAsInt32Array

func (sm SyncMap) GetAsInt32Array(key string) ([]int32, bool)

GetAsInt32Array returns the stored value of the given key.

func (SyncMap) GetAsInt64

func (sm SyncMap) GetAsInt64(key string) (int64, bool)

GetAsInt64 returns the stored value of the given key.

func (SyncMap) GetAsInt64Array

func (sm SyncMap) GetAsInt64Array(key string) ([]int64, bool)

GetAsInt64Array returns the stored value of the given key.

func (SyncMap) GetAsInt8

func (sm SyncMap) GetAsInt8(key string) (int8, bool)

GetAsInt8 returns the stored value of the given key.

func (SyncMap) GetAsInt8Array

func (sm SyncMap) GetAsInt8Array(key string) ([]int8, bool)

GetAsInt8Array returns the stored value of the given key.

func (SyncMap) GetAsIntArray

func (sm SyncMap) GetAsIntArray(key string) ([]int, bool)

GetAsIntArray returns the stored value of the given key.

func (SyncMap) GetAsString

func (sm SyncMap) GetAsString(key string) (string, bool)

GetAsString returns the stored value of the given key.

func (SyncMap) GetAsStringArray

func (sm SyncMap) GetAsStringArray(key string) ([]string, bool)

GetAsStringArray returns the stored value of the given key.

func (SyncMap) GetAsUint

func (sm SyncMap) GetAsUint(key string) (uint, bool)

GetAsUint returns the stored value of the given key.

func (SyncMap) GetAsUint16

func (sm SyncMap) GetAsUint16(key string) (uint16, bool)

GetAsUint16 returns the stored value of the given key.

func (SyncMap) GetAsUint16Array

func (sm SyncMap) GetAsUint16Array(key string) ([]uint16, bool)

GetAsUint16Array returns the stored value of the given key.

func (SyncMap) GetAsUint32

func (sm SyncMap) GetAsUint32(key string) (uint32, bool)

GetAsUint32 returns the stored value of the given key.

func (SyncMap) GetAsUint32Array

func (sm SyncMap) GetAsUint32Array(key string) ([]uint32, bool)

GetAsUint32Array returns the stored value of the given key.

func (SyncMap) GetAsUint64

func (sm SyncMap) GetAsUint64(key string) (uint64, bool)

GetAsUint64 returns the stored value of the given key.

func (SyncMap) GetAsUint64Array

func (sm SyncMap) GetAsUint64Array(key string) ([]uint64, bool)

GetAsUint64Array returns the stored value of the given key.

func (SyncMap) GetAsUint8

func (sm SyncMap) GetAsUint8(key string) (uint8, bool)

GetAsUint8 returns the stored value of the given key.

func (SyncMap) GetAsUint8Array

func (sm SyncMap) GetAsUint8Array(key string) ([]uint8, bool)

GetAsUint8Array returns the stored value of the given key.

func (SyncMap) GetAsUintArray

func (sm SyncMap) GetAsUintArray(key string) ([]uint, bool)

GetAsUintArray returns the stored value of the given key.

func (SyncMap) GetKeysByRange

func (sm SyncMap) GetKeysByRange(howmany int) []string

GetKeysByRange returns keys as an array of the length specified

[IMPORTANT] If the total number of keys is smaller than the given length,
            the returned array will be smaller than the given length.

func (SyncMap) IncrAsFloat32

func (sm SyncMap) IncrAsFloat32(key string, incr float32) (float32, error)

IncrAsFloat32 increments the value of the given key by the given incr value.

func (SyncMap) IncrAsFloat64

func (sm SyncMap) IncrAsFloat64(key string, incr float64) (float64, error)

IncrAsFloat64 increments the value of the given key by the given incr value.

func (SyncMap) IncrAsInt

func (sm SyncMap) IncrAsInt(key string, incr int) (int, error)

IncrAsInt increments the value of the given key by the given incr value.

func (SyncMap) IncrAsInt16

func (sm SyncMap) IncrAsInt16(key string, incr int16) (int16, error)

IncrAsInt16 increments the value of the given key by the given incr value.

func (SyncMap) IncrAsInt32

func (sm SyncMap) IncrAsInt32(key string, incr int32) (int32, error)

IncrAsInt32 increments the value of the given key by the given incr value.

func (SyncMap) IncrAsInt64

func (sm SyncMap) IncrAsInt64(key string, incr int64) (int64, error)

IncrAsInt64 increments the value of the given key by the given incr value.

func (SyncMap) IncrAsInt8

func (sm SyncMap) IncrAsInt8(key string, incr int8) (int8, error)

IncrAsInt8 increments the value of the given key by the given incr value.

func (SyncMap) IncrAsUint

func (sm SyncMap) IncrAsUint(key string, incr uint) (uint, error)

IncrAsUint increments the value of the given key by the given incr value.

func (SyncMap) IncrAsUint16

func (sm SyncMap) IncrAsUint16(key string, incr uint16) (uint16, error)

IncrAsUint16 increments the value of the given key by the given incr value.

func (SyncMap) IncrAsUint32

func (sm SyncMap) IncrAsUint32(key string, incr uint32) (uint32, error)

IncrAsUint32 increments the value of the given key by the given incr value.

func (SyncMap) IncrAsUint64

func (sm SyncMap) IncrAsUint64(key string, incr uint64) (uint64, error)

IncrAsUint64 increments the value of the given key by the given incr value.

func (SyncMap) IncrAsUint8

func (sm SyncMap) IncrAsUint8(key string, incr uint8) (uint8, error)

IncrAsUint8 increments the value of the given key by the given incr value.

func (SyncMap) Keys

func (sm SyncMap) Keys() []string

Keys returns all keys as an array of string.

func (SyncMap) KeysIf

func (sm SyncMap) KeysIf(cb KeysIfOperation) []string

KeysIf returns all keys that the callback KeysIfOperation returns true.

[IMPORTANT] Callback is invoked while the lock is held and because of that
            you must NOT use functions that uses lock inside the callback.

func (SyncMap) KeysIfValue

func (sm SyncMap) KeysIfValue(cb KeysIfValueOperation) []string

KeysIfValue returns all keys that the callback KeysIfOperation returns true.

[IMPORTANT] Callback is invoked while the lock is held and because of that
            you must NOT use functions that uses lock inside the callback.

func (SyncMap) MSet

func (sm SyncMap) MSet(data map[string]interface{}, allowNonPrimitive bool) error

MSet sets multiple keys and values.

[IMPORTANT] Allowed value is either a primitive, string, or byte array.

Returns an error if the given data map contains an invalid data type.

When it returns an error no key and value will be stored at all.

func (SyncMap) Range

func (sm SyncMap) Range(cb func(key string, value interface{}))

Range iterates over all keys and invokes the callback on every key. The lock is held while the callback is invoked and because of that you MUST NOT try to access the internal map with the same key.

func (SyncMap) Remove

func (sm SyncMap) Remove(key string) bool

Remove removes an element of the given key. Returns true if the element has been removed.

func (SyncMap) RemoveIf

func (sm SyncMap) RemoveIf(key string, cb RemoveOperation) bool

RemoveIf removes an element of the given key if the callback returns true.

The return value of RemoveIf indicates if the key is deleted or not.

[IMPORTANT] Callback is invoked while the lock is held and because of that
            you must NOT use functions that uses lock inside the callback.

func (SyncMap) Set

func (sm SyncMap) Set(key string, value interface{}) error

Set sets the given value with the key.

[IMPORTANT] Allowed value is either a primitive, string, or byte array.

Returns an error if the given value data type is not primitive or byte array.

func (SyncMap) SetIf

func (sm SyncMap) SetIf(key string, value interface{}, check func() bool) error

SetIf sets the given key along with the value if the given check function returns true and the key does not exists.

[IMPORTANT] check function is executed while SetIf holds a mutex lock.
            Do not use mutex lock in check function to avoid deadlock.

If either the check function returns false or the key already exists, it returns an error.

func (SyncMap) SetIfNotExists

func (sm SyncMap) SetIfNotExists(key string, value interface{}) error

SetIfNotExists sets the given key along with the value if the key does not exist.

[IMPORTANT] Allowed value is either a primitive, string, or byte array.

If the key already exists, it returns an error.

func (SyncMap) ToBool

func (sm SyncMap) ToBool(v interface{}) (bool, bool)

ToBool converts interface{}

func (SyncMap) ToBoolArray

func (sm SyncMap) ToBoolArray(v interface{}) ([]bool, bool)

ToBoolArray converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToBytes

func (sm SyncMap) ToBytes(v interface{}) ([]byte, bool)

ToBytes converts interface{}

func (SyncMap) ToBytesArray

func (sm SyncMap) ToBytesArray(v interface{}) ([][]byte, bool)

ToBytesArray converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToFloat32

func (sm SyncMap) ToFloat32(v interface{}) (float32, bool)

ToFloat32 converts interface{}.

func (SyncMap) ToFloat32Array

func (sm SyncMap) ToFloat32Array(v interface{}) ([]float32, bool)

ToFloat32Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToFloat64

func (sm SyncMap) ToFloat64(v interface{}) (float64, bool)

ToFloat64 converts interface{}

func (SyncMap) ToFloat64Array

func (sm SyncMap) ToFloat64Array(v interface{}) ([]float64, bool)

ToFloat64Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToInt

func (sm SyncMap) ToInt(v interface{}) (int, bool)

ToInt converts interface{}

func (SyncMap) ToInt16

func (sm SyncMap) ToInt16(v interface{}) (int16, bool)

ToInt16 converts interface{}.

func (SyncMap) ToInt16Array

func (sm SyncMap) ToInt16Array(v interface{}) ([]int16, bool)

ToInt16Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToInt32

func (sm SyncMap) ToInt32(v interface{}) (int32, bool)

ToInt32 converts interface{}

func (SyncMap) ToInt32Array

func (sm SyncMap) ToInt32Array(v interface{}) ([]int32, bool)

ToInt32Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToInt64

func (sm SyncMap) ToInt64(v interface{}) (int64, bool)

ToInt64 converts interface{}

func (SyncMap) ToInt64Array

func (sm SyncMap) ToInt64Array(v interface{}) ([]int64, bool)

ToInt64Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToInt8

func (sm SyncMap) ToInt8(v interface{}) (int8, bool)

ToInt8 converts interface{}.

func (SyncMap) ToInt8Array

func (sm SyncMap) ToInt8Array(v interface{}) ([]int8, bool)

ToInt8Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToIntArray

func (sm SyncMap) ToIntArray(v interface{}) ([]int, bool)

ToIntArray converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToString

func (sm SyncMap) ToString(v interface{}) (string, bool)

ToString converts interface{}

func (SyncMap) ToStringArray

func (sm SyncMap) ToStringArray(v interface{}) ([]string, bool)

ToStringArray converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToUint

func (sm SyncMap) ToUint(v interface{}) (uint, bool)

ToUint converts interface{}

func (SyncMap) ToUint16

func (sm SyncMap) ToUint16(v interface{}) (uint16, bool)

ToUint16 converts interface{}.

func (SyncMap) ToUint16Array

func (sm SyncMap) ToUint16Array(v interface{}) ([]uint16, bool)

ToUint16Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToUint32

func (sm SyncMap) ToUint32(v interface{}) (uint32, bool)

ToUint32 converts interface{}

func (SyncMap) ToUint32Array

func (sm SyncMap) ToUint32Array(v interface{}) ([]uint32, bool)

ToUint32Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToUint64

func (sm SyncMap) ToUint64(v interface{}) (uint64, bool)

ToUint64 converts interface{}

func (SyncMap) ToUint64Array

func (sm SyncMap) ToUint64Array(v interface{}) ([]uint64, bool)

ToUint64Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToUint8

func (sm SyncMap) ToUint8(v interface{}) (uint8, bool)

ToUint8 converts interface{}.

func (SyncMap) ToUint8Array

func (sm SyncMap) ToUint8Array(v interface{}) ([]uint8, bool)

ToUint8Array converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) ToUintArray

func (sm SyncMap) ToUintArray(v interface{}) ([]uint, bool)

ToUintArray converts interface{}. If other data type found in the array, it returns an empty array with false.

func (SyncMap) Upsert

func (sm SyncMap) Upsert(key string, value interface{}, cb UpsertOperation) (interface{}, error)

Upsert will updates the existing value using the return value of UpsertOperation callback if the value of the key exists.

If the key and value does not exist, it will simply add the new key along with the value.

Returns the updated or inserted value as an interface{}.

Use To...() function such as ToInt, ToString etc. To convert it to appropriate data type.

[IMPORTANT] Allowed value is either a primitive, string, or byte array.

[IMPORTANT] Callback is invoked while the lock is held.
            You must NOT use functions that uses lock inside the callback.

type UpsertOperation

type UpsertOperation func(exists bool, storedValue interface{}, updateValue interface{}) (updatedValue interface{})

UpsertOperation is a callback to be invoked when executing Upsert. The callback is called while the lock is held and because of that you MUST NOT try to access the internal map with the same key. Returned value will be stored as an updated value.