rosedb

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2022 License: Apache-2.0 Imports: 23 Imported by: 1

README

rosedb_ico.png

Go Report Card GitHub top language GitHub stars codecov CodeFactor Go Reference Mentioned in Awesome Go LICENSE

English| 简体中文

rosedb is a fast, stable, and embedded NoSQL database based on bitcask, supports a variety of data structures such as string, list, hash, set, and sorted set.

It is similar to Redis but store values on disk.

Key features:

  • Many data structures: string, list, hash, set, and sorted set
  • Easy to embed into your own Go application
  • High performance, suitable for both read and write intensive workload
  • Values are not limited by RAM

Design Overview

Quick Start

1. embedded usage: see examples

Documentation

See wiki

Contributing

If you are interested in contributing to rosedb, see CONTRIBUTING and how to contribute?

License

rosedb is licensed under the term of the Apache 2.0 License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound key not found
	ErrKeyNotFound = errors.New("key not found")

	// ErrLogFileNotFound log file not found
	ErrLogFileNotFound = errors.New("log file not found")

	// ErrWrongNumberOfArgs doesn't match key-value pair numbers
	ErrWrongNumberOfArgs = errors.New("wrong number of arguments")

	// ErrIntegerOverflow overflows int64 limitations
	ErrIntegerOverflow = errors.New("increment or decrement overflow")

	// ErrWrongValueType value is not a number
	ErrWrongValueType = errors.New("value is not an integer")

	// ErrGCRunning log file gc is running
	ErrGCRunning = errors.New("log file gc is running, retry later")
)
View Source
var ErrDiscardNoSpace = errors.New("not enough space can be allocated for the discard file")

ErrDiscardNoSpace no enough space for discard file.

Functions

This section is empty.

Types

type DataIndexMode

type DataIndexMode int

DataIndexMode the data index mode.

const (
	// KeyValueMemMode key and value are both in memory, read operation will be very fast in this mode.
	// Because there is no disk seek, just get value from the corresponding data structures in memory.
	// This mode is suitable for scenarios where the value are relatively small.
	KeyValueMemMode DataIndexMode = iota

	// KeyOnlyMemMode only key in memory, there is a disk seek while getting a value.
	// Because values are in log file on disk.
	KeyOnlyMemMode
)

type DataType

type DataType = int8

DataType Define the data structure type.

const (
	String DataType = iota
	List
	Hash
	Set
	ZSet
)

Five different data types, support String, List, Hash, Set, Sorted Set right now.

type IOType

type IOType int8

IOType represents different types of file io: FileIO(standard file io) and MMap(Memory Map).

const (
	// FileIO standard file io.
	FileIO IOType = iota
	// MMap Memory Map.
	MMap
)

type Options

type Options struct {
	// DBPath db path, will be created automatically if not exist.
	DBPath string

	// IndexMode mode of index, support KeyValueMemMode and KeyOnlyMemMode now.
	// Note that this mode is only for kv pairs, not List, Hash, Set, and ZSet.
	// Default value is KeyOnlyMemMode.
	IndexMode DataIndexMode

	// IoType file r/w io type, support FileIO and MMap now.
	// Default value is FileIO.
	IoType IOType

	// Sync is whether to sync writes from the OS buffer cache through to actual disk.
	// If false, and the machine crashes, then some recent writes may be lost.
	// Note that if it is just the process that crashes (and the machine does not) then no writes will be lost.
	// Default value is false.
	Sync bool

	// LogFileGCInterval a background goroutine will execute log file garbage collection periodically according to the interval.
	// It will pick the log file that meet the conditions for GC, then rewrite the valid data one by one.
	// Default value is 8 hours.
	LogFileGCInterval time.Duration

	// LogFileGCRatio if discarded data in log file exceeds this ratio, it can be picked up for compaction(garbage collection)
	// And if there are many files reached the ratio, we will pick the highest one by one.
	// The recommended ratio is 0.5, half of the file can be compacted.
	// Default value is 0.5.
	LogFileGCRatio float64

	// LogFileSizeThreshold threshold size of each log file, active log file will be closed if reach the threshold.
	// Important!!! This option must be set to the same value as the first startup.
	// Default value is 512MB.
	LogFileSizeThreshold int64

	// DiscardBufferSize a channel will be created to send the older entry size when a key updated or deleted.
	// Entry size will be saved in the discard file, recording the invalid size in a log file, and be used when log file gc is running.
	// This option represents the size of that channel.
	// If you got errors like `send discard chan fail`, you can increase this option to avoid it.
	DiscardBufferSize int
}

Options for opening a db.

func DefaultOptions

func DefaultOptions(path string) Options

DefaultOptions default options for opening a RoseDB.

type RoseDB

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

RoseDB a db instance.

func Open

func Open(opts Options) (*RoseDB, error)

Open a rosedb instance. You must call Close after using it.

func (*RoseDB) Append

func (db *RoseDB) Append(key, value []byte) error

Append appends the value at the end of the old value if key already exists. It will be similar to Set if key does not exist.

func (*RoseDB) Close

func (db *RoseDB) Close() error

Close db and save relative configs.

func (*RoseDB) Decr

func (db *RoseDB) Decr(key []byte) (int64, error)

Decr decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. It returns ErrWrongKeyType error if the value is not integer type. Also, it returns ErrIntegerOverflow error if the value exceeds after decrementing the value.

func (*RoseDB) DecrBy

func (db *RoseDB) DecrBy(key []byte, decr int64) (int64, error)

DecrBy decrements the number stored at key by decr. If the key doesn't exist, it is set to 0 before performing the operation. It returns ErrWrongKeyType error if the value is not integer type. Also, it returns ErrIntegerOverflow error if the value exceeds after decrementing the value.

func (*RoseDB) Delete

func (db *RoseDB) Delete(key []byte) error

Delete value at the given key.

func (*RoseDB) Get

func (db *RoseDB) Get(key []byte) ([]byte, error)

Get get the value of key. If the key does not exist an error is returned.

func (*RoseDB) GetDel

func (db *RoseDB) GetDel(key []byte) ([]byte, error)

GetDel gets the value of the key and deletes the key. This method is similar to Get method. It also deletes the key if it exists.

func (*RoseDB) HDel

func (db *RoseDB) HDel(key []byte, fields ...[]byte) (int, error)

HDel removes the specified fields from the hash stored at key. Specified fields that do not exist within this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns false.

func (*RoseDB) HExists

func (db *RoseDB) HExists(key, field []byte) (bool, error)

HExists returns whether the field exists in the hash stored at key. If the hash contains field, it returns true. If the hash does not contain field, or key does not exist, it returns false.

func (*RoseDB) HGet

func (db *RoseDB) HGet(key, field []byte) ([]byte, error)

HGet returns the value associated with field in the hash stored at key.

func (*RoseDB) HGetAll

func (db *RoseDB) HGetAll(key []byte) ([][]byte, error)

HGetAll return all fields and values of the hash stored at key.

func (*RoseDB) HKeys

func (db *RoseDB) HKeys(key []byte) ([][]byte, error)

HKeys returns all field names in the hash stored at key.

func (*RoseDB) HLen

func (db *RoseDB) HLen(key []byte) int

HLen returns the number of fields contained in the hash stored at key.

func (*RoseDB) HSet

func (db *RoseDB) HSet(key, field, value []byte) error

HSet sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten. Return num of elements in hash of the specified key.

func (*RoseDB) HStrLen

func (db *RoseDB) HStrLen(key, field []byte) int

HStrLen returns the string length of the value associated with field in the hash stored at key. If the key or the field do not exist, 0 is returned.

func (*RoseDB) HVals

func (db *RoseDB) HVals(key []byte) ([][]byte, error)

HVals return all values in the hash stored at key.

func (*RoseDB) Incr

func (db *RoseDB) Incr(key []byte) (int64, error)

Incr increments the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. It returns ErrWrongKeyType error if the value is not integer type. Also, it returns ErrIntegerOverflow error if the value exceeds after incrementing the value.

func (*RoseDB) IncrBy

func (db *RoseDB) IncrBy(key []byte, incr int64) (int64, error)

IncrBy increments the number stored at key by incr. If the key doesn't exist, it is set to 0 before performing the operation. It returns ErrWrongKeyType error if the value is not integer type. Also, it returns ErrIntegerOverflow error if the value exceeds after incrementing the value.

func (*RoseDB) LLen

func (db *RoseDB) LLen(key []byte) int

LLen returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned.

func (*RoseDB) LPop

func (db *RoseDB) LPop(key []byte) ([]byte, error)

LPop removes and returns the first elements of the list stored at key.

func (*RoseDB) LPush

func (db *RoseDB) LPush(key []byte, values ...[]byte) error

LPush insert all the specified values at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operations.

func (*RoseDB) MGet

func (db *RoseDB) MGet(keys [][]byte) ([][]byte, error)

MGet get the values of all specified keys. If the key that does not hold a string value or does not exist, nil is returned.

func (*RoseDB) MSet

func (db *RoseDB) MSet(args ...[]byte) error

MSet is multiple set command. Parameter order should be like "key", "value", "key", "value", ...

func (*RoseDB) MSetNX

func (db *RoseDB) MSetNX(args ...[]byte) error

MSetNX sets given keys to their respective values. MSetNX will not perform any operation at all even if just a single key already exists.

func (*RoseDB) RPop

func (db *RoseDB) RPop(key []byte) ([]byte, error)

RPop Removes and returns the last elements of the list stored at key.

func (*RoseDB) RPush

func (db *RoseDB) RPush(key []byte, values ...[]byte) error

RPush insert all the specified values at the tail of the list stored at key. If key does not exist, it is created as empty list before performing the push operation.

func (*RoseDB) RunLogFileGC

func (db *RoseDB) RunLogFileGC(dataType DataType, fid int, gcRatio float64) error

RunLogFileGC run log file garbage collection manually.

func (*RoseDB) SAdd

func (db *RoseDB) SAdd(key []byte, members ...[]byte) error

SAdd add the specified members to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members.

func (*RoseDB) SCard

func (db *RoseDB) SCard(key []byte) int

SCard returns the set cardinality (number of elements) of the set stored at key.

func (*RoseDB) SDiff

func (db *RoseDB) SDiff(keys ...[]byte) ([][]byte, error)

SDiff returns the members of the set difference between the first set and all the successive sets. Returns error if no key is passed as a parameter.

func (*RoseDB) SIsMember

func (db *RoseDB) SIsMember(key, member []byte) bool

SIsMember returns if member is a member of the set stored at key.

func (*RoseDB) SMembers

func (db *RoseDB) SMembers(key []byte) ([][]byte, error)

SMembers returns all the members of the set value stored at key.

func (*RoseDB) SPop

func (db *RoseDB) SPop(key []byte, count uint) ([][]byte, error)

SPop removes and returns one or more random members from the set value store at key.

func (*RoseDB) SRem

func (db *RoseDB) SRem(key []byte, members ...[]byte) error

SRem remove the specified members from the set stored at key. Specified members that are not a member of this set are ignored. If key does not exist, it is treated as an empty set and this command returns 0.

func (*RoseDB) SUnion

func (db *RoseDB) SUnion(keys ...[]byte) ([][]byte, error)

SUnion returns the members of the set resulting from the union of all the given sets.

func (*RoseDB) Set

func (db *RoseDB) Set(key, value []byte) error

Set set key to hold the string value. If key already holds a value, it is overwritten. Any previous time to live associated with the key is discarded on successful Set operation.

func (*RoseDB) SetEX

func (db *RoseDB) SetEX(key, value []byte, duration time.Duration) error

SetEX set key to hold the string value and set key to timeout after the given duration.

func (*RoseDB) SetNX

func (db *RoseDB) SetNX(key, value []byte) error

SetNX sets the key-value pair if it is not exist. It returns nil if the key already exists.

func (*RoseDB) StrLen

func (db *RoseDB) StrLen(key []byte) int

StrLen returns the length of the string value stored at key. If the key doesn't exist, it returns 0.

func (*RoseDB) Sync

func (db *RoseDB) Sync() error

Sync persist the db files to stable storage.

func (*RoseDB) ZAdd

func (db *RoseDB) ZAdd(key []byte, score float64, member []byte) error

ZAdd adds the specified member with the specified score to the sorted set stored at key.

func (*RoseDB) ZCard

func (db *RoseDB) ZCard(key []byte) int

ZCard returns the sorted set cardinality (number of elements) of the sorted set stored at key.

func (*RoseDB) ZRange

func (db *RoseDB) ZRange(key []byte, start, stop int) ([][]byte, error)

ZRange returns the specified range of elements in the sorted set stored at key.

func (*RoseDB) ZRem

func (db *RoseDB) ZRem(key, member []byte) error

ZRem removes the specified members from the sorted set stored at key. Non existing members are ignored. An error is returned when key exists and does not hold a sorted set.

func (*RoseDB) ZScore

func (db *RoseDB) ZScore(key, member []byte) (ok bool, score float64)

ZScore returns the score of member in the sorted set at key.

Directories

Path Synopsis
ds
art
examples
hash command
list command
open command
sets command
strs command

Jump to

Keyboard shortcuts

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