core

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package core provides the core types used by other Redka packages.

Index

Constants

View Source
const (
	TypeAny    = TypeID(0)
	TypeString = TypeID(1)
	TypeList   = TypeID(2)
	TypeSet    = TypeID(3)
	TypeHash   = TypeID(4)
	TypeZSet   = TypeID(5)
)

Variables

View Source
var (
	ErrKeyType    = errors.New("key type mismatch") // the key already exists with a different type
	ErrNotAllowed = errors.New("operation not allowed")
	ErrNotFound   = errors.New("key or elem not found")
	ErrValueType  = errors.New("invalid value type")
)

Common errors returned by data structure methods.

Functions

func IsValueType

func IsValueType(v any) bool

IsValueType reports if the value has a valid type to be persisted in the database. Only the following types are allowed:

  • string
  • integer
  • float
  • boolean
  • byte slice

func ToBytes added in v0.4.0

func ToBytes(v any) ([]byte, error)

ToBytes converts a value to a byte slice.

func ToBytesMany added in v0.4.0

func ToBytesMany(values ...any) ([][]byte, error)

ToBytesMany converts multiple values to byte slices.

Types

type Key

type Key struct {
	ID      int
	Key     string
	Type    TypeID
	Version int    // incremented on each update
	ETime   *int64 // expiration time in unix milliseconds
	MTime   int64  // last modification time in unix milliseconds
}

Key represents a key data structure. Each key uniquely identifies a data structure stored in the database (e.g. a string, a list, or a hash). There can be only one data structure with a given key, regardless of type. For example, you can't have a string and a hash map with the same key.

func (Key) Exists

func (k Key) Exists() bool

Exists reports whether the key exists. Returns false for expired keys.

func (Key) TypeName

func (k Key) TypeName() string

TypeName returns the name of the key type.

type TypeID added in v0.2.0

type TypeID int

A TypeID identifies the type of the key and thus the data structure of the value with that key.

type Value

type Value []byte

Value represents a value stored in a database (a byte slice). It can be converted to other scalar types.

func (Value) Bool

func (v Value) Bool() (bool, error)

Bool returns the value as a boolean.

func (Value) Bytes

func (v Value) Bytes() []byte

Bytes returns the value as a byte slice.

func (Value) Float

func (v Value) Float() (float64, error)

Float returns the value as a float64.

func (Value) Int

func (v Value) Int() (int, error)

Int returns the value as an integer.

func (Value) IsZero added in v0.4.0

func (v Value) IsZero() bool

IsZero reports whether the value is empty.

func (Value) MustBool

func (v Value) MustBool() bool

MustBool returns the value as a boolean, and panics if the value is not a valid boolean. Use this method only if you are sure of the value type.

func (Value) MustFloat

func (v Value) MustFloat() float64

MustFloat returns the value as a float64, and panics if the value is not a valid float. Use this method only if you are sure of the value type.

func (Value) MustInt

func (v Value) MustInt() int

MustInt returns the value as an integer, and panics if the value is not a valid integer. Use this method only if you are sure of the value type.

func (Value) String

func (v Value) String() string

String returns the value as a string.

Jump to

Keyboard shortcuts

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