nutsdb

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrorParamEmpty liberr.CodeError = iota + liberr.MinPkgNutsDB
	ErrorParamMissing
	ErrorParamMismatching
	ErrorParamInvalid
	ErrorParamInvalidNumber
	ErrorValidateConfig
	ErrorValidateNutsDB
	ErrorClusterInit
	ErrorFileTemp
	ErrorFolderCheck
	ErrorFolderCreate
	ErrorFolderCopy
	ErrorFolderDelete
	ErrorFolderExtract
	ErrorFolderArchive
	ErrorFolderCompress
	ErrorDatabaseClosed
	ErrorDatabaseKeyInvalid
	ErrorDatabaseBackup
	ErrorDatabaseSnapshot
	ErrorTransactionInit
	ErrorTransactionClosed
	ErrorTransactionCommit
	ErrorTransactionPutKey
	ErrorCommandInvalid
	ErrorCommandUnmarshal
	ErrorCommandMarshal
	ErrorCommandResultUnmarshal
	ErrorCommandResultMarshal
	ErrorLogEntryAdd
	ErrorClientCommandInvalid
	ErrorClientCommandParamsBadNumber
	ErrorClientCommandParamsMismatching
	ErrorClientCommandCall
	ErrorClientCommandCommit
	ErrorClientCommandResponseInvalid
)
View Source
const LogLib = "NutsDB"

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	Commands
	Run(cmd CmdCode, args []string) (*CommandResponse, liberr.Error)
}

type CmdCode

type CmdCode uint32
const (
	// CmdUnknown is no Command.
	CmdUnknown CmdCode = iota
	// Command for transaction.
	CmdPut
	CmdPutWithTimestamp
	// Command for BPTree.
	CmdGet
	CmdGetAll
	CmdRangeScan
	CmdPrefixScan
	CmdPrefixSearchScan
	CmdDelete
	CmdFindTxIDOnDisk
	CmdFindOnDisk
	CmdFindLeafOnDisk
	// Command for Set.
	CmdSAdd
	CmdSRem
	CmdSAreMembers
	CmdSIsMember
	CmdSMembers
	CmdSHasKey
	CmdSPop
	CmdSCard
	CmdSDiffByOneBucket
	CmdSDiffByTwoBuckets
	CmdSMoveByOneBucket
	CmdSMoveByTwoBuckets
	CmdSUnionByOneBucket
	CmdSUnionByTwoBuckets
	// Command for List.
	CmdRPop
	CmdRPeek
	CmdRPush
	CmdLPush
	CmdLPop
	CmdLPeek
	CmdLSize
	CmdLRange
	CmdLRem
	CmdLSet
	CmdLTrim
	// Command for ZSet.
	CmdZAdd
	CmdZMembers
	CmdZCard
	CmdZCount
	CmdZPopMax
	CmdZPopMin
	CmdZPeekMax
	CmdZPeekMin
	CmdZRangeByScore
	CmdZRangeByRank
	CmdZRem
	CmdZRemRangeByRank
	CmdZRank
	CmdZRevRank
	CmdZScore
	CmdZGetByKey
)

func CmdCodeFromName

func CmdCodeFromName(name string) CmdCode

nolint #funlen

func (CmdCode) Desc added in v1.7.0

func (c CmdCode) Desc() string

nolint #funlen

func (CmdCode) Name

func (c CmdCode) Name() string

nolint #funlen

func (CmdCode) Usage added in v1.7.0

func (c CmdCode) Usage() string

nolint #funlen

type CommandBPTree

type CommandBPTree interface {
	// Get retrieves the value for a key in the bucket.
	// The returned value is only valid for the life of the transaction.
	Get(bucket string, key []byte) (e *nutsdb.Entry, err error)

	//GetAll returns all keys and values of the bucket stored at given bucket.
	GetAll(bucket string) (entries nutsdb.Entries, err error)

	// RangeScan query a range at given bucket, start and end slice.
	RangeScan(bucket string, start, end []byte) (es nutsdb.Entries, err error)

	// PrefixScan iterates over a key prefix at given bucket, prefix and limitNum.
	// LimitNum will limit the number of entries return.
	PrefixScan(bucket string, prefix []byte, offsetNum int, limitNum int) (es nutsdb.Entries, off int, err error)

	// PrefixSearchScan iterates over a key prefix at given bucket, prefix, match regular expression and limitNum.
	// LimitNum will limit the number of entries return.
	PrefixSearchScan(bucket string, prefix []byte, reg string, offsetNum int, limitNum int) (es nutsdb.Entries, off int, err error)

	// Delete removes a key from the bucket at given bucket and key.
	Delete(bucket string, key []byte) error
}

type CommandList

type CommandList interface {
	// RPop removes and returns the last element of the list stored in the bucket at given bucket and key.
	RPop(bucket string, key []byte) (item []byte, err error)

	// RPeek returns the last element of the list stored in the bucket at given bucket and key.
	RPeek(bucket string, key []byte) (item []byte, err error)

	// RPush inserts the values at the tail of the list stored in the bucket at given bucket,key and values.
	RPush(bucket string, key []byte, values ...[]byte) error

	// LPush inserts the values at the head of the list stored in the bucket at given bucket,key and values.
	LPush(bucket string, key []byte, values ...[]byte) error

	// LPop removes and returns the first element of the list stored in the bucket at given bucket and key.
	LPop(bucket string, key []byte) (item []byte, err error)

	// LPeek returns the first element of the list stored in the bucket at given bucket and key.
	LPeek(bucket string, key []byte) (item []byte, err error)

	// LSize returns the size of key in the bucket in the bucket at given bucket and key.
	LSize(bucket string, key []byte) (int, error)

	// LRange returns the specified elements of the list stored in the bucket at given bucket,key, start and end.
	// The offsets start and stop are zero-based indexes 0 being the first element of the list (the head of the list),
	// 1 being the next element and so on.
	// Start and end can also be negative numbers indicating offsets from the end of the list,
	// where -1 is the last element of the list, -2 the penultimate element and so on.
	LRange(bucket string, key []byte, start, end int) (list [][]byte, err error)

	// LRem removes the first count occurrences of elements equal to value from the list stored in the bucket at given bucket,key,count.
	// The count argument influences the operation in the following ways:
	// count > 0: Remove elements equal to value moving from head to tail.
	// count < 0: Remove elements equal to value moving from tail to head.
	// count = 0: Remove all elements equal to value.
	LRem(bucket string, key []byte, count int, value []byte) (removedNum int, err error)

	// LSet sets the list element at index to value.
	LSet(bucket string, key []byte, index int, value []byte) error

	// LTrim trims an existing list so that it will contain only the specified range of elements specified.
	// the offsets start and stop are zero-based indexes 0 being the first element of the list (the head of the list),
	// 1 being the next element and so on.
	// start and end can also be negative numbers indicating offsets from the end of the list,
	// where -1 is the last element of the list, -2 the penultimate element and so on.
	LTrim(bucket string, key []byte, start, end int) error
}

type CommandRequest

type CommandRequest struct {
	Cmd    CmdCode       `mapstructure:"cmd" json:"cmd" yaml:"cmd" toml:"cmd" cbor:"cmd"`
	Params []interface{} `mapstructure:"params" json:"params" yaml:"params" toml:"params" cbor:"params"`
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand() *CommandRequest

func NewCommandByCaller

func NewCommandByCaller(params ...interface{}) *CommandRequest

func NewCommandByDecode

func NewCommandByDecode(l liblog.FuncLog, p []byte) (*CommandRequest, liberr.Error)

func (*CommandRequest) DecodeResult

func (c *CommandRequest) DecodeResult(p []byte) (*CommandResponse, liberr.Error)

func (*CommandRequest) EncodeRequest

func (c *CommandRequest) EncodeRequest() ([]byte, liberr.Error)

func (*CommandRequest) GetLogger added in v1.7.0

func (c *CommandRequest) GetLogger() liblog.Logger

func (*CommandRequest) InitParams

func (c *CommandRequest) InitParams(num int)

func (*CommandRequest) InitParamsCounter

func (c *CommandRequest) InitParamsCounter(num int) int

func (*CommandRequest) Run

func (c *CommandRequest) Run(tx *nutsdb.Tx) ([]byte, liberr.Error)

func (*CommandRequest) RunLocal

func (c *CommandRequest) RunLocal(tx *nutsdb.Tx) (*CommandResponse, liberr.Error)

func (*CommandRequest) SetLogger added in v1.7.0

func (c *CommandRequest) SetLogger(l liblog.FuncLog)

func (*CommandRequest) SetParams

func (c *CommandRequest) SetParams(num int, val interface{})

func (*CommandRequest) SetParamsInc

func (c *CommandRequest) SetParamsInc(num int, val interface{}) int

type CommandResponse

type CommandResponse struct {
	Error error         `mapstructure:"error" json:"error" yaml:"error" toml:"error" cbor:"error"`
	Value []interface{} `mapstructure:"value" json:"value" yaml:"value" toml:"value" cbor:"value"`
}

type CommandSet

type CommandSet interface {
	// SAdd adds the specified members to the set stored int the bucket at given bucket,key and items.
	SAdd(bucket string, key []byte, items ...[]byte) error

	// SRem removes the specified members from the set stored int the bucket at given bucket,key and items.
	SRem(bucket string, key []byte, items ...[]byte) error

	// SAreMembers returns if the specified members are the member of the set int the bucket at given bucket,key and items.
	SAreMembers(bucket string, key []byte, items ...[]byte) (bool, error)

	// SIsMember returns if member is a member of the set stored int the bucket at given bucket,key and item.
	SIsMember(bucket string, key, item []byte) (bool, error)

	// SMembers returns all the members of the set value stored int the bucket at given bucket and key.
	SMembers(bucket string, key []byte) (list [][]byte, err error)

	// SHasKey returns if the set in the bucket at given bucket and key.
	SHasKey(bucket string, key []byte) (bool, error)

	// SPop removes and returns one or more random elements from the set value store in the bucket at given bucket and key.
	SPop(bucket string, key []byte) ([]byte, error)

	// SCard returns the set cardinality (number of elements) of the set stored in the bucket at given bucket and key.
	SCard(bucket string, key []byte) (int, error)

	// SDiffByOneBucket returns the members of the set resulting from the difference
	// between the first set and all the successive sets in one bucket.
	SDiffByOneBucket(bucket string, key1, key2 []byte) (list [][]byte, err error)

	// SDiffByTwoBuckets returns the members of the set resulting from the difference
	// between the first set and all the successive sets in two buckets.
	SDiffByTwoBuckets(bucket1 string, key1 []byte, bucket2 string, key2 []byte) (list [][]byte, err error)

	// SMoveByOneBucket moves member from the set at source to the set at destination in one bucket.
	SMoveByOneBucket(bucket string, key1, key2, item []byte) (bool, error)

	// SMoveByTwoBuckets moves member from the set at source to the set at destination in two buckets.
	SMoveByTwoBuckets(bucket1 string, key1 []byte, bucket2 string, key2, item []byte) (bool, error)

	// SUnionByOneBucket the members of the set resulting from the union of all the given sets in one bucket.
	SUnionByOneBucket(bucket string, key1, key2 []byte) (list [][]byte, err error)

	// SUnionByTwoBuckets the members of the set resulting from the union of all the given sets in two buckets.
	SUnionByTwoBuckets(bucket1 string, key1 []byte, bucket2 string, key2 []byte) (list [][]byte, err error)
}

type CommandTransaction

type CommandTransaction interface {
	// Put sets the value for a key in the bucket.
	Put(bucket string, key, value []byte, ttl uint32) error

	// PutWithTimestamp sets the value for a key in the bucket but allow capabilities to custom the timestamp for ttl.
	PutWithTimestamp(bucket string, key, value []byte, ttl uint32, timestamp uint64) error
}

type CommandZSet

type CommandZSet interface {
	// ZAdd adds the specified member key with the specified score and specified val to the sorted set stored at bucket.
	ZAdd(bucket string, key []byte, score float64, val []byte) error

	// ZMembers returns all the members of the set value stored at bucket.
	ZMembers(bucket string) (map[string]*nutsdb.SortedSetMember, error)

	// ZCard returns the sorted set cardinality (number of elements) of the sorted set stored at bucket.
	ZCard(bucket string) (int, error)

	// ZCount returns the number of elements in the sorted set at bucket with a score between min and max and opts.
	// opts includes the following parameters:
	// Limit        int  // limit the max nodes to return
	// ExcludeStart bool // exclude start value, so it search in interval (start, end] or (start, end)
	// ExcludeEnd   bool // exclude end value, so it search in interval [start, end) or (start, end)
	ZCount(bucket string, start, end float64, opts *nutsdb.GetByScoreRangeOptions) (int, error)

	// ZPopMax removes and returns the member with the highest score in the sorted set stored at bucket.
	ZPopMax(bucket string) (*nutsdb.SortedSetMember, error)

	// ZPopMin removes and returns the member with the lowest score in the sorted set stored at bucket.
	ZPopMin(bucket string) (*nutsdb.SortedSetMember, error)

	// ZPeekMax returns the member with the highest score in the sorted set stored at bucket.
	ZPeekMax(bucket string) (*nutsdb.SortedSetMember, error)

	// ZPeekMin returns the member with the lowest score in the sorted set stored at bucket.
	ZPeekMin(bucket string) (*nutsdb.SortedSetMember, error)

	// ZRangeByScore returns all the elements in the sorted set at bucket with a score between min and max.
	ZRangeByScore(bucket string, start, end float64, opts *nutsdb.GetByScoreRangeOptions) ([]*nutsdb.SortedSetMember, error)

	// ZRangeByRank returns all the elements in the sorted set in one bucket and key
	// with a rank between start and end (including elements with rank equal to start or end).
	ZRangeByRank(bucket string, start, end int) ([]*nutsdb.SortedSetMember, error)

	// ZRem removes the specified members from the sorted set stored in one bucket at given bucket and key.
	ZRem(bucket, key string) error

	// ZRemRangeByRank removes all elements in the sorted set stored in one bucket at given bucket with rank between start and end.
	// the rank is 1-based integer. Rank 1 means the first node; Rank -1 means the last node.
	ZRemRangeByRank(bucket string, start, end int) error

	// ZRank returns the rank of member in the sorted set stored in the bucket at given bucket and key,
	// with the scores ordered from low to high.
	ZRank(bucket string, key []byte) (int, error)

	// ZRevRank returns the rank of member in the sorted set stored in the bucket at given bucket and key,
	// with the scores ordered from high to low.
	ZRevRank(bucket string, key []byte) (int, error)

	// ZScore returns the score of member in the sorted set in the bucket at given bucket and key.
	ZScore(bucket string, key []byte) (float64, error)

	// ZGetByKey returns node in the bucket at given bucket and key.
	ZGetByKey(bucket string, key []byte) (*nutsdb.SortedSetMember, error)
}

type Config

type Config struct {
	DB        NutsDBOptions `mapstructure:"db" json:"db" yaml:"db" toml:"db" validate:""`
	Cluster   libclu.Config `mapstructure:"cluster" json:"cluster" yaml:"cluster" toml:"cluster" validate:""`
	Directory NutsDBFolder  `mapstructure:"directories" json:"directories" yaml:"directories" toml:"directories" validate:""`
	Monitor   moncfg.Config `mapstructure:"monitor" json:"monitor" yaml:"monitor" toml:"monitor" validate:""`
}

func (Config) GetConfigCluster

func (c Config) GetConfigCluster() (libclu.Config, liberr.Error)

func (Config) GetConfigDB

func (c Config) GetConfigDB() (nutsdb.Options, liberr.Error)

func (Config) GetConfigFolder

func (c Config) GetConfigFolder() NutsDBFolder

func (Config) GetOptions

func (c Config) GetOptions() (Options, liberr.Error)

func (Config) Validate

func (c Config) Validate() liberr.Error

func (Config) ValidateCluster

func (c Config) ValidateCluster() liberr.Error

func (Config) ValidateDB

func (c Config) ValidateDB() liberr.Error

type NutsDB

type NutsDB interface {
	Listen() liberr.Error
	Restart() liberr.Error
	Shutdown() liberr.Error

	ForceRestart()
	ForceShutdown()

	IsRunning() bool
	IsReady(ctx context.Context) bool
	IsReadyTimeout(parent context.Context, dur time.Duration) bool
	WaitReady(ctx context.Context, tick time.Duration)

	GetLogger() liblog.Logger
	SetLogger(l liblog.FuncLog)

	Monitor(ctx libctx.FuncContext, vrs libver.Version) (montps.Monitor, error)

	Cluster() libclu.Cluster
	Client(ctx context.Context, tickSync time.Duration) Client
	ShellCommand(ctx func() context.Context, tickSync time.Duration) []shlcmd.Command
}

func New

func New(c Config) NutsDB

type NutsDBFolder

type NutsDBFolder struct {
	// Working represents the main working folder witch will include sub directories : data, backup, temp...
	// If the base directory is empty, all the sub directory will be absolute directories.
	Base string `mapstructure:"base" json:"base" yaml:"base" toml:"base" validate:"dir,required"`

	// Data represents the sub-dir for the opening database.
	// By default, it will use `data` as sub folder.
	Data string `mapstructure:"sub_data" json:"sub_data" yaml:"sub_data" toml:"sub_data" validate:"printascii,required"`

	// Backup represents the sub-dir with all backup sub-folder.
	// By default, it will use `backup` as sub folder.
	Backup string `mapstructure:"sub_backup" json:"sub_backup" yaml:"sub_backup" toml:"sub_backup" validate:"printascii,required"`

	// Temp represents the sub-dir for temporary file/dir.
	// By default, it will use the system temporary folder.
	Temp string `mapstructure:"sub_temp" json:"sub_temp" yaml:"sub_temp" toml:"sub_temp" validate:"printascii,required"`

	// WalDir represents the sub-dir for cluster negociation.
	// By default, it will use `wal` as sub folder.
	WalDir string `mapstructure:"wal_dir" json:"wal_dir" yaml:"wal_dir" toml:"wal_dir" validate:"printascii,required"`

	// HostDir represents the sub-dir for cluster storage.
	// By default, it will use `host` as sub folder.
	HostDir string `mapstructure:"host_dir" json:"host_dir" yaml:"host_dir" toml:"host_dir" validate:"printascii,required"`

	// LimitNumberBackup represents how many backup will be keep.
	LimitNumberBackup uint8 `mapstructure:"limit_number_backup" json:"limit_number_backup" yaml:"limit_number_backup" toml:"limit_number_backup"`

	// Permission represents the permission apply to folder created.
	// By default, it will use `0755` as permission.
	Permission os.FileMode `mapstructure:"permission" json:"permission" yaml:"permission" toml:"permission"`
}

func (NutsDBFolder) GetDirectoryBackup

func (f NutsDBFolder) GetDirectoryBackup() (string, liberr.Error)

func (NutsDBFolder) GetDirectoryBase

func (f NutsDBFolder) GetDirectoryBase() (string, liberr.Error)

func (NutsDBFolder) GetDirectoryData

func (f NutsDBFolder) GetDirectoryData() (string, liberr.Error)

func (NutsDBFolder) GetDirectoryHost

func (f NutsDBFolder) GetDirectoryHost() (string, liberr.Error)

func (NutsDBFolder) GetDirectoryTemp

func (f NutsDBFolder) GetDirectoryTemp() (string, liberr.Error)

func (NutsDBFolder) GetDirectoryWal

func (f NutsDBFolder) GetDirectoryWal() (string, liberr.Error)

func (NutsDBFolder) Validate

func (f NutsDBFolder) Validate() liberr.Error

type NutsDBOptions

type NutsDBOptions struct {
	// EntryIdxMode represents using which mode to index the entries.
	EntryIdxMode nutsdb.EntryIdxMode `mapstructure:"entry_idx_mode" json:"entry_idx_mode" yaml:"entry_idx_mode" toml:"entry_idx_mode"`

	// RWMode represents the read and write mode.
	// RWMode includes two options: FileIO and MMap.
	// FileIO represents the read and write mode using standard I/O.
	// MMap represents the read and write mode using mmap.
	RWMode nutsdb.RWMode `mapstructure:"rw_mode" json:"rw_mode" yaml:"rw_mode" toml:"rw_mode"`

	// SegmentSize default value is 8 MBytes
	SegmentSize int64 `mapstructure:"segment_size" json:"segment_size" yaml:"segment_size" toml:"segment_size"`

	// SyncEnable represents if call Sync() function.
	// if SyncEnable is false, high write performance but potential data loss likely.
	// if SyncEnable is true, slower but persistent.
	SyncEnable bool `mapstructure:"sync_enable" json:"sync_enable" yaml:"sync_enable" toml:"sync_enable"`
}

func (NutsDBOptions) GetNutsDBOptions

func (o NutsDBOptions) GetNutsDBOptions(dataDir string) nutsdb.Options

func (NutsDBOptions) Validate

func (o NutsDBOptions) Validate() liberr.Error

type Options

type Options interface {
	NutsDBOptions() nutsdb.Options

	NewBackup(db *nutsdb.DB) (string, liberr.Error)
	NewBackupTemp(db *nutsdb.DB) (string, liberr.Error)

	NewTempFolder() (string, liberr.Error)
	NewTempFilePattern(extension string) string
	GetTempDir() string

	CleanBackup() liberr.Error
	Permission() os.FileMode

	RestoreBackup(dir string) liberr.Error
}

func NewOptions

func NewOptions(cfgNuts NutsDBOptions, cfgFs NutsDBFolder) (Options, liberr.Error)

type Snapshot

type Snapshot interface {
	Prepare(opt Options, db *nutsdb.DB) liberr.Error

	Save(opt Options, writer io.Writer) liberr.Error
	Load(opt Options, reader io.Reader) liberr.Error

	Apply(opt Options) liberr.Error

	Finish()
}

Jump to

Keyboard shortcuts

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